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