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