]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/route_mgr.cxx
Move viewer-related sources to separate folder.
[flightgear.git] / src / Autopilot / route_mgr.cxx
index bb93274838717230f386111c5d1ca030a6950d5f..b16318da76f5906054f5a081a3de6afa6ad18b6f 100644 (file)
@@ -258,7 +258,9 @@ static bool commandDeleteWaypt(const SGPropertyNode* arg)
 FGRouteMgr::FGRouteMgr() :
   _currentIndex(0),
   input(fgGetNode( RM "input", true )),
-  mirror(fgGetNode( RM "route", true ))
+  mirror(fgGetNode( RM "route", true )),
+  _departureWatcher(NULL),
+  _arrivalWatcher(NULL)
 {
   listener = new InputListener(this);
   input->setStringValue("");
@@ -278,6 +280,8 @@ FGRouteMgr::~FGRouteMgr()
 {
   input->removeChangeListener(listener);
   delete listener;
+  delete _departureWatcher;
+  delete _arrivalWatcher;
 }
 
 
@@ -296,6 +300,7 @@ void FGRouteMgr::init() {
     &FGRouteMgr::getDepartureName, NULL));
   departure->setStringValue("runway", "");
   
+  delete _departureWatcher;
   _departureWatcher = createWatcher(this, &FGRouteMgr::departureChanged);
   _departureWatcher->watch(departure->getChild("runway"));
   
@@ -311,6 +316,7 @@ void FGRouteMgr::init() {
   destination->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this, 
     &FGRouteMgr::getDestinationName, NULL));
   
+  delete _arrivalWatcher;
   _arrivalWatcher = createWatcher(this, &FGRouteMgr::arrivalChanged);
   _arrivalWatcher->watch(destination->getChild("runway", 0, true));
   
