6 #include <Navaids/routePath.hxx>
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/magvar/magvar.hxx>
10 #include <simgear/timing/sg_time.hxx>
12 #include <Main/globals.hxx>
13 #include <Airports/runways.hxx>
14 #include <Navaids/waypoint.hxx>
15 #include <Navaids/positioned.hxx>
17 namespace flightgear {
18 bool geocRadialIntersection(const SGGeoc& a, double r1, const SGGeoc& b, double r2, SGGeoc& result);
21 using namespace flightgear;
23 // implement Point(s) known distance from a great circle
25 static double sqr(const double x)
30 double pointsKnownDistanceFromGC(const SGGeoc& a, const SGGeoc&b, const SGGeoc& d, double dist)
32 double A = SGGeodesy::courseRad(a, d) - SGGeodesy::courseRad(a, b);
33 double bDist = SGGeodesy::distanceRad(a, d);
35 // r=(cos(b)^2+sin(b)^2*cos(A)^2)^(1/2)
36 double r = pow(sqr(cos(bDist)) + sqr(sin(bDist)) * sqr(cos(A)), 0.5);
38 double p = atan2(sin(bDist)*cos(A), cos(bDist));
40 if (sqr(cos(dist)) > sqr(r)) {
41 SG_LOG(SG_GENERAL, SG_INFO, "pointsKnownDistanceFromGC, no points exist");
45 double dp1 = p + acos(cos(dist)/r);
46 double dp2 = p - acos(cos(dist)/r);
48 double dp1Nm = fabs(dp1 * SG_RAD_TO_NM);
49 double dp2Nm = fabs(dp2 * SG_RAD_TO_NM);
51 return SGMiscd::min(dp1Nm, dp2Nm);
54 RoutePath::RoutePath(const flightgear::WayptVec& wpts) :
60 RoutePath::RoutePath(const flightgear::FlightPlan* fp)
62 for (int l=0; l<fp->numLegs(); ++l) {
63 _waypts.push_back(fp->legAtIndex(l)->waypoint());
68 void RoutePath::commonInit()
71 _pathDescentFPM = 800;
73 _pathTurnRate = 3.0; // 3 deg/sec = 180def/min = standard rate turn
76 SGGeodVec RoutePath::pathForIndex(int index) const
79 return SGGeodVec(); // no path for first waypoint
82 if (_waypts[index]->type() == "vectors") {
83 return SGGeodVec(); // empty
86 if (_waypts[index]->type() == "hold") {
87 return pathForHold((Hold*) _waypts[index].get());
92 if (!computedPositionForIndex(index-1, pos)) {
97 if (!computedPositionForIndex(index, pos)) {
103 if (_waypts[index]->type() == "runway") {
104 // runways get an extra point, at the end. this is particularly
105 // important so missed approach segments draw correctly
106 FGRunway* rwy = static_cast<RunwayWaypt*>(_waypts[index].get())->runway();
107 r.push_back(rwy->end());
113 SGGeod RoutePath::positionForIndex(int index) const
116 bool ok = computedPositionForIndex(index, r);
124 SGGeodVec RoutePath::pathForHold(Hold* hold) const
127 double hdg = hold->inboundRadial();
128 double turnDelta = 180.0 / turnSteps;
132 double stepTime = turnDelta / _pathTurnRate; // in seconds
133 double stepDist = _pathIAS * (stepTime / 3600.0) * SG_NM_TO_METER;
134 double legDist = hold->isDistance() ?
135 hold->timeOrDistance()
136 : _pathIAS * (hold->timeOrDistance() / 3600.0);
137 legDist *= SG_NM_TO_METER;
139 if (hold->isLeftHanded()) {
140 turnDelta = -turnDelta;
142 SGGeod pos = hold->position();
145 // turn+leg sides are a mirror
146 for (int j=0; j < 2; ++j) {
148 for (int i=0;i<turnSteps; ++i) {
150 SGGeodesy::direct(pos, hdg, stepDist, pos, az2);
155 SGGeodesy::direct(pos, hdg, legDist, pos, az2);
157 } // of leg+turn duplication
163 * the path context holds the state of of an imaginary aircraft traversing
164 * the route, and limits the rate at which heading / altitude / position can
167 class RoutePath::PathCtx
174 bool RoutePath::computedPositionForIndex(int index, SGGeod& r) const
176 if ((index < 0) || (index >= (int) _waypts.size())) {
177 throw sg_range_exception("waypt index out of range",
178 "RoutePath::computedPositionForIndex");
181 WayptRef w = _waypts[index];
182 if (!w->flag(WPT_DYNAMIC)) {
187 if (w->type() == "radialIntercept") {
188 // radial intersection along track
190 if (!computedPositionForIndex(index - 1, prev)) {
194 SGGeoc prevGc = SGGeoc::fromGeod(prev);
195 SGGeoc navid = SGGeoc::fromGeod(w->position());
197 double magVar = magVarFor(prev);
199 RadialIntercept* i = (RadialIntercept*) w.get();
200 double radial = i->radialDegMagnetic() + magVar;
201 double track = i->courseDegMagnetic() + magVar;
202 bool ok = geocRadialIntersection(prevGc, track, navid, radial, rGc);
207 r = SGGeod::fromGeoc(rGc);
209 } else if (w->type() == "dmeIntercept") {
210 // find the point along the DME track, from prev, that is the correct distance
213 if (!computedPositionForIndex(index - 1, prev)) {
217 DMEIntercept* di = (DMEIntercept*) w.get();
219 SGGeoc prevGc = SGGeoc::fromGeod(prev);
220 SGGeoc navid = SGGeoc::fromGeod(w->position());
221 double distRad = di->dmeDistanceNm() * SG_NM_TO_RAD;
225 double crs = di->courseDegMagnetic() + magVarFor(prev);
226 SGGeodesy::advanceRadM(prevGc, crs, 100 * SG_NM_TO_RAD, bPt);
228 double dNm = pointsKnownDistanceFromGC(prevGc, bPt, navid, distRad);
234 SGGeodesy::direct(prev, crs, dNm * SG_NM_TO_METER, r, az2);
236 } else if (w->type() == "hdgToAlt") {
237 HeadingToAltitude* h = (HeadingToAltitude*) w.get();
238 double climb = h->altitudeFt() - computeAltitudeForIndex(index - 1);
239 double d = distanceForClimb(climb);
242 if (!computedPositionForIndex(index - 1, prevPos)) {
246 double hdg = h->headingDegMagnetic() + magVarFor(prevPos);
249 SGGeodesy::direct(prevPos, hdg, d * SG_NM_TO_METER, r, az2);
251 } else if (w->type() == "vectors"){
253 } else if (w->type() == "hold") {
258 SG_LOG(SG_GENERAL, SG_INFO, "RoutePath::computedPositionForIndex: unhandled type:" << w->type());
262 double RoutePath::computeAltitudeForIndex(int index) const
264 if ((index < 0) || (index >= (int) _waypts.size())) {
265 throw sg_range_exception("waypt index out of range",
266 "RoutePath::computeAltitudeForIndex");
269 WayptRef w = _waypts[index];
270 if (w->altitudeRestriction() != RESTRICT_NONE) {
271 return w->altitudeFt(); // easy!
274 if (w->type() == "runway") {
275 FGRunway* rwy = static_cast<RunwayWaypt*>(w.get())->runway();
276 return rwy->threshold().getElevationFt();
277 } else if ((w->type() == "hold") || (w->type() == "vectors")) {
278 // pretend we don't change altitude in holds/vectoring
279 return computeAltitudeForIndex(index - 1);
282 double prevAlt = computeAltitudeForIndex(index - 1);
283 // find distance to previous, and hence climb/descent
286 if (!computedPositionForIndex(index, pos) ||
287 !computedPositionForIndex(index - 1, prevPos))
289 SG_LOG(SG_GENERAL, SG_WARN, "unable to compute position for waypoints");
290 throw sg_range_exception("unable to compute position for waypoints");
293 double d = SGGeodesy::distanceNm(prevPos, pos);
294 double tMinutes = (d / _pathIAS) * 60.0; // (nm / knots) * 60 = time in minutes
296 double deltaFt; // change in altitude in feet
297 if (w->flag(WPT_ARRIVAL) && !w->flag(WPT_MISS)) {
298 deltaFt = -_pathDescentFPM * tMinutes;
300 deltaFt = _pathClimbFPM * tMinutes;
303 return prevAlt + deltaFt;
306 double RoutePath::computeTrackForIndex(int index) const
308 if ((index < 0) || (index >= (int) _waypts.size())) {
309 throw sg_range_exception("waypt index out of range",
310 "RoutePath::computeTrackForIndex");
313 WayptRef w = _waypts[index];
314 if (w->type() == "radialIntercept") {
315 RadialIntercept* r = (RadialIntercept*) w.get();
316 return r->courseDegMagnetic();
317 } else if (w->type() == "dmeIntercept") {
318 DMEIntercept* d = (DMEIntercept*) w.get();
319 return d->courseDegMagnetic();
320 } else if (w->type() == "hdgToAlt") {
321 HeadingToAltitude* h = (HeadingToAltitude*) w.get();
322 return h->headingDegMagnetic();
323 } else if (w->type() == "hold") {
324 Hold* h = (Hold*) w.get();
325 return h->inboundRadial();
326 } else if (w->type() == "vectors") {
327 SG_LOG(SG_GENERAL, SG_WARN, "asked for track from VECTORS");
328 throw sg_range_exception("asked for track from vectors waypt");
329 } else if (w->type() == "runway") {
330 FGRunway* rwy = static_cast<RunwayWaypt*>(w.get())->runway();
331 return rwy->headingDeg();
334 // course based upon previous and current pos
337 if (!computedPositionForIndex(index, pos) ||
338 !computedPositionForIndex(index - 1, prevPos))
340 SG_LOG(SG_GENERAL, SG_WARN, "unable to compute position for waypoints");
341 throw sg_range_exception("unable to compute position for waypoints");
344 return SGGeodesy::courseDeg(prevPos, pos);
347 double RoutePath::distanceForClimb(double climbFt) const
349 double t = 0.0; // in seconds
351 t = (climbFt / _pathClimbFPM) * 60.0;
352 } else if (climbFt < 0.0) {
353 t = (climbFt / _pathDescentFPM) * 60.0;
356 return _pathIAS * (t / 3600.0);
359 double RoutePath::magVarFor(const SGGeod& geod) const
361 double jd = globals->get_time_params()->getJD();
362 return sgGetMagVar(geod, jd) * SG_RADIANS_TO_DEGREES;