| 1 | /* |
| 2 | * SPDX-FileName: groundnet.cxx |
| 3 | * SPDX-FileComment: Implementation of the FlightGear airport ground handling code |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2004 Durk Talsma |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <config.h> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <cmath> |
| 12 | #include <fstream> |
| 13 | #include <iterator> |
| 14 | #include <map> |
| 15 | |
| 16 | #include <simgear/scene/util/OsgMath.hxx> |
| 17 | #include <simgear/debug/logstream.hxx> |
| 18 | #include <simgear/math/SGGeometryFwd.hxx> |
| 19 | #include <simgear/math/SGIntersect.hxx> |
| 20 | #include <simgear/math/SGLineSegment.hxx> |
| 21 | #include <simgear/structure/exception.hxx> |
| 22 | #include <simgear/timing/timestamp.hxx> |
| 23 | |
| 24 | #include <Airports/airport.hxx> |
| 25 | #include <Airports/runways.hxx> |
| 26 | #include <Scenery/scenery.hxx> |
| 27 | |
| 28 | #include "groundnetwork.hxx" |
| 29 | |
| 30 | using std::string; |
| 31 | |
| 32 | |
| 33 | /*************************************************************************** |
| 34 | * FGTaxiSegment |
| 35 | **************************************************************************/ |
| 36 | |
| 37 | FGTaxiSegment::FGTaxiSegment(FGTaxiNode* aStart, FGTaxiNode* aEnd) : startNode(aStart), |
| 38 | endNode(aEnd), |
| 39 | isActive(0), |
| 40 | index(0), |
| 41 | oppositeDirection(0) |
| 42 | { |
| 43 | if (!aStart || !aEnd) { |
| 44 | throw sg_exception("Missing node arguments creating FGTaxiSegment"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | SGGeod FGTaxiSegment::getCenter() const |
| 49 | { |
| 50 | FGTaxiNode *start(getStart()), *end(getEnd()); |
| 51 | double heading, length, az2; |
| 52 | SGGeodesy::inverse(start->geod(), end->geod(), heading, az2, length); |
| 53 | return SGGeodesy::direct(start->geod(), heading, length * 0.5); |
| 54 | } |
| 55 | |
| 56 | FGTaxiNodeRef FGTaxiSegment::getEnd() const |
| 57 | { |
| 58 | return const_cast<FGTaxiNode*>(endNode); |
| 59 | } |
| 60 | |
| 61 | FGTaxiNodeRef FGTaxiSegment::getStart() const |
| 62 | { |
| 63 | return const_cast<FGTaxiNode*>(startNode); |
| 64 | } |
| 65 | |
| 66 | double FGTaxiSegment::getLength() const |
| 67 | { |
| 68 | return dist(getStart()->cart(), getEnd()->cart()); |
| 69 | } |
| 70 | |
| 71 | double FGTaxiSegment::getHeading() const |
| 72 | { |
| 73 | return SGGeodesy::courseDeg(getStart()->geod(), getEnd()->geod()); |
| 74 | } |
| 75 | |
| 76 | void FGTaxiSegment::block(int id, time_t blockTime, time_t now) |
| 77 | { |
| 78 | BlockListIterator i = blockTimes.begin(); |
| 79 | while (i != blockTimes.end()) { |
| 80 | if (i->getId() == id) |
| 81 | break; |
| 82 | i++; |
| 83 | } |
| 84 | if (i == blockTimes.end()) { |
| 85 | blockTimes.push_back(Block(id, blockTime, now)); |
| 86 | sort(blockTimes.begin(), blockTimes.end()); |
| 87 | } else { |
| 88 | i->updateTimeStamps(blockTime, now); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // The segment has a block if any of the block times listed in the block list is |
| 93 | // smaller than the current time. |
| 94 | bool FGTaxiSegment::hasBlock(time_t now) |
| 95 | { |
| 96 | for (BlockListIterator i = blockTimes.begin(); i != blockTimes.end(); i++) { |
| 97 | if (i->getBlockTime() < now) |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | void FGTaxiSegment::unblock(time_t now) |
| 104 | { |
| 105 | if (blockTimes.empty()) |
| 106 | return; |
| 107 | |
| 108 | if (blockTimes.front().getTimeStamp() < (now - 30)) { |
| 109 | blockTimes.erase(blockTimes.begin()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /*************************************************************************** |
| 114 | * FGTaxiRoute |
| 115 | **************************************************************************/ |
| 116 | |
| 117 | FGTaxiRoute::FGTaxiRoute(const FGTaxiNodeVector& nds, const intVec& rts, double dist, int /* dpth */) : nodes(nds), |
| 118 | routes(rts), |
| 119 | distance(dist) |
| 120 | { |
| 121 | currNode = nodes.begin(); |
| 122 | currRoute = routes.begin(); |
| 123 | |
| 124 | if (nodes.size() != (routes.size()) + 1) { |
| 125 | SG_LOG(SG_GENERAL, SG_ALERT, "ALERT: Misconfigured TaxiRoute : " << nodes.size() << " " << routes.size()); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | bool FGTaxiRoute::next(FGTaxiNodeRef& node, int* rte) |
| 130 | { |
| 131 | if (nodes.size() != (routes.size()) + 1) { |
| 132 | throw sg_range_exception("Misconfigured taxi route"); |
| 133 | } |
| 134 | |
| 135 | if (currNode == nodes.end()) |
| 136 | return false; |
| 137 | node = *(currNode); |
| 138 | if (currNode != nodes.begin()) { |
| 139 | // work-around for FLIGHTGEAR-NJN: throw an exception here |
| 140 | // instead of crashing, to aid debugging |
| 141 | if (currRoute == routes.end()) { |
| 142 | throw sg_range_exception("Misconfigured taxi route"); |
| 143 | } |
| 144 | |
| 145 | *rte = *(currRoute); |
| 146 | ++currRoute; |
| 147 | } else { |
| 148 | // Handle special case for the first node. |
| 149 | *rte = -1 * *(currRoute); |
| 150 | } |
| 151 | ++currNode; |
| 152 | return true; |
| 153 | }; |
| 154 | |
| 155 | /*************************************************************************** |
| 156 | * FGGroundNetwork() |
| 157 | **************************************************************************/ |
| 158 | |
| 159 | FGGroundNetwork::FGGroundNetwork(FGAirport* airport) : parent(airport) |
| 160 | { |
| 161 | hasNetwork = false; |
| 162 | version = 0; |
| 163 | networkInitialized = false; |
| 164 | } |
| 165 | |
| 166 | FGGroundNetwork::~FGGroundNetwork() |
| 167 | { |
| 168 | for (auto seg : segments) { |
| 169 | delete seg; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void FGGroundNetwork::init() |
| 174 | { |
| 175 | if (networkInitialized) { |
| 176 | SG_LOG(SG_GENERAL, SG_WARN, "duplicate ground-network init"); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | hasNetwork = true; |
| 181 | int index = 1; |
| 182 | |
| 183 | // establish pairing of segments |
| 184 | for (auto segment : segments) { |
| 185 | segment->setIndex(index++); |
| 186 | |
| 187 | if (segment->oppositeDirection) { |
| 188 | continue; // already established |
| 189 | } |
| 190 | |
| 191 | FGTaxiSegment* opp = findSegment(segment->endNode, segment->startNode); |
| 192 | if (opp) { |
| 193 | assert(opp->oppositeDirection == NULL); |
| 194 | segment->oppositeDirection = opp; |
| 195 | opp->oppositeDirection = segment; |
| 196 | } |
| 197 | |
| 198 | // establish node -> segment end cache |
| 199 | m_segmentsEndingAtNodeMap.insert(NodeFromSegmentMap::value_type{segment->getEnd(), segment}); |
| 200 | } |
| 201 | |
| 202 | networkInitialized = true; |
| 203 | } |
| 204 | |
| 205 | FGTaxiNodeRef FGGroundNetwork::findNearestNode(const SGGeod& aGeod) const |
| 206 | { |
| 207 | double d = DBL_MAX; |
| 208 | SGVec3d cartPos = SGVec3d::fromGeod(aGeod); |
| 209 | FGTaxiNodeRef result; |
| 210 | |
| 211 | FGTaxiNodeVector::const_iterator it; |
| 212 | for (it = m_nodes.begin(); it != m_nodes.end(); ++it) { |
| 213 | double localDistanceSqr = distSqr(cartPos, (*it)->cart()); |
| 214 | if (localDistanceSqr < d) { |
| 215 | d = localDistanceSqr; |
| 216 | result = *it; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | FGTaxiNodeRef FGGroundNetwork::findNearestNodeOffRunway(const SGGeod& aGeod, FGRunway* rwy, double marginM) const |
| 224 | { |
| 225 | FGTaxiNodeVector nodes; |
| 226 | const SGLineSegmentd runwayLine(rwy->cart(), SGVec3d::fromGeod(rwy->end())); |
| 227 | const double marginMSqr = marginM * marginM; |
| 228 | const SGVec3d cartPos = SGVec3d::fromGeod(aGeod); |
| 229 | |
| 230 | std::copy_if(m_nodes.begin(), m_nodes.end(), std::back_inserter(nodes), |
| 231 | [runwayLine, cartPos, marginMSqr](const FGTaxiNodeRef& a) { |
| 232 | if (a->getIsOnRunway()) return false; |
| 233 | |
| 234 | // exclude parking positions from consideration. This helps to |
| 235 | // exclude airports whose ground nets only list parking positions, |
| 236 | // since these typically produce bad results. See discussion in |
| 237 | // https://sourceforge.net/p/flightgear/codetickets/2110/ |
| 238 | if (a->type() == FGPositioned::PARKING) return false; |
| 239 | |
| 240 | return (distSqr(runwayLine, a->cart()) >= marginMSqr); |
| 241 | }); |
| 242 | |
| 243 | |
| 244 | // find closest of matching nodes |
| 245 | auto node = std::min_element(nodes.begin(), nodes.end(), |
| 246 | [cartPos](const FGTaxiNodeRef& a, const FGTaxiNodeRef& b) { return distSqr(cartPos, a->cart()) < distSqr(cartPos, b->cart()); }); |
| 247 | |
| 248 | if (node == nodes.end()) { |
| 249 | return FGTaxiNodeRef(); |
| 250 | } |
| 251 | |
| 252 | return *node; |
| 253 | } |
| 254 | |
| 255 | FGTaxiNodeRef FGGroundNetwork::findNearestNodeOnRunwayEntry(const SGGeod& aGeod) const |
| 256 | { |
| 257 | double d = DBL_MAX; |
| 258 | SGVec3d cartPos = SGVec3d::fromGeod(aGeod); |
| 259 | FGTaxiNodeRef result; |
| 260 | for (auto it = m_nodes.begin(); it != m_nodes.end(); ++it) { |
| 261 | if (!(*it)->getIsOnRunway()) |
| 262 | continue; |
| 263 | double localDistanceSqr = distSqr(cartPos, (*it)->cart()); |
| 264 | if (localDistanceSqr < d) { |
| 265 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunway from Threshold " << localDistanceSqr); |
| 266 | d = localDistanceSqr; |
| 267 | result = *it; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | FGTaxiNodeRef FGGroundNetwork::findNearestNodeOnRunwayExit(const SGGeod& aGeod, FGRunway* aRunway) const |
| 275 | { |
| 276 | double d = DBL_MAX; |
| 277 | SGVec3d cartPos = SGVec3d::fromGeod(aGeod); |
| 278 | FGTaxiNodeRef result = 0; |
| 279 | FGTaxiNodeVector::const_iterator it; |
| 280 | if (aRunway) { |
| 281 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit " << aRunway->ident() << " " << aRunway->headingDeg()); |
| 282 | for (it = m_nodes.begin(); it != m_nodes.end(); ++it) { |
| 283 | if (!(*it)->getIsOnRunway()) |
| 284 | continue; |
| 285 | double localDistanceSqr = distSqr(cartPos, (*it)->cart()); |
| 286 | double headingTowardsExit = SGGeodesy::courseDeg(aGeod, (*it)->geod()); |
| 287 | double diff = fabs(SGMiscd::normalizePeriodic(-180, 180, aRunway->headingDeg() - headingTowardsExit)); |
| 288 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit Diff : " << diff << " Id : " << (*it)->getIndex()); |
| 289 | if (diff > 10) { |
| 290 | // Only ahead |
| 291 | continue; |
| 292 | } |
| 293 | FGTaxiNodeVector exitSegments = findSegmentsFrom((*it)); |
| 294 | // Some kind of star |
| 295 | if (exitSegments.size() > 2) { |
| 296 | continue; |
| 297 | } |
| 298 | // two segments and next points are on runway, too. Must be a segment before end |
| 299 | // single runway point not at end is ok |
| 300 | if (exitSegments.size() == 2 && |
| 301 | ((*exitSegments.at(0)).getIsOnRunway() || (*exitSegments.at(0)).getIsOnRunway())) { |
| 302 | continue; |
| 303 | } |
| 304 | if (exitSegments.empty()) { |
| 305 | SG_LOG(SG_AI, SG_ALERT, "findNearestNodeOnRunwayExit Broken :" << diff << " Id : " << (*it)->getIndex() << " Apt : " << aRunway->airport()->getId()); |
| 306 | continue; |
| 307 | } |
| 308 | double exitHeading = SGGeodesy::courseDeg((*it)->geod(), |
| 309 | (exitSegments.back())->geod()); |
| 310 | diff = fabs(SGMiscd::normalizePeriodic(-180, 180, aRunway->headingDeg() - exitHeading)); |
| 311 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit2 Diff :" << diff << " Id : " << (*it)->getIndex()); |
| 312 | if (diff > 70) { |
| 313 | // Only exits going in our direction |
| 314 | continue; |
| 315 | } |
| 316 | if (localDistanceSqr < d) { |
| 317 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit3 " << localDistanceSqr << " " << (*it)->getIndex()); |
| 318 | d = localDistanceSqr; |
| 319 | result = *it; |
| 320 | } |
| 321 | } |
| 322 | } else { |
| 323 | SG_LOG(SG_AI, SG_BULK, "No Runway findNearestNodeOnRunwayExit"); |
| 324 | } |
| 325 | if (result) { |
| 326 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit found :" << result->getIndex()); |
| 327 | return result; |
| 328 | } |
| 329 | // Ok then fallback only exits ahead |
| 330 | for (it = m_nodes.begin(); it != m_nodes.end(); ++it) { |
| 331 | if (!(*it)->getIsOnRunway()) |
| 332 | continue; |
| 333 | double localDistanceSqr = distSqr(cartPos, (*it)->cart()); |
| 334 | if (aRunway) { |
| 335 | double headingTowardsExit = SGGeodesy::courseDeg(aGeod, (*it)->geod()); |
| 336 | double diff = fabs(aRunway->headingDeg() - headingTowardsExit); |
| 337 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback1 " << aRunway->headingDeg() << " " |
| 338 | << " Diff : " << diff << " " << (*it)->getIndex()); |
| 339 | if (diff > 10) { |
| 340 | // Only ahead |
| 341 | continue; |
| 342 | } |
| 343 | } |
| 344 | if (localDistanceSqr < d) { |
| 345 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback1 " << localDistanceSqr); |
| 346 | d = localDistanceSqr; |
| 347 | result = *it; |
| 348 | } |
| 349 | } |
| 350 | if (result) { |
| 351 | return result; |
| 352 | } |
| 353 | // Ok then fallback only exits |
| 354 | for (it = m_nodes.begin(); it != m_nodes.end(); ++it) { |
| 355 | if (!(*it)->getIsOnRunway()) |
| 356 | continue; |
| 357 | double localDistanceSqr = distSqr(cartPos, (*it)->cart()); |
| 358 | if (localDistanceSqr < d) { |
| 359 | SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback2 " << localDistanceSqr); |
| 360 | d = localDistanceSqr; |
| 361 | result = *it; |
| 362 | } |
| 363 | } |
| 364 | if (result) { |
| 365 | return result; |
| 366 | } else { |
| 367 | SG_LOG(SG_AI, SG_WARN, "No runway exit found " << aRunway->airport()->getId() << "/" << aRunway->name()); |
| 368 | return 0; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | FGTaxiSegment* FGGroundNetwork::findOppositeSegment(unsigned int index) const |
| 373 | { |
| 374 | FGTaxiSegment* seg = findSegment(index); |
| 375 | if (!seg) |
| 376 | return NULL; |
| 377 | return seg->opposite(); |
| 378 | } |
| 379 | |
| 380 | const FGParkingList& FGGroundNetwork::allParkings() const |
| 381 | { |
| 382 | return m_parkings; |
| 383 | } |
| 384 | |
| 385 | FGTaxiSegment* FGGroundNetwork::findSegment(unsigned idx) const |
| 386 | { |
| 387 | if ((idx > 0) && (idx <= segments.size())) |
| 388 | return segments[idx - 1]; |
| 389 | else { |
| 390 | //cerr << "Alert: trying to find invalid segment " << idx << endl; |
| 391 | return 0; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | FGTaxiSegment* FGGroundNetwork::findSegment(const FGTaxiNode* from, const FGTaxiNode* to) const |
| 396 | { |
| 397 | if (from == 0) { |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | // completely boring linear search of segments. Can be improved if/when |
| 402 | // this ever becomes a hot-spot |
| 403 | for (auto seg : segments) { |
| 404 | if (seg->startNode != from) { |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | if ((to == 0) || (seg->endNode == to)) { |
| 409 | return seg; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return NULL; // not found |
| 414 | } |
| 415 | |
| 416 | static int edgePenalty(FGTaxiNode* tn) |
| 417 | { |
| 418 | return (tn->type() == FGPositioned::PARKING ? 10000 : 0) + |
| 419 | (tn->getIsOnRunway() ? 1000 : 0); |
| 420 | } |
| 421 | |
| 422 | class ShortestPathData |
| 423 | { |
| 424 | public: |
| 425 | ShortestPathData() : score(HUGE_VAL) |
| 426 | { |
| 427 | } |
| 428 | |
| 429 | double score; |
| 430 | FGTaxiNodeRef previousNode; |
| 431 | }; |
| 432 | |
| 433 | FGTaxiRoute FGGroundNetwork::findShortestRoute(FGTaxiNode* start, FGTaxiNode* end, bool fullSearch) |
| 434 | { |
| 435 | if (!start || !end) { |
| 436 | throw sg_exception("Bad arguments to findShortestRoute"); |
| 437 | } |
| 438 | //implements Dijkstra's algorithm to find shortest distance route from start to end |
| 439 | //taken from http://en.wikipedia.org/wiki/Dijkstra's_algorithm |
| 440 | FGTaxiNodeVector unvisited(m_nodes); |
| 441 | std::map<FGTaxiNode*, ShortestPathData> searchData; |
| 442 | |
| 443 | searchData[start].score = 0.0; |
| 444 | |
| 445 | while (!unvisited.empty()) { |
| 446 | // find lowest scored unvisited |
| 447 | FGTaxiNodeRef best = unvisited.front(); |
| 448 | for (auto i : unvisited) { |
| 449 | if (searchData[i].score < searchData[best].score) { |
| 450 | best = i; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // remove 'best' from the unvisited set |
| 455 | FGTaxiNodeVectorIterator newend = |
| 456 | remove(unvisited.begin(), unvisited.end(), best); |
| 457 | unvisited.erase(newend, unvisited.end()); |
| 458 | |
| 459 | if (best == end) { // found route or best not connected |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | for (auto target : findSegmentsFrom(best)) { |
| 464 | double edgeLength = dist(best->cart(), target->cart()); |
| 465 | double alt = searchData[best].score + edgeLength + edgePenalty(target); |
| 466 | if (alt < searchData[target].score) { // Relax (u,v) |
| 467 | searchData[target].score = alt; |
| 468 | searchData[target].previousNode = best; |
| 469 | } |
| 470 | } // of outgoing arcs/segments from current best node iteration |
| 471 | } // of unvisited nodes remaining |
| 472 | |
| 473 | if (searchData[end].score == HUGE_VAL) { |
| 474 | // no valid route found |
| 475 | if (fullSearch && start && end) { |
| 476 | SG_LOG(SG_GENERAL, SG_ALERT, |
| 477 | "Failed to find route from waypoint " << start->getIndex() << " to " |
| 478 | << end->getIndex() << " at " << parent->getId()); |
| 479 | } |
| 480 | |
| 481 | return FGTaxiRoute(); |
| 482 | } |
| 483 | |
| 484 | // assemble route from backtrace information |
| 485 | FGTaxiNodeVector nodes; |
| 486 | intVec routes; |
| 487 | FGTaxiNode* bt = end; |
| 488 | |
| 489 | while (searchData[bt].previousNode != 0) { |
| 490 | nodes.push_back(bt); |
| 491 | FGTaxiSegment* segment = findSegment(searchData[bt].previousNode, bt); |
| 492 | int idx = segment->getIndex(); |
| 493 | routes.push_back(idx); |
| 494 | bt = searchData[bt].previousNode; |
| 495 | } |
| 496 | nodes.push_back(start); |
| 497 | reverse(nodes.begin(), nodes.end()); |
| 498 | reverse(routes.begin(), routes.end()); |
| 499 | return FGTaxiRoute(nodes, routes, searchData[end].score, 0); |
| 500 | } |
| 501 | |
| 502 | void FGGroundNetwork::unblockAllSegments(time_t now) |
| 503 | { |
| 504 | for (auto& seg : segments) { |
| 505 | seg->unblock(now); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void FGGroundNetwork::blockSegmentsEndingAt(const FGTaxiSegment* seg, int blockId, time_t blockTime, time_t now) |
| 510 | { |
| 511 | if (!seg) |
| 512 | throw sg_exception("Passed invalid segment"); |
| 513 | |
| 514 | const auto range = m_segmentsEndingAtNodeMap.equal_range(seg->getEnd()); |
| 515 | for (auto it = range.first; it != range.second; ++it) { |
| 516 | // our inbound segment will be included, so skip it |
| 517 | if (it->second == seg) |
| 518 | continue; |
| 519 | |
| 520 | it->second->block(blockId, blockTime, now); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | FGTaxiNodeRef FGGroundNetwork::findNodeByIndex(int index) const |
| 525 | { |
| 526 | FGTaxiNodeVector::const_iterator it; |
| 527 | for (it = m_nodes.begin(); it != m_nodes.end(); ++it) { |
| 528 | if ((*it)->getIndex() == index) { |
| 529 | return *it; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return FGTaxiNodeRef(); |
| 534 | } |
| 535 | |
| 536 | FGParkingRef FGGroundNetwork::getParkingByIndex(unsigned int index) const |
| 537 | { |
| 538 | FGTaxiNodeRef tn = findNodeByIndex(index); |
| 539 | if (!tn.valid() || (tn->type() != FGPositioned::PARKING)) { |
| 540 | return FGParkingRef(); |
| 541 | } |
| 542 | |
| 543 | return FGParkingRef(static_cast<FGParking*>(tn.ptr())); |
| 544 | } |
| 545 | |
| 546 | FGParkingRef FGGroundNetwork::findParkingByName(const string& name) const |
| 547 | { |
| 548 | auto it = std::find_if(m_parkings.begin(), m_parkings.end(), [name](const FGParkingRef& p) { |
| 549 | return p->ident() == name; |
| 550 | }); |
| 551 | |
| 552 | if (it == m_parkings.end()) |
| 553 | return nullptr; |
| 554 | |
| 555 | return *it; |
| 556 | } |
| 557 | |
| 558 | void FGGroundNetwork::addSegment(const FGTaxiNodeRef& from, const FGTaxiNodeRef& to) |
| 559 | { |
| 560 | FGTaxiSegment* seg = new FGTaxiSegment(from, to); |
| 561 | segments.push_back(seg); |
| 562 | |
| 563 | FGTaxiNodeVector::iterator it = std::find(m_nodes.begin(), m_nodes.end(), from); |
| 564 | if (it == m_nodes.end()) { |
| 565 | m_nodes.push_back(from); |
| 566 | } |
| 567 | |
| 568 | it = std::find(m_nodes.begin(), m_nodes.end(), to); |
| 569 | if (it == m_nodes.end()) { |
| 570 | m_nodes.push_back(to); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | void FGGroundNetwork::addParking(const FGParkingRef& park) |
| 575 | { |
| 576 | m_parkings.push_back(park); |
| 577 | |
| 578 | |
| 579 | FGTaxiNodeVector::iterator it = std::find(m_nodes.begin(), m_nodes.end(), park); |
| 580 | if (it == m_nodes.end()) { |
| 581 | m_nodes.push_back(park); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | FGTaxiNodeVector FGGroundNetwork::findSegmentsFrom(const FGTaxiNodeRef& from) const |
| 586 | { |
| 587 | FGTaxiNodeVector result; |
| 588 | FGTaxiSegmentVector::const_iterator it; |
| 589 | for (it = segments.begin(); it != segments.end(); ++it) { |
| 590 | if ((*it)->getStart() == from) { |
| 591 | result.push_back((*it)->getEnd()); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return result; |
| 596 | } |
| 597 | |
| 598 | FGTaxiSegment* FGGroundNetwork::findSegmentByHeading(const FGTaxiNode* from, const double heading) const |
| 599 | { |
| 600 | if (from == 0) { |
| 601 | return NULL; |
| 602 | } |
| 603 | |
| 604 | FGTaxiSegment* best = nullptr; |
| 605 | |
| 606 | // completely boring linear search of segments. Can be improved if/when |
| 607 | // this ever becomes a hot-spot |
| 608 | for (auto seg : segments) { |
| 609 | if (seg->startNode != from) { |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | if (!best || fabs(best->getHeading() - heading) > fabs(seg->getHeading() - heading)) { |
| 614 | best = seg; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return best; // not found |
| 619 | } |
| 620 | |
| 621 | const intVec& FGGroundNetwork::getApproachFrequencies() const |
| 622 | { |
| 623 | return freqApproach; |
| 624 | } |
| 625 | |
| 626 | const intVec& FGGroundNetwork::getTowerFrequencies() const |
| 627 | { |
| 628 | return freqTower; |
| 629 | } |
| 630 | |
| 631 | const intVec& FGGroundNetwork::getGroundFrequencies() const |
| 632 | { |
| 633 | return freqGround; |
| 634 | } |