]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/positioned.hxx
Clean up/simplify NasalPositioned_cppbind
[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   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   const SGVec3d& cart() const;
124
125   double latitude() const
126   { return mPosition.getLatitudeDeg(); }
127   
128   double longitude() const
129   { return mPosition.getLongitudeDeg(); }
130   
131   double elevation() const
132   { return mPosition.getElevationFt(); }
133   
134   double elevationM() const
135   { return mPosition.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);
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   private:
177     std::vector<Type> types;
178     Type mMinType, mMaxType;
179   };
180   
181   static FGPositionedList findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter = NULL);
182   
183   static FGPositionedList findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial);
184         
185   static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
186
187   static FGPositionedRef findFirstWithIdent(const std::string& aIdent, Filter* aFilter = NULL);
188
189   /**
190    * Find all items with the specified ident
191    * @param aFilter - optional filter on items
192    */
193   static FGPositionedList findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true);
194   
195   /**
196    * As above, but searches names instead of idents
197    */
198   static FGPositionedList findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true);
199   
200   /**
201    * Sort an FGPositionedList by distance from a position
202    */
203   static void sortByRange(FGPositionedList&, const SGGeod& aPos);
204   
205   /**
206    * Find the closest item to a position, which pass the specified filter
207    * A cutoff range in NM must be specified, to constrain the search acceptably.
208    * Very large cutoff values will make this slow.
209    * 
210    * @result The closest item passing the filter, or NULL
211    * @param aCutoffNm - maximum distance to search within, in nautical miles
212    */
213   static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
214   
215   /**
216    * Find the closest N items to a position, which pass the specified filter
217    * A cutoff range in NM must be specified, to constrain the search acceptably.
218    * Very large cutoff values will make this slow.
219    * 
220    * @result The matches (possibly less than N, depending on the filter and cutoff),
221    *    sorted by distance from the search pos
222    * @param aN - number of matches to find
223    * @param aCutoffNm - maximum distance to search within, in nautical miles
224    */
225   static FGPositionedList findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
226     
227   /**
228    * Same as above, but with a time-bound in msec too.
229    */
230   static FGPositionedList findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter,
231                            bool& aPartial);
232   
233   template<class T>
234   static SGSharedPtr<T> loadById(PositionedID id)
235   {
236     return static_pointer_cast<T>( loadByIdImpl(id) );
237   }
238
239   template<class T>
240   static SGSharedPtr<T> loadById(const PositionedIDVec& id_vec, size_t index)
241   {
242     assert(index >= 0 && index < id_vec.size());
243     return loadById<T>(id_vec[index]);
244   }
245
246   template<class T>
247   static std::vector<SGSharedPtr<T> > loadAllById(const PositionedIDVec& id_vec)
248   {
249     std::vector<SGSharedPtr<T> > vec(id_vec.size());
250
251     for(size_t i = 0; i < id_vec.size(); ++i)
252       vec[i] = loadById<T>(id_vec[i]);
253
254     return vec;
255   }
256
257   /**
258    * Map a candidate type string to a real type. Returns INVALID if the string
259    * does not correspond to a defined type.
260    */
261   static Type typeFromName(const std::string& aName);
262
263   /**
264    * Map a type to a human-readable string
265    */
266   static const char* nameForType(Type aTy);
267
268   static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
269   static void deleteUserWaypoint(const std::string& aIdent);
270 protected:
271   friend class flightgear::NavDataCache;
272
273   FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos);
274
275   void modifyPosition(const SGGeod& newPos);
276
277   static FGPositionedRef loadByIdImpl(PositionedID id);
278
279   const PositionedID mGuid;
280   const SGGeod mPosition;
281   const SGVec3d mCart;
282   const Type mType;
283   const std::string mIdent;
284   
285 private:
286   SG_DISABLE_COPY(FGPositioned);
287 };
288
289 #endif // of FG_POSITIONED_HXX