@@ -335,6 +341,8 @@ void FGRouteMgr::init() {
   
   totalDistance = fgGetNode(RM "total-distance", true);
   totalDistance->setDoubleValue(0.0);
+  distanceToGo = fgGetNode(RM "distance-remaining-nm", true);
+  distanceToGo->setDoubleValue(0.0);
   
   ete = fgGetNode(RM "ete", true);
   ete->setDoubleValue(0.0);
@@ -379,7 +387,7 @@ void FGRouteMgr::init() {
 void FGRouteMgr::postinit()
 {
   SGPath path(_pathNode->getStringValue());
-  if (path.exists()) {
+  if (!path.isNull()) {
     SG_LOG(SG_AUTOPILOT, SG_INFO, "loading flight-plan from: " << path.str());
     loadRoute(path);
   }
@@ -398,6 +406,7 @@ void FGRouteMgr::postinit()
     }
     
     SG_LOG(SG_AUTOPILOT, SG_INFO, "loaded initial waypoints:" << _route.size());
+    update_mirror();
   }
 
   weightOnWheels = fgGetNode("/gear/gear[0]/wow", true);
@@ -449,34 +458,44 @@ void FGRouteMgr::update( double dt )
   double distanceM;
   boost::tie(courseDeg, distanceM) = curWpt->courseAndDistanceFrom(currentPos);
   
-// update wp0 / wp1 / wp-last for legacy users
+// update wp0 / wp1 / wp-last
   wp0->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
+  wp0->setDoubleValue("true-bearing-deg", courseDeg);
   courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
   wp0->setDoubleValue("bearing-deg", courseDeg);
   setETAPropertyFromDistance(wp0->getChild("eta"), distanceM);
   
+  double totalPathDistance = totalDistance->getDoubleValue() * SG_NM_TO_METER;
   double totalDistanceRemaining = distanceM; // distance to current waypoint
+  double pathDistance = cachedWaypointPathTotalDistance(_currentIndex);
+  
+// total distance to go, is direct distance to wp0, plus the remaining
+// path distance from wp0
+  totalDistanceRemaining += (totalPathDistance - pathDistance);
+  
+  wp0->setDoubleValue("distance-along-route-nm", 
+                      pathDistance * SG_METER_TO_NM);
+  wp0->setDoubleValue("remaining-distance-nm", 
+                      (totalPathDistance - pathDistance) * SG_METER_TO_NM);
   
   Waypt* nextWpt = nextWaypt();
   if (nextWpt) {
     boost::tie(courseDeg, distanceM) = nextWpt->courseAndDistanceFrom(currentPos);
      
     wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
+    wp1->setDoubleValue("true-bearing-deg", courseDeg);
     courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
     wp1->setDoubleValue("bearing-deg", courseDeg);
     setETAPropertyFromDistance(wp1->getChild("eta"), distanceM);
-  }
-  
-  Waypt* prev = curWpt;
-  for (unsigned int i=_currentIndex + 1; i<_route.size(); ++i) {
-    Waypt* w = _route[i];
-    if (w->flag(WPT_DYNAMIC)) continue;
-    totalDistanceRemaining += SGGeodesy::distanceM(prev->position(), w->position());
-    prev = w;
-    
     
+    double pathDistance = cachedWaypointPathTotalDistance(_currentIndex + 1);    
+    wp1->setDoubleValue("distance-along-route-nm", 
+                        pathDistance * SG_METER_TO_NM);
+    wp1->setDoubleValue("remaining-distance-nm", 
+                        (totalPathDistance - pathDistance) * SG_METER_TO_NM);
   }
   
+  distanceToGo->setDoubleValue(totalDistanceRemaining * SG_METER_TO_NM);
   wpn->setDoubleValue("dist", totalDistanceRemaining * SG_METER_TO_NM);
   ete->setDoubleValue(totalDistanceRemaining * SG_METER_TO_NM / groundSpeed * 3600.0);
   setETAPropertyFromDistance(wpn->getChild("eta"), totalDistanceRemaining);
@@ -539,9 +558,22 @@ flightgear::WayptRef FGRouteMgr::removeWayptAtIndex(int aIndex)
   return w;
 }
   
+struct NotGeneratedWayptPredicate : public std::unary_function<const Waypt*, bool>
+{
+  bool operator() (const Waypt* w) const
+  {
+    return (w->flag(WPT_GENERATED) == false);
+  }
+};
+
+
 void FGRouteMgr::clearRoute()
 {
-  _route.clear();
+// erase all non-generated waypoints
+  WayptVec::iterator r =  
+    std::remove_if(_route.begin(), _route.end(), NotGeneratedWayptPredicate());
+  _route.erase(r, _route.end());
+  
   _currentIndex = -1;
   
   update_mirror();
@@ -972,13 +1004,14 @@ void FGRouteMgr::update_mirror()
   mirror->removeChildren("wp");
   
   int num = numWaypts();
+  double totalDistanceEnroute = 0.0;
+    
   for (int i = 0; i < num; i++) {
     Waypt* wp = _route[i];
     SGPropertyNode *prop = mirror->getChild("wp", i, 1);
 
     const SGGeod& pos(wp->position());
     prop->setStringValue("id", wp->ident().c_str());
-    //prop->setStringValue("name", wp.get_name().c_str());
     prop->setDoubleValue("longitude-deg", pos.getLongitudeDeg());
     prop->setDoubleValue("latitude-deg",pos.getLatitudeDeg());
    
@@ -989,12 +1022,15 @@ void FGRouteMgr::update_mirror()
         next->courseAndDistanceFrom(pos);
       prop->setDoubleValue("leg-bearing-true-deg", crsDist.first);
       prop->setDoubleValue("leg-distance-nm", crsDist.second * SG_METER_TO_NM);
+      prop->setDoubleValue("distance-along-route-nm", totalDistanceEnroute);
+      totalDistanceEnroute += crsDist.second * SG_METER_TO_NM;
     }
     
     if (wp->altitudeRestriction() != RESTRICT_NONE) {
       double ft = wp->altitudeFt();
       prop->setDoubleValue("altitude-m", ft * SG_FEET_TO_METER);
       prop->setDoubleValue("altitude-ft", ft);
+      prop->setIntValue("flight-level", static_cast<int>(ft / 1000) * 10);
     } else {
       prop->setDoubleValue("altitude-m", -9999.9);
       prop->setDoubleValue("altitude-ft", -9999.9);
@@ -1029,6 +1065,28 @@ void FGRouteMgr::update_mirror()
   if (rmDlg) {
     rmDlg->updateValues();
   }
+    
+  if (_departure) {
+    departure->setDoubleValue("field-elevation-ft", _departure->getElevation());
+  }
+  
+  if (_destination) {
+    destination->setDoubleValue("field-elevation-ft", _destination->getElevation());
+  }
+  
+  totalDistance->setDoubleValue(totalDistanceEnroute);
+}
+
+double FGRouteMgr::cachedLegPathDistanceM(int index) const
+{
+  SGPropertyNode *prop = mirror->getChild("wp", index, 1);
+  return prop->getDoubleValue("leg-distance-nm") * SG_NM_TO_METER;
+}
+
+double FGRouteMgr::cachedWaypointPathTotalDistance(int index) const
+{
+  SGPropertyNode *prop = mirror->getChild("wp", index, 1);
+  return prop->getDoubleValue("distance-along-route-nm") * SG_NM_TO_METER;
 }
 
 // command interface /autopilot/route-manager/input:
@@ -1135,15 +1193,7 @@ void FGRouteMgr::initAtPosition()
 
 bool FGRouteMgr::haveUserWaypoints() const
 {
-  for (int i = 0; i < numWaypts(); i++) {
-    if (!_route[i]->flag(WPT_GENERATED)) {
-      // have a non-generated waypoint, we're done
-      return true;
-    }
-  }
-  
-  // all waypoints are generated
-  return false;
+  return std::find_if(_route.begin(), _route.end(), NotGeneratedWayptPredicate()) != _route.end();
 }
 
 bool FGRouteMgr::activate()
@@ -1273,6 +1323,15 @@ Waypt* FGRouteMgr::wayptAtIndex(int index) const
   return _route[index];
 }
 
+SGPropertyNode_ptr FGRouteMgr::wayptNodeAtIndex(int index) const
+{
+    if ((index < 0) || (index >= numWaypts())) {
+        throw sg_range_exception("waypt index out of range", "FGRouteMgr::wayptAtIndex");
+    }
+    
+    return mirror->getChild("wp", index);
+}
+
 bool FGRouteMgr::saveRoute(const SGPath& path)
 {
   SG_LOG(SG_IO, SG_INFO, "Saving route to " << path.str());
@@ -1310,6 +1369,13 @@ bool FGRouteMgr::saveRoute(const SGPath& path)
 
 bool FGRouteMgr::loadRoute(const SGPath& path)
 {
+  if (!path.exists())
+  {
+      SG_LOG(SG_IO, SG_ALERT, "Failed to load flight-plan '" << path.str()
+              << "'. The file does not exist.");
+      return false;
+  }
+    
   // deactivate route first
   active->setBoolValue(false);
   
@@ -1317,28 +1383,37 @@ bool FGRouteMgr::loadRoute(const SGPath& path)
   
   SG_LOG(SG_IO, SG_INFO, "going to read flight-plan from:" << path.str());
     
+  bool Status = false;
   try {
     readProperties(path.str(), routeData);
   } catch (sg_exception& ) {
     // if XML parsing fails, the file might be simple textual list of waypoints
-    return loadPlainTextRoute(path);
+    Status = loadPlainTextRoute(path);
+    routeData = 0;
   }
-  
-  try {
-    int version = routeData->getIntValue("version", 1);
-    if (version == 1) {
-      loadVersion1XMLRoute(routeData);
-    } else if (version == 2) {
-      loadVersion2XMLRoute(routeData);
-    } else {
-      throw sg_io_exception("unsupported XML route version");
-    }
-    return true;
-  } catch (sg_exception& e) {
-    SG_LOG(SG_IO, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin()
-      << "'. " << e.getMessage());
-    return false;
+
+  if (routeData.valid())
+  {
+      try {
+        int version = routeData->getIntValue("version", 1);
+        if (version == 1) {
+          loadVersion1XMLRoute(routeData);
+        } else if (version == 2) {
+          loadVersion2XMLRoute(routeData);
+        } else {
+          throw sg_io_exception("unsupported XML route version");
+        }
+        Status = true;
+      } catch (sg_exception& e) {
+        SG_LOG(SG_IO, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin()
+          << "'. " << e.getMessage());
+        Status = false;
+      }
   }
+
+  update_mirror();
+
+  return Status;
 }
 
 void FGRouteMgr::loadXMLRouteHeader(SGPropertyNode_ptr routeData)
@@ -1456,12 +1531,12 @@ WayptRef FGRouteMgr::parseVersion1XMLWaypt(SGPropertyNode* aWP)
 
 bool FGRouteMgr::loadPlainTextRoute(const SGPath& path)
 {
-  sg_gzifstream in(path.str().c_str());
-  if (!in.is_open()) {
-    return false;
-  }
-  
   try {
+    sg_gzifstream in(path.str().c_str());
+    if (!in.is_open()) {
+        throw sg_io_exception("Cannot open file for reading.");
+    }
+  
     WayptVec wpts;
     while (!in.eof()) {
       string line;
@@ -1549,3 +1624,42 @@ void FGRouteMgr::setDestinationICAO(const char* aIdent)
   
   arrivalChanged();
 }
+
+FGAirportRef FGRouteMgr::departureAirport() const
+{
+    return _departure;
+}
+
+FGAirportRef FGRouteMgr::destinationAirport() const
+{
+    return _destination;
+}
+
+FGRunway* FGRouteMgr::departureRunway() const
+{
+    if (!_departure) {
+        return NULL;
+    }
+    
+    string runwayId(departure->getStringValue("runway"));
+    if (!_departure->hasRunwayWithIdent(runwayId)) {
+        return NULL;
+    }
+    
+    return _departure->getRunwayByIdent(runwayId);
+}
+
+FGRunway* FGRouteMgr::destinationRunway() const
+{
+    if (!_destination) {
+        return NULL;
+    }
+    
+    string runwayId(destination->getStringValue("runway"));
+    if (!_destination->hasRunwayWithIdent(runwayId)) {
+        return NULL;
+    }
+    
+    return _destination->getRunwayByIdent(runwayId);
+}
+