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