| 1 | /** |
| 2 | * FlightPlan.hxx - defines a full flight-plan object, including |
| 3 | * departure, cruise, arrival information and waypoints |
| 4 | */ |
| 5 | |
| 6 | // Written by James Turner, started 2012. |
| 7 | // |
| 8 | // Copyright (C) 2012 James Turner |
| 9 | // |
| 10 | // This program is free software; you can redistribute it and/or |
| 11 | // modify it under the terms of the GNU General Public License as |
| 12 | // published by the Free Software Foundation; either version 2 of the |
| 13 | // License, or (at your option) any later version. |
| 14 | // |
| 15 | // This program is distributed in the hope that it will be useful, but |
| 16 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | // General Public License for more details. |
| 19 | // |
| 20 | // You should have received a copy of the GNU General Public License |
| 21 | // along with this program; if not, write to the Free Software |
| 22 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | |
| 24 | #ifndef FG_FLIGHTPLAN_HXX |
| 25 | #define FG_FLIGHTPLAN_HXX |
| 26 | |
| 27 | #include <functional> |
| 28 | |
| 29 | #include <Navaids/route.hxx> |
| 30 | #include <Airports/airport.hxx> |
| 31 | |
| 32 | namespace flightgear |
| 33 | { |
| 34 | |
| 35 | class Transition; |
| 36 | class FlightPlan; |
| 37 | |
| 38 | typedef SGSharedPtr<FlightPlan> FlightPlanRef; |
| 39 | |
| 40 | enum class ICAOFlightRules |
| 41 | { |
| 42 | VFR = 0, |
| 43 | IFR, |
| 44 | IFR_VFR, // type Y |
| 45 | VFR_IFR // type Z |
| 46 | }; |
| 47 | |
| 48 | enum class ICAOFlightType |
| 49 | { |
| 50 | Scheduled = 0, |
| 51 | NonScheduled, |
| 52 | GeneralAviation, |
| 53 | Military, |
| 54 | Other // type X |
| 55 | }; |
| 56 | |
| 57 | class FlightPlan : public RouteBase |
| 58 | { |
| 59 | public: |
| 60 | virtual ~FlightPlan(); |
| 61 | |
| 62 | /** |
| 63 | create a FlightPlan with isRoute *not* set |
| 64 | */ |
| 65 | static FlightPlanRef create(); |
| 66 | |
| 67 | /** |
| 68 | @factory to create a FlightPlan with isRoute=true |
| 69 | */ |
| 70 | static FlightPlanRef createRoute(); |
| 71 | |
| 72 | virtual std::string ident() const; |
| 73 | void setIdent(const std::string& s); |
| 74 | |
| 75 | // propogate the GPS/FMS setting for this through to the RoutePath |
| 76 | void setFollowLegTrackToFixes(bool tf); |
| 77 | bool followLegTrackToFixes() const; |
| 78 | |
| 79 | void setMaxFlyByTurnAngle(double deg); |
| 80 | double maxFlyByTurnAngle() const; |
| 81 | |
| 82 | // aircraft approach category as per CFR 97.3, etc |
| 83 | // http://www.flightsimaviation.com/data/FARS/part_97-3.html |
| 84 | std::string icaoAircraftCategory() const; |
| 85 | void setIcaoAircraftCategory(const std::string& cat); |
| 86 | |
| 87 | std::string icaoAircraftType() const |
| 88 | { return _aircraftType; } |
| 89 | |
| 90 | void setIcaoAircraftType(const std::string& ty); |
| 91 | |
| 92 | FlightPlanRef clone(const std::string& newIdent = {}, bool convertToFlightPlan = false) const; |
| 93 | |
| 94 | /** |
| 95 | is this flight-pan a route (for planning) or an active flight-plan (which can be flown?) |
| 96 | Routes can contain Via, and cannot be active: FlightPlans contain Legs for procedures and |
| 97 | airways, i.e what the GPS/FMC actally flies. |
| 98 | */ |
| 99 | bool isRoute() const; |
| 100 | |
| 101 | /** |
| 102 | * flight-plan leg encapsulation |
| 103 | */ |
| 104 | class Leg : public SGReferenced |
| 105 | { |
| 106 | public: |
| 107 | FlightPlan* owner() const |
| 108 | { return const_cast<FlightPlan*>(_parent); } |
| 109 | |
| 110 | Waypt* waypoint() const |
| 111 | { return _waypt; } |
| 112 | |
| 113 | // return the next leg after this one |
| 114 | Leg* nextLeg() const; |
| 115 | |
| 116 | /** |
| 117 | * requesting holding at the waypoint upon reaching it. This will |
| 118 | * convert the waypt to a Hold if not already defined as one, but |
| 119 | * with default hold data. |
| 120 | * |
| 121 | * If the waypt is not of a type suitable for holding at, returns false |
| 122 | * (eg a runway or dynamic waypoint) |
| 123 | */ |
| 124 | bool setHoldCount(int count); |
| 125 | |
| 126 | int holdCount() const; |
| 127 | |
| 128 | |
| 129 | bool convertWaypointToHold(); |
| 130 | |
| 131 | unsigned int index() const; |
| 132 | |
| 133 | int altitudeFt() const; |
| 134 | double speed(RouteUnits units = DEFAULT_UNITS) const; |
| 135 | double altitude(RouteUnits units = DEFAULT_UNITS) const; |
| 136 | |
| 137 | int speedKts() const; |
| 138 | double speedMach() const; |
| 139 | |
| 140 | RouteRestriction altitudeRestriction() const; |
| 141 | RouteRestriction speedRestriction() const; |
| 142 | |
| 143 | void setSpeed(RouteRestriction ty, double speed, RouteUnits units = DEFAULT_UNITS); |
| 144 | void setAltitude(RouteRestriction ty, double alt, RouteUnits units = DEFAULT_UNITS); |
| 145 | |
| 146 | double courseDeg() const; |
| 147 | double distanceNm() const; |
| 148 | double distanceAlongRoute() const; |
| 149 | |
| 150 | /** |
| 151 | * helper function, if the waypoint is modified in some way, to |
| 152 | * notify the flightplan owning this leg, and hence any delegates |
| 153 | * obsering us |
| 154 | */ |
| 155 | void markWaypointDirty(); |
| 156 | private: |
| 157 | friend class FlightPlan; |
| 158 | |
| 159 | Leg(FlightPlan* owner, WayptRef wpt); |
| 160 | |
| 161 | Leg* cloneFor(FlightPlan* owner) const; |
| 162 | |
| 163 | void writeToProperties(SGPropertyNode* node) const; |
| 164 | |
| 165 | const FlightPlan* _parent; |
| 166 | RouteRestriction _speedRestrict = RESTRICT_NONE, |
| 167 | _altRestrict = RESTRICT_NONE; |
| 168 | double _speed = 0.0; |
| 169 | double _altitude = 0.0; |
| 170 | RouteUnits _speedUnits = SPEED_KNOTS; |
| 171 | RouteUnits _altitudeUnits = ALTITUDE_FEET; |
| 172 | |
| 173 | // if > 0, we will hold at the waypoint using |
| 174 | // the published hold side/course |
| 175 | // This only works if _waypt is a Hold, either defined by a procedure |
| 176 | // or modified to become one |
| 177 | int _holdCount = 0; |
| 178 | |
| 179 | WayptRef _waypt; |
| 180 | /// length of this leg following the flown path |
| 181 | mutable double _pathDistance = -1.0; |
| 182 | mutable double _courseDeg = -1.0; |
| 183 | /// total distance of this leg from departure point |
| 184 | mutable double _distanceAlongPath = 11.0; |
| 185 | }; |
| 186 | |
| 187 | using LegRef = SGSharedPtr<Leg>; |
| 188 | |
| 189 | class DelegateFactory; |
| 190 | using DelegateFactoryRef = std::shared_ptr<DelegateFactory>; |
| 191 | |
| 192 | class Delegate |
| 193 | { |
| 194 | public: |
| 195 | virtual ~Delegate(); |
| 196 | |
| 197 | virtual void departureChanged() { } |
| 198 | virtual void arrivalChanged() { } |
| 199 | virtual void waypointsChanged() { } |
| 200 | virtual void cruiseChanged() { } |
| 201 | virtual void cleared() { } |
| 202 | virtual void activated() { } |
| 203 | |
| 204 | /** |
| 205 | * Invoked when the C++ code determines the active leg is done / next |
| 206 | * leg should be sequenced. The default route-manager delegate will |
| 207 | * advance to the next waypoint when handling this. |
| 208 | * |
| 209 | * If multiple delegates are installed, take special care not to sequence |
| 210 | * the waypoint twice. |
| 211 | */ |
| 212 | virtual void sequence() { } |
| 213 | |
| 214 | virtual void currentWaypointChanged() { } |
| 215 | virtual void endOfFlightPlan() { } |
| 216 | |
| 217 | virtual void loaded() { } |
| 218 | protected: |
| 219 | Delegate(); |
| 220 | |
| 221 | private: |
| 222 | friend class FlightPlan; |
| 223 | |
| 224 | // record the factory which created us, so we have the option to clean up |
| 225 | DelegateFactoryRef _factory; |
| 226 | }; |
| 227 | |
| 228 | LegRef insertWayptAtIndex(Waypt* aWpt, int aIndex); |
| 229 | void insertWayptsAtIndex(const WayptVec& wps, int aIndex); |
| 230 | |
| 231 | void deleteIndex(int index); |
| 232 | void clearAll(); |
| 233 | void clearLegs(); |
| 234 | int clearWayptsWithFlag(WayptFlag flag); |
| 235 | |
| 236 | int currentIndex() const |
| 237 | { return _currentIndex; } |
| 238 | |
| 239 | void sequence(); |
| 240 | |
| 241 | void setCurrentIndex(int index); |
| 242 | |
| 243 | void activate(); |
| 244 | |
| 245 | void finish(); |
| 246 | |
| 247 | bool isActive() const; |
| 248 | |
| 249 | LegRef currentLeg() const; |
| 250 | LegRef nextLeg() const; |
| 251 | LegRef previousLeg() const; |
| 252 | |
| 253 | int numLegs() const |
| 254 | { return static_cast<int>(_legs.size()); } |
| 255 | |
| 256 | LegRef legAtIndex(int index) const; |
| 257 | |
| 258 | int findWayptIndex(const SGGeod& aPos) const; |
| 259 | int findWayptIndex(const FGPositionedRef aPos) const; |
| 260 | |
| 261 | int indexOfFirstNonDepartureWaypoint() const; |
| 262 | int indexOfFirstArrivalWaypoint() const; |
| 263 | int indexOfFirstApproachWaypoint() const; |
| 264 | int indexOfDestinationRunwayWaypoint() const; |
| 265 | |
| 266 | bool load(const SGPath& p); |
| 267 | bool save(const SGPath& p) const; |
| 268 | |
| 269 | bool save(std::ostream& stream) const; |
| 270 | bool load(std::istream& stream); |
| 271 | |
| 272 | FGAirportRef departureAirport() const |
| 273 | { return _departure; } |
| 274 | |
| 275 | FGAirportRef destinationAirport() const |
| 276 | { return _destination; } |
| 277 | |
| 278 | FGRunway* departureRunway() const |
| 279 | { return _departureRunway; } |
| 280 | |
| 281 | FGRunway* destinationRunway() const |
| 282 | { return _destinationRunway; } |
| 283 | |
| 284 | Approach* approach() const |
| 285 | { return _approach; } |
| 286 | |
| 287 | void setDeparture(FGAirport* apt); |
| 288 | void setDeparture(FGRunway* rwy); |
| 289 | |
| 290 | void clearDeparture(); |
| 291 | |
| 292 | SID* sid() const |
| 293 | { return _sid; } |
| 294 | |
| 295 | Transition* sidTransition() const; |
| 296 | |
| 297 | void setSID(SID* sid, const std::string& transition = std::string()); |
| 298 | |
| 299 | void setSID(Transition* sidWithTrans); |
| 300 | |
| 301 | void clearSID(); |
| 302 | |
| 303 | void setDestination(FGAirport* apt); |
| 304 | void setDestination(FGRunway* rwy); |
| 305 | |
| 306 | void clearDestination(); |
| 307 | |
| 308 | FGAirportRef alternate() const; |
| 309 | void setAlternate(FGAirportRef alt); |
| 310 | |
| 311 | /** |
| 312 | * note setting an approach will implicitly update the destination |
| 313 | * airport and runway to match |
| 314 | */ |
| 315 | void setApproach(Approach* app, const std::string& transition = {}); |
| 316 | |
| 317 | void setApproach(Transition* approachWithTrans); |
| 318 | |
| 319 | |
| 320 | Transition* approachTransition() const; |
| 321 | |
| 322 | STAR* star() const |
| 323 | { return _star; } |
| 324 | |
| 325 | Transition* starTransition() const; |
| 326 | |
| 327 | void setSTAR(STAR* star, const std::string& transition = std::string()); |
| 328 | |
| 329 | void setSTAR(Transition* starWithTrans); |
| 330 | |
| 331 | void clearSTAR(); |
| 332 | |
| 333 | double totalDistanceNm() const |
| 334 | { return _totalDistance; } |
| 335 | |
| 336 | int estimatedDurationMinutes() const |
| 337 | { return _estimatedDuration; } |
| 338 | |
| 339 | void setEstimatedDurationMinutes(int minutes); |
| 340 | |
| 341 | /** |
| 342 | * @brief computeDurationMinutes - use performance data and cruise data |
| 343 | * to estimate enroute time |
| 344 | */ |
| 345 | void computeDurationMinutes(); |
| 346 | |
| 347 | /** |
| 348 | * given a waypoint index, and an offset in NM, find the geodetic |
| 349 | * position on the route path. I.e the point 10nm before or after |
| 350 | * a particular waypoint. |
| 351 | */ |
| 352 | SGGeod pointAlongRoute(int aIndex, double aOffsetNm) const; |
| 353 | |
| 354 | /** |
| 355 | given a waypoint index, find a point at a normalised offset, which must be [-1 .. 1] |
| 356 | eg an offset of -0.5 will be half-way between aIndex and the preceeding waypoint, |
| 357 | and an offset of 0.3 will be 30% of the distance from aIndex to the next waypoint. |
| 358 | */ |
| 359 | SGGeod pointAlongRouteNorm(int aIndex, double aOffsetNorm) const; |
| 360 | |
| 361 | /** |
| 362 | @brief given an index to insert a waypoint into the plan, find the geographical vicinity. |
| 363 | This is used to aid disambiguration searches, etc: see the vicinity paramter to 'waypointFromString' |
| 364 | below, for example |
| 365 | |
| 366 | When aIndex is negative, the vicinity used is the end of the current flight-plan, i.e appending to |
| 367 | the waypoints rather than appending. |
| 368 | */ |
| 369 | SGGeod vicinityForInsertIndex(int aIndex) const; |
| 370 | |
| 371 | /** |
| 372 | * Create a WayPoint from a string in the following format: |
| 373 | * - |
| 374 | * 'vicinity' specifies the search area, to disambiguate navaids, etc with duplicate names |
| 375 | */ |
| 376 | WayptRef waypointFromString(const std::string& target, const SGGeod& vicinity = SGGeod::invalid()); |
| 377 | |
| 378 | /** |
| 379 | * attempt to replace the route waypoints (and potentially the SID and |
| 380 | * STAR) based on an ICAO standard route string, i.e item 15. |
| 381 | * Returns true if the rotue was parsed successfully (and this flight |
| 382 | * plan modified accordingly) or false if the string could not be |
| 383 | * parsed. |
| 384 | */ |
| 385 | bool parseICAORouteString(const std::string& routeData); |
| 386 | |
| 387 | std::string asICAORouteString() const; |
| 388 | |
| 389 | // ICAO flight-plan data |
| 390 | void setFlightRules(ICAOFlightRules rules); |
| 391 | ICAOFlightRules flightRules() const; |
| 392 | |
| 393 | void setFlightType(ICAOFlightType type); |
| 394 | ICAOFlightType flightType() const; |
| 395 | |
| 396 | void setCallsign(const std::string& callsign); |
| 397 | std::string callsign() const |
| 398 | { return _callsign; } |
| 399 | |
| 400 | void setRemarks(const std::string& remarks); |
| 401 | std::string remarks() const |
| 402 | { return _remarks; } |
| 403 | |
| 404 | // cruise data |
| 405 | void setCruiseSpeedKnots(int kts); |
| 406 | int cruiseSpeedKnots() const; |
| 407 | |
| 408 | void setCruiseSpeedMach(double mach); |
| 409 | double cruiseSpeedMach() const; |
| 410 | |
| 411 | void setCruiseSpeedKPH(int kmh); |
| 412 | int cruiseSpeedKPH() const; |
| 413 | |
| 414 | void setCruiseFlightLevel(int flightLevel); |
| 415 | int cruiseFlightLevel() const; |
| 416 | |
| 417 | void setCruiseAltitudeFt(int altFt); |
| 418 | int cruiseAltitudeFt() const; |
| 419 | |
| 420 | void setCruiseAltitudeM(int altM); |
| 421 | int cruiseAltitudeM() const; |
| 422 | |
| 423 | /** |
| 424 | * abstract interface for creating delegates automatically when a |
| 425 | * flight-plan is created or loaded |
| 426 | */ |
| 427 | class DelegateFactory |
| 428 | { |
| 429 | public: |
| 430 | virtual Delegate* createFlightPlanDelegate(FlightPlan* fp) = 0; |
| 431 | virtual void destroyFlightPlanDelegate(FlightPlan* fp, Delegate* d); |
| 432 | }; |
| 433 | |
| 434 | static void registerDelegateFactory(DelegateFactoryRef df); |
| 435 | static void unregisterDelegateFactory(DelegateFactoryRef df); |
| 436 | |
| 437 | void addDelegate(Delegate* d); |
| 438 | void removeDelegate(Delegate* d); |
| 439 | |
| 440 | using LegVisitor = std::function<void(Leg*)>; |
| 441 | void forEachLeg(const LegVisitor& lv); |
| 442 | private: |
| 443 | FlightPlan(bool isRoute); |
| 444 | |
| 445 | friend class Leg; |
| 446 | |
| 447 | int findLegIndex(const Leg* l) const; |
| 448 | |
| 449 | void runDelegates(); |
| 450 | void lockDelegates(); |
| 451 | void unlockDelegates(); |
| 452 | |
| 453 | void notifyCleared(); |
| 454 | |
| 455 | unsigned int _delegateLock = 0; |
| 456 | bool _arrivalChanged = false, |
| 457 | _departureChanged = false, |
| 458 | _waypointsChanged = false, |
| 459 | _currentWaypointChanged = false, |
| 460 | _cruiseDataChanged = false; |
| 461 | bool _didLoadFP = false; |
| 462 | |
| 463 | void saveToProperties(SGPropertyNode* d) const; |
| 464 | |
| 465 | bool loadXmlFormat(const SGPath& path); |
| 466 | bool loadGpxFormat(const SGPath& path); |
| 467 | bool loadPlainTextFormat(const SGPath& path); |
| 468 | |
| 469 | bool loadVersion1XMLRoute(SGPropertyNode_ptr routeData); |
| 470 | bool loadVersion2XMLRoute(SGPropertyNode_ptr routeData); |
| 471 | void loadXMLRouteHeader(SGPropertyNode_ptr routeData); |
| 472 | WayptRef parseVersion1XMLWaypt(SGPropertyNode* aWP); |
| 473 | |
| 474 | double magvarDegAt(const SGGeod& pos) const; |
| 475 | bool parseICAOLatLon(const std::string &s, SGGeod &p); |
| 476 | |
| 477 | /** |
| 478 | helper to convert VIA legs into a list of regular waypoints after load, etc |
| 479 | */ |
| 480 | bool expandVias(); |
| 481 | |
| 482 | std::string _ident; |
| 483 | std::string _callsign; |
| 484 | std::string _remarks; |
| 485 | std::string _aircraftType; |
| 486 | const bool _isRoute; |
| 487 | |
| 488 | int _currentIndex; |
| 489 | bool _followLegTrackToFix; |
| 490 | char _aircraftCategory; |
| 491 | ICAOFlightType _flightType = ICAOFlightType::Other; |
| 492 | ICAOFlightRules _flightRules = ICAOFlightRules::VFR; |
| 493 | int _cruiseAirspeedKnots = 0; |
| 494 | double _cruiseAirspeedMach = 0.0; |
| 495 | int _cruiseAirspeedKph = 0; |
| 496 | int _cruiseFlightLevel = 0; |
| 497 | int _cruiseAltitudeFt = 0; |
| 498 | int _cruiseAltitudeM = 0; |
| 499 | int _estimatedDuration = 0; |
| 500 | double _maxFlyByTurnAngle = 90.0; |
| 501 | |
| 502 | FGAirportRef _departure, _destination; |
| 503 | FGAirportRef _alternate; |
| 504 | FGRunway *_departureRunway, *_destinationRunway; |
| 505 | SGSharedPtr<SID> _sid; |
| 506 | SGSharedPtr<STAR> _star; |
| 507 | SGSharedPtr<Approach> _approach; |
| 508 | std::string _sidTransition, _starTransition, _approachTransition; |
| 509 | |
| 510 | double _totalDistance; |
| 511 | void rebuildLegData(); |
| 512 | |
| 513 | using LegVec = std::vector<LegRef>; |
| 514 | LegVec _legs; |
| 515 | |
| 516 | std::vector<Delegate*> _delegates; |
| 517 | }; |
| 518 | |
| 519 | } // of namespace flightgear |
| 520 | |
| 521 | #endif // of FG_FLIGHTPLAN_HXX |