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