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;
35 typedef std::vector<FGPositionedRef> FGPositionedList;
37 typedef int64_t PositionedID;
38 typedef std::vector<PositionedID> PositionedIDVec;
40 namespace flightgear { class NavDataCache; }
42 class FGPositioned : public SGReferenced
65 /// important that DME & TACAN are adjacent to keep the TacanFilter
66 /// efficient - DMEs are proxies for TACAN/VORTAC stations
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
84 PARKING, ///< parking position - might be a gate, or stand
95 virtual ~FGPositioned();
100 const char* typeString() const
101 { return nameForType(mType); }
103 const std::string& ident() const
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.
111 virtual const std::string& name() const
114 const SGGeod& geod() const
115 { return mPosition; }
117 PositionedID guid() const
121 * The cartesian position associated with this object
123 const SGVec3d& cart() const;
125 double latitude() const
126 { return mPosition.getLatitudeDeg(); }
128 double longitude() const
129 { return mPosition.getLongitudeDeg(); }
131 double elevation() const
132 { return mPosition.getElevationFt(); }
134 double elevationM() const
135 { return mPosition.getElevationM(); }
138 * Predicate class to support custom filtering of FGPositioned queries
139 * Default implementation of this passes any FGPositioned instance.
144 virtual ~Filter() { ; }
147 * Over-rideable filter method. Default implementation returns true.
149 virtual bool pass(FGPositioned* aPos) const
152 virtual Type minType() const
155 virtual Type maxType() const
159 bool operator()(FGPositioned* aPos) const
160 { return pass(aPos); }
163 class TypeFilter : public Filter
166 TypeFilter(Type aTy = INVALID);
167 virtual bool pass(FGPositioned* aPos) const;
169 virtual Type minType() const
172 virtual Type maxType() const
175 void addType(Type aTy);
177 static TypeFilter fromString(const std::string& aFilterSpec);
180 std::vector<Type> types;
181 Type mMinType, mMaxType;
184 static FGPositionedList findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter = NULL);
186 static FGPositionedList findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial);
188 static FGPositionedRef findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter = NULL);
190 static FGPositionedRef findFirstWithIdent(const std::string& aIdent, Filter* aFilter = NULL);
193 * Find all items with the specified ident
194 * @param aFilter - optional filter on items
196 static FGPositionedList findAllWithIdent(const std::string& aIdent, Filter* aFilter = NULL, bool aExact = true);
199 * As above, but searches names instead of idents
201 static FGPositionedList findAllWithName(const std::string& aName, Filter* aFilter = NULL, bool aExact = true);
204 * Sort an FGPositionedList by distance from a position
206 static void sortByRange(FGPositionedList&, const SGGeod& aPos);
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.
213 * @result The closest item passing the filter, or NULL
214 * @param aCutoffNm - maximum distance to search within, in nautical miles
216 static FGPositionedRef findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter = NULL);
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.
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
228 static FGPositionedList findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter = NULL);
231 * Same as above, but with a time-bound in msec too.
233 static FGPositionedList findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter,
237 static SGSharedPtr<T> loadById(PositionedID id)
239 return static_pointer_cast<T>( loadByIdImpl(id) );
243 static SGSharedPtr<T> loadById(const PositionedIDVec& id_vec, size_t index)
245 assert(index >= 0 && index < id_vec.size());
246 return loadById<T>(id_vec[index]);
250 static std::vector<SGSharedPtr<T> > loadAllById(const PositionedIDVec& id_vec)
252 std::vector<SGSharedPtr<T> > vec(id_vec.size());
254 for(size_t i = 0; i < id_vec.size(); ++i)
255 vec[i] = loadById<T>(id_vec[i]);
261 * Map a candidate type string to a real type. Returns INVALID if the string
262 * does not correspond to a defined type.
264 static Type typeFromName(const std::string& aName);
267 * Map a type to a human-readable string
269 static const char* nameForType(Type aTy);
271 static FGPositioned* createUserWaypoint(const std::string& aIdent, const SGGeod& aPos);
272 static bool deleteUserWaypoint(const std::string& aIdent);
274 friend class flightgear::NavDataCache;
276 FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos);
278 void modifyPosition(const SGGeod& newPos);
280 static FGPositionedRef loadByIdImpl(PositionedID id);
282 const PositionedID mGuid;
283 const SGGeod mPosition;
286 const std::string mIdent;
289 SG_DISABLE_COPY(FGPositioned);
292 #endif // of FG_POSITIONED_HXX