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