]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/positioned.hxx
Trying to bullet-proof the traffic code.
[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 typedef std::vector<FGPositionedRef> FGPositionedList;
36
37 typedef int64_t PositionedID;
38 typedef std::vector<PositionedID> PositionedIDVec;
39
40 namespace flightgear { class NavDataCache; }
41
42 class FGPositioned : public SGReferenced
43 {
44 public:
45     static const PositionedID TRANSIENT_ID;
46
47   typedef enum {
48     INVALID = 0,
49     AIRPORT,
50     HELIPORT,
51     SEAPORT,
52     RUNWAY,
53     HELIPAD,
54     TAXIWAY,
55     PAVEMENT,
56     WAYPOINT,
57     FIX,
58     NDB,
59     VOR,
60     ILS,
61     LOC,
62     GS,
63     OM,
64     MM,
65     IM,
66 /// important that DME & TACAN are adjacent to keep the TacanFilter
67 /// efficient - DMEs are proxies for TACAN/VORTAC stations
68     DME,
69     TACAN,
70     MOBILE_TACAN,
71     OBSTACLE,
72 /// an actual airport tower - not a radio comms facility!
73 /// some airports have multiple towers, eg EHAM, although our data source
74 /// doesn't necessarily include them     
75     TOWER,
76     FREQ_GROUND,
77     FREQ_TOWER,
78     FREQ_ATIS,
79     FREQ_AWOS,
80     FREQ_APP_DEP,
81     FREQ_ENROUTE,
82     FREQ_CLEARANCE,
83     FREQ_UNICOM,
84 // groundnet items
85     PARKING,  ///< parking position - might be a gate, or stand
86     TAXI_NODE,
87 // POI items
88     COUNTRY,
89     CITY,
90     TOWN,
91     VILLAGE,
92       
93     LAST_TYPE
94   } Type;
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   virtual 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   virtual const SGVec3d& cart() const;
125
126   double latitude() const
127   { return geod().getLatitudeDeg(); }
128   
129   double longitude() const
130   { return geod().getLongitudeDeg(); }
131   
132   double elevation() const
133   { return geod().getElevationFt(); }
134   
135   double elevationM() const
136   { return geod().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 = INVALID);
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       
178     static TypeFilter fromString(const std::string& aFilterSpec);
179   private:
180       
181     std::vector<Type> types;
182     Type mMinType, mMaxType;
183   };
184   
185   static FGPositionedList findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter);
186   
187   static FGPositionedList findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial);
188         
189   static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
190
191   static FGPositionedRef findFirstWithIdent(const std::string& aIdent, Filter* aFilter);
192
193   /**
194    * Find all items with the specified ident
195    * @param aFilter - optional filter on items
196    */
197   static FGPositionedList findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true);
198   
199   /**
200    * As above, but searches names instead of idents
201    */
202   static FGPositionedList findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true);
203   
204   /**
205    * Sort an FGPositionedList by distance from a position
206    */
207   static void sortByRange(FGPositionedList&, const SGGeod& aPos);
208   
209   /**
210    * Find the closest item to a position, which pass the specified filter
211    * A cutoff range in NM must be specified, to constrain the search acceptably.
212    * Very large cutoff values will make this slow.
213    * 
214    * @result The closest item passing the filter, or NULL
215    * @param aCutoffNm - maximum distance to search within, in nautical miles
216    */
217   static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
218   
219   /**
220    * Find the closest N items to a position, which pass the specified filter
221    * A cutoff range in NM must be specified, to constrain the search acceptably.
222    * Very large cutoff values will make this slow.
223    * 
224    * @result The matches (possibly less than N, depending on the filter and cutoff),
225    *    sorted by distance from the search pos
226    * @param aN - number of matches to find
227    * @param aCutoffNm - maximum distance to search within, in nautical miles
228    */
229   static FGPositionedList findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
230     
231   /**
232    * Same as above, but with a time-bound in msec too.
233    */
234   static FGPositionedList findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter,
235                            bool& aPartial);
236   
237   template<class T>
238   static SGSharedPtr<T> loadById(PositionedID id)
239   {
240     return static_pointer_cast<T>( loadByIdImpl(id) );
241   }
242
243   template<class T>
244   static SGSharedPtr<T> loadById(const PositionedIDVec& id_vec, size_t index)
245   {
246     assert(index >= 0 && index < id_vec.size());
247     return loadById<T>(id_vec[index]);
248   }
249
250   template<class T>
251   static std::vector<SGSharedPtr<T> > loadAllById(const PositionedIDVec& id_vec)
252   {
253     std::vector<SGSharedPtr<T> > vec(id_vec.size());
254
255     for(size_t i = 0; i < id_vec.size(); ++i)
256       vec[i] = loadById<T>(id_vec[i]);
257
258     return vec;
259   }
260
261   /**
262    * Map a candidate type string to a real type. Returns INVALID if the string
263    * does not correspond to a defined type.
264    */
265   static Type typeFromName(const std::string& aName);
266
267   /**
268    * Map a type to a human-readable string
269    */
270   static const char* nameForType(Type aTy);
271
272   static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
273   static bool deleteUserWaypoint(const std::string& aIdent);
274 protected:
275   friend class flightgear::NavDataCache;
276
277   FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos);
278
279   void modifyPosition(const SGGeod& newPos);
280   void invalidatePosition();
281
282   static FGPositionedRef loadByIdImpl(PositionedID id);
283
284   const PositionedID mGuid;
285   const Type mType;
286   const std::string mIdent;
287   
288 private:
289   SG_DISABLE_COPY(FGPositioned);
290
291   const SGGeod mPosition;
292   const SGVec3d mCart;
293 };
294
295 #endif // of FG_POSITIONED_HXX