2 * route.hxx - defines basic route and route-element classes. Route elements
3 * are specialised into waypoints and related things. Routes are any class tha
4 * owns a collection (list, tree, graph) of route elements - such as airways,
5 * procedures or a flight plan.
8 // Written by James Turner, started 2009.
10 // Copyright (C) 2009 Curtis L. Olson
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 #include <simgear/structure/SGReferenced.hxx>
36 #include <simgear/structure/SGSharedPtr.hxx>
37 #include <simgear/props/props.hxx>
44 #include <Airports/simple.hxx>
45 typedef SGSharedPtr<FGAirport> FGAirportRef;
58 typedef SGSharedPtr<Waypt> WayptRef;
61 WPT_MAP = 1 << 0, ///< missed approach point
62 WPT_IAF = 1 << 1, ///< initial approach fix
63 WPT_FAF = 1 << 2, ///< final approach fix
64 WPT_OVERFLIGHT = 1 << 3, ///< must overfly the point directly
65 WPT_TRANSITION = 1 << 4, ///< transition to/from enroute structure
66 WPT_MISS = 1 << 5, ///< segment is part of missed approach
67 /// waypoint position is dynamic, i.e moves based on other criteria,
68 /// such as altitude, inbound course, or so on.
70 /// waypoint was created automatically (not manually entered/loaded)
71 /// for example waypoints from airway routing or a procedure
72 WPT_GENERATED = 1 << 7,
74 WPT_DEPARTURE = 1 << 8,
77 /// waypoint generated by VNAV / speed management profile,
78 /// for step climbs or top of descent
80 WPT_APPROACH = 1 << 11
88 SPEED_RESTRICT_MACH, ///< encode an 'AT' restriction in Mach, not IAS
89 RESTRICT_DELETE, ///< ignore underlying restriction (on a leg)
90 RESTRICT_COMPUTED, ///< data is computed, not a real restriction
91 SPEED_COMPUTED_MACH ///< variant on above to encode a Mach value
94 bool isMachRestrict(RouteRestriction rr);
97 * Abstract base class for waypoints (and things that are treated similarly
98 * by navigation systems)
100 class Waypt : public SGReferenced
105 RouteBase* owner() const
109 * Return true course (in degrees) and distance (in metres) from the provided
110 * position to this waypoint
112 virtual std::pair<double, double> courseAndDistanceFrom(const SGGeod& aPos) const;
114 virtual SGGeod position() const = 0;
117 * The Positioned associated with this element, if one exists
119 virtual FGPositioned* source() const
122 virtual double altitudeFt() const
123 { return _altitudeFt; }
125 virtual double speed() const
128 // wrapper - asserts if restriction type is _MACH
129 double speedKts() const;
131 // wrapper - asserts if restriction type is not _MACH
132 double speedMach() const;
134 virtual RouteRestriction altitudeRestriction() const
135 { return _altRestrict; }
137 virtual RouteRestriction speedRestriction() const
138 { return _speedRestrict; }
140 void setAltitude(double aAlt, RouteRestriction aRestrict);
141 void setSpeed(double aSpeed, RouteRestriction aRestrict);
144 * Identifier assoicated with the waypoint. Human-readable, but
145 * possibly quite terse, and definitiely not unique.
147 virtual std::string ident() const;
150 * Test if the specified flag is set for this element
152 virtual bool flag(WayptFlag aFlag) const;
154 void setFlag(WayptFlag aFlag, bool aV = true);
159 static WayptRef createFromProperties(RouteBase* aOwner, SGPropertyNode_ptr aProp);
161 void saveAsNode(SGPropertyNode* node) const;
164 * Test if this element and another are 'the same', i.e matching
165 * ident and lat/lon are approximately equal
167 bool matches(Waypt* aOther) const;
170 * Test if this element and a position 'the same'
171 * this can be defined by either position, ident or both
173 bool matches(const SGGeod& aPos) const;
175 virtual std::string type() const = 0;
178 * Magentic variation at/in the vicinity of the waypoint.
179 * For some waypoint types this will always return 0.
181 virtual double magvarDeg() const;
184 * return the assoicated heading or radial for this waypoint.
185 * The exact meaning varies by type - for a hold it's the inbound radial,
186 * for a DME intercept it's the heading to hold, and so on.
188 virtual double headingRadialDeg() const;
190 friend class NavdataVisitor;
192 Waypt(RouteBase* aOwner);
195 * Persistence helper - read node properties from a file
197 virtual void initFromProperties(SGPropertyNode_ptr aProp);
200 * Persistence helper - save this element to a node
202 virtual void writeToProperties(SGPropertyNode_ptr aProp) const;
204 typedef Waypt* (FactoryFunction)(RouteBase* aOwner) ;
205 static void registerFactory(const std::string aNodeType, FactoryFunction* aFactory);
208 double _speed; // knots IAS or mach
209 RouteRestriction _altRestrict;
210 RouteRestriction _speedRestrict;
214 * Create an instance of a concrete subclass, or throw an exception
216 static Waypt* createInstance(RouteBase* aOwner, const std::string& aTypeName);
219 unsigned short _flags;
220 mutable double _magVarDeg;
223 typedef std::vector<WayptRef> WayptVec;
231 virtual std::string ident() const = 0;
233 static void loadAirportProcedures(const SGPath& aPath, FGAirport* aApt);
235 static void dumpRouteToKML(const WayptVec& aRoute, const std::string& aName);
237 static void dumpRouteToKMLLineString(const std::string& aIdent,
238 const WayptVec& aRoute, std::ostream& aStream);
243 class FlightPlan : public RouteBase
247 virtual ~FlightPlan();
249 virtual std::string ident() const;
250 void setIdent(const std::string& s);
252 FlightPlan* clone(const std::string& newIdent = std::string()) const;
255 * flight-plan leg encapsulation
260 FlightPlan* owner() const
263 Waypt* waypoint() const
266 // reutrn the next leg after this one
267 Leg* nextLeg() const;
269 unsigned int index() const;
271 int altitudeFt() const;
274 int speedKts() const;
275 double speedMach() const;
277 RouteRestriction altitudeRestriction() const;
278 RouteRestriction speedRestriction() const;
280 void setSpeed(RouteRestriction ty, double speed);
281 void setAltitude(RouteRestriction ty, int altFt);
283 double courseDeg() const;
284 double distanceNm() const;
285 double distanceAlongRoute() const;
287 friend class FlightPlan;
289 Leg(FlightPlan* owner, WayptRef wpt);
291 Leg* cloneFor(FlightPlan* owner) const;
294 RouteRestriction _speedRestrict, _altRestrict;
298 /// length of this leg following the flown path
299 mutable double _pathDistance;
300 mutable double _courseDeg;
301 /// total distance of this leg from departure point
302 mutable double _distanceAlongPath;
310 virtual void departureChanged() { }
311 virtual void arrivalChanged() { }
312 virtual void waypointsChanged() { }
314 virtual void currentWaypointChanged() { }
320 void removeInner(Delegate* d);
322 void runDepartureChanged();
323 void runArrivalChanged();
324 void runWaypointsChanged();
325 void runCurrentWaypointChanged();
327 friend class FlightPlan;
332 Leg* insertWayptAtIndex(Waypt* aWpt, int aIndex);
333 void insertWayptsAtIndex(const WayptVec& wps, int aIndex);
335 void deleteIndex(int index);
337 int clearWayptsWithFlag(WayptFlag flag);
339 int currentIndex() const
340 { return _currentIndex; }
342 void setCurrentIndex(int index);
344 Leg* currentLeg() const;
345 Leg* nextLeg() const;
346 Leg* previousLeg() const;
349 { return _legs.size(); }
351 Leg* legAtIndex(int index) const;
352 int findLegIndex(const Leg* l) const;
354 int findWayptIndex(const SGGeod& aPos) const;
356 bool load(const SGPath& p);
357 bool save(const SGPath& p);
359 FGAirportRef departureAirport() const
360 { return _departure; }
362 FGAirportRef destinationAirport() const
363 { return _destination; }
365 FGRunway* departureRunway() const
366 { return _departureRunway; }
368 FGRunway* destinationRunway() const
369 { return _destinationRunway; }
371 Approach* approach() const
372 { return _approach; }
374 void setDeparture(FGAirport* apt);
375 void setDeparture(FGRunway* rwy);
380 Transition* sidTransition() const;
382 void setSID(SID* sid, const std::string& transition = std::string());
384 void setSID(Transition* sidWithTrans);
386 void setDestination(FGAirport* apt);
387 void setDestination(FGRunway* rwy);
390 * note setting an approach will implicitly update the destination
391 * airport and runway to match
393 void setApproach(Approach* app);
398 Transition* starTransition() const;
400 void setSTAR(STAR* star, const std::string& transition = std::string());
402 void setSTAR(Transition* starWithTrans);
404 double totalDistanceNm() const
405 { return _totalDistance; }
408 * Create a WayPoint from a string in the following format:
409 * - simple identifier
410 * - decimal-lon,decimal-lat
411 * - airport-id/runway-id
412 * - navaid/radial-deg/offset-nm
414 WayptRef waypointFromString(const std::string& target);
416 void setDelegate(Delegate* d);
417 void removeDelegate(Delegate* d);
420 bool loadPlainTextRoute(const SGPath& path);
422 void loadVersion1XMLRoute(SGPropertyNode_ptr routeData);
423 void loadVersion2XMLRoute(SGPropertyNode_ptr routeData);
424 void loadXMLRouteHeader(SGPropertyNode_ptr routeData);
425 WayptRef parseVersion1XMLWaypt(SGPropertyNode* aWP);
427 double magvarDegAt(const SGGeod& pos) const;
432 FGAirportRef _departure, _destination;
433 FGRunway* _departureRunway, *_destinationRunway;
437 std::string _sidTransition, _starTransition;
439 double _totalDistance;
440 void rebuildLegData();
442 typedef std::vector<Leg*> LegVec;
448 } // of namespace flightgear
450 #endif // of FG_ROUTE_HXX