]> git.mxchange.org Git - flightgear.git/blobdiff - src/Navaids/positioned.cxx
Merge branch 'topics/bug150' into next
[flightgear.git] / src / Navaids / positioned.cxx
index bbd0443063330acacd69b9d72c22a3cc7088f3cb..591f52e45f0bf48344e4ef698df67a1217e50ad5 100644 (file)
@@ -22,6 +22,8 @@
 #  include "config.h"
 #endif
 
+#include "positioned.hxx"
+
 #include <map>
 #include <set>
 #include <algorithm> // for sort
 #include <boost/algorithm/string/case_conv.hpp>
 #include <boost/algorithm/string/predicate.hpp>
 
-#include <simgear/math/sg_geodesy.hxx>
 #include <simgear/timing/timestamp.hxx>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/structure/exception.hxx>
-#include <simgear/math/SGBox.hxx>
+#include <simgear/math/SGGeometry.hxx>
+
 
-#include "positioned.hxx"
 
 typedef std::multimap<std::string, FGPositioned*> NamedPositionedIndex;
 typedef std::pair<NamedPositionedIndex::const_iterator, NamedPositionedIndex::const_iterator> NamedIndexRange;
@@ -55,27 +56,6 @@ namespace Octree
 const double LEAF_SIZE = SG_NM_TO_METER * 8.0;
 const double LEAF_SIZE_SQR = LEAF_SIZE * LEAF_SIZE;
 
