]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/positioned.hxx
Merge branch 'tat/configure'
[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 <vector>
25
26 #include <simgear/structure/SGSharedPtr.hxx>
27 #include <simgear/bucket/newbucket.hxx>
28
29 class FGPositioned;
30
31 typedef SGSharedPtr<FGPositioned> FGPositionedRef;
32
33 class FGPositioned : public SGReferenced
34 {
35 public:
36
37   typedef enum {
38     INVALID = 0,
39     AIRPORT,
40     HELIPORT,
41     SEAPORT,
42     RUNWAY,
43     TAXIWAY,
44     PAVEMENT,
45     PARK_STAND,
46     FIX,
47     VOR,
48     NDB,
49     ILS,
50     LOC,
51     GS,
52     OM,
53     MM,
54     IM,
55     DME,
56     TACAN,
57     OBSTACLE,
58     WAYPOINT, // user-defined waypoint
59     FREQ_GND,
60     FREQ_TWR,
61     FREQ_ATIS,
62     LAST_TYPE
63   } Type;
64
65   typedef std::vector<FGPositionedRef> List;
66   
67   virtual ~FGPositioned();
68   
69   Type type() const
70   { return mType; }
71
72   const std::string& ident() const
73   { return mIdent; }
74
75   /**
76    * Return the name of this positioned. By default this is the same as the
77    * ident, but for many derived classes it's more meaningful - the aiport or
78    * navaid name, for example.
79    */
80   virtual const std::string& name() const
81   { return mIdent; }
82
83   const SGGeod& geod() const
84   { return mPosition; }
85
86   /**
87    * Compute the cartesian position associated with this object
88    */
89   SGVec3d cart() const;
90
91   SGBucket bucket() const;
92   
93   double latitude() const
94   { return mPosition.getLatitudeDeg(); }
95   
96   double longitude() const
97   { return mPosition.getLongitudeDeg(); }
98   
99   double elevation() const
100   { return mPosition.getElevationFt(); }
101   
102   /**
103    * Predicate class to support custom filtering of FGPositioned queries
104    * Default implementation of this passes any FGPositioned instance.
105    */
106   class Filter
107   {
108   public:
109     virtual ~Filter() { ; }
110     
111     /**
112      * Over-rideable filter method. Default implementation returns true.
113      */
114     virtual bool pass(FGPositioned* aPos) const
115     { return true; }
116     
117     virtual Type minType() const
118     { return INVALID; }
119     
120     virtual Type maxType() const
121     { return INVALID; }
122     
123     /**
124      * Test if this filter has a non-empty type range
125      */
126     bool hasTypeRange() const;
127     
128     /**
129      * Assuming hasTypeRange is true, test if a given type passes the range
130      */
131     bool passType(Type aTy) const;
132     
133     bool operator()(FGPositioned* aPos) const
134     { return pass(aPos); }
135   };
136   
137   class TypeFilter : public Filter
138   {
139   public:
140     TypeFilter(Type aTy) : mType(aTy) { ; }
141     virtual bool pass(FGPositioned* aPos) const
142     { return (mType == aPos->type()); }
143   private:
144     const Type mType;
145   };
146   
147   static List findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter = NULL);
148         
149   static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
150   
151   /**
152    * Find the next item with the specified partial ID, after the 'current' item
153    * Note this function is not hyper-efficient, particular where the partial id
154    * spans a large number of candidates.
155    *
156    * @param aCur - Current item, or NULL to retrieve the first item with partial id
157    * @param aId - the (partial) id to lookup
158    */
159   static FGPositionedRef findNextWithPartialId(FGPositionedRef aCur, const std::string& aId, Filter* aFilter = NULL);
160   
161   /**
162    * As above, but searches using an offset index
163    */
164   static FGPositionedRef findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset, bool& aNext);
165
166   /**
167    * As above, but search names instead of idents
168    */
169   static FGPositionedRef findWithPartialName(const std::string& aName, Filter* aFilter, int aOffset, bool& aNext);
170   
171   /**
172    * Find all items with the specified ident, and return then sorted by
173    * distance from a position
174    *
175    * @param aFilter - optional filter on items
176    */
177   static List findAllWithIdentSortedByRange(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
178   
179   /**
180    * As above, but searches names instead of idents
181    */
182   static List findAllWithNameSortedByRange(const std::string& aName, const SGGeod& aPos, Filter* aFilter = NULL);
183   
184   /**
185    * Find the closest item to a position, which pass the specified filter
186    * A cutoff range in NM must be specified, to constrain the search acceptably.
187    * Very large cutoff values will make this slow.
188    * 
189    * @result The closest item passing the filter, or NULL
190    * @param aCutoffNm - maximum distance to search within, in nautical miles
191    */
192   static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
193   
194   /**
195    * Find the closest N items to a position, which pass the specified filter
196    * A cutoff range in NM must be specified, to constrain the search acceptably.
197    * Very large cutoff values will make this slow.
198    * 
199    * @result The matches (possibly less than N, depending on the filter and cutoff),
200    *    sorted by distance from the search pos
201    * @param aN - number of matches to find
202    * @param aCutoffNm - maximum distance to search within, in nautical miles
203    */
204   static List findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
205   
206   /**
207    * Find the closest match based on partial id (with an offset to allow selecting the n-th closest).
208    * Cutoff distance is limited internally, to avoid making this very slow.
209    */
210   static FGPositionedRef findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset, bool& aNext);
211
212   /**
213    * As above, but matches on name
214    */
215   static FGPositionedRef findClosestWithPartialName(const SGGeod& aPos, const std::string& aName, Filter* aFilter, int aOffset, bool& aNext);
216   
217   
218   /**
219    * Map a candidate type string to a real type. Returns INVALID if the string
220    * does not correspond to a defined type.
221    */
222   static Type typeFromName(const std::string& aName);
223   
224   /**
225    * Map a type to a human-readable string
226    */
227   static const char* nameForType(Type aTy);
228   
229   static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
230 protected:
231   
232   FGPositioned(Type ty, const std::string& aIdent, const SGGeod& aPos, bool aIndex = true);
233   
234   // can't be const right now, navrecord at least needs to fix up the position
235   // after navaids are parsed
236   SGGeod mPosition; 
237   
238   const Type mType;
239   const std::string mIdent;
240 };
241
242 #endif // of FG_POSITIONED_HXX