1
0
Fork 0

Route-manager: cache the routePath

Re-creating the route-path every update is wasteful, just cache it
until the roue changes. Since we already have similar logic for
the property-tree waypoint mirror, just piggy-back off that.
This commit is contained in:
James Turner 2021-07-21 15:45:13 +01:00
parent 3082789582
commit e2ef3dacd6
2 changed files with 15 additions and 11 deletions

View file

@ -417,17 +417,15 @@ void FGRouteMgr::postinit()
// then the global initial waypoint list could die. // then the global initial waypoint list could die.
string_list *waypoints = globals->get_initial_waypoints(); string_list *waypoints = globals->get_initial_waypoints();
if (waypoints) { if (waypoints) {
string_list::iterator it; for (const auto& wpStr : *waypoints) {
for (it = waypoints->begin(); it != waypoints->end(); ++it) { WayptRef w = waypointFromString(wpStr, -1);
WayptRef w = waypointFromString(*it, -1);
if (w) { if (w) {
_plan->insertWayptAtIndex(w, -1); _plan->insertWayptAtIndex(w, -1);
} else { } else {
SG_LOG(SG_AUTOPILOT, SG_WARN, "Failed to create waypoint from '" << *it << "'"); SG_LOG(SG_AUTOPILOT, SG_WARN, "Failed to create waypoint from '" << wpStr << "'");
} }
} }
SG_LOG(SG_AUTOPILOT, SG_INFO, "loaded initial waypoints:" << numLegs());
update_mirror(); update_mirror();
} }
@ -538,8 +536,11 @@ void FGRouteMgr::update( double dt )
} }
// use RoutePath to compute location of active WP // use RoutePath to compute location of active WP
RoutePath path(_plan); if (!_routePath) {
SGGeod wpPos = path.positionForIndex(_plan->currentIndex()); _routePath.reset(new RoutePath{_plan});
}
SGGeod wpPos = _routePath->positionForIndex(_plan->currentIndex());
double courseDeg, az2, distanceM; double courseDeg, az2, distanceM;
SGGeodesy::inverse(currentPos, wpPos, courseDeg, az2, distanceM); SGGeodesy::inverse(currentPos, wpPos, courseDeg, az2, distanceM);
@ -564,7 +565,7 @@ void FGRouteMgr::update( double dt )
FlightPlan::Leg* nextLeg = _plan->nextLeg(); FlightPlan::Leg* nextLeg = _plan->nextLeg();
if (nextLeg) { if (nextLeg) {
wpPos = path.positionForIndex(_plan->currentIndex() + 1); wpPos = _routePath->positionForIndex(_plan->currentIndex() + 1);
SGGeodesy::inverse(currentPos, wpPos, courseDeg, az2, distanceM); SGGeodesy::inverse(currentPos, wpPos, courseDeg, az2, distanceM);
wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM); wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
@ -586,6 +587,7 @@ void FGRouteMgr::update( double dt )
void FGRouteMgr::clearRoute() void FGRouteMgr::clearRoute()
{ {
_routePath.reset();
if (_plan) { if (_plan) {
_plan->clearLegs(); _plan->clearLegs();
} }
@ -670,6 +672,7 @@ void FGRouteMgr::waypointsChanged()
// mirror internal route to the property system for inspection by other subsystems // mirror internal route to the property system for inspection by other subsystems
void FGRouteMgr::update_mirror() void FGRouteMgr::update_mirror()
{ {
_routePath.reset(); // wipe this so we re-compute on next update()
mirror->removeChildren("wp"); mirror->removeChildren("wp");
NewGUI * gui = (NewGUI *)globals->get_subsystem("gui"); NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL; FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;

View file

@ -21,8 +21,7 @@
// $Id$ // $Id$
#ifndef _ROUTE_MGR_HXX #pragma once
#define _ROUTE_MGR_HXX 1
#include <simgear/props/props.hxx> #include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx> #include <simgear/structure/subsystem_mgr.hxx>
@ -32,6 +31,7 @@
// forward decls // forward decls
class SGPath; class SGPath;
class PropertyWatcher; class PropertyWatcher;
class RoutePath;
/** /**
* Top level route manager class * Top level route manager class
@ -181,6 +181,8 @@ private:
InputListener *listener; InputListener *listener;
SGPropertyNode_ptr mirror; SGPropertyNode_ptr mirror;
std::unique_ptr<RoutePath> _routePath;
/** /**
* Helper to keep various pieces of state in sync when the route is * Helper to keep various pieces of state in sync when the route is
* modified (waypoints added, inserted, removed). Notably, this fires the * modified (waypoints added, inserted, removed). Notably, this fires the
@ -236,4 +238,3 @@ private:
void setAlternate(const std::string &icao); void setAlternate(const std::string &icao);
}; };
#endif // _ROUTE_MGR_HXX