1 // positioned.cxx - 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.
25 #include "positioned.hxx"
29 #include <algorithm> // for sort
33 #include <boost/algorithm/string/case_conv.hpp>
34 #include <boost/algorithm/string/predicate.hpp>
36 #include <simgear/timing/timestamp.hxx>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/math/SGGeometry.hxx>
40 #include <simgear/sg_inlines.h>
42 #include "Navaids/PositionedOctree.hxx"
45 using namespace flightgear;
47 static void validateSGGeod(const SGGeod& geod)
49 if (SGMisc<double>::isNaN(geod.getLatitudeDeg()) ||
50 SGMisc<double>::isNaN(geod.getLongitudeDeg()))
52 throw sg_range_exception("position is invalid, NaNs");
57 ///////////////////////////////////////////////////////////////////////////////
59 FGPositioned::FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos) :
62 mCart(SGVec3d::fromGeod(mPosition)),
68 FGPositioned::~FGPositioned()
70 // std::cout << "destroying:" << mIdent << "/" << nameForType(mType) << std::endl;
74 FGPositioned::createUserWaypoint(const std::string& aIdent, const SGGeod& aPos)
76 PositionedID id = NavDataCache::instance()->createUserWaypoint(aIdent, aPos);
77 return NavDataCache::instance()->loadById(id);
81 FGPositioned::cart() const
86 FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
88 if (aName.empty() || (aName == "")) {
97 const NameTypeEntry names[] = {
110 {"tower", FREQ_TOWER},
111 {"ground", FREQ_GROUND},
112 {"approach", FREQ_APP_DEP},
113 {"departure", FREQ_APP_DEP},
115 {"gnd", FREQ_GROUND},
117 {"waypoint", WAYPOINT},
126 std::string lowerName(boost::to_lower_copy(aName));
128 for (const NameTypeEntry* n = names; (n->_name != NULL); ++n) {
129 if (::strcmp(n->_name, lowerName.c_str()) == 0) {
134 SG_LOG(SG_NAVAID, SG_WARN, "FGPositioned::typeFromName: couldn't match:" << aName);
138 const char* FGPositioned::nameForType(Type aTy)
141 case RUNWAY: return "runway";
142 case TAXIWAY: return "taxiway";
143 case PAVEMENT: return "pavement";
144 case PARKING: return "parking stand";
145 case FIX: return "fix";
146 case VOR: return "VOR";
147 case NDB: return "NDB";
148 case ILS: return "ILS";
149 case LOC: return "localiser";
150 case GS: return "glideslope";
151 case OM: return "outer-marker";
152 case MM: return "middle-marker";
153 case IM: return "inner-marker";
154 case AIRPORT: return "airport";
155 case HELIPORT: return "heliport";
156 case SEAPORT: return "seaport";
157 case WAYPOINT: return "waypoint";
158 case DME: return "dme";
159 case TACAN: return "tacan";
160 case FREQ_TOWER: return "tower";
161 case FREQ_ATIS: return "atis";
162 case FREQ_AWOS: return "awos";
163 case FREQ_GROUND: return "ground";
164 case FREQ_CLEARANCE: return "clearance";
165 case FREQ_UNICOM: return "unicom";
166 case FREQ_APP_DEP: return "approach-departure";
167 case TAXI_NODE: return "taxi-node";
173 ///////////////////////////////////////////////////////////////////////////////
174 // search / query functions
177 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
179 validateSGGeod(aPos);
180 return NavDataCache::instance()->findClosestWithIdent(aIdent, aPos, aFilter);
184 FGPositioned::findFirstWithIdent(const std::string& aIdent, Filter* aFilter)
186 if (aIdent.empty()) {
190 List r = NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, true);
199 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
201 validateSGGeod(aPos);
204 Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
205 aRangeNm * SG_NM_TO_METER, aFilter, result, 0xffffff);
210 FGPositioned::findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial)
212 validateSGGeod(aPos);
216 aPartial = Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
217 aRangeNm * SG_NM_TO_METER, aFilter, result,
223 FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter, bool aExact)
225 return NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, aExact);
229 FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter, bool aExact)
231 return NavDataCache::instance()->findAllWithName(aName, aFilter, aExact);
235 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
237 validateSGGeod(aPos);
239 List l(findClosestN(aPos, 1, aCutoffNm, aFilter));
244 assert(l.size() == 1);
249 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
251 validateSGGeod(aPos);
254 int limitMsec = 0xffff;
255 Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result, limitMsec);
260 FGPositioned::findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter, bool &aPartial)
262 validateSGGeod(aPos);
266 aPartial = Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result,
272 FGPositioned::sortByRange(List& aResult, const SGGeod& aPos)
274 validateSGGeod(aPos);
276 SGVec3d cartPos(SGVec3d::fromGeod(aPos));
277 // computer ordering values
278 Octree::FindNearestResults r;
279 List::iterator it = aResult.begin(), lend = aResult.end();
280 for (; it != lend; ++it) {
281 double d2 = distSqr((*it)->cart(), cartPos);
282 r.push_back(Octree::OrderedPositioned(*it, d2));
286 std::sort(r.begin(), r.end());
288 // convert to a plain list
289 unsigned int count = aResult.size();
290 for (unsigned int i=0; i<count; ++i) {
291 aResult[i] = r[i].get();
295 void FGPositioned::modifyPosition(const SGGeod& newPos)
297 const_cast<SGGeod&>(mPosition) = newPos;
298 const_cast<SGVec3d&>(mCart) = SGVec3d::fromGeod(newPos);
301 FGPositioned::TypeFilter::TypeFilter(Type aTy) :
308 void FGPositioned::TypeFilter::addType(Type aTy)
310 if (aTy == INVALID) {
314 types.push_back(aTy);
315 mMinType = std::min(mMinType, aTy);
316 mMaxType = std::max(mMaxType, aTy);
320 FGPositioned::TypeFilter::pass(FGPositioned* aPos) const
326 std::vector<Type>::const_iterator it = types.begin(),
328 for (; it != end; ++it) {
329 return aPos->type() == *it;