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.
29 #include <simgear/math/sg_geodesy.hxx>
31 #include "positioned.hxx"
33 typedef std::multimap<std::string, FGPositioned*> NamedPositionedIndex;
34 typedef std::pair<NamedPositionedIndex::const_iterator, NamedPositionedIndex::const_iterator> NamedIndexRange;
37 * Order positioned elements by type, then pointer address. This allows us to
38 * use range searches (lower_ and upper_bound) to grab items of a particular
39 * type out of bucket efficently.
44 bool operator()(const FGPositioned* a, const FGPositioned* b) const
46 if (a->type() == b->type()) return a < b;
47 return a->type() < b->type();
51 typedef std::set<FGPositioned*, OrderByType> BucketEntry;
52 typedef std::map<long int, BucketEntry> SpatialPositionedIndex;
54 static NamedPositionedIndex global_namedIndex;
55 static SpatialPositionedIndex global_spatialIndex;
57 SpatialPositionedIndex::iterator
58 bucketEntryForPositioned(FGPositioned* aPos)
60 int bucketIndex = aPos->bucket().gen_index();
61 SpatialPositionedIndex::iterator it = global_spatialIndex.find(bucketIndex);
62 if (it != global_spatialIndex.end()) {
66 // create a new BucketEntry
67 return global_spatialIndex.insert(it, std::make_pair(bucketIndex, BucketEntry()));
71 addToIndices(FGPositioned* aPos)
74 global_namedIndex.insert(global_namedIndex.begin(),
75 std::make_pair(aPos->ident(), aPos));
77 SpatialPositionedIndex::iterator it = bucketEntryForPositioned(aPos);
78 it->second.insert(aPos);
82 removeFromIndices(FGPositioned* aPos)
86 NamedPositionedIndex::iterator it = global_namedIndex.find(aPos->ident());
87 while (it != global_namedIndex.end() && (it->first == aPos->ident())) {
88 if (it->second == aPos) {
89 global_namedIndex.erase(it);
96 SpatialPositionedIndex::iterator sit = bucketEntryForPositioned(aPos);
97 sit->second.erase(aPos);
101 spatialFilterInBucket(const SGBucket& aBucket, FGPositioned::Filter* aFilter, FGPositioned::List& aResult)
103 SpatialPositionedIndex::const_iterator it;
104 it = global_spatialIndex.find(aBucket.gen_index());
105 if (it == global_spatialIndex.end()) {
109 BucketEntry::const_iterator l = it->second.begin();
110 BucketEntry::const_iterator u = it->second.end();
112 if (!aFilter) { // pass everything
113 aResult.insert(aResult.end(), l, u);
117 for ( ; l != u; ++l) {
118 if ((*aFilter)(*l)) {
119 aResult.push_back(*l);
125 spatialFind(const SGGeod& aPos, double aRange,
126 FGPositioned::Filter* aFilter, FGPositioned::List& aResult)
129 double lat = aPos.getLatitudeDeg(),
130 lon = aPos.getLongitudeDeg();
132 int bx = (int)( aRange*SG_NM_TO_METER / buck.get_width_m() / 2);
133 int by = (int)( aRange*SG_NM_TO_METER / buck.get_height_m() / 2 );
135 // loop over bucket range
136 for ( int i=-bx; i<=bx; i++) {
137 for ( int j=-by; j<=by; j++) {
138 spatialFilterInBucket(sgBucketOffset(lon, lat, i, j), aFilter, aResult);
144 class LowerLimitOfType
147 bool operator()(const FGPositioned* a, const FGPositioned::Type b) const
149 return a->type() < b;
152 bool operator()(const FGPositioned::Type a, const FGPositioned* b) const
154 return a < b->type();
160 spatialFindTyped(const SGGeod& aPos, double aRange, FGPositioned::Type aLower, FGPositioned::Type aUpper, FGPositioned::List& aResult)
163 double lat = aPos.getLatitudeDeg(),
164 lon = aPos.getLongitudeDeg();
166 int bx = (int)( aRange*SG_NM_TO_METER / buck.get_width_m() / 2);
167 int by = (int)( aRange*SG_NM_TO_METER / buck.get_height_m() / 2 );
169 // loop over bucket range
170 for ( int i=-bx; i<=bx; i++) {
171 for ( int j=-by; j<=by; j++) {
172 buck = sgBucketOffset(lon, lat, i, j);
174 SpatialPositionedIndex::const_iterator it;
175 it = global_spatialIndex.find(buck.gen_index());
176 if (it == global_spatialIndex.end()) {
180 BucketEntry::const_iterator l = std::lower_bound(it->second.begin(), it->second.end(), aLower, LowerLimitOfType());
181 BucketEntry::const_iterator u = std::upper_bound(l, it->second.end(), aUpper, LowerLimitOfType());
183 for ( ; l != u; ++l) {
184 aResult.push_back(*l);
194 class RangePredictate
197 RangePredictate(const SGGeod& aOrigin, double aRange) :
202 bool operator()(const FGPositionedRef& aPos)
205 SGGeodesy::inverse(aPos->geod(), mOrigin, az1, az2, d);
215 filterListByRange(const SGGeod& aPos, double aRange, FGPositioned::List& aResult)
217 RangePredictate pred(aPos, aRange * SG_NM_TO_METER);
218 FGPositioned::List::iterator newEnd;
219 newEnd = std::remove_if(aResult.begin(), aResult.end(), pred);
220 aResult.erase(newEnd, aResult.end());
223 class DistanceOrdering
226 DistanceOrdering(const SGGeod& aPos) :
230 bool operator()(const FGPositionedRef& a, const FGPositionedRef& b) const
232 double dA, dB, az1, az2;
233 SGGeodesy::inverse(mPos, a->geod(), az1, az2, dA);
234 SGGeodesy::inverse(mPos, b->geod(), az1, az2, dB);
243 sortByDistance(const SGGeod& aPos, FGPositioned::List& aResult)
245 std::sort(aResult.begin(), aResult.end(), DistanceOrdering(aPos));
248 static FGPositionedRef
249 namedFindClosest(const std::string& aIdent, const SGGeod& aOrigin, FGPositioned::Filter* aFilter)
251 NamedIndexRange range = global_namedIndex.equal_range(aIdent);
252 if (range.first == range.second) {
256 // common case, only one result. looks a bit ugly because these are
257 // sequential iterators, not random-access ones
258 NamedPositionedIndex::const_iterator check = range.first;
259 if (++check == range.second) {
260 // excellent, only one match in the range - all we care about is the type
261 if (aFilter && !aFilter->pass(range.first->second)) {
262 return NULL; // type check failed
265 return range.first->second;
266 } // of short-circuit logic for single-element range
268 // multiple matches, we need to actually check the distance to each one
269 double minDist = HUGE_VAL;
270 FGPositionedRef result;
271 NamedPositionedIndex::const_iterator it = range.first;
273 for (; it != range.second; ++it) {
275 FGPositioned::Type ty = it->second->type();
276 if (aFilter && !aFilter->pass(range.first->second)) {
282 SGGeodesy::inverse(aOrigin, it->second->geod(), az2, az2, d);
292 static FGPositioned::List
293 spatialGetClosest(const SGGeod& aPos, unsigned int aN, double aCutoffNm, FGPositioned::Filter* aFilter)
295 FGPositioned::List result;
296 int radius = 1; // start at 1, radius 0 is handled explicitly
298 double lat = aPos.getLatitudeDeg(),
299 lon = aPos.getLongitudeDeg();
300 // final cutoff is in metres, and scaled to account for testing the corners
301 // of the 'box' instead of the centre of each edge
302 double cutoffM = aCutoffNm * SG_NM_TO_METER * 1.5;
304 // base case, simplifes loop to do it seperately here
305 spatialFilterInBucket(sgBucketOffset(lon, lat, 0, 0), aFilter, result);
307 for (;result.size() < aN; ++radius) {
309 double az1, az2, d1, d2;
310 SGGeodesy::inverse(aPos, sgBucketOffset(lon, lat, -radius, -radius).get_center(), az1, az2, d1);
311 SGGeodesy::inverse(aPos, sgBucketOffset(lon, lat, radius, radius).get_center(), az1, az2, d2);
313 if ((d1 > cutoffM) && (d2 > cutoffM)) {
314 //std::cerr << "spatialGetClosest terminating due to range cutoff" << std::endl;
318 FGPositioned::List hits;
319 for ( int i=-radius; i<=radius; i++) {
320 spatialFilterInBucket(sgBucketOffset(lon, lat, i, -radius), aFilter, hits);
321 spatialFilterInBucket(sgBucketOffset(lon, lat, -radius, i), aFilter, hits);
322 spatialFilterInBucket(sgBucketOffset(lon, lat, i, radius), aFilter, hits);
323 spatialFilterInBucket(sgBucketOffset(lon, lat, radius, i), aFilter, hits);
326 result.insert(result.end(), hits.begin(), hits.end()); // append
329 if (result.size() > aN) {
330 result.resize(aN); // truncate at requested number of matches
333 sortByDistance(aPos, result);
337 ///////////////////////////////////////////////////////////////////////////////
339 FGPositioned::FGPositioned(Type ty, const std::string& aIdent, double aLat, double aLon, double aElev) :
341 mPosition(SGGeod::fromDegFt(aLon, aLat, aElev)),
345 SGReferenced::get(this); // hold an owning ref, for the moment
348 FGPositioned::FGPositioned(Type ty, const std::string& aIdent, const SGGeod& aPos) :
354 SGReferenced::get(this); // hold an owning ref, for the moment
357 FGPositioned::~FGPositioned()
359 removeFromIndices(this);
363 FGPositioned::bucket() const
365 return SGBucket(mPosition);
368 const char* FGPositioned::nameForType(Type aTy)
371 case FIX: return "fix";
372 case VOR: return "VOR";
373 case NDB: return "NDB";
374 case OM: return "outer-marker";
375 case MM: return "middle-marker";
376 case IM: return "inner-marker";
377 case AIRPORT: return "airport";
378 case HELIPORT: return "heliport";
379 case SEAPORT: return "seaport";
380 case WAYPOINT: return "waypoint";
386 ///////////////////////////////////////////////////////////////////////////////
387 // search / query functions
390 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
392 return namedFindClosest(aIdent, aPos, aFilter);
396 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
399 spatialFind(aPos, aRangeNm, aFilter, result);
400 filterListByRange(aPos, aRangeNm, result);
405 FGPositioned::findAllWithIdentSortedByRange(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
408 NamedIndexRange range = global_namedIndex.equal_range(aIdent);
409 for (; range.first != range.second; ++range.first) {
410 if (aFilter && !aFilter->pass(range.first->second)) {
414 result.push_back(range.first->second);
417 sortByDistance(aPos, result);
422 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
424 FGPositioned::List l(spatialGetClosest(aPos, 1, aCutoffNm, aFilter));
429 assert(l.size() == 1);
434 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
436 return spatialGetClosest(aPos, aN, aCutoffNm, aFilter);
440 FGPositioned::findNextWithPartialId(FGPositionedRef aCur, const std::string& aId, Filter* aFilter)
442 NamedIndexRange range = global_namedIndex.equal_range(aId);
443 for (; range.first != range.second; ++range.first) {
444 FGPositionedRef candidate = range.first->second;
445 if (aCur == candidate) {
446 aCur = NULL; // found our start point, next match will pass
450 if (aFilter && !aFilter->pass(candidate)) {
459 return NULL; // fell out, no match in range