| 1 | // airways.hxx - storage of airways network, and routing between nodes |
| 2 | // Written by James Turner, started 2009. |
| 3 | // |
| 4 | // Copyright (C) 2009 Curtis L. Olson |
| 5 | // |
| 6 | // This program is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU General Public License as |
| 8 | // published by the Free Software Foundation; either version 2 of the |
| 9 | // License, or (at your option) any later version. |
| 10 | // |
| 11 | // This program is distributed in the hope that it will be useful, but |
| 12 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | // General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with this program; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | |
| 20 | #ifndef FG_AIRWAYS_HXX |
| 21 | #define FG_AIRWAYS_HXX |
| 22 | |
| 23 | #include <map> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <Navaids/route.hxx> |
| 27 | #include <Navaids/positioned.hxx> |
| 28 | |
| 29 | class SGPath; |
| 30 | typedef SGSharedPtr<FGPositioned> FGPositionedRef; |
| 31 | |
| 32 | namespace flightgear { |
| 33 | |
| 34 | // forward declare some helpers |
| 35 | struct SearchContext; |
| 36 | class AdjacentWaypoint; |
| 37 | class InAirwayFilter; |
| 38 | class Airway; |
| 39 | |
| 40 | using AirwayRef = SGSharedPtr<Airway>; |
| 41 | |
| 42 | class Airway : public RouteBase |
| 43 | { |
| 44 | public: |
| 45 | enum Level { |
| 46 | UnknownLevel = 0, |
| 47 | LowLevel = 1, /// Victor airways |
| 48 | HighLevel = 2, /// Jet airways |
| 49 | Both = 3 |
| 50 | }; |
| 51 | |
| 52 | std::string ident() const override |
| 53 | { return _ident; } |
| 54 | |
| 55 | int cacheId() const |
| 56 | { return _cacheId; } |
| 57 | |
| 58 | Level level() const |
| 59 | { return _level; } |
| 60 | |
| 61 | static void loadAWYDat(const SGPath& path); |
| 62 | |
| 63 | double topAltitudeFt() const |
| 64 | { return _topAltitudeFt; } |
| 65 | |
| 66 | double bottomAltitudeFt() const |
| 67 | { return _bottomAltitudeFt; } |
| 68 | |
| 69 | static AirwayRef loadByCacheId(int cacheId); |
| 70 | |
| 71 | static AirwayRef findByIdent(const std::string& aIdent, Level level); |
| 72 | |
| 73 | /** |
| 74 | * Find the airway based on its ident. IF both high- and low- level idents |
| 75 | * exist, select the one which can route between the from and to waypoints |
| 76 | * correctly, preferring high-level airways. |
| 77 | */ |
| 78 | static AirwayRef findByIdentAndVia(const std::string& aIdent, const WayptRef& from, const WayptRef& to); |
| 79 | |
| 80 | /** |
| 81 | * Find an airway by ident, and containing a particula rnavaid/fix. |
| 82 | */ |
| 83 | static AirwayRef findByIdentAndNavaid(const std::string& aIdent, const FGPositionedRef nav); |
| 84 | |
| 85 | WayptRef findEnroute(const std::string& aIdent) const; |
| 86 | |
| 87 | bool canVia(const WayptRef& from, const WayptRef& to) const; |
| 88 | |
| 89 | WayptVec via(const WayptRef& from, const WayptRef& to) const; |
| 90 | |
| 91 | bool containsNavaid(const FGPositionedRef& navaid) const; |
| 92 | |
| 93 | WayptRef findEnroute(const FGPositionedRef& navaid) const; |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Track a network of airways |
| 98 | * |
| 99 | */ |
| 100 | class Network |
| 101 | { |
| 102 | public: |
| 103 | friend class Airway; |
| 104 | friend class InAirwayFilter; |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Principal routing algorithm. Attempts to find the best route beween |
| 109 | * two points. If either point is part of the airway network (e.g, a SID |
| 110 | * or STAR transition), it will <em>not</em> be duplicated in the result |
| 111 | * path. |
| 112 | * |
| 113 | * Returns true if a route could be found, or false otherwise. |
| 114 | */ |
| 115 | bool route(WayptRef aFrom, WayptRef aTo, WayptVec& aPath); |
| 116 | |
| 117 | /** |
| 118 | * Overloaded version working with a raw SGGeod |
| 119 | */ |
| 120 | |
| 121 | std::pair<FGPositionedRef, bool> findClosestNode(const SGGeod& aGeod); |
| 122 | |
| 123 | FGPositionedRef findNodeByIdent(const std::string& ident, const SGGeod& near) const; |
| 124 | private: |
| 125 | void addEdge(int aWay, const SGGeod& aStartPos, |
| 126 | const std::string& aStartIdent, |
| 127 | const SGGeod& aEndPos, const std::string& aEndIdent); |
| 128 | |
| 129 | int findAirway(const std::string& aName); |
| 130 | |
| 131 | bool cleanGeneratedPath(WayptRef aFrom, WayptRef aTo, WayptVec& aPath, |
| 132 | bool exactTo, bool exactFrom); |
| 133 | |
| 134 | bool search2(FGPositionedRef aStart, FGPositionedRef aDest, WayptVec& aRoute); |
| 135 | |
| 136 | /** |
| 137 | * Test if a positioned item is part of this airway network or not. |
| 138 | */ |
| 139 | bool inNetwork(PositionedID pos) const; |
| 140 | |
| 141 | /** |
| 142 | * Find the closest node on the network, to the specified waypoint |
| 143 | * |
| 144 | * May return NULL,false if no match could be found; the search is |
| 145 | * internally limited to avoid very poor performance; for example, |
| 146 | * in the middle of an ocean. |
| 147 | * |
| 148 | * The second return value indicates if the returned value is |
| 149 | * equal (true) or distinct (false) to the input waypoint. |
| 150 | * Equality here means being physically within a close tolerance, |
| 151 | * on the order of a hundred metres. |
| 152 | */ |
| 153 | std::pair<FGPositionedRef, bool> findClosestNode(WayptRef aRef); |
| 154 | |
| 155 | /** |
| 156 | * cache which positioned items are in this network |
| 157 | */ |
| 158 | typedef std::map<PositionedID, bool> NetworkMembershipDict; |
| 159 | mutable NetworkMembershipDict _inNetworkCache; |
| 160 | |
| 161 | Level _networkID; |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | static Network* highLevel(); |
| 166 | static Network* lowLevel(); |
| 167 | |
| 168 | private: |
| 169 | Airway(const std::string& aIdent, const Level level, int dbId, int aTop, int aBottom); |
| 170 | |
| 171 | void loadWaypoints() const; |
| 172 | |
| 173 | WayptVec::const_iterator find(WayptRef wpt) const; |
| 174 | |
| 175 | friend class Network; |
| 176 | friend class NavDataCache; |
| 177 | |
| 178 | const std::string _ident; |
| 179 | const Level _level; |
| 180 | const int _cacheId; |
| 181 | |
| 182 | int _topAltitudeFt; |
| 183 | int _bottomAltitudeFt; |
| 184 | |
| 185 | mutable WayptVec _elements; |
| 186 | }; |
| 187 | |
| 188 | } // of namespace flightgear |
| 189 | |
| 190 | |
| 191 | #endif //of FG_AIRWAYS_HXX |