| 1 | /* |
| 2 | * SPDX-FileCopyrightText: (C) 2008 James Turner <[email protected]> |
| 3 | * SPDX_FileComment: base class for objects which are spatially located in the simulated world |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <cassert> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #include <simgear/sg_inlines.h> |
| 15 | #include <simgear/structure/SGSharedPtr.hxx> |
| 16 | #include <simgear/math/SGMath.hxx> |
| 17 | |
| 18 | class FGPositioned; |
| 19 | typedef SGSharedPtr<FGPositioned> FGPositionedRef; |
| 20 | typedef std::vector<FGPositionedRef> FGPositionedList; |
| 21 | |
| 22 | typedef int64_t PositionedID; |
| 23 | typedef std::vector<PositionedID> PositionedIDVec; |
| 24 | |
| 25 | namespace flightgear { class NavDataCache; } |
| 26 | |
| 27 | class FGPositioned : public SGReferenced |
| 28 | { |
| 29 | public: |
| 30 | typedef enum { |
| 31 | INVALID = 0, |
| 32 | AIRPORT, |
| 33 | HELIPORT, |
| 34 | SEAPORT, |
| 35 | RUNWAY, |
| 36 | HELIPAD, |
| 37 | TAXIWAY, |
| 38 | PAVEMENT, |
| 39 | WAYPOINT, |
| 40 | FIX, |
| 41 | NDB, |
| 42 | VOR, |
| 43 | ILS, |
| 44 | LOC, |
| 45 | GS, |
| 46 | OM, |
| 47 | MM, |
| 48 | IM, |
| 49 | /// important that DME & TACAN are adjacent to keep the TacanFilter |
| 50 | /// efficient - DMEs are proxies for TACAN/VORTAC stations |
| 51 | DME, |
| 52 | TACAN, |
| 53 | MOBILE_TACAN, |
| 54 | OBSTACLE, |
| 55 | /// an actual airport tower - not a radio comms facility! |
| 56 | /// some airports have multiple towers, eg EHAM, although our data source |
| 57 | /// doesn't necessarily include them |
| 58 | TOWER, |
| 59 | //comm stations : if extending this, be sure to update the isType check in |
| 60 | // CommStation.hxx |
| 61 | FREQ_GROUND, |
| 62 | FREQ_TOWER, |
| 63 | FREQ_ATIS, |
| 64 | FREQ_AWOS, |
| 65 | FREQ_APP_DEP, |
| 66 | FREQ_ENROUTE, |
| 67 | FREQ_CLEARANCE, |
| 68 | FREQ_UNICOM, |
| 69 | // groundnet items |
| 70 | PARKING, ///< parking position - might be a gate, or stand |
| 71 | TAXI_NODE, |
| 72 | // POI items |
| 73 | COUNTRY, |
| 74 | CITY, |
| 75 | TOWN, |
| 76 | VILLAGE, |
| 77 | VISUAL_REPORTING_POINT, |
| 78 | LAST_POI_TYPE, |
| 79 | LAST_TYPE |
| 80 | } Type; |
| 81 | |
| 82 | virtual ~FGPositioned(); |
| 83 | |
| 84 | Type type() const |
| 85 | { |
| 86 | return mType; |
| 87 | } |
| 88 | |
| 89 | // True for the following types: AIRPORT, HELIPORT, SEAPORT. |
| 90 | // False for other types, as well as if pos == nullptr. |
| 91 | static bool isAirportType(FGPositioned* pos); |
| 92 | // True for the following type: RUNWAY. |
| 93 | // False for other types, as well as if pos == nullptr. |
| 94 | static bool isRunwayType(FGPositioned* pos); |
| 95 | // True for the following types: NDB, VOR, ILS, LOC, GS, DME, TACAN. |
| 96 | // False for other types, as well as if pos == nullptr. |
| 97 | static bool isNavaidType(FGPositioned* pos); |
| 98 | |
| 99 | const char* typeString() const |
| 100 | { return nameForType(mType); } |
| 101 | |
| 102 | const std::string& ident() const |
| 103 | { return mIdent; } |
| 104 | |
| 105 | /** |
| 106 | * Return the name of this positioned. By default this is the same as the |
| 107 | * ident, but for many derived classes it's more meaningful - the aiport or |
| 108 | * navaid name, for example. |
| 109 | */ |
| 110 | virtual const std::string& name() const |
| 111 | { return mIdent; } |
| 112 | |
| 113 | virtual const SGGeod& geod() const |
| 114 | { return mPosition; } |
| 115 | |
| 116 | PositionedID guid() const |
| 117 | { return mGuid; } |
| 118 | |
| 119 | /** |
| 120 | * The cartesian position associated with this object |
| 121 | */ |
| 122 | virtual const SGVec3d& cart() const; |
| 123 | |
| 124 | double latitude() const |
| 125 | { return geod().getLatitudeDeg(); } |
| 126 | |
| 127 | double longitude() const |
| 128 | { return geod().getLongitudeDeg(); } |
| 129 | |
| 130 | double elevation() const |
| 131 | { return geod().getElevationFt(); } |
| 132 | |
| 133 | double elevationM() const |
| 134 | { return geod().getElevationM(); } |
| 135 | |
| 136 | /** |
| 137 | * Predicate class to support custom filtering of FGPositioned queries |
| 138 | * Default implementation of this passes any FGPositioned instance. |
| 139 | */ |
| 140 | class Filter |
| 141 | { |
| 142 | public: |
| 143 | virtual ~Filter() { ; } |
| 144 | |
| 145 | /** |
| 146 | * Over-rideable filter method. Default implementation returns true. |
| 147 | */ |
| 148 | virtual bool pass(FGPositioned* aPos) const |
| 149 | { return true; } |
| 150 | |
| 151 | virtual Type minType() const |
| 152 | { return INVALID; } |
| 153 | |
| 154 | virtual Type maxType() const |
| 155 | { return INVALID; } |
| 156 | |
| 157 | |
| 158 | bool operator()(FGPositioned* aPos) const |
| 159 | { return pass(aPos); } |
| 160 | }; |
| 161 | |
| 162 | class TypeFilter : public Filter |
| 163 | { |
| 164 | public: |
| 165 | TypeFilter(Type aTy = INVALID); |
| 166 | |
| 167 | TypeFilter(std::initializer_list<Type> types); |
| 168 | |
| 169 | /** |
| 170 | * @brief Construct a new Type Filter based on a sequential range of types |
| 171 | * |
| 172 | * @param aMinType |
| 173 | * @param aMaxType |
| 174 | */ |
| 175 | TypeFilter(Type aMinType, Type aMaxType); |
| 176 | |
| 177 | bool pass(FGPositioned* aPos) const override; |
| 178 | |
| 179 | Type minType() const override |
| 180 | { return mMinType; } |
| 181 | |
| 182 | Type maxType() const override |
| 183 | { return mMaxType; } |
| 184 | |
| 185 | void addType(Type aTy); |
| 186 | |
| 187 | static TypeFilter fromString(const std::string& aFilterSpec); |
| 188 | private: |
| 189 | |
| 190 | std::vector<Type> types; |
| 191 | Type mMinType = LAST_TYPE, |
| 192 | mMaxType = INVALID; |
| 193 | }; |
| 194 | |
| 195 | static FGPositionedList findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter); |
| 196 | |
| 197 | static FGPositionedList findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial); |
| 198 | |
| 199 | static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL); |
| 200 | |
| 201 | static FGPositionedRef findFirstWithIdent(const std::string& aIdent, Filter* aFilter); |
| 202 | |
| 203 | /** |
| 204 | * Find all items with the specified ident |
| 205 | * @param aFilter - optional filter on items |
| 206 | */ |
| 207 | static FGPositionedList findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true); |
| 208 | |
| 209 | /** |
| 210 | * As above, but searches names instead of idents |
| 211 | */ |
| 212 | static FGPositionedList findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true); |
| 213 | |
| 214 | /** |
| 215 | * Sort an FGPositionedList by distance from a position |
| 216 | */ |
| 217 | static void sortByRange(FGPositionedList&, const SGGeod& aPos); |
| 218 | |
| 219 | /** |
| 220 | * Find the closest item to a position, which pass the specified filter |
| 221 | * A cutoff range in NM must be specified, to constrain the search acceptably. |
| 222 | * Very large cutoff values will make this slow. |
| 223 | * |
| 224 | * @result The closest item passing the filter, or NULL |
| 225 | * @param aCutoffNm - maximum distance to search within, in nautical miles |
| 226 | */ |
| 227 | static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL); |
| 228 | |
| 229 | /** |
| 230 | * Find the closest N items to a position, which pass the specified filter |
| 231 | * A cutoff range in NM must be specified, to constrain the search acceptably. |
| 232 | * Very large cutoff values will make this slow. |
| 233 | * |
| 234 | * @result The matches (possibly less than N, depending on the filter and cutoff), |
| 235 | * sorted by distance from the search pos |
| 236 | * @param aN - number of matches to find |
| 237 | * @param aCutoffNm - maximum distance to search within, in nautical miles |
| 238 | */ |
| 239 | static FGPositionedList findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL); |
| 240 | |
| 241 | /** |
| 242 | * Same as above, but with a time-bound in msec too. |
| 243 | */ |
| 244 | static FGPositionedList findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter, |
| 245 | bool& aPartial); |
| 246 | |
| 247 | template<class T> |
| 248 | static SGSharedPtr<T> loadById(PositionedID id) |
| 249 | { |
| 250 | return static_pointer_cast<T>( loadByIdImpl(id) ); |
| 251 | } |
| 252 | |
| 253 | template<class T> |
| 254 | static SGSharedPtr<T> loadById(const PositionedIDVec& id_vec, size_t index) |
| 255 | { |
| 256 | assert(index >= 0 && index < id_vec.size()); |
| 257 | return loadById<T>(id_vec[index]); |
| 258 | } |
| 259 | |
| 260 | template<class T> |
| 261 | static std::vector<SGSharedPtr<T> > loadAllById(const PositionedIDVec& id_vec) |
| 262 | { |
| 263 | std::vector<SGSharedPtr<T> > vec(id_vec.size()); |
| 264 | |
| 265 | for(size_t i = 0; i < id_vec.size(); ++i) |
| 266 | vec[i] = loadById<T>(id_vec[i]); |
| 267 | |
| 268 | return vec; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Map a candidate type string to a real type. Returns INVALID if the string |
| 273 | * does not correspond to a defined type. |
| 274 | */ |
| 275 | static Type typeFromName(const std::string& aName); |
| 276 | |
| 277 | /** |
| 278 | * Map a type to a human-readable string |
| 279 | */ |
| 280 | static const char* nameForType(Type aTy); |
| 281 | |
| 282 | static FGPositionedRef createWaypoint(FGPositioned::Type aType, const std::string& aIdent, const SGGeod& aPos, |
| 283 | bool isTemporary = false, |
| 284 | const std::string& aName = {}); |
| 285 | static bool deleteWaypoint(FGPositionedRef aWpt); |
| 286 | protected: |
| 287 | friend class flightgear::NavDataCache; |
| 288 | |
| 289 | FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos); |
| 290 | |
| 291 | // called by NavDataCache::updatePosition |
| 292 | void modifyPosition(const SGGeod& newPos); |
| 293 | void invalidatePosition(); |
| 294 | |
| 295 | static FGPositionedRef loadByIdImpl(PositionedID id); |
| 296 | |
| 297 | const PositionedID mGuid; |
| 298 | const Type mType; |
| 299 | const std::string mIdent; |
| 300 | |
| 301 | private: |
| 302 | SG_DISABLE_COPY(FGPositioned); |
| 303 | |
| 304 | const SGGeod mPosition; |
| 305 | const SGVec3d mCart; |
| 306 | }; |
| 307 | |
| 308 | |
| 309 | |
| 310 | template <class T> |
| 311 | T* fgpositioned_cast(FGPositioned* p) |
| 312 | { |
| 313 | if (T::isType(p->type())) { |
| 314 | return static_cast<T*>(p); |
| 315 | } |
| 316 | |
| 317 | return nullptr; |
| 318 | } |
| 319 | |
| 320 | template <class T> |
| 321 | T* fgpositioned_cast(FGPositionedRef p) |
| 322 | { |
| 323 | if (!p) return nullptr; |
| 324 | |
| 325 | if (T::isType(p->type())) { |
| 326 | return static_cast<T*>(p.ptr()); |
| 327 | } |
| 328 | |
| 329 | return nullptr; |
| 330 | } |
| 331 | |
| 332 | class POI : public FGPositioned |
| 333 | { |
| 334 | public: |
| 335 | POI(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos, const std::string& aName); |
| 336 | |
| 337 | const std::string& name() const override |
| 338 | { |
| 339 | return mName; |
| 340 | } |
| 341 | |
| 342 | static bool isType(FGPositioned::Type ty); |
| 343 | |
| 344 | private: |
| 345 | std::string mName; |
| 346 | }; |