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.
27 #include <algorithm> // for sort
28 #include <locale> // for char-traits toupper
32 #include <boost/algorithm/string/case_conv.hpp>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/timing/timestamp.hxx>
36 #include <simgear/debug/logstream.hxx>
38 #include "positioned.hxx"
40 typedef std::multimap<std::string, FGPositioned*> NamedPositionedIndex;
41 typedef std::pair<NamedPositionedIndex::const_iterator, NamedPositionedIndex::const_iterator> NamedIndexRange;
43 using std::lower_bound;
44 using std::upper_bound;
47 * Order positioned elements by type, then pointer address. This allows us to
48 * use range searches (lower_ and upper_bound) to grab items of a particular
49 * type out of bucket efficently.
54 bool operator()(const FGPositioned* a, const FGPositioned* b) const
56 if (a->type() == b->type()) return a < b;
57 return a->type() < b->type();
61 class LowerLimitOfType
64 bool operator()(const FGPositioned* a, const FGPositioned::Type b) const
69 bool operator()(const FGPositioned::Type a, const FGPositioned* b) const
74 // The operator below is required by VS2005 in debug mode
75 bool operator()(const FGPositioned* a, const FGPositioned* b) const
77 return a->type() < b->type();
82 typedef std::set<FGPositioned*, OrderByType> BucketEntry;
83 typedef std::map<long int, BucketEntry> SpatialPositionedIndex;
85 static NamedPositionedIndex global_identIndex;
86 static NamedPositionedIndex global_nameIndex;
87 static SpatialPositionedIndex global_spatialIndex;
89 SpatialPositionedIndex::iterator
90 bucketEntryForPositioned(FGPositioned* aPos)
92 int bucketIndex = aPos->bucket().gen_index();
93 SpatialPositionedIndex::iterator it = global_spatialIndex.find(bucketIndex);
94 if (it != global_spatialIndex.end()) {
98 // create a new BucketEntry
99 return global_spatialIndex.insert(it, std::make_pair(bucketIndex, BucketEntry()));
103 addToIndices(FGPositioned* aPos)
106 if (!aPos->ident().empty()) {
107 global_identIndex.insert(global_identIndex.begin(),
108 std::make_pair(aPos->ident(), aPos));
111 if (!aPos->name().empty()) {
112 global_nameIndex.insert(global_nameIndex.begin(),
113 std::make_pair(aPos->name(), aPos));
117 SpatialPositionedIndex::iterator it = bucketEntryForPositioned(aPos);
118 it->second.insert(aPos);
122 removeFromIndices(FGPositioned* aPos)
126 if (!aPos->ident().empty()) {
127 NamedPositionedIndex::iterator it = global_identIndex.find(aPos->ident());
128 while (it != global_identIndex.end() && (it->first == aPos->ident())) {
129 if (it->second == aPos) {
130 global_identIndex.erase(it);
135 } // of multimap walk
138 if (!aPos->name().empty()) {
139 NamedPositionedIndex::iterator it = global_nameIndex.find(aPos->name());
140 while (it != global_nameIndex.end() && (it->first == aPos->name())) {
141 if (it->second == aPos) {
142 global_nameIndex.erase(it);
147 } // of multimap walk
150 SpatialPositionedIndex::iterator sit = bucketEntryForPositioned(aPos);
151 sit->second.erase(aPos);
155 spatialFilterInBucket(const SGBucket& aBucket, FGPositioned::Filter* aFilter, FGPositioned::List& aResult)
157 SpatialPositionedIndex::const_iterator it;
158 it = global_spatialIndex.find(aBucket.gen_index());
159 if (it == global_spatialIndex.end()) {
163 BucketEntry::const_iterator l = it->second.begin();
164 BucketEntry::const_iterator u = it->second.end();
166 if (!aFilter) { // pass everything
167 aResult.insert(aResult.end(), l, u);
171 if (aFilter->hasTypeRange()) {
172 // avoid many calls to the filter hook
173 l = lower_bound(it->second.begin(), it->second.end(), aFilter->minType(), LowerLimitOfType());
174 u = upper_bound(l, it->second.end(), aFilter->maxType(), LowerLimitOfType());
177 for ( ; l != u; ++l) {
178 if ((*aFilter)(*l)) {
179 aResult.push_back(*l);
185 spatialFind(const SGGeod& aPos, double aRange,
186 FGPositioned::Filter* aFilter, FGPositioned::List& aResult)
189 double lat = aPos.getLatitudeDeg(),
190 lon = aPos.getLongitudeDeg();
192 int bx = (int)( aRange*SG_NM_TO_METER / buck.get_width_m() / 2);
193 int by = (int)( aRange*SG_NM_TO_METER / buck.get_height_m() / 2 );
195 // loop over bucket range
196 for ( int i=-bx; i<=bx; i++) {
197 for ( int j=-by; j<=by; j++) {
198 spatialFilterInBucket(sgBucketOffset(lon, lat, i, j), aFilter, aResult);
205 class RangePredictate
208 RangePredictate(const SGGeod& aOrigin, double aRange) :
209 mOrigin(SGVec3d::fromGeod(aOrigin)),
210 mRangeSqr(aRange * aRange)
213 bool operator()(const FGPositionedRef& aPos)
215 double dSqr = distSqr(aPos->cart(), mOrigin);
216 return (dSqr > mRangeSqr);
225 filterListByRange(const SGGeod& aPos, double aRange, FGPositioned::List& aResult)
227 RangePredictate pred(aPos, aRange * SG_NM_TO_METER);
228 FGPositioned::List::iterator newEnd;
229 newEnd = std::remove_if(aResult.begin(), aResult.end(), pred);
230 aResult.erase(newEnd, aResult.end());
233 class DistanceOrdering
236 DistanceOrdering(const SGGeod& aPos) :
237 mPos(SGVec3d::fromGeod(aPos))
240 bool operator()(const FGPositionedRef& a, const FGPositionedRef& b) const
242 double dA = distSqr(a->cart(), mPos),
243 dB = distSqr(b->cart(), mPos);
252 sortByDistance(const SGGeod& aPos, FGPositioned::List& aResult)
254 std::sort(aResult.begin(), aResult.end(), DistanceOrdering(aPos));
257 static FGPositionedRef
258 namedFindClosest(const NamedPositionedIndex& aIndex, const std::string& aName,
259 const SGGeod& aOrigin, FGPositioned::Filter* aFilter)
261 NamedIndexRange range = aIndex.equal_range(aName);
262 if (range.first == range.second) {
266 // common case, only one result. looks a bit ugly because these are
267 // sequential iterators, not random-access ones
268 NamedPositionedIndex::const_iterator check = range.first;
269 if (++check == range.second) {
270 // excellent, only one match in the range
271 FGPositioned* r = range.first->second;
273 if (aFilter->hasTypeRange() && !aFilter->passType(r->type())) {
277 if (!aFilter->pass(r)) {
280 } // of have a filter
283 } // of short-circuit logic for single-element range
285 // multiple matches, we need to actually check the distance to each one
286 double minDist = HUGE_VAL;
287 FGPositionedRef result;
288 NamedPositionedIndex::const_iterator it = range.first;
289 SGVec3d cartOrigin(SGVec3d::fromGeod(aOrigin));
291 for (; it != range.second; ++it) {
292 FGPositioned* r = it->second;
294 if (aFilter->hasTypeRange() && !aFilter->passType(r->type())) {
298 if (!aFilter->pass(r)) {
304 double d2 = distSqr(cartOrigin, r->cart());
314 static FGPositioned::List
315 spatialGetClosest(const SGGeod& aPos, unsigned int aN, double aCutoffNm, FGPositioned::Filter* aFilter)
317 FGPositioned::List result;
318 int radius = 1; // start at 1, radius 0 is handled explicitly
320 double lat = aPos.getLatitudeDeg(),
321 lon = aPos.getLongitudeDeg();
322 // final cutoff is in metres, and scaled to account for testing the corners
323 // of the 'box' instead of the centre of each edge
324 double cutoffM = aCutoffNm * SG_NM_TO_METER * 1.5;
326 // base case, simplifes loop to do it seperately here
327 spatialFilterInBucket(sgBucketOffset(lon, lat, 0, 0), aFilter, result);
329 for (;result.size() < aN; ++radius) {
331 double az1, az2, d1, d2;
332 SGGeodesy::inverse(aPos, sgBucketOffset(lon, lat, -radius, -radius).get_center(), az1, az2, d1);
333 SGGeodesy::inverse(aPos, sgBucketOffset(lon, lat, radius, radius).get_center(), az1, az2, d2);
335 if ((d1 > cutoffM) && (d2 > cutoffM)) {
336 //std::cerr << "spatialGetClosest terminating due to range cutoff" << std::endl;
340 FGPositioned::List hits;
341 for ( int i=-radius; i<=radius; i++) {
342 spatialFilterInBucket(sgBucketOffset(lon, lat, i, -radius), aFilter, hits);
343 spatialFilterInBucket(sgBucketOffset(lon, lat, -radius, i), aFilter, hits);
344 spatialFilterInBucket(sgBucketOffset(lon, lat, i, radius), aFilter, hits);
345 spatialFilterInBucket(sgBucketOffset(lon, lat, radius, i), aFilter, hits);
348 result.insert(result.end(), hits.begin(), hits.end()); // append
351 sortByDistance(aPos, result);
352 if (result.size() > aN) {
353 result.resize(aN); // truncate at requested number of matches
359 //////////////////////////////////////////////////////////////////////////////
364 bool operator()(FGPositioned* a, FGPositioned* b) const
366 return a->name() < b->name();
371 * A special purpose helper (imported by FGAirport::searchNamesAndIdents) to
372 * implement the AirportList dialog. It's unfortunate that it needs to reside
373 * here, but for now it's least ugly solution.
375 char** searchAirportNamesAndIdents(const std::string& aFilter)
377 const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(std::locale());
378 std::string filter(aFilter);
379 bool hasFilter = !filter.empty();
381 ct.toupper((char *)filter.data(), (char *)filter.data() + filter.size());
384 NamedPositionedIndex::const_iterator it = global_identIndex.begin();
385 NamedPositionedIndex::const_iterator end = global_identIndex.end();
387 // note this is a vector of raw pointers, not smart pointers, because it
388 // may get very large and smart-pointer-atomicity-locking then becomes a
389 // bottleneck for this case.
390 std::vector<FGPositioned*> matches;
393 for (; it != end; ++it) {
394 FGPositioned::Type ty = it->second->type();
395 if ((ty < FGPositioned::AIRPORT) || (ty > FGPositioned::SEAPORT)) {
399 if (hasFilter && (it->second->ident().find(aFilter) == std::string::npos)) {
400 upper = it->second->name(); // string copy, sadly
401 ct.toupper((char *)upper.data(), (char *)upper.data() + upper.size());
402 if (upper.find(aFilter) == std::string::npos) {
407 matches.push_back(it->second);
410 // sort alphabetically on name
411 std::sort(matches.begin(), matches.end(), OrderByName());
413 // convert results to format comptible with puaList
414 unsigned int numMatches = matches.size();
415 char** result = new char*[numMatches + 1];
416 result[numMatches] = NULL; // end-of-list marker
418 // nasty code to avoid excessive string copying and allocations.
419 // We format results as follows (note whitespace!):
420 // ' name-of-airport-chars (ident)'
421 // so the total length is:
422 // 1 + strlen(name) + 4 + 4 (for the ident) + 1 + 1 (for the null)
423 // which gives a grand total of 11 + the length of the name.
424 // note the ident is sometimes only three letters for non-ICAO small strips
425 for (unsigned int i=0; i<numMatches; ++i) {
426 int nameLength = matches[i]->name().size();
427 int icaoLength = matches[i]->ident().size();
428 char* entry = new char[nameLength + 11];
431 memcpy(dst, matches[i]->name().c_str(), nameLength);
437 memcpy(dst, matches[i]->ident().c_str(), icaoLength);
447 ///////////////////////////////////////////////////////////////////////////////
450 FGPositioned::Filter::hasTypeRange() const
452 assert(minType() <= maxType());
453 return (minType() != INVALID) && (maxType() != INVALID);
457 FGPositioned::Filter::passType(Type aTy) const
459 assert(hasTypeRange());
460 return (minType() <= aTy) && (maxType() >= aTy);
463 static FGPositioned::List
464 findAllSortedByRange(const NamedPositionedIndex& aIndex,
465 const std::string& aName, const SGGeod& aPos, FGPositioned::Filter* aFilter)
467 FGPositioned::List result;
468 NamedIndexRange range = aIndex.equal_range(aName);
469 for (; range.first != range.second; ++range.first) {
470 FGPositioned* candidate = range.first->second;
472 if (aFilter->hasTypeRange() && !aFilter->passType(candidate->type())) {
476 if (!aFilter->pass(candidate)) {
481 result.push_back(range.first->second);
484 sortByDistance(aPos, result);
488 static FGPositionedRef
489 findWithPartial(const NamedPositionedIndex& aIndex, const std::string& aName,
490 FGPositioned::Filter* aFilter, int aOffset, bool& aNext)
492 // see comment in findNextWithPartialId concerning upperBoundId
493 std::string upperBoundId = aName;
494 upperBoundId[upperBoundId.size()-1]++;
495 NamedPositionedIndex::const_iterator upperBound = aIndex.lower_bound(upperBoundId);
497 NamedIndexRange range = aIndex.equal_range(aName);
498 FGPositionedRef result;
500 while (range.first != upperBound) {
501 for (; range.first != range.second; ++range.first) {
502 FGPositionedRef candidate = range.first->second;
504 if (aFilter->hasTypeRange() && !aFilter->passType(candidate->type())) {
508 if (!aFilter->pass(candidate)) {
516 } else if (aOffset == 0) {
517 // okay, found our result. we need to go around once more to set aNext
520 --aOffset; // seen one more valid result, decrement the count
524 // Unable to match the filter with this range - try the next range.
525 range = aIndex.equal_range(range.second->first);
528 // if we fell out, we reached the end of the valid range. We might have a
529 // valid result, but we definitiely don't have a next result.
534 ///////////////////////////////////////////////////////////////////////////////
536 FGPositioned::FGPositioned(Type ty, const std::string& aIdent, const SGGeod& aPos, bool aIndexed) :
541 SGReferenced::get(this); // hold an owning ref, for the moment
544 assert(ty != TAXIWAY && ty != PAVEMENT);
549 FGPositioned::~FGPositioned()
551 //std::cout << "destroying:" << mIdent << "/" << nameForType(mType) << std::endl;
552 removeFromIndices(this);
556 FGPositioned::createUserWaypoint(const std::string& aIdent, const SGGeod& aPos)
558 return new FGPositioned(WAYPOINT, aIdent, aPos, true);
562 FGPositioned::bucket() const
564 return SGBucket(mPosition);
568 FGPositioned::cart() const
570 return SGVec3d::fromGeod(mPosition);
573 FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
575 if (aName.empty() || (aName == "")) {
584 const NameTypeEntry names[] = {
585 {"airport", AIRPORT},
593 {"waypoint", WAYPOINT},
601 std::string lowerName(boost::to_lower_copy(aName));
603 for (const NameTypeEntry* n = names; (n->_name != NULL); ++n) {
604 if (::strcmp(n->_name, lowerName.c_str()) == 0) {
609 SG_LOG(SG_GENERAL, SG_WARN, "FGPositioned::typeFromName: couldn't match:" << aName);
613 const char* FGPositioned::nameForType(Type aTy)
616 case RUNWAY: return "runway";
617 case TAXIWAY: return "taxiway";
618 case PAVEMENT: return "pavement";
619 case PARK_STAND: return "parking stand";
620 case FIX: return "fix";
621 case VOR: return "VOR";
622 case NDB: return "NDB";
623 case ILS: return "ILS";
624 case LOC: return "localiser";
625 case GS: return "glideslope";
626 case OM: return "outer-marker";
627 case MM: return "middle-marker";
628 case IM: return "inner-marker";
629 case AIRPORT: return "airport";
630 case HELIPORT: return "heliport";
631 case SEAPORT: return "seaport";
632 case WAYPOINT: return "waypoint";
633 case DME: return "dme";
634 case TACAN: return "tacan";
640 ///////////////////////////////////////////////////////////////////////////////
641 // search / query functions
644 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
646 return namedFindClosest(global_identIndex, aIdent, aPos, aFilter);
650 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
653 spatialFind(aPos, aRangeNm, aFilter, result);
654 filterListByRange(aPos, aRangeNm, result);
659 FGPositioned::findAllWithIdentSortedByRange(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
661 return findAllSortedByRange(global_identIndex, aIdent, aPos, aFilter);
665 FGPositioned::findAllWithNameSortedByRange(const std::string& aName, const SGGeod& aPos, Filter* aFilter)
667 return findAllSortedByRange(global_nameIndex, aName, aPos, aFilter);
671 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
673 FGPositioned::List l(spatialGetClosest(aPos, 1, aCutoffNm, aFilter));
678 assert(l.size() == 1);
683 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
685 return spatialGetClosest(aPos, aN, aCutoffNm, aFilter);
689 FGPositioned::findNextWithPartialId(FGPositionedRef aCur, const std::string& aId, Filter* aFilter)
691 // It is essential to bound our search, to avoid iterating all the way to the end of the database.
692 // Do this by generating a second ID with the final character incremented by 1.
693 // e.g., if the partial ID is "KI", we wish to search "KIxxx" but not "KJ".
694 std::string upperBoundId = aId;
695 upperBoundId[upperBoundId.size()-1]++;
696 NamedPositionedIndex::const_iterator upperBound = global_identIndex.lower_bound(upperBoundId);
698 NamedIndexRange range = global_identIndex.equal_range(aId);
699 while (range.first != upperBound) {
700 for (; range.first != range.second; ++range.first) {
701 FGPositionedRef candidate = range.first->second;
702 if (aCur == candidate) {
703 aCur = NULL; // found our start point, next match will pass
708 if (aFilter->hasTypeRange() && !aFilter->passType(candidate->type())) {
712 if (!aFilter->pass(candidate)) {
722 // Unable to match the filter with this range - try the next range.
723 range = global_identIndex.equal_range(range.second->first);
726 return NULL; // Reached the end of the valid sequence with no match.
730 FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset, bool& aNext)
732 return findWithPartial(global_identIndex, aId, aFilter, aOffset, aNext);
737 FGPositioned::findWithPartialName(const std::string& aName, Filter* aFilter, int aOffset, bool& aNext)
739 return findWithPartial(global_nameIndex, aName, aFilter, aOffset, aNext);
743 * Wrapper filter which proxies to an inner filter, but also ensures the
744 * ident starts with supplied partial ident.
746 class PartialIdentFilter : public FGPositioned::Filter
749 PartialIdentFilter(const std::string& ident, FGPositioned::Filter* filter) :
752 _ident = boost::to_upper_copy(ident);
755 virtual bool pass(FGPositioned* aPos) const
757 if (!_inner->pass(aPos)) {
761 return (::strncmp(aPos->ident().c_str(), _ident.c_str(), _ident.size()) == 0);
764 virtual FGPositioned::Type minType() const
765 { return _inner->minType(); }
767 virtual FGPositioned::Type maxType() const
768 { return _inner->maxType(); }
772 FGPositioned::Filter* _inner;
775 static FGPositionedRef
776 findClosestWithPartial(const SGGeod& aPos, FGPositioned::Filter* aFilter, int aOffset, bool& aNext)
778 // why aOffset +2 ? at offset=3, we want the fourth search result, but also
779 // to know if the fifth result exists (to set aNext flag for iterative APIs)
780 FGPositioned::List matches =
781 spatialGetClosest(aPos, aOffset + 2, 1000.0, aFilter);
783 if ((int) matches.size() <= aOffset) {
784 SG_LOG(SG_GENERAL, SG_INFO, "findClosestWithPartial, couldn't match enough with prefix");
786 return NULL; // couldn't find a match within the cutoff distance
789 aNext = ((int) matches.size() >= (aOffset + 2));
790 return matches[aOffset];
795 FGPositioned::findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset, bool& aNext)
797 PartialIdentFilter pf(aId, aFilter);
798 return findClosestWithPartial(aPos, &pf, aOffset, aNext);
802 * Wrapper filter which proxies to an inner filter, but also ensures the
803 * name starts with supplied partial name.
805 class PartialNameFilter : public FGPositioned::Filter
808 PartialNameFilter(const std::string& nm, FGPositioned::Filter* filter) :
814 virtual bool pass(FGPositioned* aPos) const
816 if (!_inner->pass(aPos)) {
820 return (::strncasecmp(aPos->name().c_str(), _name.c_str(), _name.size()) == 0);
823 virtual FGPositioned::Type minType() const
824 { return _inner->minType(); }
826 virtual FGPositioned::Type maxType() const
827 { return _inner->maxType(); }
831 FGPositioned::Filter* _inner;
835 FGPositioned::findClosestWithPartialName(const SGGeod& aPos, const std::string& aName, Filter* aFilter, int aOffset, bool& aNext)
837 PartialNameFilter pf(aName, aFilter);
838 return findClosestWithPartial(aPos, &pf, aOffset, aNext);