]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/positioned.hxx
42476d8545e17c44d97f78de2c5fa5703371e8df
[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 <cassert>
25 #include <string>
26 #include <vector>
27 #include <stdint.h>
28
29 #include <simgear/sg_inlines.h>
30 #include <simgear/structure/SGSharedPtr.hxx>
31 #include <simgear/math/SGMath.hxx>
32
33 class FGPositioned;
34 typedef SGSharedPtr<FGPositioned> FGPositionedRef;
35
36 typedef int64_t PositionedID;
37 typedef std::vector<PositionedID> PositionedIDVec;
38
39 namespace flightgear { class NavDataCache; }
40
41 class FGPositioned : public SGReferenced
42 {
43 public:
44
45   typedef enum {
46     INVALID = 0,
47     AIRPORT,
48     HELIPORT,
49     SEAPORT,
50     RUNWAY,
51     HELIPAD,
52     TAXIWAY,
53     PAVEMENT,
54     WAYPOINT,
55     FIX,
56     NDB,
57     VOR,
58     ILS,
59     LOC,
60     GS,
61     OM,
62     MM,
63     IM,
64 /// important that DME & TACAN are adjacent to keep the TacanFilter
65 /// efficient - DMEs are proxies for TACAN/VORTAC stations
66     DME,
67     TACAN,
68     MOBILE_TACAN,
69     OBSTACLE,
70 /// an actual airport tower - not a radio comms facility!
71 /// some airports have multiple towers, eg EHAM, although our data source
72 /// doesn't necessarily include them     
73     TOWER,
74     FREQ_GROUND,
75     FREQ_TOWER,
76     FREQ_ATIS,
77     FREQ_AWOS,
78     FREQ_APP_DEP,
79     FREQ_ENROUTE,
80     FREQ_CLEARANCE,
81     FREQ_UNICOM,
82 // groundnet items
83     PARKING,  ///< parking position - might be a gate, or stand
84     TAXI_NODE,
85 // POI items
86     COUNTRY,
87     CITY,
88     TOWN,
89     VILLAGE,
90       
91     LAST_TYPE
92   } Type;
93
94   typedef std::vector<FGPositionedRef> List;
95   
96   virtual ~FGPositioned();
97   
98   Type type() const
99   { return mType; }
100
101   const char* typeString() const
102   { return nameForType(mType); }
103
104   const std::string& ident() const
105   { return mIdent; }
106
107   /**
108    * Return the name of this positioned. By default this is the same as the
109    * ident, but for many derived classes it's more meaningful - the aiport or
110    * navaid name, for example.
111    */
112   virtual const std::string& name() const
113   { return mIdent; }
114
115   const SGGeod& geod() const
116   { return mPosition; }
117   
118   PositionedID guid() const
119   { return mGuid; }
120
121   /**
122    *  The cartesian position associated with this object
123    */
124   const SGVec3d& cart() const;
125
126   double latitude() const
127   { return mPosition.getLatitudeDeg(); }
128   
129   double longitude() const
130   { return mPosition.getLongitudeDeg(); }
131   
132   double elevation() const
133   { return mPosition.getElevationFt(); }
134   
135   double elevationM() const
136   { return mPosition.getElevationM(); }
137
138   /**
139    * Predicate class to support custom filtering of FGPositioned queries
140    * Default implementation of this passes any FGPositioned instance.
141    */
142   class Filter
143   {
144   public:
145     virtual ~Filter() { ; }
146     
147     /**
148      * Over-rideable filter method. Default implementation returns true.
149      */
150     virtual bool pass(FGPositioned* aPos) const
151     { return true; }
152     
153     virtual Type minType() const
154     { return INVALID; }
155     
156     virtual Type maxType() const
157     { return INVALID; }
158     
159     
160     bool operator()(FGPositioned* aPos) const
161     { return pass(aPos); }
162   };
163   
164   class TypeFilter : public Filter
165   {
166   public:
167     TypeFilter(Type aTy);
168     virtual bool pass(FGPositioned* aPos) const;
169     
170     virtual Type minType() const
171     { return mMinType; }
172     
173     virtual Type maxType() const
174     { return mMaxType; }
175     
176     void addType(Type aTy);
177   private:
178     std::vector<Type> types;
179     Type mMinType, mMaxType;
180   };
181   
182   static List findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter = NULL);
183   
184   static List findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial);
185         
186   static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
187
188   static FGPositionedRef findFirstWithIdent(const std::string& aIdent, Filter* aFilter = NULL);
189
190   /**
191    * Find all items with the specified ident
192    * @param aFilter - optional filter on items
193    */
194   static List findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true);
195   
196   /**
197    * As above, but searches names instead of idents
198    */
199   static List findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true);
200   
201   /**
202    * Sort an FGPositionedList by distance from a position
203    */
204   static void sortByRange(List&, const SGGeod& aPos);
205   
206   /**
207    * Find the closest item to a position, which pass the specified filter
208    * A cutoff range in NM must be specified, to constrain the search acceptably.
209    * Very large cutoff values will make this slow.
210    * 
211    * @result The closest item passing the filter, or NULL
212    * @param aCutoffNm - maximum distance to search within, in nautical miles
213    */
214   static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
215   
216   /**
217    * Find the closest N items to a position, which pass the specified filter
218    * A cutoff range in NM must be specified, to constrain the search acceptably.
219    * Very large cutoff values will make this slow.
220    * 
221    * @result The matches (possibly less than N, depending on the filter and cutoff),
222    *    sorted by distance from the search pos
223    * @param aN - number of matches to find
224    * @param aCutoffNm - maximum distance to search within, in nautical miles
225    */
226   static List findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
227     
228   /**
229    * Same as above, but with a time-bound in msec too.
230    */
231   static List findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter,
232                            bool& aPartial);
233     
234   /**
235    * Map a candidate type string to a real type. Returns INVALID if the string
236    * does not correspond to a defined type.
237    */
238   static Type typeFromName(const std::string& aName);
239   
240   /**
241    * Map a type to a human-readable string
242    */
243   static const char* nameForType(Type aTy);
244   
245   static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
246   static void deleteUserWaypoint(const std::string& aIdent);
247 protected:
248   friend class flightgear::NavDataCache;
249   
250   FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos);
251     
252   void modifyPosition(const SGGeod& newPos);
253
254   static FGPositioned* loadByIdImpl(PositionedID id);
255
256   template<class T>
257   static T* loadById(PositionedID id)
258   {
259     return static_cast<T*>( loadByIdImpl(id) );
260   }
261
262   template<class T>
263   static T* loadById(const PositionedIDVec& id_vec, size_t index)
264   {
265     assert(index >= 0 && index < id_vec.size());
266     return loadById<T>(id_vec[index]);
267   }
268
269   template<class T>
270   static std::vector<T*> loadAllById(const PositionedIDVec& id_vec)
271   {
272     std::vector<T*> vec(id_vec.size());
273
274     for(size_t i = 0; i < id_vec.size(); ++i)
275       vec[i] = loadById<T>(id_vec[i]);
276
277     return vec;
278   }
279
280   const PositionedID mGuid;
281   const SGGeod mPosition;
282   const SGVec3d mCart;
283   const Type mType;
284   const std::string mIdent;
285   
286 private:
287   SG_DISABLE_COPY(FGPositioned);
288 };
289
290 #endif // of FG_POSITIONED_HXX