-typedef SGBox<double> SGBoxd;
-
-template<typename T1, typename T2>
-inline bool
-intersects(const SGVec3<T1>& v, const SGBox<T2>& box)
-{
-  if (v[0] < box.getMin()[0])
-    return false;
-  if (box.getMax()[0] < v[0])
-    return false;
-  if (v[1] < box.getMin()[1])
-    return false;
-  if (box.getMax()[1] < v[1])
-    return false;
-  if (v[2] < box.getMin()[2])
-    return false;
-  if (box.getMax()[2] < v[2])
-    return false;
-  return true;
-}
-
 /**
  * Decorate an object with a double value, and use that value to order 
  * items, for the purpoises of the STL algorithms
@@ -156,28 +136,11 @@ public:
 
     double distSqrToNearest(const SGVec3d& aPos) const
     {
-        return distSqr(aPos, getClosestPoint(aPos));
+        return distSqr(aPos, _box.getClosestPoint(aPos));
     }
     
     virtual void insert(FGPositioned* aP) = 0;
     
-    SGVec3d getClosestPoint(const SGVec3d& aPos) const
-    {
-      SGVec3d r;
-      
-      for (unsigned int i=0;i<3; ++i) {
-        if (aPos[i] < _box.getMin()[i]) {
-          r[i] = _box.getMin()[i];
-        } else if (aPos[i] > _box.getMax()[i]) {
-          r[i] = _box.getMax()[i];
-        } else {
-          r[i] = aPos[i];
-        }
-      } // of axis iteration
-      
-      return r;
-    }
-    
     virtual void visit(const SGVec3d& aPos, double aCutoff, 
       FGPositioned::Filter* aFilter, 
       FindNearestResults& aResults, FindNearestPQueue&) = 0;
@@ -441,91 +404,6 @@ removeFromIndices(FGPositioned* aPos)
   }
 }
 
-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;
-}
-
 //////////////////////////////////////////////////////////////////////////////
 
 class OrderByName
@@ -589,13 +467,14 @@ char** searchAirportNamesAndIdents(const std::string& aFilter)
   // We format results as follows (note whitespace!):
   //   ' name-of-airport-chars   (ident)'
   // so the total length is:
-  //    1 + strlen(name) + 4 + 4 (for the ident) + 1 + 1 (for the null)
-  // which gives a grand total of 11 + the length of the name.
-  // note the ident is sometimes only three letters for non-ICAO small strips
+  //    1 + strlen(name) + 4 + strlen(icao) + 1 + 1 (for the null)
+  // which gives a grand total of 7 + name-length + icao-length.
+  // note the ident can be three letters (non-ICAO local strip), four
+  // (default ICAO) or more (extended format ICAO)
   for (unsigned int i=0; i<numMatches; ++i) {
     int nameLength = matches[i]->name().size();
     int icaoLength = matches[i]->ident().size();
-    char* entry = new char[nameLength + 11];
+    char* entry = new char[7 + nameLength + icaoLength];
     char* dst = entry;
     *dst++ = ' ';
     memcpy(dst, matches[i]->name().c_str(), nameLength);
@@ -630,88 +509,54 @@ FGPositioned::Filter::passType(Type aTy) const
   return (minType() <= aTy) && (maxType() >= aTy);
 }
 
-static FGPositioned::List
-findAllSortedByRange(const NamedPositionedIndex& aIndex, 
-                             const std::string& aName, const SGGeod& aPos, FGPositioned::Filter* aFilter)
+static FGPositioned::List 
+findAll(const NamedPositionedIndex& aIndex, 
+                             const std::string& aName, FGPositioned::Filter* aFilter)
 {
   FGPositioned::List result;
-  NamedIndexRange range = aIndex.equal_range(aName);
-  for (; range.first != range.second; ++range.first) {
-    FGPositioned* candidate = range.first->second;
-    if (aFilter) {
-      if (aFilter->hasTypeRange() && !aFilter->passType(candidate->type())) {
-        continue;
-      }
-      
-      if (!aFilter->pass(candidate)) {
-        continue;
-      }
-    }
-    
-    result.push_back(range.first->second);
+  if (aName.empty()) {
+    return result;
   }
   
-  sortByDistance(aPos, result);
-  return result;
-}
-
-static FGPositionedRef
-findWithPartial(const NamedPositionedIndex& aIndex, const std::string& aName, 
-                    FGPositioned::Filter* aFilter, int aOffset, bool& aNext)
-{
-  // see comment in findNextWithPartialId concerning upperBoundId
   std::string upperBoundId = aName;
   upperBoundId[upperBoundId.size()-1]++;
   NamedPositionedIndex::const_iterator upperBound = aIndex.lower_bound(upperBoundId);
+  NamedPositionedIndex::const_iterator it = aIndex.lower_bound(aName);
   
-  NamedIndexRange range = aIndex.equal_range(aName);
-  FGPositionedRef result;
-  
-  while (range.first != upperBound) {
-    for (; range.first != range.second; ++range.first) {
-      FGPositionedRef candidate = range.first->second;
-      if (aFilter) {
-        if (aFilter->hasTypeRange() && !aFilter->passType(candidate->type())) {
-          continue;
-        }
-        
-        if (!aFilter->pass(candidate)) {
-          continue;
-        }
+  for (; it != upperBound; ++it) {
+    FGPositionedRef candidate = it->second;
+    if (aFilter) {
+      if (aFilter->hasTypeRange() && !aFilter->passType(candidate->type())) {
+        continue;
       }
       
-      if (result) {
-        aNext = true;
-        return result;
-      } else if (aOffset == 0) {
-        // okay, found our result. we need to go around once more to set aNext
-        result = candidate;
-      } else {
-        --aOffset; // seen one more valid result, decrement the count
+      if (!aFilter->pass(candidate)) {
+        continue;
       }
     }
     
-    // Unable to match the filter with this range - try the next range.
-    range = aIndex.equal_range(range.second->first);
+    result.push_back(candidate);
   }
   
-  // if we fell out, we reached the end of the valid range. We might have a
-  // valid result, but we definitiely don't have a next result.
-  aNext = false;
-  return result;  
+  return result;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
-FGPositioned::FGPositioned(Type ty, const std::string& aIdent, const SGGeod& aPos, bool aIndexed) :
-  mType(ty),
+FGPositioned::FGPositioned(Type ty, const std::string& aIdent, const SGGeod& aPos) :
   mPosition(aPos),
+  mType(ty),
   mIdent(aIdent)
 {  
+}
+
+void FGPositioned::init(bool aIndexed)
+{
   SGReferenced::get(this); // hold an owning ref, for the moment
+  mCart = SGVec3d::fromGeod(mPosition);
   
   if (aIndexed) {
-    assert(ty != TAXIWAY && ty != PAVEMENT);
+    assert(mType != TAXIWAY && mType != PAVEMENT);
     addToIndices(this);
   }
 }
@@ -725,19 +570,15 @@ FGPositioned::~FGPositioned()
 FGPositioned*
 FGPositioned::createUserWaypoint(const std::string& aIdent, const SGGeod& aPos)
 {
-  return new FGPositioned(WAYPOINT, aIdent, aPos, true);
+  FGPositioned* wpt = new FGPositioned(WAYPOINT, aIdent, aPos);
+  wpt->init(true);
+  return wpt;
 }
 
-SGBucket
-FGPositioned::bucket() const
-{
-  return SGBucket(mPosition);
-}
-
-SGVec3d
+const SGVec3d&
 FGPositioned::cart() const
 {
-  return SGVec3d::fromGeod(mPosition);
+  return mCart;
 }
 
 FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
@@ -814,7 +655,13 @@ const char* FGPositioned::nameForType(Type aTy)
 FGPositionedRef
 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
 {
-  return namedFindClosest(global_identIndex, aIdent, aPos, aFilter);
+  FGPositioned::List r(findAll(global_identIndex, aIdent, aFilter));
+  if (r.empty()) {
+    return FGPositionedRef();
+  }
+  
+  sortByRange(r, aPos);
+  return r.front();
 }
 
 FGPositioned::List
@@ -827,15 +674,15 @@ FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilt
 }
 
 FGPositioned::List
-FGPositioned::findAllWithIdentSortedByRange(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
+FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter)
 {
-  return findAllSortedByRange(global_identIndex, aIdent, aPos, aFilter);
+  return findAll(global_identIndex, aIdent, aFilter);
 }
 
 FGPositioned::List
-FGPositioned::findAllWithNameSortedByRange(const std::string& aName, const SGGeod& aPos, Filter* aFilter)
+FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter)
 {
-  return findAllSortedByRange(global_nameIndex, aName, aPos, aFilter);
+  return findAll(global_nameIndex, aName, aFilter);
 }
 
 FGPositionedRef
@@ -905,115 +752,24 @@ FGPositioned::findNextWithPartialId(FGPositionedRef aCur, const std::string& aId
   return NULL; // Reached the end of the valid sequence with no match.  
 }
   
-FGPositionedRef
-FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset, bool& aNext)
-{
-  return findWithPartial(global_identIndex, aId, aFilter, aOffset, aNext);
-}
-
-
-FGPositionedRef
-FGPositioned::findWithPartialName(const std::string& aName, Filter* aFilter, int aOffset, bool& aNext)
-{
-  return findWithPartial(global_nameIndex, aName, aFilter, aOffset, aNext);
-}
-
-/**
- * Wrapper filter which proxies to an inner filter, but also ensures the
- * ident starts with supplied partial ident.
- */
-class PartialIdentFilter : public FGPositioned::Filter
-{
-public:
-  PartialIdentFilter(const std::string& ident, FGPositioned::Filter* filter) :
-    _inner(filter)
-  {
-    _ident = boost::to_upper_copy(ident);
-  }
-  
-  virtual bool pass(FGPositioned* aPos) const
-  {
-    if (!_inner->pass(aPos)) {
-      return false;
-    }
-    
-    return (boost::algorithm::starts_with(aPos->ident(), _ident));
-  }
-    
-  virtual FGPositioned::Type minType() const
-  { return _inner->minType(); }
-    
-  virtual FGPositioned::Type maxType() const
-  { return _inner->maxType(); }
-    
-private:
-  std::string _ident;
-  FGPositioned::Filter* _inner;
-};
-
-static FGPositionedRef
-findClosestWithPartial(const SGGeod& aPos, FGPositioned::Filter* aFilter, int aOffset, bool& aNext)
+void
+FGPositioned::sortByRange(List& aResult, const SGGeod& aPos)
 {
-  // why aOffset +2 ? at offset=3, we want the fourth search result, but also
-  // to know if the fifth result exists (to set aNext flag for iterative APIs)
-  FGPositioned::List matches;
-  Octree::findNearestN(SGVec3d::fromGeod(aPos), aOffset + 2, 1000 * SG_NM_TO_METER, aFilter, matches);
-  
-  if ((int) matches.size() <= aOffset) {
-    SG_LOG(SG_GENERAL, SG_INFO, "findClosestWithPartial, couldn't match enough with prefix");
-    aNext = false;
-    return NULL; // couldn't find a match within the cutoff distance
+  SGVec3d cartPos(SGVec3d::fromGeod(aPos));
+// computer ordering values
+  Octree::FindNearestResults r;
+  List::iterator it = aResult.begin(), lend = aResult.end();
+  for (; it != lend; ++it) {
+    double d2 = distSqr((*it)->cart(), cartPos);
+    r.push_back(Octree::OrderedPositioned(*it, d2));
   }
   
-  aNext = ((int) matches.size() >= (aOffset + 2));
-  return matches[aOffset];
-
-}
-
-FGPositionedRef
-FGPositioned::findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset, bool& aNext)
-{
-  PartialIdentFilter pf(aId, aFilter);
-  return findClosestWithPartial(aPos, &pf, aOffset, aNext);
-}
-
-/**
- * Wrapper filter which proxies to an inner filter, but also ensures the
- * name starts with supplied partial name.
- */
-class PartialNameFilter : public FGPositioned::Filter
-{
-public:
-  PartialNameFilter(const std::string& nm, FGPositioned::Filter* filter) :
-  _inner(filter)
-  {
-    _name = nm;
-  }
+// sort
+  std::sort(r.begin(), r.end());
   
-  virtual bool pass(FGPositioned* aPos) const
-  {
-    if (!_inner->pass(aPos)) {
-      return false;
-    }
-    
-    return (boost::algorithm::istarts_with(aPos->name(), _name));
+// convert to a plain list
+  unsigned int count = aResult.size();
+  for (unsigned int i=0; i<count; ++i) {
+    aResult[i] = r[i].get();
   }
-  
-  virtual FGPositioned::Type minType() const
-  { return _inner->minType(); }
-  
-  virtual FGPositioned::Type maxType() const
-  { return _inner->maxType(); }
-  
-private:
-  std::string _name;
-  FGPositioned::Filter* _inner;
-};
-
-FGPositionedRef
-FGPositioned::findClosestWithPartialName(const SGGeod& aPos, const std::string& aName, Filter* aFilter, int aOffset, bool& aNext)
-{
-  PartialNameFilter pf(aName, aFilter);
-  return findClosestWithPartial(aPos, &pf, aOffset, aNext);
 }
-