| 1 | /* |
| 2 | * SPDX-FileName: groundnet.hxx |
| 3 | * SPDX-FileComment: A number of classes to handle taxiway assignments by the AI code |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2004 Durk Talsma |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <string> |
| 11 | #include <unordered_map> |
| 12 | |
| 13 | #include <simgear/compiler.h> |
| 14 | |
| 15 | #include "gnnode.hxx" |
| 16 | #include "parking.hxx" |
| 17 | |
| 18 | |
| 19 | class FGAirportDynamicsXMLLoader; |
| 20 | |
| 21 | typedef std::vector<int> intVec; |
| 22 | typedef std::vector<int>::iterator intVecIterator; |
| 23 | |
| 24 | class Block |
| 25 | { |
| 26 | private: |
| 27 | int id; |
| 28 | time_t blocktime; |
| 29 | time_t touch; |
| 30 | |
| 31 | public: |
| 32 | Block(int i, time_t bt, time_t curr) : blocktime{bt}, touch{curr} |
| 33 | { |
| 34 | id = i; |
| 35 | } |
| 36 | |
| 37 | ~Block() {} |
| 38 | |
| 39 | int getId() { return id; } |
| 40 | |
| 41 | void updateTimeStamps(time_t bt, time_t now) |
| 42 | { |
| 43 | blocktime = (bt < blocktime) ? bt : blocktime; |
| 44 | touch = now; |
| 45 | } |
| 46 | |
| 47 | time_t getBlockTime() const { return blocktime; } |
| 48 | time_t getTimeStamp() { return touch; } |
| 49 | bool operator<(const Block& other) const { return blocktime < other.blocktime; } |
| 50 | }; |
| 51 | |
| 52 | /*************************************************************************************** |
| 53 | * class FGTaxiSegment |
| 54 | **************************************************************************************/ |
| 55 | class FGTaxiSegment |
| 56 | { |
| 57 | private: |
| 58 | // weak (non-owning) pointers deliberately here: |
| 59 | // the ground-network owns the nodes |
| 60 | const FGTaxiNode* startNode; |
| 61 | const FGTaxiNode* endNode; |
| 62 | |
| 63 | bool isActive; |
| 64 | BlockList blockTimes; |
| 65 | |
| 66 | int index; |
| 67 | FGTaxiSegment* oppositeDirection; // also deliberately weak |
| 68 | |
| 69 | friend class FGGroundNetwork; |
| 70 | |
| 71 | public: |
| 72 | FGTaxiSegment(FGTaxiNode* start, FGTaxiNode* end); |
| 73 | |
| 74 | void setIndex(int val) |
| 75 | { |
| 76 | index = val; |
| 77 | } |
| 78 | |
| 79 | void setDimensions(double elevation); |
| 80 | void block(int id, time_t blockTime, time_t now); |
| 81 | void unblock(time_t now); |
| 82 | bool hasBlock(time_t now); |
| 83 | |
| 84 | FGTaxiNodeRef getEnd() const; |
| 85 | FGTaxiNodeRef getStart() const; |
| 86 | |
| 87 | double getLength() const; |
| 88 | |
| 89 | // compute the center of the arc |
| 90 | SGGeod getCenter() const; |
| 91 | |
| 92 | double getHeading() const; |
| 93 | |
| 94 | int getIndex() |
| 95 | { |
| 96 | return index; |
| 97 | } |
| 98 | |
| 99 | int getPenalty(int nGates); |
| 100 | |
| 101 | bool operator<(const FGTaxiSegment& other) const |
| 102 | { |
| 103 | return index < other.index; |
| 104 | } |
| 105 | |
| 106 | FGTaxiSegment* opposite() |
| 107 | { |
| 108 | return oppositeDirection; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | /*************************************************************************************** |
| 113 | * class FGTaxiRoute |
| 114 | **************************************************************************************/ |
| 115 | class FGTaxiRoute |
| 116 | { |
| 117 | private: |
| 118 | FGTaxiNodeVector nodes; |
| 119 | intVec routes; |
| 120 | double distance; |
| 121 | FGTaxiNodeVector::iterator currNode; |
| 122 | intVec::iterator currRoute; |
| 123 | |
| 124 | public: |
| 125 | FGTaxiRoute() |
| 126 | { |
| 127 | distance = 0; |
| 128 | currNode = nodes.begin(); |
| 129 | currRoute = routes.begin(); |
| 130 | } |
| 131 | |
| 132 | FGTaxiRoute(const FGTaxiNodeVector& nds, const intVec& rts, double dist, int dpth); |
| 133 | |
| 134 | |
| 135 | FGTaxiRoute& operator=(const FGTaxiRoute& other) |
| 136 | { |
| 137 | nodes = other.nodes; |
| 138 | routes = other.routes; |
| 139 | distance = other.distance; |
| 140 | currNode = nodes.begin(); |
| 141 | currRoute = routes.begin(); |
| 142 | return *this; |
| 143 | } |
| 144 | |
| 145 | FGTaxiRoute(const FGTaxiRoute& copy) : nodes(copy.nodes), |
| 146 | routes(copy.routes), |
| 147 | distance(copy.distance), |
| 148 | currNode(nodes.begin()), |
| 149 | currRoute(routes.begin()) {} |
| 150 | |
| 151 | bool operator<(const FGTaxiRoute& other) const |
| 152 | { |
| 153 | return distance < other.distance; |
| 154 | } |
| 155 | bool empty() |
| 156 | { |
| 157 | return nodes.empty(); |
| 158 | } |
| 159 | bool next(FGTaxiNodeRef& nde, int* rte); |
| 160 | |
| 161 | void first() |
| 162 | { |
| 163 | currNode = nodes.begin(); |
| 164 | currRoute = routes.begin(); |
| 165 | } |
| 166 | int size() |
| 167 | { |
| 168 | return nodes.size(); |
| 169 | } |
| 170 | int nodesLeft() |
| 171 | { |
| 172 | return nodes.end() - currNode; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | /************************************************************************************** |
| 177 | * class FGGroundNetWork |
| 178 | *************************************************************************************/ |
| 179 | class FGGroundNetwork |
| 180 | { |
| 181 | private: |
| 182 | friend class FGGroundNetXMLLoader; |
| 183 | |
| 184 | bool hasNetwork; |
| 185 | bool networkInitialized; |
| 186 | |
| 187 | int version; |
| 188 | |
| 189 | FGTaxiSegmentVector segments; |
| 190 | |
| 191 | FGAirport* parent; |
| 192 | |
| 193 | FGParkingList m_parkings; |
| 194 | FGTaxiNodeVector m_nodes; |
| 195 | |
| 196 | FGTaxiNodeRef findNodeByIndex(int index) const; |
| 197 | |
| 198 | //void printRoutingError(string); |
| 199 | |
| 200 | void checkSpeedAdjustment(int id, double lat, double lon, |
| 201 | double heading, double speed, double alt); |
| 202 | void checkHoldPosition(int id, double lat, double lon, |
| 203 | double heading, double speed, double alt); |
| 204 | |
| 205 | |
| 206 | void addSegment(const FGTaxiNodeRef& from, const FGTaxiNodeRef& to); |
| 207 | void addParking(const FGParkingRef& park); |
| 208 | |
| 209 | void addAwosFreq(int val) |
| 210 | { |
| 211 | freqAwos.push_back(val); |
| 212 | } |
| 213 | void addUnicomFreq(int val) |
| 214 | { |
| 215 | freqUnicom.push_back(val); |
| 216 | } |
| 217 | void addClearanceFreq(int val) |
| 218 | { |
| 219 | freqClearance.push_back(val); |
| 220 | } |
| 221 | void addGroundFreq(int val) |
| 222 | { |
| 223 | freqGround.push_back(val); |
| 224 | } |
| 225 | void addTowerFreq(int val) |
| 226 | { |
| 227 | freqTower.push_back(val); |
| 228 | } |
| 229 | void addApproachFreq(int val) |
| 230 | { |
| 231 | freqApproach.push_back(val); |
| 232 | } |
| 233 | |
| 234 | intVec freqAwos; // </AWOS> |
| 235 | intVec freqUnicom; // </UNICOM> |
| 236 | intVec freqClearance; // </CLEARANCE> |
| 237 | intVec freqGround; // </GROUND> |
| 238 | intVec freqTower; // </TOWER> |
| 239 | intVec freqApproach; // </APPROACH> |
| 240 | |
| 241 | using NodeFromSegmentMap = std::unordered_multimap<FGTaxiNode*, FGTaxiSegment*>; |
| 242 | |
| 243 | /// this map exists specifically to make blockSegmentsEndingAt not be a bottleneck |
| 244 | NodeFromSegmentMap m_segmentsEndingAtNodeMap; |
| 245 | |
| 246 | public: |
| 247 | explicit FGGroundNetwork(FGAirport* pr); |
| 248 | virtual ~FGGroundNetwork(); |
| 249 | |
| 250 | void setVersion(int v) { version = v; } |
| 251 | int getVersion() { return version; } |
| 252 | |
| 253 | void init(); |
| 254 | bool exists() |
| 255 | { |
| 256 | return hasNetwork; |
| 257 | } |
| 258 | |
| 259 | FGAirport* airport() const |
| 260 | { |
| 261 | return parent; |
| 262 | } |
| 263 | |
| 264 | FGTaxiNodeRef findNearestNode(const SGGeod& aGeod) const; |
| 265 | FGTaxiNodeRef findNearestNodeOnRunwayEntry(const SGGeod& aGeod) const; |
| 266 | /**Returns the nearest node in that is in direction of runway heading. Falls back to ones behind aircraft*/ |
| 267 | FGTaxiNodeRef findNearestNodeOnRunwayExit(const SGGeod& aGeod, FGRunway* aRunway = NULL) const; |
| 268 | |
| 269 | FGTaxiNodeRef findNearestNodeOffRunway(const SGGeod& aGeod, FGRunway* aRunway, double distanceM) const; |
| 270 | |
| 271 | FGTaxiSegment* findOppositeSegment(unsigned int index) const; |
| 272 | |
| 273 | const FGParkingList& allParkings() const; |
| 274 | |
| 275 | FGParkingRef getParkingByIndex(unsigned int index) const; |
| 276 | |
| 277 | FGParkingRef findParkingByName(const std::string& name) const; |
| 278 | |
| 279 | /** |
| 280 | * Find the taxiway segment joining two (ground-net) nodes. Returns |
| 281 | * NULL if no such segment exists. |
| 282 | * It is permitted to pass HULL for the 'to' indicating that any |
| 283 | * segment originating at 'from' is acceptable. |
| 284 | */ |
| 285 | FGTaxiSegment* findSegment(const FGTaxiNode* from, const FGTaxiNode* to) const; |
| 286 | /** Find the taxiway segment best matching the heading*/ |
| 287 | FGTaxiSegment* findSegmentByHeading(const FGTaxiNode* from, const double heading) const; |
| 288 | FGTaxiSegment* findSegment(unsigned int idx) const; |
| 289 | /** |
| 290 | * Find the segments connected to the node. |
| 291 | */ |
| 292 | FGTaxiNodeVector findSegmentsFrom(const FGTaxiNodeRef& from) const; |
| 293 | |
| 294 | |
| 295 | FGTaxiRoute findShortestRoute(FGTaxiNode* start, FGTaxiNode* end, bool fullSearch = true); |
| 296 | |
| 297 | |
| 298 | void blockSegmentsEndingAt(const FGTaxiSegment* seg, int blockId, time_t blockTime, time_t now); |
| 299 | |
| 300 | void addVersion(int v) { version = v; }; |
| 301 | void unblockAllSegments(time_t now); |
| 302 | |
| 303 | const intVec& getApproachFrequencies() const; |
| 304 | const intVec& getTowerFrequencies() const; |
| 305 | const intVec& getGroundFrequencies() const; |
| 306 | }; |