]> git.mxchange.org Git - flightgear.git/blobdiff - src/Navaids/positioned.cxx
Prepare and implement reinit methods for instruments
[flightgear.git] / src / Navaids / positioned.cxx
index 591f52e45f0bf48344e4ef698df67a1217e50ad5..a3f93bf757bf95668e3cf39e6c2266cbf2374cd9 100644 (file)
 #include <set>
 #include <algorithm> // for sort
 #include <queue>
+#include <memory>
 
 #include <boost/algorithm/string/case_conv.hpp>
 #include <boost/algorithm/string/predicate.hpp>
 
+#include <osg/Math> // for osg::isNaN
+
 #include <simgear/timing/timestamp.hxx>
+#include <simgear/props/props.hxx>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/structure/exception.hxx>
 #include <simgear/math/SGGeometry.hxx>
+#include <simgear/sg_inlines.h>
+#include <simgear/structure/commands.hxx>
 
-
+#include "Airports/simple.hxx"
+#include "Main/fg_props.hxx"
 
 typedef std::multimap<std::string, FGPositioned*> NamedPositionedIndex;
 typedef std::pair<NamedPositionedIndex::const_iterator, NamedPositionedIndex::const_iterator> NamedIndexRange;
@@ -357,13 +364,17 @@ addToIndices(FGPositioned* aPos)
 {
   assert(aPos);
   if (!aPos->ident().empty()) {
+    std::string u(boost::to_upper_copy(aPos->ident()));
+    
     global_identIndex.insert(global_identIndex.begin(), 
-      std::make_pair(aPos->ident(), aPos));
+      std::make_pair(u, aPos));
   }
   
   if (!aPos->name().empty()) {
+    std::string u(boost::to_upper_copy(aPos->name()));
+    
     global_nameIndex.insert(global_nameIndex.begin(), 
-                             std::make_pair(aPos->name(), aPos));
+                             std::make_pair(u, aPos));
   }
 
   if (!Octree::global_spatialOctree) {
@@ -380,8 +391,9 @@ 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())) {
+    std::string u(boost::to_upper_copy(aPos->ident()));
+    NamedPositionedIndex::iterator it = global_identIndex.find(u);
+    while (it != global_identIndex.end() && (it->first == u)) {
       if (it->second == aPos) {
         global_identIndex.erase(it);
         break;
@@ -392,8 +404,9 @@ removeFromIndices(FGPositioned* aPos)
   }
   
   if (!aPos->name().empty()) {
-    NamedPositionedIndex::iterator it = global_nameIndex.find(aPos->name());
-    while (it != global_nameIndex.end() && (it->first == aPos->name())) {
+    std::string u(boost::to_upper_copy(aPos->name()));
+    NamedPositionedIndex::iterator it = global_nameIndex.find(u);
+    while (it != global_nameIndex.end() && (it->first == u)) {
       if (it->second == aPos) {
         global_nameIndex.erase(it);
         break;
@@ -415,50 +428,51 @@ public:
   }
 };
 
-/**
- * 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)
+void findInIndex(NamedPositionedIndex& aIndex, const std::string& aFind, std::vector<FGPositioned*>& aResult)
 {
-  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());
-  }
-  
-  NamedPositionedIndex::const_iterator it = global_identIndex.begin();
-  NamedPositionedIndex::const_iterator end = global_identIndex.end();
-  
-  // note this is a vector of raw pointers, not smart pointers, because it
-  // may get very large and smart-pointer-atomicity-locking then becomes a
-  // bottleneck for this case.
-  std::vector<FGPositioned*> matches;
-  std::string upper;
-  
+  NamedPositionedIndex::const_iterator it = aIndex.begin();
+  NamedPositionedIndex::const_iterator end = aIndex.end();
+
+  bool haveFilter = !aFind.empty();
+
   for (; it != end; ++it) {
     FGPositioned::Type ty = it->second->type();
     if ((ty < FGPositioned::AIRPORT) || (ty > FGPositioned::SEAPORT)) {
       continue;
     }
     
-    if (hasFilter && (it->second->ident().find(aFilter) == std::string::npos)) {
-      upper = it->second->name(); // string copy, sadly
-      ct.toupper((char *)upper.data(), (char *)upper.data() + upper.size());
-      if (upper.find(aFilter) == std::string::npos) {
-        continue;
-      }
+    if (haveFilter && it->first.find(aFind) == std::string::npos) {
+      continue;
     }
     
-    matches.push_back(it->second);
+    aResult.push_back(it->second);
+  } // of index iteration
+}
+
+/**
+ * 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)
+{
+// note this is a vector of raw pointers, not smart pointers, because it
+// may get very large and smart-pointer-atomicity-locking then becomes a
+// bottleneck for this case.
+  std::vector<FGPositioned*> matches;
+  if (!aFilter.empty()) {
+    std::string filter = boost::to_upper_copy(aFilter);
+    findInIndex(global_identIndex, filter, matches);
+    findInIndex(global_nameIndex, filter, matches);
+  } else {
+    
+    findInIndex(global_identIndex, std::string(), matches);
   }
   
-  // sort alphabetically on name
+// sort alphabetically on name
   std::sort(matches.begin(), matches.end(), OrderByName());
   
-  // convert results to format comptible with puaList
+// convert results to format comptible with puaList
   unsigned int numMatches = matches.size();
   char** result = new char*[numMatches + 1];
   result[numMatches] = NULL; // end-of-list marker
@@ -493,6 +507,15 @@ char** searchAirportNamesAndIdents(const std::string& aFilter)
   return result;
 }
 
+static void validateSGGeod(const SGGeod& geod)
+{
+  if (osg::isNaN(geod.getLatitudeDeg()) ||
+      osg::isNaN(geod.getLongitudeDeg()))
+  {
+    throw sg_range_exception("position is invalid, NaNs");
+  }
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 bool
@@ -511,17 +534,27 @@ FGPositioned::Filter::passType(Type aTy) const
 
 static FGPositioned::List 
 findAll(const NamedPositionedIndex& aIndex, 
-                             const std::string& aName, FGPositioned::Filter* aFilter)
+                             const std::string& aName,
+                             FGPositioned::Filter* aFilter,
+                             bool aExact)
 {
   FGPositioned::List result;
   if (aName.empty()) {
     return result;
   }
   
-  std::string upperBoundId = aName;
-  upperBoundId[upperBoundId.size()-1]++;
-  NamedPositionedIndex::const_iterator upperBound = aIndex.lower_bound(upperBoundId);
-  NamedPositionedIndex::const_iterator it = aIndex.lower_bound(aName);
+  std::string name = boost::to_upper_copy(aName);
+  NamedPositionedIndex::const_iterator upperBound;
+  
+  if (aExact) {
+    upperBound = aIndex.upper_bound(name);
+  } else {
+    std::string upperBoundId = name;
+    upperBoundId[upperBoundId.size()-1]++;
+    upperBound = aIndex.lower_bound(upperBoundId);
+  }
+  
+  NamedPositionedIndex::const_iterator it = aIndex.lower_bound(name);
   
   for (; it != upperBound; ++it) {
     FGPositionedRef candidate = it->second;
@@ -595,12 +628,23 @@ FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
   const NameTypeEntry names[] = {
     {"airport", AIRPORT},
     {"vor", VOR},
+    {"loc", LOC},
+    {"ils", ILS},
+    {"gs", GS},
     {"ndb", NDB},
     {"wpt", WAYPOINT},
     {"fix", FIX},
     {"tacan", TACAN},
     {"dme", DME},
+    {"atis", FREQ_ATIS},
+    {"awos", FREQ_AWOS},
+    {"tower", FREQ_TOWER},
+    {"ground", FREQ_GROUND},
+    {"approach", FREQ_APP_DEP},
+    {"departure", FREQ_APP_DEP},
   // aliases
+    {"gnd", FREQ_GROUND},
+    {"twr", FREQ_TOWER},
     {"waypoint", WAYPOINT},
     {"apt", AIRPORT},
     {"arpt", AIRPORT},
@@ -644,6 +688,13 @@ const char* FGPositioned::nameForType(Type aTy)
  case WAYPOINT: return "waypoint";
  case DME: return "dme";
  case TACAN: return "tacan";
+ case FREQ_TOWER: return "tower";
+ case FREQ_ATIS: return "atis";
+ case FREQ_AWOS: return "awos";
+ case FREQ_GROUND: return "ground";
+ case FREQ_CLEARANCE: return "clearance";
+ case FREQ_UNICOM: return "unicom";
+ case FREQ_APP_DEP: return "approach-departure";
  default:
   return "unknown";
  }
@@ -655,7 +706,9 @@ const char* FGPositioned::nameForType(Type aTy)
 FGPositionedRef
 FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos, Filter* aFilter)
 {
-  FGPositioned::List r(findAll(global_identIndex, aIdent, aFilter));
+  validateSGGeod(aPos);
+
+  FGPositioned::List r(findAll(global_identIndex, aIdent, aFilter, true));
   if (r.empty()) {
     return FGPositionedRef();
   }
@@ -667,6 +720,8 @@ FGPositioned::findClosestWithIdent(const std::string& aIdent, const SGGeod& aPos
 FGPositioned::List
 FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilter)
 {
+  validateSGGeod(aPos);
+
   List result;
   Octree::findAllWithinRange(SGVec3d::fromGeod(aPos), 
     aRangeNm * SG_NM_TO_METER, aFilter, result);
@@ -674,32 +729,36 @@ FGPositioned::findWithinRange(const SGGeod& aPos, double aRangeNm, Filter* aFilt
 }
 
 FGPositioned::List
-FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter)
+FGPositioned::findAllWithIdent(const std::string& aIdent, Filter* aFilter, bool aExact)
 {
-  return findAll(global_identIndex, aIdent, aFilter);
+  return findAll(global_identIndex, aIdent, aFilter, aExact);
 }
 
 FGPositioned::List
-FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter)
+FGPositioned::findAllWithName(const std::string& aName, Filter* aFilter, bool aExact)
 {
-  return findAll(global_nameIndex, aName, aFilter);
+  return findAll(global_nameIndex, aName, aFilter, aExact);
 }
 
 FGPositionedRef
 FGPositioned::findClosest(const SGGeod& aPos, double aCutoffNm, Filter* aFilter)
 {
-   List l(findClosestN(aPos, 1, aCutoffNm, aFilter));
-   if (l.empty()) {
-      return NULL;
-   }
-   
-   assert(l.size() == 1);
-   return l.front();
+  validateSGGeod(aPos);
+  
+  List l(findClosestN(aPos, 1, aCutoffNm, aFilter));
+  if (l.empty()) {
+    return NULL;
+  }
+
+  assert(l.size() == 1);
+  return l.front();
 }
 
 FGPositioned::List
 FGPositioned::findClosestN(const SGGeod& aPos, unsigned int aN, double aCutoffNm, Filter* aFilter)
 {
+  validateSGGeod(aPos);
+  
   List result;
   Octree::findNearestN(SGVec3d::fromGeod(aPos), aN, aCutoffNm * SG_NM_TO_METER, aFilter, result);
   return result;
@@ -755,6 +814,8 @@ FGPositioned::findNextWithPartialId(FGPositionedRef aCur, const std::string& aId
 void
 FGPositioned::sortByRange(List& aResult, const SGGeod& aPos)
 {
+  validateSGGeod(aPos);
+  
   SGVec3d cartPos(SGVec3d::fromGeod(aPos));
 // computer ordering values
   Octree::FindNearestResults r;
@@ -773,3 +834,35 @@ FGPositioned::sortByRange(List& aResult, const SGGeod& aPos)
     aResult[i] = r[i].get();
   }
 }
+
+FGPositioned::TypeFilter::TypeFilter(Type aTy)
+{
+  addType(aTy);
+}
+
+void FGPositioned::TypeFilter::addType(Type aTy)
+{
+  if (aTy == INVALID) {
+    return;
+    
+  }
+  
+  types.push_back(aTy);
+}
+
+bool
+FGPositioned::TypeFilter::pass(FGPositioned* aPos) const
+{
+  if (types.empty()) {
+    return true;
+  }
+  
+    std::vector<Type>::const_iterator it = types.begin(),
+        end = types.end();
+    for (; it != end; ++it) {
+        return aPos->type() == *it;
+    }
+    
+    return false;
+}
+