- return a->type() < b->type();
- }
-};
-
-
-typedef std::set<FGPositioned*, OrderByType> BucketEntry;
-typedef std::map<long int, BucketEntry> SpatialPositionedIndex;
-
-static NamedPositionedIndex global_identIndex;
-static NamedPositionedIndex global_nameIndex;
-static SpatialPositionedIndex global_spatialIndex;
-
-SpatialPositionedIndex::iterator
-bucketEntryForPositioned(FGPositioned* aPos)
-{
- int bucketIndex = aPos->bucket().gen_index();
- SpatialPositionedIndex::iterator it = global_spatialIndex.find(bucketIndex);
- if (it != global_spatialIndex.end()) {
- return it;
- }
-
- // create a new BucketEntry
- return global_spatialIndex.insert(it, std::make_pair(bucketIndex, BucketEntry()));
-}
-
-static void
-addToIndices(FGPositioned* aPos)
-{
- assert(aPos);
- if (!aPos->ident().empty()) {
- global_identIndex.insert(global_identIndex.begin(),
- std::make_pair(aPos->ident(), aPos));
- }
-
- if (!aPos->name().empty()) {
- global_nameIndex.insert(global_nameIndex.begin(),
- std::make_pair(aPos->name(), aPos));
- }
-
-
- SpatialPositionedIndex::iterator it = bucketEntryForPositioned(aPos);
- it->second.insert(aPos);
-}
-
-static void
-removeFromIndices(FGPositioned* aPos)
-{
- assert(aPos);
-
- if (!aPos->ident().empty()) {
- NamedPositionedIndex::iterator it = global_identIndex.find(aPos->ident());
- while (it != global_identIndex.end() && (it->first == aPos->ident())) {
- if (it->second == aPos) {
- global_identIndex.erase(it);
- break;
- }
-
- ++it;
- } // of multimap walk
- }
-
- if (!aPos->name().empty()) {
- NamedPositionedIndex::iterator it = global_nameIndex.find(aPos->name());
- while (it != global_nameIndex.end() && (it->first == aPos->name())) {
- if (it->second == aPos) {
- global_nameIndex.erase(it);
- break;
- }
-
- ++it;
- } // of multimap walk
- }
-
- SpatialPositionedIndex::iterator sit = bucketEntryForPositioned(aPos);
- sit->second.erase(aPos);
-}
-
-static void
-spatialFilterInBucket(const SGBucket& aBucket, FGPositioned::Filter* aFilter, FGPositioned::List& aResult)
-{
- SpatialPositionedIndex::const_iterator it;
- it = global_spatialIndex.find(aBucket.gen_index());
- if (it == global_spatialIndex.end()) {
- return;
- }
-
- BucketEntry::const_iterator l = it->second.begin();
- BucketEntry::const_iterator u = it->second.end();
-
- if (!aFilter) { // pass everything
- aResult.insert(aResult.end(), l, u);
- return;
- }
-
- if (aFilter->hasTypeRange()) {
- // avoid many calls to the filter hook
- l = lower_bound(it->second.begin(), it->second.end(), aFilter->minType(), LowerLimitOfType());
- u = upper_bound(l, it->second.end(), aFilter->maxType(), LowerLimitOfType());
- }
-
- for ( ; l != u; ++l) {
- if ((*aFilter)(*l)) {
- aResult.push_back(*l);
- }
- }
-}
-
-static void
-spatialFind(const SGGeod& aPos, double aRange,
- FGPositioned::Filter* aFilter, FGPositioned::List& aResult)
-{
- SGBucket buck(aPos);
- double lat = aPos.getLatitudeDeg(),
- lon = aPos.getLongitudeDeg();
-
- int bx = (int)( aRange*SG_NM_TO_METER / buck.get_width_m() / 2);
- int by = (int)( aRange*SG_NM_TO_METER / buck.get_height_m() / 2 );
-
- // loop over bucket range
- for ( int i=-bx; i<=bx; i++) {
- for ( int j=-by; j<=by; j++) {
- spatialFilterInBucket(sgBucketOffset(lon, lat, i, j), aFilter, aResult);
- } // of j-iteration
- } // of i-iteration
-}
-
-/**
- */
-class RangePredictate
-{
-public:
- RangePredictate(const SGGeod& aOrigin, double aRange) :
- mOrigin(SGVec3d::fromGeod(aOrigin)),
- mRangeSqr(aRange * aRange)
- { ; }
-
- bool operator()(const FGPositionedRef& aPos)
- {
- double dSqr = distSqr(aPos->cart(), mOrigin);
- return (dSqr > mRangeSqr);
- }
-
-private:
- SGVec3d mOrigin;
- double mRangeSqr;
-};
-
-static void
-filterListByRange(const SGGeod& aPos, double aRange, FGPositioned::List& aResult)
-{
- RangePredictate pred(aPos, aRange * SG_NM_TO_METER);
- FGPositioned::List::iterator newEnd;
- newEnd = std::remove_if(aResult.begin(), aResult.end(), pred);
- aResult.erase(newEnd, aResult.end());
-}
-
-class DistanceOrdering
-{
-public:
- DistanceOrdering(const SGGeod& aPos) :
- mPos(SGVec3d::fromGeod(aPos))
- { }
-
- bool operator()(const FGPositionedRef& a, const FGPositionedRef& b) const
- {
- if (!a || !b) {
- throw sg_exception("empty reference passed to DistanceOrdering");
- }
-
- double dA = distSqr(a->cart(), mPos),
- dB = distSqr(b->cart(), mPos);
- return dA < dB;
- }
-
-private:
- SGVec3d mPos;
-};
-
-static void
-sortByDistance(const SGGeod& aPos, FGPositioned::List& aResult)
-{
- std::sort(aResult.begin(), aResult.end(), DistanceOrdering(aPos));
-}
-
-static FGPositionedRef
-namedFindClosest(const NamedPositionedIndex& aIndex, const std::string& aName,
- const SGGeod& aOrigin, FGPositioned::Filter* aFilter)
-{
- NamedIndexRange range = aIndex.equal_range(aName);
- if (range.first == range.second) {
- return NULL;
- }
-
-// common case, only one result. looks a bit ugly because these are
-// sequential iterators, not random-access ones
- NamedPositionedIndex::const_iterator check = range.first;
- if (++check == range.second) {
- // excellent, only one match in the range
- FGPositioned* r = range.first->second;
- if (aFilter) {
- if (aFilter->hasTypeRange() && !aFilter->passType(r->type())) {
- return NULL;
- }
-
- if (!aFilter->pass(r)) {
- return NULL;
- }
- } // of have a filter
-
- return r;
- } // of short-circuit logic for single-element range
-
-// multiple matches, we need to actually check the distance to each one
- double minDist = HUGE_VAL;
- FGPositionedRef result;
- NamedPositionedIndex::const_iterator it = range.first;
- SGVec3d cartOrigin(SGVec3d::fromGeod(aOrigin));
-
- for (; it != range.second; ++it) {
- FGPositioned* r = it->second;
- if (aFilter) {
- if (aFilter->hasTypeRange() && !aFilter->passType(r->type())) {
- continue;
- }
-
- if (!aFilter->pass(r)) {
- continue;
- }
- }
-
- // find distance
- double d2 = distSqr(cartOrigin, r->cart());
- if (d2 < minDist) {
- minDist = d2;
- result = r;
- }
- }
-
- return result;
-}
-
-static FGPositioned::List
-spatialGetClosest(const SGGeod& aPos, unsigned int aN, double aCutoffNm, FGPositioned::Filter* aFilter)
-{
- FGPositioned::List result;
- int radius = 1; // start at 1, radius 0 is handled explicitly
- SGBucket buck;
- double lat = aPos.getLatitudeDeg(),
- lon = aPos.getLongitudeDeg();
- // final cutoff is in metres, and scaled to account for testing the corners
- // of the 'box' instead of the centre of each edge
- double cutoffM = aCutoffNm * SG_NM_TO_METER * 1.5;
-
- // base case, simplifes loop to do it seperately here
- spatialFilterInBucket(sgBucketOffset(lon, lat, 0, 0), aFilter, result);
-
- for (;result.size() < aN; ++radius) {
- // cutoff check
- double az1, az2, d1, d2;
- SGGeodesy::inverse(aPos, sgBucketOffset(lon, lat, -radius, -radius).get_center(), az1, az2, d1);
- SGGeodesy::inverse(aPos, sgBucketOffset(lon, lat, radius, radius).get_center(), az1, az2, d2);
-
- if ((d1 > cutoffM) && (d2 > cutoffM)) {
- //std::cerr << "spatialGetClosest terminating due to range cutoff" << std::endl;
- break;
- }
-
- FGPositioned::List hits;
- for ( int i=-radius; i<=radius; i++) {
- spatialFilterInBucket(sgBucketOffset(lon, lat, i, -radius), aFilter, hits);
- spatialFilterInBucket(sgBucketOffset(lon, lat, -radius, i), aFilter, hits);
- spatialFilterInBucket(sgBucketOffset(lon, lat, i, radius), aFilter, hits);
- spatialFilterInBucket(sgBucketOffset(lon, lat, radius, i), aFilter, hits);
- }
-
- result.insert(result.end(), hits.begin(), hits.end()); // append
- } // of outer loop
-
- sortByDistance(aPos, result);
- if (result.size() > aN) {
- result.resize(aN); // truncate at requested number of matches
- }
-
- return result;
-}
-
-//////////////////////////////////////////////////////////////////////////////
-
-class OrderByName
-{
-public:
- bool operator()(FGPositioned* a, FGPositioned* b) const
- {
- return a->name() < b->name();
- }
-};
-
-/**
- * A special purpose helper (imported by FGAirport::searchNamesAndIdents) to
- * implement the AirportList dialog. It's unfortunate that it needs to reside
- * here, but for now it's least ugly solution.
- */
-char** searchAirportNamesAndIdents(const std::string& aFilter)
-{
- const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(std::locale());
- std::string filter(aFilter);
- bool hasFilter = !filter.empty();
- if (hasFilter) {
- ct.toupper((char *)filter.data(), (char *)filter.data() + filter.size());