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");
58 ///////////////////////////////////////////////////////////////////////////////
60 FGPositioned::FGPositioned(PositionedID aGuid, Type ty, const std::string& aIdent, const SGGeod& aPos) :
63 mCart(SGVec3d::fromGeod(mPosition)),
69 FGPositioned::~FGPositioned()
71 // std::cout << "destroying:" << mIdent << "/" << nameForType(mType) << std::endl;
75 FGPositioned::createUserWaypoint(const std::string& aIdent, const SGGeod& aPos)
77 NavDataCache* cache = NavDataCache::instance();
78 TypeFilter filter(WAYPOINT);
79 FGPositionedList existing = cache->findAllWithIdent(aIdent, &filter, true);
80 if (!existing.empty()) {
81 SG_LOG(SG_NAVAID, SG_WARN, "attempt to insert duplicate WAYPOINT:" << aIdent);
82 return existing.front().ptr();
85 PositionedID id = cache->createPOI(WAYPOINT, aIdent, aPos);
86 return cache->loadById(id);
89 bool FGPositioned::deleteUserWaypoint(const std::string& aIdent)
91 NavDataCache* cache = NavDataCache::instance();
92 return cache->removePOI(WAYPOINT, aIdent);
97 FGPositioned::cart() const
102 FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
104 if (aName.empty() || (aName == "")) {
113 const NameTypeEntry names[] = {
114 {"airport", AIRPORT},
126 {"tower", FREQ_TOWER},
127 {"ground", FREQ_GROUND},
128 {"approach", FREQ_APP_DEP},
129 {"departure", FREQ_APP_DEP},
131 {"helipad", HELIPAD},
132 {"country", COUNTRY},
135 {"village", VILLAGE},
139 {"gnd", FREQ_GROUND},
141 {"waypoint", WAYPOINT},
151 std::string lowerName(boost::to_lower_copy(aName));
153 for (const NameTypeEntry* n = names; (n->_name != NULL); ++n) {
154 if (::strcmp(n->_name, lowerName.c_str()) == 0) {
159 SG_LOG(SG_NAVAID, SG_WARN, "FGPositioned::typeFromName: couldn't match:" << aName);
163 const char* FGPositioned::nameForType(Type aTy)
166 case RUNWAY: return "runway";
167 case HELIPAD: return "helipad";
168 case TAXIWAY: return "taxiway";
169 case PAVEMENT: return "pavement";
170 case PARKING: return "parking stand";
171 case FIX: return "fix";
172 case VOR: return "VOR";
173 case NDB: return "NDB";
174 case ILS: return "ILS";
175 case LOC: return "localizer";
176 case GS: return "glideslope";
177 case OM: return "outer-marker";
178 case MM: return "middle-marker";
179 case IM: return "inner-marker";
180 case AIRPORT: return "airport";
181 case HELIPORT: return "heliport";
182 case SEAPORT: return "seaport";
183 case WAYPOINT: return "waypoint";
184 case DME: return "dme";
185 case TACAN: return "tacan";
186 case FREQ_TOWER: return "tower";
187 case FREQ_ATIS: return "atis";
188 case FREQ_AWOS: return "awos";
189 case FREQ_GROUND: return "ground";
190 case FREQ_CLEARANCE: return "clearance";
191 case FREQ_UNICOM: return "unicom";
192 case FREQ_APP_DEP: return "approach-departure";
193 case TAXI_NODE: return "taxi-node";
194 case COUNTRY: return "country";
195 case CITY: return "city";
196 case TOWN: return "town";
197 case VILLAGE: return "village";
203 ///////////////////////////////////////////////////////////////////////////////
204 // search / query functions
207 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
209 validateSGGeod(aPos);
210 return NavDataCache::instance()->findClosestWithIdent(aIdent, aPos, aFilter);
214 FGPositioned::findFirstWithIdent(const std::string& aIdent, Filter* aFilter)
216 if (aIdent.empty()) {
221 NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, true);
230 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
232 validateSGGeod(aPos);
234 FGPositionedList result;
235 Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
236 aRangeNm * SG_NM_TO_METER, aFilter, result, 0xffffff);
241 FGPositioned::findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial)
243 validateSGGeod(aPos);
246 FGPositionedList result;
247 aPartial = Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
248 aRangeNm * SG_NM_TO_METER, aFilter, result,
254 FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter, bool aExact)
256 return NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, aExact);
260 FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter, bool aExact)
262 return NavDataCache::instance()->findAllWithName(aName, aFilter, aExact);
266 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
268 validateSGGeod(aPos);
270 FGPositionedList l(findClosestN(aPos, 1, aCutoffNm, aFilter));
275 assert(l.size() == 1);
280 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
282 validateSGGeod(aPos);
284 FGPositionedList result;
285 int limitMsec = 0xffff;
286 Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result, limitMsec);
291 FGPositioned::findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter, bool &aPartial)
293 validateSGGeod(aPos);
295 FGPositionedList result;
297 aPartial = Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result,
303 FGPositioned::sortByRange(FGPositionedList& aResult, const SGGeod& aPos)
305 validateSGGeod(aPos);
307 SGVec3d cartPos(SGVec3d::fromGeod(aPos));
308 // computer ordering values
309 Octree::FindNearestResults r;
310 FGPositionedList::iterator it = aResult.begin(), lend = aResult.end();
311 for (; it != lend; ++it) {
312 double d2 = distSqr((*it)->cart(), cartPos);
313 r.push_back(Octree::OrderedPositioned(*it, d2));
317 std::sort(r.begin(), r.end());
319 // convert to a plain list
320 unsigned int count = aResult.size();
321 for (unsigned int i=0; i<count; ++i) {
322 aResult[i] = r[i].get();
326 void FGPositioned::modifyPosition(const SGGeod& newPos)
328 const_cast<SGGeod&>(mPosition) = newPos;
329 const_cast<SGVec3d&>(mCart) = SGVec3d::fromGeod(newPos);
332 //------------------------------------------------------------------------------
333 FGPositionedRef FGPositioned::loadByIdImpl(PositionedID id)
335 return flightgear::NavDataCache::instance()->loadById(id);
338 FGPositioned::TypeFilter::TypeFilter(Type aTy) :
345 void FGPositioned::TypeFilter::addType(Type aTy)
347 if (aTy == INVALID) {
351 types.push_back(aTy);
352 mMinType = std::min(mMinType, aTy);
353 mMaxType = std::max(mMaxType, aTy);
356 FGPositioned::TypeFilter
357 FGPositioned::TypeFilter::fromString(const std::string& aFilterSpec)
359 if (aFilterSpec.empty()) {
360 throw sg_format_exception("empty filter spec:", aFilterSpec);
363 string_list parts = simgear::strutils::split(aFilterSpec, ",");
366 BOOST_FOREACH(std::string token, parts) {
367 f.addType(typeFromName(token));
374 FGPositioned::TypeFilter::pass(FGPositioned* aPos) const
380 std::vector<Type>::const_iterator it = types.begin(),
382 for (; it != end; ++it) {
383 if (aPos->type() == *it) {