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 NavDataCache* cache = NavDataCache::instance();
77 TypeFilter filter(WAYPOINT);
78 FGPositionedList existing = cache->findAllWithIdent(aIdent, &filter, true);
79 if (!existing.empty()) {
80 SG_LOG(SG_NAVAID, SG_WARN, "attempt to insert duplicate WAYPOINT:" << aIdent);
81 return existing.front().ptr();
84 PositionedID id = cache->createPOI(WAYPOINT, aIdent, aPos);
85 return cache->loadById(id);
88 void FGPositioned::deleteUserWaypoint(const std::string& aIdent)
90 NavDataCache* cache = NavDataCache::instance();
91 cache->removePOI(WAYPOINT, aIdent);
96 FGPositioned::cart() const
101 FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
103 if (aName.empty() || (aName == "")) {
112 const NameTypeEntry names[] = {
113 {"airport", AIRPORT},
125 {"tower", FREQ_TOWER},
126 {"ground", FREQ_GROUND},
127 {"approach", FREQ_APP_DEP},
128 {"departure", FREQ_APP_DEP},
130 {"helipad", HELIPAD},
131 {"country", COUNTRY},
134 {"village", VILLAGE},
138 {"gnd", FREQ_GROUND},
140 {"waypoint", WAYPOINT},
150 std::string lowerName(boost::to_lower_copy(aName));
152 for (const NameTypeEntry* n = names; (n->_name != NULL); ++n) {
153 if (::strcmp(n->_name, lowerName.c_str()) == 0) {
158 SG_LOG(SG_NAVAID, SG_WARN, "FGPositioned::typeFromName: couldn't match:" << aName);
162 const char* FGPositioned::nameForType(Type aTy)
165 case RUNWAY: return "runway";
166 case HELIPAD: return "helipad";
167 case TAXIWAY: return "taxiway";
168 case PAVEMENT: return "pavement";
169 case PARKING: return "parking stand";
170 case FIX: return "fix";
171 case VOR: return "VOR";
172 case NDB: return "NDB";
173 case ILS: return "ILS";
174 case LOC: return "localizer";
175 case GS: return "glideslope";
176 case OM: return "outer-marker";
177 case MM: return "middle-marker";
178 case IM: return "inner-marker";
179 case AIRPORT: return "airport";
180 case HELIPORT: return "heliport";
181 case SEAPORT: return "seaport";
182 case WAYPOINT: return "waypoint";
183 case DME: return "dme";
184 case TACAN: return "tacan";
185 case FREQ_TOWER: return "tower";
186 case FREQ_ATIS: return "atis";
187 case FREQ_AWOS: return "awos";
188 case FREQ_GROUND: return "ground";
189 case FREQ_CLEARANCE: return "clearance";
190 case FREQ_UNICOM: return "unicom";
191 case FREQ_APP_DEP: return "approach-departure";
192 case TAXI_NODE: return "taxi-node";
193 case COUNTRY: return "country";
194 case CITY: return "city";
195 case TOWN: return "town";
196 case VILLAGE: return "village";
202 ///////////////////////////////////////////////////////////////////////////////
203 // search / query functions
206 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
208 validateSGGeod(aPos);
209 return NavDataCache::instance()->findClosestWithIdent(aIdent, aPos, aFilter);
213 FGPositioned::findFirstWithIdent(const std::string& aIdent, Filter* aFilter)
215 if (aIdent.empty()) {
220 NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, true);
229 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
231 validateSGGeod(aPos);
233 FGPositionedList result;
234 Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
235 aRangeNm * SG_NM_TO_METER, aFilter, result, 0xffffff);
240 FGPositioned::findWithinRangePartial(const SGGeod& aPos, double aRangeNm, Filter* aFilter, bool& aPartial)
242 validateSGGeod(aPos);
245 FGPositionedList result;
246 aPartial = Octree::findAllWithinRange(SGVec3d::fromGeod(aPos),
247 aRangeNm * SG_NM_TO_METER, aFilter, result,
253 FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter, bool aExact)
255 return NavDataCache::instance()->findAllWithIdent(aIdent, aFilter, aExact);
259 FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter, bool aExact)
261 return NavDataCache::instance()->findAllWithName(aName, aFilter, aExact);
265 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
267 validateSGGeod(aPos);
269 FGPositionedList l(findClosestN(aPos, 1, aCutoffNm, aFilter));
274 assert(l.size() == 1);
279 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
281 validateSGGeod(aPos);
283 FGPositionedList result;
284 int limitMsec = 0xffff;
285 Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result, limitMsec);
290 FGPositioned::findClosestNPartial(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter, bool &aPartial)
292 validateSGGeod(aPos);
294 FGPositionedList result;
296 aPartial = Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result,
302 FGPositioned::sortByRange(FGPositionedList& aResult, const SGGeod& aPos)
304 validateSGGeod(aPos);
306 SGVec3d cartPos(SGVec3d::fromGeod(aPos));
307 // computer ordering values
308 Octree::FindNearestResults r;
309 FGPositionedList::iterator it = aResult.begin(), lend = aResult.end();
310 for (; it != lend; ++it) {
311 double d2 = distSqr((*it)->cart(), cartPos);
312 r.push_back(Octree::OrderedPositioned(*it, d2));
316 std::sort(r.begin(), r.end());
318 // convert to a plain list
319 unsigned int count = aResult.size();
320 for (unsigned int i=0; i<count; ++i) {
321 aResult[i] = r[i].get();
325 void FGPositioned::modifyPosition(const SGGeod& newPos)
327 const_cast<SGGeod&>(mPosition) = newPos;
328 const_cast<SGVec3d&>(mCart) = SGVec3d::fromGeod(newPos);
331 //------------------------------------------------------------------------------
332 FGPositionedRef FGPositioned::loadByIdImpl(PositionedID id)
334 return flightgear::NavDataCache::instance()->loadById(id);
337 FGPositioned::TypeFilter::TypeFilter(Type aTy) :
344 void FGPositioned::TypeFilter::addType(Type aTy)
346 if (aTy == INVALID) {
350 types.push_back(aTy);
351 mMinType = std::min(mMinType, aTy);
352 mMaxType = std::max(mMaxType, aTy);
356 FGPositioned::TypeFilter::pass(FGPositioned* aPos) const
362 std::vector<Type>::const_iterator it = types.begin(),
364 for (; it != end; ++it) {
365 if (aPos->type() == *it) {