1 // positioned.hxx - base class for objects which are positioned
3 // Copyright (C) 2008 James Turner
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.
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.
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.
21 #ifndef FG_POSITIONED_HXX
22 #define FG_POSITIONED_HXX
29 #include <simgear/sg_inlines.h>
30 #include <simgear/structure/SGSharedPtr.hxx>
31 #include <simgear/math/SGMath.hxx>
34 typedef SGSharedPtr<FGPositioned> FGPositionedRef;
36 typedef int64_t PositionedID;
37 typedef std::vector<PositionedID> PositionedIDVec;
39 namespace flightgear { class NavDataCache; }
41 class FGPositioned : public SGReferenced
64 /// important that DME & TACAN are adjacent to keep the TacanFilter
65 /// efficient - DMEs are proxies for TACAN/VORTAC stations
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
83 PARKING, ///< parking position - might be a gate, or stand
94 typedef std::vector<FGPositionedRef> List;
96 virtual ~FGPositioned();
101 const std::string& ident() const
105 * Return the name of this positioned. By default this is the same as the
106 * ident, but for many derived classes it's more meaningful - the aiport or
107 * navaid name, for example.
109 virtual const std::string& name() const
112 const SGGeod& geod() const
113 { return mPosition; }
115 PositionedID guid() const
119 * The cartesian position associated with this object
121 const SGVec3d& cart() const;
123 double latitude() const
124 { return mPosition.getLatitudeDeg(); }
126 double longitude() const
127 { return mPosition.getLongitudeDeg(); }
129 double elevation() const
130 { return mPosition.getElevationFt(); }
132 double elevationM() const
133 { return mPosition.getElevationM(); }
136 * Predicate class to support custom filtering of FGPositioned queries
137 * Default implementation of this passes any FGPositioned instance.
142 virtual ~Filter() { ; }
145 * Over-rideable filter method. Default implementation returns true.
147 virtual bool pass(FGPositioned* aPos) const
150 virtual Type minType() const
153 virtual Type maxType() const
157 bool operator()(FGPositioned* aPos) const
158 { return pass(aPos); }
161 class TypeFilter : public Filter
164 TypeFilter(Type aTy);
165 virtual bool pass(FGPositioned* aPos) const;
167 virtual Type minType() const
170 virtual Type maxType() const
173 void addType(Type aTy);
175 std::vector<Type> types;
176 Type mMinType, mMaxType;
179 static List findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter = NULL);
181 static List findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial);
183 static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
185 static FGPositionedRef findFirstWithIdent(const std::string& aIdent, Filter* aFilter = NULL);
188 * Find all items with the specified ident
189 * @param aFilter - optional filter on items
191 static List findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true);
194 * As above, but searches names instead of idents
196 static List findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true);
199 * Sort an FGPositionedList by distance from a position
201 static void sortByRange(List&, const SGGeod& aPos);
204 * Find the closest item to a position, which pass the specified filter
205 * A cutoff range in NM must be specified, to constrain the search acceptably.
206 * Very large cutoff values will make this slow.
208 * @result The closest item passing the filter, or NULL
209 * @param aCutoffNm - maximum distance to search within, in nautical miles
211 static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
214 * Find the closest N items to a position, which pass the specified filter
215 * A cutoff range in NM must be specified, to constrain the search acceptably.
216 * Very large cutoff values will make this slow.
218 * @result The matches (possibly less than N, depending on the filter and cutoff),
219 * sorted by distance from the search pos
220 * @param aN - number of matches to find
221 * @param aCutoffNm - maximum distance to search within, in nautical miles
223 static List findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
226 * Same as above, but with a time-bound in msec too.
228 static List findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter,
232 * Map a candidate type string to a real type. Returns INVALID if the string
233 * does not correspond to a defined type.
235 static Type typeFromName(const std::string& aName);
238 * Map a type to a human-readable string
240 static const char* nameForType(Type aTy);
242 static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
243 static void deleteUserWaypoint(const std::string& aIdent);
245 friend class flightgear::NavDataCache;
247 FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos);
249 void modifyPosition(const SGGeod& newPos);
251 static FGPositioned* loadByIdImpl(PositionedID id);
254 static T* loadById(PositionedID id)
256 return static_cast<T*>( loadByIdImpl(id) );
260 static T* loadById(const PositionedIDVec& id_vec, size_t index)
262 assert(index >= 0 && index < id_vec.size());
263 return loadById<T>(id_vec[index]);
267 static std::vector<T*> loadAllById(const PositionedIDVec& id_vec)
269 std::vector<T*> vec(id_vec.size());
271 for(size_t i = 0; i < id_vec.size(); ++i)
272 vec[i] = loadById<T>(id_vec[i]);
277 const PositionedID mGuid;
278 const SGGeod mPosition;
281 const std::string mIdent;
284 SG_DISABLE_COPY(FGPositioned);
287 #endif // of FG_POSITIONED_HXX