]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/positioned.hxx
6d77d34208bfa2c4d9f0b6fa63f6608fb690e533
[flightgear.git] / src / Navaids / positioned.hxx
1 // positioned.hxx - base class for objects which are positioned 
2 //
3 // Copyright (C) 2008 James Turner
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21 #ifndef FG_POSITIONED_HXX
22 #define FG_POSITIONED_HXX
23
24 #include <string>
25 #include <vector>
26
27 #include <simgear/structure/SGSharedPtr.hxx>
28 #include <simgear/math/SGMath.hxx>
29
30 class FGPositioned;
31 class SGPropertyNode;
32
33 typedef SGSharedPtr<FGPositioned> FGPositionedRef;
34
35 namespace flightgear
36 {
37     class PositionedBinding;
38 }
39
40 class FGPositioned : public SGReferenced
41 {
42 public:
43
44   typedef enum {
45     INVALID = 0,
46     AIRPORT,
47     HELIPORT,
48     SEAPORT,
49     RUNWAY,
50     TAXIWAY,
51     PAVEMENT,
52     PARK_STAND,
53     WAYPOINT,
54     FIX,
55     VOR,
56     NDB,
57     ILS,
58     LOC,
59     GS,
60     OM,
61     MM,
62     IM,
63     DME,
64     TACAN,
65     OBSTACLE,
66     FREQ_GROUND,
67     FREQ_TOWER,
68     FREQ_ATIS,
69     FREQ_AWOS,
70     FREQ_APP_DEP,
71     FREQ_ENROUTE,
72     FREQ_CLEARANCE,
73     FREQ_UNICOM,
74     LAST_TYPE
75   } Type;
76
77   typedef std::vector<FGPositionedRef> List;
78   
79   virtual ~FGPositioned();
80   
81   Type type() const
82   { return mType; }
83
84   const std::string& ident() const
85   { return mIdent; }
86
87   /**
88    * Return the name of this positioned. By default this is the same as the
89    * ident, but for many derived classes it's more meaningful - the aiport or
90    * navaid name, for example.
91    */
92   virtual const std::string& name() const
93   { return mIdent; }
94
95   const SGGeod& geod() const
96   { return mPosition; }
97
98   /**
99    *  The cartesian position associated with this object
100    */
101   const SGVec3d& cart() const;
102
103   double latitude() const
104   { return mPosition.getLatitudeDeg(); }
105   
106   double longitude() const
107   { return mPosition.getLongitudeDeg(); }
108   
109   double elevation() const
110   { return mPosition.getElevationFt(); }
111   
112
113   virtual flightgear::PositionedBinding* createBinding(SGPropertyNode* nd) const;
114
115   /**
116    * Predicate class to support custom filtering of FGPositioned queries
117    * Default implementation of this passes any FGPositioned instance.
118    */
119   class Filter
120   {
121   public:
122     virtual ~Filter() { ; }
123     
124     /**
125      * Over-rideable filter method. Default implementation returns true.
126      */
127     virtual bool pass(FGPositioned* aPos) const
128     { return true; }
129     
130     virtual Type minType() const
131     { return INVALID; }
132     
133     virtual Type maxType() const
134     { return INVALID; }
135     
136     /**
137      * Test if this filter has a non-empty type range
138      */
139     bool hasTypeRange() const;
140     
141     /**
142      * Assuming hasTypeRange is true, test if a given type passes the range
143      */
144     bool passType(Type aTy) const;
145     
146     bool operator()(FGPositioned* aPos) const
147     { return pass(aPos); }
148   };
149   
150   class TypeFilter : public Filter
151   {
152   public:
153     TypeFilter(Type aTy);
154     virtual bool pass(FGPositioned* aPos) const;
155     void addType(Type aTy);
156   private:
157       std::vector<Type> types;
158   };
159   
160   static void installCommands();
161   
162   static List findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter = NULL);
163         
164   static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
165   
166   /**
167    * Find the next item with the specified partial ID, after the 'current' item
168    * Note this function is not hyper-efficient, particular where the partial id
169    * spans a large number of candidates.
170    *
171    * @param aCur - Current item, or NULL to retrieve the first item with partial id
172    * @param aId - the (partial) id to lookup
173    */
174   static FGPositionedRef findNextWithPartialId(FGPositionedRef aCur, const std::string& aId, Filter* aFilter = NULL);
175   
176   /**
177    * Find all items with the specified ident
178    * @param aFilter - optional filter on items
179    */
180   static List findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true);
181   
182   /**
183    * As above, but searches names instead of idents
184    */
185   static List findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true);
186   
187   /**
188    * Sort an FGPositionedList by distance from a position
189    */
190   static void sortByRange(List&, const SGGeod& aPos);
191   
192   /**
193    * Find the closest item to a position, which pass the specified filter
194    * A cutoff range in NM must be specified, to constrain the search acceptably.
195    * Very large cutoff values will make this slow.
196    * 
197    * @result The closest item passing the filter, or NULL
198    * @param aCutoffNm - maximum distance to search within, in nautical miles
199    */
200   static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
201   
202   /**
203    * Find the closest N items to a position, which pass the specified filter
204    * A cutoff range in NM must be specified, to constrain the search acceptably.
205    * Very large cutoff values will make this slow.
206    * 
207    * @result The matches (possibly less than N, depending on the filter and cutoff),
208    *    sorted by distance from the search pos
209    * @param aN - number of matches to find
210    * @param aCutoffNm - maximum distance to search within, in nautical miles
211    */
212   static List findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
213   
214   /**
215    * Map a candidate type string to a real type. Returns INVALID if the string
216    * does not correspond to a defined type.
217    */
218   static Type typeFromName(const std::string& aName);
219   
220   /**
221    * Map a type to a human-readable string
222    */
223   static const char* nameForType(Type aTy);
224   
225   static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
226 protected:
227   
228   FGPositioned(Type ty, const std::string& aIdent, const SGGeod& aPos);
229   
230   void init(bool aIndexed);
231   
232   // can't be const right now, navrecord at least needs to fix up the position
233   // after navaids are parsed
234   SGGeod mPosition; 
235   
236   SGVec3d mCart; // once mPosition is const, this can be const too
237   const Type mType;
238   const std::string mIdent;
239 };
240
241 #endif // of FG_POSITIONED_HXX