]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/FlightPlan.hxx
Fix distance-along-path computation
[flightgear.git] / src / Navaids / FlightPlan.hxx
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 <Navaids/route.hxx>
28 #include <Airports/airport.hxx>
29
30 namespace flightgear
31 {
32
33 class Transition;
34 class FlightPlan;
35     
36 typedef SGSharedPtr<FlightPlan> FlightPlanRef;
37     
38 class FlightPlan : public RouteBase
39 {
40 public:
41   FlightPlan();
42   virtual ~FlightPlan();
43   
44   virtual std::string ident() const;
45   void setIdent(const std::string& s);
46   
47   FlightPlan* clone(const std::string& newIdent = std::string()) const;
48   
49   /**
50    * flight-plan leg encapsulation
51    */
52   class Leg
53   {
54   public:
55     FlightPlan* owner() const
56     { return _parent; }
57     
58     Waypt* waypoint() const
59     { return _waypt; }
60     
61     // reutrn the next leg after this one
62     Leg* nextLeg() const;
63     
64     unsigned int index() const;
65     
66     int altitudeFt() const;             
67     int speed() const;
68     
69     int speedKts() const;
70     double speedMach() const;
71     
72     RouteRestriction altitudeRestriction() const;    
73     RouteRestriction speedRestriction() const;
74     
75     void setSpeed(RouteRestriction ty, double speed);
76     void setAltitude(RouteRestriction ty, int altFt);
77     
78     double courseDeg() const;
79     double distanceNm() const;
80     double distanceAlongRoute() const;
81   private:
82     friend class FlightPlan;
83     
84     Leg(FlightPlan* owner, WayptRef wpt);
85     
86     Leg* cloneFor(FlightPlan* owner) const;
87     
88     FlightPlan* _parent;
89     RouteRestriction _speedRestrict, _altRestrict;
90     int _speed;
91     int _altitudeFt;
92     WayptRef _waypt;
93     /// length of this leg following the flown path
94     mutable double _pathDistance;
95     mutable double _courseDeg;
96     /// total distance of this leg from departure point
97     mutable double _distanceAlongPath; 
98   };
99   
100   class Delegate
101   {
102   public:
103     virtual ~Delegate();
104         
105     virtual void departureChanged() { }
106     virtual void arrivalChanged() { }
107     virtual void waypointsChanged() { }
108     virtual void cleared() { }
109     virtual void currentWaypointChanged() { }
110     virtual void endOfFlightPlan() { }
111   protected:
112     Delegate();
113     
114   private:
115     void removeInner(Delegate* d);
116     
117     void runDepartureChanged();
118     void runArrivalChanged();
119     void runWaypointsChanged();
120     void runCurrentWaypointChanged();
121     void runCleared();
122     void runFinished();
123       
124     friend class FlightPlan;
125     
126     bool _deleteWithPlan;
127     Delegate* _inner;
128   };
129   
130   Leg* insertWayptAtIndex(Waypt* aWpt, int aIndex);
131   void insertWayptsAtIndex(const WayptVec& wps, int aIndex);
132   
133   void deleteIndex(int index);
134   void clear();
135   int clearWayptsWithFlag(WayptFlag flag);
136   
137   int currentIndex() const
138   { return _currentIndex; }
139   
140   void setCurrentIndex(int index);
141   
142   void finish();
143     
144   Leg* currentLeg() const;
145   Leg* nextLeg() const;
146   Leg* previousLeg() const;
147   
148   int numLegs() const
149   { return _legs.size(); }
150   
151   Leg* legAtIndex(int index) const;
152   int findLegIndex(const Leg* l) const;
153   
154   int findWayptIndex(const SGGeod& aPos) const;
155   int findWayptIndex(const FGPositionedRef aPos) const;
156   
157   bool load(const SGPath& p);
158   bool save(const SGPath& p);
159   
160   FGAirportRef departureAirport() const
161   { return _departure; }
162   
163   FGAirportRef destinationAirport() const
164   { return _destination; }
165   
166   FGRunway* departureRunway() const
167   { return _departureRunway; }
168   
169   FGRunway* destinationRunway() const
170   { return _destinationRunway; }
171   
172   Approach* approach() const
173   { return _approach; }
174   
175   void setDeparture(FGAirport* apt);
176   void setDeparture(FGRunway* rwy);
177   
178   SID* sid() const
179   { return _sid; }
180   
181   Transition* sidTransition() const;
182   
183   void setSID(SID* sid, const std::string& transition = std::string());
184   
185   void setSID(Transition* sidWithTrans);
186   
187   void setDestination(FGAirport* apt);
188   void setDestination(FGRunway* rwy);
189   
190   /**
191     * note setting an approach will implicitly update the destination
192     * airport and runway to match
193     */
194   void setApproach(Approach* app);
195   
196   STAR* star() const
197   { return _star; }
198   
199   Transition* starTransition() const;
200   
201   void setSTAR(STAR* star, const std::string& transition = std::string());
202   
203   void setSTAR(Transition* starWithTrans);
204   
205   double totalDistanceNm() const
206   { return _totalDistance; }
207   
208   /**
209    * given a waypoint index, and an offset in NM, find the geodetic
210    * position on the route path. I.e the point 10nm before or after
211    * a particular waypoint.
212    */
213   SGGeod pointAlongRoute(int aIndex, double aOffsetNm) const;
214     
215   /**
216    * Create a WayPoint from a string in the following format:
217    *  - simple identifier
218    *  - decimal-lon,decimal-lat
219    *  - airport-id/runway-id
220    *  - navaid/radial-deg/offset-nm
221    */
222   WayptRef waypointFromString(const std::string& target);
223   
224   /**
225    * abstract interface for creating delegates automatically when a
226    * flight-plan is created or loaded
227    */
228   class DelegateFactory
229   {
230   public:
231     virtual Delegate* createFlightPlanDelegate(FlightPlan* fp) = 0;
232   };
233   
234   static void registerDelegateFactory(DelegateFactory* df);
235   static void unregisterDelegateFactory(DelegateFactory* df);
236   
237   void addDelegate(Delegate* d);
238   void removeDelegate(Delegate* d);
239 private:
240   void lockDelegate();
241   void unlockDelegate();
242   
243   int _delegateLock;
244   bool _arrivalChanged, 
245     _departureChanged, 
246     _waypointsChanged, 
247     _currentWaypointChanged;
248   
249   bool loadXmlFormat(const SGPath& path);
250   bool loadGpxFormat(const SGPath& path);
251   bool loadPlainTextFormat(const SGPath& path);
252   
253   void loadVersion1XMLRoute(SGPropertyNode_ptr routeData);
254   void loadVersion2XMLRoute(SGPropertyNode_ptr routeData);
255   void loadXMLRouteHeader(SGPropertyNode_ptr routeData);
256   WayptRef parseVersion1XMLWaypt(SGPropertyNode* aWP);
257   
258   double magvarDegAt(const SGGeod& pos) const;
259   
260   std::string _ident;
261   int _currentIndex;
262   
263   FGAirportRef _departure, _destination;
264   FGRunway* _departureRunway, *_destinationRunway;
265   SGSharedPtr<SID> _sid;
266   SGSharedPtr<STAR> _star;
267   SGSharedPtr<Approach> _approach;
268   std::string _sidTransition, _starTransition;
269   
270   double _totalDistance;
271   void rebuildLegData();
272   
273   typedef std::vector<Leg*> LegVec;
274   LegVec _legs;
275   
276   Delegate* _delegate;
277 };
278   
279 } // of namespace flightgear
280
281 #endif // of FG_FLIGHTPLAN_HXX