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/foreach.hpp>
34 #include <boost/algorithm/string/case_conv.hpp>
35 #include <boost/algorithm/string/predicate.hpp>
37 #include <simgear/timing/timestamp.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/structure/exception.hxx>
40 #include <simgear/math/SGGeometry.hxx>
41 #include <simgear/sg_inlines.h>
43 #include "Navaids/PositionedOctree.hxx"
46 using namespace flightgear;
48 static void validateSGGeod(const SGGeod& geod)
50 if (SGMisc<double>::isNaN(geod.getLatitudeDeg()) ||
51 SGMisc<double>::isNaN(geod.getLongitudeDeg()))
53 throw sg_range_exception("position is invalid, NaNs");
57 static bool validateFilter(FGPositioned::Filter* filter)
59 if (filter->maxType() < filter->minType()) {
60 SG_LOG(SG_GENERAL, SG_WARN, "invalid positioned filter specified");
68 ///////////////////////////////////////////////////////////////////////////////
70 FGPositioned::FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos) :
73 mCart(SGVec3d::fromGeod(mPosition)),
79 FGPositioned::~FGPositioned()
81 // std::cout << "destroying:" << mIdent << "/" << nameForType(mType) << std::endl;
85 FGPositioned::createUserWaypoint(const std::string& aIdent, const SGGeod& aPos)
87 NavDataCache* cache = NavDataCache::instance();
88 TypeFilter filter(WAYPOINT);
89 FGPositionedList existing = cache->findAllWithIdent(aIdent, &filter, true);
90 if (!existing.empty()) {
91 SG_LOG(SG_NAVAID, SG_WARN, "attempt to insert duplicate WAYPOINT:" << aIdent);
92 return existing.front().ptr();
95 PositionedID id = cache->createPOI(WAYPOINT, aIdent, aPos);
96 return cache->loadById(id);
99 bool FGPositioned::deleteUserWaypoint(const std::string& aIdent)
101 NavDataCache* cache = NavDataCache::instance();
102 return cache->removePOI(WAYPOINT, aIdent);
107 FGPositioned::cart() const
112 FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
114 if (aName.empty() || (aName == "")) {
123 const NameTypeEntry names[] = {
124 {"airport", AIRPORT},
136 {"tower", FREQ_TOWER},
137 {"ground", FREQ_GROUND},
138 {"approach", FREQ_APP_DEP},
139 {"departure", FREQ_APP_DEP},
141 {"helipad", HELIPAD},
142 {"country", COUNTRY},
145 {"village", VILLAGE},
149 {"gnd", FREQ_GROUND},
151 {"waypoint", WAYPOINT},
161 std::string lowerName(boost::to_lower_copy(aName));
163 for (const NameTypeEntry* n = names; (n->_name != NULL); ++n) {
164 if (::strcmp(n->_name, lowerName.c_str()) == 0) {
169 SG_LOG(SG_NAVAID, SG_WARN, "FGPositioned::typeFromName: couldn't match:" << aName);
173 const char* FGPositioned::nameForType(Type aTy)
176 case RUNWAY: return "runway";
177 case HELIPAD: return "helipad";
178 case TAXIWAY: return "taxiway";
179 case PAVEMENT: return "pavement";
180 case PARKING: return "parking stand";
181 case FIX: return "fix";
182 case VOR: return "VOR";
183 case NDB: return "NDB";
184 case ILS: return "ILS";
185 case LOC: return "localizer";
186 case GS: return "glideslope";
187 case OM: return "outer-marker";
188 case MM: return "middle-marker";
189 case IM: return "inner-marker";
190 case AIRPORT: return "airport";
191 case HELIPORT: return "heliport";
192 case SEAPORT: return "seaport";
193 case WAYPOINT: return "waypoint";
194 case DME: return "dme";
195 case TACAN: return "tacan";
196 case FREQ_TOWER: return "tower";
197 case FREQ_ATIS: return "atis";
198 case FREQ_AWOS: return "awos";
199 case FREQ_GROUND: return "ground";
200 case FREQ_CLEARANCE: return "clearance";
201 case FREQ_UNICOM: return "unicom";
202 case FREQ_APP_DEP: return "approach-departure";
203 case TAXI_NODE: return "taxi-node";
204 case COUNTRY: return "country";
205 case CITY: return "city";
206 case TOWN: return "town";
207 case VILLAGE: return "village";
213 ///////////////////////////////////////////////////////////////////////////////
214 // search / query functions
217 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
219 validateSGGeod(aPos);
220 return NavDataCache::instance()->findClosestWithIdent(aIdent, aPos, aFilter);
224 FGPositioned::findFirstWithIdent(const std::string& aIdent, Filter* aFilter)
226 if (aIdent.empty()) {
231 NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, true);
240 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
242 validateSGGeod(aPos);
244 if (!validateFilter(aFilter)) {
245 return FGPositionedList();
248 FGPositionedList result;
249 Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
250 aRangeNm * SG_NM_TO_METER, aFilter, result, 0xffffff);
255 FGPositioned::findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial)
257 validateSGGeod(aPos);
259 if (!validateFilter(aFilter)) {
260 return FGPositionedList();
264 FGPositionedList result;
265 aPartial = Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
266 aRangeNm * SG_NM_TO_METER, aFilter, result,
272 FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter, bool aExact)
274 if (!validateFilter(aFilter)) {
275 return FGPositionedList();
278 return NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, aExact);
282 FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter, bool aExact)
284 if (!validateFilter(aFilter)) {
285 return FGPositionedList();
288 return NavDataCache::instance()->findAllWithName(aName, aFilter, aExact);
292 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
294 validateSGGeod(aPos);
296 if (!validateFilter(aFilter)) {
300 FGPositionedList l(findClosestN(aPos, 1, aCutoffNm, aFilter));
305 assert(l.size() == 1);
310 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
312 validateSGGeod(aPos);
314 FGPositionedList result;
315 int limitMsec = 0xffff;
316 Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result, limitMsec);
321 FGPositioned::findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter, bool &aPartial)
323 validateSGGeod(aPos);
325 FGPositionedList result;
327 aPartial = Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result,
333 FGPositioned::sortByRange(FGPositionedList& aResult, const SGGeod& aPos)
335 validateSGGeod(aPos);
337 SGVec3d cartPos(SGVec3d::fromGeod(aPos));
338 // computer ordering values
339 Octree::FindNearestResults r;
340 FGPositionedList::iterator it = aResult.begin(), lend = aResult.end();
341 for (; it != lend; ++it) {
342 double d2 = distSqr((*it)->cart(), cartPos);
343 r.push_back(Octree::OrderedPositioned(*it, d2));
347 std::sort(r.begin(), r.end());
349 // convert to a plain list
350 unsigned int count = aResult.size();
351 for (unsigned int i=0; i<count; ++i) {
352 aResult[i] = r[i].get();
356 void FGPositioned::modifyPosition(const SGGeod& newPos)
358 const_cast<SGGeod&>(mPosition) = newPos;
359 const_cast<SGVec3d&>(mCart) = SGVec3d::fromGeod(newPos);
362 //------------------------------------------------------------------------------
363 FGPositionedRef FGPositioned::loadByIdImpl(PositionedID id)
365 return flightgear::NavDataCache::instance()->loadById(id);
368 FGPositioned::TypeFilter::TypeFilter(Type aTy) :
375 void FGPositioned::TypeFilter::addType(Type aTy)
377 if (aTy == INVALID) {
381 types.push_back(aTy);
382 mMinType = std::min(mMinType, aTy);
383 mMaxType = std::max(mMaxType, aTy);
386 FGPositioned::TypeFilter
387 FGPositioned::TypeFilter::fromString(const std::string& aFilterSpec)
389 if (aFilterSpec.empty()) {
390 throw sg_format_exception("empty filter spec:", aFilterSpec);
393 string_list parts = simgear::strutils::split(aFilterSpec, ",");
396 BOOST_FOREACH(std::string token, parts) {
397 f.addType(typeFromName(token));
404 FGPositioned::TypeFilter::pass(FGPositioned* aPos) const
410 std::vector<Type>::const_iterator it = types.begin(),
412 for (; it != end; ++it) {
413 if (aPos->type() == *it) {