| 1 | // routePath.hxx - compute data about planned route |
| 2 | // |
| 3 | // Copyright (C) 2018 James Turner <[email protected]> |
| 4 | // This program is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU General Public License as |
| 6 | // published by the Free Software Foundation; either version 2 of the |
| 7 | // License, or (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, but |
| 10 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | // General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | #include "config.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | #include <Navaids/routePath.hxx> |
| 23 | |
| 24 | #include <simgear/structure/exception.hxx> |
| 25 | #include <simgear/magvar/magvar.hxx> |
| 26 | #include <simgear/timing/sg_time.hxx> |
| 27 | |
| 28 | #include <Main/globals.hxx> |
| 29 | #include <Airports/runways.hxx> |
| 30 | #include <Navaids/waypoint.hxx> |
| 31 | #include <Navaids/FlightPlan.hxx> |
| 32 | #include <Navaids/positioned.hxx> |
| 33 | #include <Aircraft/AircraftPerformance.hxx> |
| 34 | |
| 35 | namespace flightgear { |
| 36 | |
| 37 | // implementation of |
| 38 | // http://williams.best.vwh.net/avform.htm#Intersection |
| 39 | bool geocRadialIntersection(const SGGeoc& a, double r1, const SGGeoc& b, double r2, SGGeoc& result) |
| 40 | { |
| 41 | double crs13 = r1 * SG_DEGREES_TO_RADIANS; |
| 42 | double crs23 = r2 * SG_DEGREES_TO_RADIANS; |
| 43 | double dst12 = SGGeodesy::distanceRad(a, b); |
| 44 | |
| 45 | //IF sin(lon2-lon1)<0 |
| 46 | // crs12=acos((sin(lat2)-sin(lat1)*cos(dst12))/(sin(dst12)*cos(lat1))) |
| 47 | // crs21=2.*pi-acos((sin(lat1)-sin(lat2)*cos(dst12))/(sin(dst12)*cos(lat2))) |
| 48 | // ELSE |
| 49 | // crs12=2.*pi-acos((sin(lat2)-sin(lat1)*cos(dst12))/(sin(dst12)*cos(lat1))) |
| 50 | // crs21=acos((sin(lat1)-sin(lat2)*cos(dst12))/(sin(dst12)*cos(lat2))) |
| 51 | // ENDIF |
| 52 | |
| 53 | double sinLat1 = sin(a.getLatitudeRad()); |
| 54 | double cosLat1 = cos(a.getLatitudeRad()); |
| 55 | double sinDst12 = sin(dst12); |
| 56 | double cosDst12 = cos(dst12); |
| 57 | |
| 58 | double crs12 = SGGeodesy::courseRad(a, b), |
| 59 | crs21 = SGGeodesy::courseRad(b, a); |
| 60 | |
| 61 | |
| 62 | // normalise to -pi .. pi range |
| 63 | double ang1 = SGMiscd::normalizeAngle(crs13-crs12); |
| 64 | double ang2 = SGMiscd::normalizeAngle(crs21-crs23); |
| 65 | |
| 66 | if ((sin(ang1) == 0.0) && (sin(ang2) == 0.0)) { |
| 67 | SG_LOG(SG_INSTR, SG_INFO, "geocRadialIntersection: infinity of intersections"); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if ((sin(ang1)*sin(ang2))<0.0) { |
| 72 | // SG_LOG(SG_INSTR, SG_INFO, "geocRadialIntersection: intersection ambiguous:" |
| 73 | // << ang1 << " " << ang2 << " sin1 " << sin(ang1) << " sin2 " << sin(ang2)); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | ang1 = fabs(ang1); |
| 78 | ang2 = fabs(ang2); |
| 79 | |
| 80 | //ang3=acos(-cos(ang1)*cos(ang2)+sin(ang1)*sin(ang2)*cos(dst12)) |
| 81 | //dst13=atan2(sin(dst12)*sin(ang1)*sin(ang2),cos(ang2)+cos(ang1)*cos(ang3)) |
| 82 | //lat3=asin(sin(lat1)*cos(dst13)+cos(lat1)*sin(dst13)*cos(crs13)) |
| 83 | |
| 84 | //lon3=mod(lon1-dlon+pi,2*pi)-pi |
| 85 | |
| 86 | double ang3 = acos(-cos(ang1) * cos(ang2) + sin(ang1) * sin(ang2) * cosDst12); |
| 87 | double dst13 = atan2(sinDst12 * sin(ang1) * sin(ang2), cos(ang2) + cos(ang1)*cos(ang3)); |
| 88 | |
| 89 | SGGeoc pt3; |
| 90 | SGGeodesy::advanceRadM(a, crs13, dst13 * SG_RAD_TO_NM * SG_NM_TO_METER, pt3); |
| 91 | |
| 92 | double lat3 = asin(sinLat1 * cos(dst13) + cosLat1 * sin(dst13) * cos(crs13)); |
| 93 | |
| 94 | //dlon=atan2(sin(crs13)*sin(dst13)*cos(lat1),cos(dst13)-sin(lat1)*sin(lat3)) |
| 95 | double dlon = atan2(sin(crs13)*sin(dst13)*cosLat1, cos(dst13)- (sinLat1 * sin(lat3))); |
| 96 | double lon3 = SGMiscd::normalizeAngle(-a.getLongitudeRad()-dlon); |
| 97 | |
| 98 | result = SGGeoc::fromRadM(-lon3, lat3, a.getRadiusM()); |
| 99 | //result = pt3; |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | using namespace flightgear; |
| 105 | |
| 106 | // implement Point(s) known distance from a great circle |
| 107 | |
| 108 | static double sqr(const double x) |
| 109 | { |
| 110 | return x * x; |
| 111 | } |
| 112 | |
| 113 | // http://williams.best.vwh.net/avform.htm#POINTDME |
| 114 | double pointsKnownDistanceFromGC(const SGGeoc& a, const SGGeoc&b, const SGGeoc& d, double dist) |
| 115 | { |
| 116 | double A = SGGeodesy::courseRad(a, d) - SGGeodesy::courseRad(a, b); |
| 117 | double bDist = SGGeodesy::distanceRad(a, d); |
| 118 | |
| 119 | // r=(cos(b)^2+sin(b)^2*cos(A)^2)^(1/2) |
| 120 | double r = pow(sqr(cos(bDist)) + sqr(sin(bDist)) * sqr(cos(A)), 0.5); |
| 121 | |
| 122 | double p = atan2(sin(bDist)*cos(A), cos(bDist)); |
| 123 | |
| 124 | if (sqr(cos(dist)) > sqr(r)) { |
| 125 | SG_LOG(SG_NAVAID, SG_INFO, "pointsKnownDistanceFromGC, no points exist"); |
| 126 | return -1.0; |
| 127 | } |
| 128 | |
| 129 | double dp1 = p + acos(cos(dist)/r); |
| 130 | double dp2 = p - acos(cos(dist)/r); |
| 131 | |
| 132 | double dp1Nm = fabs(dp1 * SG_RAD_TO_NM); |
| 133 | double dp2Nm = fabs(dp2 * SG_RAD_TO_NM); |
| 134 | |
| 135 | return SGMiscd::min(dp1Nm, dp2Nm); |
| 136 | } |
| 137 | |
| 138 | // http://williams.best.vwh.net/avform.htm#Int |
| 139 | double latitudeForGCLongitude(const SGGeoc& a, const SGGeoc& b, double lon) |
| 140 | { |
| 141 | #if 0 |
| 142 | Intermediate points {lat,lon} lie on the great circle connecting points 1 and 2 when: |
| 143 | |
| 144 | lat=atan((sin(lat1)*cos(lat2)*sin(lon-lon2) |
| 145 | -sin(lat2)*cos(lat1)*sin(lon-lon1))/(cos(lat1)*cos(lat2)*sin(lon1-lon2))) |
| 146 | #endif |
| 147 | double lonDiff = a.getLongitudeRad() - b.getLongitudeRad(); |
| 148 | double cosLat1 = cos(a.getLatitudeRad()), |
| 149 | cosLat2 = cos(b.getLatitudeRad()); |
| 150 | double x = sin(a.getLatitudeRad()) * cosLat2 * sin(lon - b.getLongitudeRad()); |
| 151 | double y = sin(b.getLatitudeRad()) * cosLat1 * sin(lon - a.getLongitudeRad()); |
| 152 | double denom = cosLat1 * cosLat2 * sin(lonDiff); |
| 153 | double lat = atan((x - y) / denom); |
| 154 | return lat; |
| 155 | } |
| 156 | |
| 157 | static double magVarFor(const SGGeod& geod) |
| 158 | { |
| 159 | double jd = globals->get_time_params()->getJD(); |
| 160 | return sgGetMagVar(geod, jd) * SG_RADIANS_TO_DEGREES; |
| 161 | } |
| 162 | |
| 163 | SGGeod turnCenterOverflight(const SGGeod& pt, double inHeadingDeg, |
| 164 | double turnAngleDeg, double turnRadiusM) |
| 165 | { |
| 166 | double p = copysign(90.0, turnAngleDeg); |
| 167 | return SGGeodesy::direct(pt, inHeadingDeg + p, turnRadiusM); |
| 168 | } |
| 169 | |
| 170 | SGGeod turnCenterFlyBy(const SGGeod& pt, double inHeadingDeg, |
| 171 | double turnAngleDeg, double turnRadiusM) |
| 172 | { |
| 173 | double halfAngle = turnAngleDeg * 0.5; |
| 174 | double turnCenterOffset = turnRadiusM / cos(halfAngle * SG_DEGREES_TO_RADIANS); |
| 175 | double p = copysign(90.0, turnAngleDeg); |
| 176 | return SGGeodesy::direct(pt, inHeadingDeg + halfAngle + p, turnCenterOffset); |
| 177 | } |
| 178 | |
| 179 | SGGeod turnCenterFromExit(const SGGeod& pt, double outHeadingDeg, |
| 180 | double turnAngleDeg, double turnRadiusM) |
| 181 | { |
| 182 | double p = copysign(90.0, turnAngleDeg); |
| 183 | return SGGeodesy::direct(pt, outHeadingDeg + p, turnRadiusM); |
| 184 | } |
| 185 | |
| 186 | struct TurnInfo |
| 187 | { |
| 188 | SGGeod turnCenter; |
| 189 | double inboundCourseDeg = 0.0; |
| 190 | double turnAngleDeg = 0.0; |
| 191 | bool valid = false; |
| 192 | }; |
| 193 | |
| 194 | /** |
| 195 | * given a turn exit position and heading, and an arbitrary origin position, |
| 196 | * compute the turn center / angle that matches. Certain configurations may |
| 197 | * fail, especially if the origin is less than two turn radii from the exit pos. |
| 198 | */ |
| 199 | TurnInfo turnCenterAndAngleFromExit(const SGGeod& pt, double outHeadingDeg, |
| 200 | double turnRadiusM, const SGGeod&origin) |
| 201 | { |
| 202 | double bearingToExit = SGGeodesy::courseDeg(origin, pt); |
| 203 | // not the final turn angle, but we need to know which side of the |
| 204 | // exit course we're on, to decide if it's a left-hand or right-hand turn |
| 205 | double turnAngle = outHeadingDeg - bearingToExit; |
| 206 | SG_NORMALIZE_RANGE(turnAngle, -180.0, 180.0); |
| 207 | double p = copysign(90.0, turnAngle); |
| 208 | |
| 209 | TurnInfo r; |
| 210 | r.turnCenter = SGGeodesy::direct(pt, outHeadingDeg + p, turnRadiusM); |
| 211 | |
| 212 | double courseToTC, distanceToTC, az2; |
| 213 | SGGeodesy::inverse(origin, r.turnCenter, courseToTC, az2, distanceToTC); |
| 214 | if (distanceToTC < turnRadiusM) { |
| 215 | return r; // invalid |
| 216 | } |
| 217 | |
| 218 | // find additional course angle away from the exit pos to intersect |
| 219 | // the turn circle. |
| 220 | double theta = asin(turnRadiusM / distanceToTC) * SG_RADIANS_TO_DEGREES; |
| 221 | // invert angle sign so we increase the turn angle |
| 222 | theta = -copysign(theta, turnAngle); |
| 223 | |
| 224 | r.inboundCourseDeg = courseToTC + theta; |
| 225 | SG_NORMALIZE_RANGE(r.inboundCourseDeg, 0.0, 360.0); |
| 226 | |
| 227 | // turn angle must have same direction (sign) as turnAngle above, even if |
| 228 | // the turn radius means the sign would cross over (happens if origin point |
| 229 | // is close by |
| 230 | r.turnAngleDeg = outHeadingDeg - r.inboundCourseDeg; |
| 231 | if (r.turnAngleDeg > 0.0) { |
| 232 | if (turnAngle < 0.0) r.turnAngleDeg -= 360.0; |
| 233 | } else { |
| 234 | if (turnAngle > 0.0) r.turnAngleDeg += 360.0; |
| 235 | } |
| 236 | |
| 237 | r.valid = true; |
| 238 | return r; |
| 239 | } |
| 240 | |
| 241 | class WayptData; |
| 242 | using WayptDataVec = std::vector<WayptData>; |
| 243 | using WpDataIt = WayptDataVec::iterator; |
| 244 | |
| 245 | class WayptData |
| 246 | { |
| 247 | public: |
| 248 | explicit WayptData(WayptRef w) : |
| 249 | wpt(w), |
| 250 | hasEntry(false), |
| 251 | posValid(false), |
| 252 | legCourseValid(false), |
| 253 | skipped(false), |
| 254 | turnEntryAngle(0.0), |
| 255 | turnExitAngle(0.0), |
| 256 | turnRadius(0.0), |
| 257 | legCourseTrue(0.0), |
| 258 | pathDistanceM(0.0), |
| 259 | turnPathDistanceM(0.0), |
| 260 | overflightCompensationAngle(0.0), |
| 261 | flyOver(w->flag(WPT_OVERFLIGHT)) |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | void initPass0() |
| 266 | { |
| 267 | const std::string& ty(wpt->type()); |
| 268 | if (wpt->flag(WPT_DYNAMIC)) { |
| 269 | // presumption is that we always overfly such a waypoint |
| 270 | if (ty == "hdgToAlt") { |
| 271 | flyOver = true; |
| 272 | } |
| 273 | } else if (ty == "discontinuity") { |
| 274 | |
| 275 | } else { |
| 276 | pos = wpt->position(); |
| 277 | posValid = true; |
| 278 | } // of static waypt |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * test if course of this leg can be adjusted or is contrained to an exact value |
| 283 | */ |
| 284 | bool isCourseConstrained() const |
| 285 | { |
| 286 | const std::string& ty(wpt->type()); |
| 287 | return (ty == "hdgToAlt") || (ty == "radialIntercept") || (ty == "dmeIntercept"); |
| 288 | } |
| 289 | |
| 290 | // compute leg courses for all static legs (both ends are fixed) |
| 291 | void initPass1(const WayptData* previous, WayptData* next) |
| 292 | { |
| 293 | const auto ty = wpt->type(); |
| 294 | if ((ty == "vectors") || (ty == "discontinuity")) { |
| 295 | // relying on the fact vectors will be followed by a static fix/wpt |
| 296 | if (next && next->posValid) { |
| 297 | posValid = true; |
| 298 | pos = next->pos; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | const bool previousPosValid = previous && previous->posValid; |
| 303 | if (ty == "via") { |
| 304 | // even though both ends may be known, we don't |
| 305 | // want to compute a leg course for a VIA |
| 306 | } else if (posValid && !legCourseValid && previousPosValid) { |
| 307 | const auto prevWP = previous->wpt; |
| 308 | // check for duplicate points now |
| 309 | if (prevWP->matches(wpt)) { |
| 310 | skipped = true; |
| 311 | } |
| 312 | |
| 313 | // we can compute leg course now |
| 314 | if (prevWP->type() == "runway") { |
| 315 | // use the runway departure end pos |
| 316 | FGRunway* rwy = static_cast<RunwayWaypt*>(prevWP.get())->runway(); |
| 317 | legCourseTrue = SGGeodesy::courseDeg(rwy->end(), pos); |
| 318 | } else if (ty == "discontinuity") { |
| 319 | // we want to keep the path across DISCONs sharp (no turn entry/exit) |
| 320 | // so we keep the leg course invalid, even though we actually |
| 321 | // could compute it |
| 322 | } else if (ty != "runway") { |
| 323 | // need to wait to compute runway leg course |
| 324 | legCourseTrue = SGGeodesy::courseDeg(previous->pos, pos); |
| 325 | legCourseValid = true; |
| 326 | } |
| 327 | } // of not a VIA |
| 328 | } |
| 329 | |
| 330 | void computeLegCourse(const WayptData* previous, double radiusM) |
| 331 | { |
| 332 | if (legCourseValid) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if ((wpt->type() == "via") || (wpt->type() == "discontinuty")) |
| 337 | { |
| 338 | // do nothing, we can't compute a valid leg course for these types |
| 339 | // we'll generate sharp turns in the path but that's no problem. |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | if (!previous || !previous->posValid) { |
| 344 | // all the cases below assume previous pas exists and has a valid position |
| 345 | SG_LOG(SG_NAVAID, SG_WARN, "RoutePath: Asked to compute leg course, but, previous is invalid"); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (wpt->type() == "runway") { |
| 350 | FGRunway* rwy = static_cast<RunwayWaypt*>(wpt.get())->runway(); |
| 351 | flyOver = true; |
| 352 | |
| 353 | TurnInfo ti = turnCenterAndAngleFromExit(rwy->threshold(), |
| 354 | rwy->headingDeg(), |
| 355 | radiusM, previous->pos); |
| 356 | if (ti.valid) { |
| 357 | legCourseTrue = ti.inboundCourseDeg; |
| 358 | turnEntryAngle = ti.turnAngleDeg; |
| 359 | turnEntryCenter = ti.turnCenter; |
| 360 | turnRadius = radiusM; |
| 361 | hasEntry = true; |
| 362 | turnEntryPos = pointOnEntryTurnFromHeading(ti.inboundCourseDeg); |
| 363 | } else { |
| 364 | // couldn't compute entry, never mind |
| 365 | legCourseTrue = SGGeodesy::courseDeg(previous->pos, rwy->threshold()); |
| 366 | } |
| 367 | legCourseValid = true; |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | SGGeod previousPos = previous->pos; |
| 372 | // use correct location for runway, otherwise use threshold |
| 373 | // and get weird courses |
| 374 | if (previous->wpt->type() == "runway") { |
| 375 | FGRunway* rwy = static_cast<RunwayWaypt*>(previous->wpt.get())->runway(); |
| 376 | previousPos = rwy->end(); |
| 377 | } |
| 378 | |
| 379 | if (posValid) { |
| 380 | legCourseTrue = SGGeodesy::courseDeg(previousPos, pos); |
| 381 | legCourseValid = true; |
| 382 | } else if (isCourseConstrained()) { |
| 383 | double magVar = magVarFor(previousPos); |
| 384 | legCourseTrue = wpt->headingRadialDeg() + magVar; |
| 385 | legCourseValid = true; |
| 386 | } // of pos not valid |
| 387 | } |
| 388 | |
| 389 | SGGeod pointOnEntryTurnFromHeading(double headingDeg) const |
| 390 | { |
| 391 | assert(hasEntry); |
| 392 | double p = copysign(90.0, turnEntryAngle); |
| 393 | return SGGeodesy::direct(turnEntryCenter, headingDeg - p, turnRadius); |
| 394 | } |
| 395 | |
| 396 | SGGeod pointOnExitTurnFromHeading(double headingDeg) const |
| 397 | { |
| 398 | double p = copysign(90.0, turnExitAngle); |
| 399 | return SGGeodesy::direct(turnExitCenter, headingDeg - p, turnRadius); |
| 400 | } |
| 401 | |
| 402 | double pathDistanceForTurnAngle(double angleDeg) const |
| 403 | { |
| 404 | return turnRadius * fabs(angleDeg) * SG_DEGREES_TO_RADIANS; |
| 405 | } |
| 406 | |
| 407 | void computeTurn(double radiusM, bool constrainLegCourse, double maxFlyByTurnAngleDeg, WayptData& next) |
| 408 | { |
| 409 | assert(!skipped); |
| 410 | assert(next.legCourseValid); |
| 411 | bool isRunway = (wpt->type() == "runway"); |
| 412 | |
| 413 | if (legCourseValid) { |
| 414 | if (isRunway) { |
| 415 | FGRunway* rwy = static_cast<RunwayWaypt*>(wpt.get())->runway(); |
| 416 | turnExitAngle = next.legCourseTrue - rwy->headingDeg(); |
| 417 | } else { |
| 418 | turnExitAngle = next.legCourseTrue - legCourseTrue; |
| 419 | } |
| 420 | } else { |
| 421 | // happens for first leg |
| 422 | if (isRunway) { |
| 423 | legCourseValid = true; |
| 424 | FGRunway* rwy = static_cast<RunwayWaypt*>(wpt.get())->runway(); |
| 425 | turnExitAngle = next.legCourseTrue - rwy->headingDeg(); |
| 426 | legCourseTrue = rwy->headingDeg(); |
| 427 | flyOver = true; |
| 428 | // fall through |
| 429 | } else { |
| 430 | legCourseValid = true; |
| 431 | legCourseTrue = next.legCourseTrue; |
| 432 | turnExitAngle = 0.0; |
| 433 | turnExitPos = pos; |
| 434 | flyOver = true; |
| 435 | return; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | SG_NORMALIZE_RANGE(turnExitAngle, -180.0, 180.0); |
| 440 | turnRadius = radiusM; |
| 441 | |
| 442 | if (!flyOver && fabs(turnExitAngle) > maxFlyByTurnAngleDeg) { |
| 443 | // flyBy logic blows up for sharp turns - due to the tan() term |
| 444 | // heading towards infinity. By converting to flyOver we do something |
| 445 | // closer to what was requested. This matches logic in the RNAV |
| 446 | // Leg controller |
| 447 | flyOver = true; |
| 448 | } |
| 449 | |
| 450 | if (flyOver) { |
| 451 | if (isRunway) { |
| 452 | FGRunway* rwy = static_cast<RunwayWaypt*>(wpt.get())->runway(); |
| 453 | turnExitCenter = turnCenterOverflight(rwy->end(), rwy->headingDeg(), turnExitAngle, turnRadius); |
| 454 | } else { |
| 455 | turnEntryPos = pos; |
| 456 | turnExitCenter = turnCenterOverflight(pos, legCourseTrue, turnExitAngle, turnRadius); |
| 457 | } |
| 458 | turnExitPos = pointOnExitTurnFromHeading(next.legCourseTrue); |
| 459 | |
| 460 | if (!next.wpt->flag(WPT_DYNAMIC)) { |
| 461 | // distance perpendicular to next leg course, after turning |
| 462 | // through turnAngle |
| 463 | double xtk = turnRadius * (1 - cos(turnExitAngle * SG_DEGREES_TO_RADIANS)); |
| 464 | |
| 465 | if (constrainLegCourse || next.isCourseConstrained()) { |
| 466 | // next leg course is constrained. We need to swing back onto the |
| 467 | // desired course, using a compensation turn |
| 468 | |
| 469 | // compensation angle to turn back on course |
| 470 | double theta = acos((turnRadius - (xtk * 0.5)) / turnRadius) * SG_RADIANS_TO_DEGREES; |
| 471 | theta = copysign(theta, turnExitAngle); |
| 472 | turnExitAngle += theta; |
| 473 | |
| 474 | double p = copysign(90, turnExitAngle); |
| 475 | const double offsetAngle = legCourseTrue + turnExitAngle - p; |
| 476 | SGGeod tc2 = SGGeodesy::direct(turnExitCenter, |
| 477 | offsetAngle, |
| 478 | turnRadius * 2.0); |
| 479 | |
| 480 | |
| 481 | turnExitPos = SGGeodesy::direct(tc2, next.legCourseTrue + p , turnRadius); |
| 482 | overflightCompensationAngle = -theta; |
| 483 | |
| 484 | // sign of angles will differ, so compute distances seperately |
| 485 | turnPathDistanceM = pathDistanceForTurnAngle(turnExitAngle) + |
| 486 | pathDistanceForTurnAngle(overflightCompensationAngle); |
| 487 | } else { |
| 488 | // next leg course can be adjusted. increase the turn angle |
| 489 | // and modify the next leg's course accordingly. |
| 490 | |
| 491 | // hypotenuse of triangle, opposite edge has length turnRadius |
| 492 | double distAlongPath = std::min(1.0, sin(fabs(turnExitAngle) * SG_DEGREES_TO_RADIANS)) * turnRadius; |
| 493 | double nextLegDistance = SGGeodesy::distanceM(pos, next.pos) - distAlongPath; |
| 494 | double increaseAngle = atan2(xtk, nextLegDistance) * SG_RADIANS_TO_DEGREES; |
| 495 | increaseAngle = copysign(increaseAngle, turnExitAngle); |
| 496 | |
| 497 | turnExitAngle += increaseAngle; |
| 498 | turnExitPos = pointOnExitTurnFromHeading(legCourseTrue + turnExitAngle); |
| 499 | // modify next leg course |
| 500 | next.legCourseTrue = SGGeodesy::courseDeg(turnExitPos, next.pos); |
| 501 | turnPathDistanceM = pathDistanceForTurnAngle(turnExitAngle); |
| 502 | } // of next leg isn't course constrained |
| 503 | } else { |
| 504 | // next point is dynamic |
| 505 | // no compensation needed |
| 506 | turnPathDistanceM = pathDistanceForTurnAngle(turnExitAngle); |
| 507 | } |
| 508 | } else { |
| 509 | hasEntry = true; |
| 510 | turnEntryCenter = turnCenterFlyBy(pos, legCourseTrue, turnExitAngle, turnRadius); |
| 511 | |
| 512 | turnExitAngle = turnExitAngle * 0.5; |
| 513 | turnEntryAngle = turnExitAngle; |
| 514 | turnExitCenter = turnEntryCenter; // important that these match |
| 515 | |
| 516 | turnEntryPos = pointOnEntryTurnFromHeading(legCourseTrue); |
| 517 | turnExitPos = pointOnExitTurnFromHeading(next.legCourseTrue); |
| 518 | turnPathDistanceM = pathDistanceForTurnAngle(turnEntryAngle); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | double turnDistanceM() const |
| 523 | { |
| 524 | return turnPathDistanceM; |
| 525 | } |
| 526 | |
| 527 | void turnEntryPath(SGGeodVec& path) const |
| 528 | { |
| 529 | if (!hasEntry || fabs(turnEntryAngle) < 0.5 ) { |
| 530 | path.push_back(pos); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | int steps = std::max(SGMiscd::roundToInt(fabs(turnEntryAngle) / 3.0), 1); |
| 535 | double stepIncrement = turnEntryAngle / steps; |
| 536 | double h = legCourseTrue; |
| 537 | for (int s=0; s<steps; ++s) { |
| 538 | path.push_back(pointOnEntryTurnFromHeading(h)); |
| 539 | h += stepIncrement; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void turnExitPath(SGGeodVec& path) const |
| 544 | { |
| 545 | if (fabs(turnExitAngle) < 0.5) { |
| 546 | path.push_back(turnExitPos); |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | int steps = std::max(SGMiscd::roundToInt(fabs(turnExitAngle) / 3.0), 1); |
| 551 | double stepIncrement = turnExitAngle / steps; |
| 552 | |
| 553 | // initial exit heading |
| 554 | double h = legCourseTrue + (flyOver ? 0.0 : turnEntryAngle); |
| 555 | if (wpt->type() == "runway") { |
| 556 | FGRunway* rwy = static_cast<RunwayWaypt*>(wpt.get())->runway(); |
| 557 | h = rwy->headingDeg(); |
| 558 | } |
| 559 | |
| 560 | for (int s=0; s<steps; ++s) { |
| 561 | path.push_back(pointOnExitTurnFromHeading(h)); |
| 562 | h += stepIncrement; |
| 563 | } |
| 564 | |
| 565 | // we can use an exact check on the compensation angle, because we |
| 566 | // initialise it directly. Depending on the next element we might be |
| 567 | // doing compensation or adjusting the next leg's course; if we did |
| 568 | // adjust the course everything 'just works' above. |
| 569 | |
| 570 | if (flyOver && (overflightCompensationAngle != 0.0)) { |
| 571 | // skew by compensation angle back |
| 572 | steps = std::max(SGMiscd::roundToInt(fabs(overflightCompensationAngle) / 3.0), 1); |
| 573 | |
| 574 | // step in opposite direction to the turn angle to swing back onto |
| 575 | // the next leg course |
| 576 | stepIncrement = overflightCompensationAngle / steps; |
| 577 | |
| 578 | SGGeod p = path.back(); |
| 579 | double stepDist = (fabs(stepIncrement) / 360.0) * SGMiscd::twopi() * turnRadius; |
| 580 | |
| 581 | for (int s=0; s<steps; ++s) { |
| 582 | h += stepIncrement; |
| 583 | p = SGGeodesy::direct(p, h, stepDist); |
| 584 | path.push_back(p); |
| 585 | |
| 586 | } |
| 587 | } // of overflight compensation turn |
| 588 | } |
| 589 | |
| 590 | SGGeod pointAlongExitPath(double distanceM) const |
| 591 | { |
| 592 | double basicTurnDistance = pathDistanceForTurnAngle(turnExitAngle); |
| 593 | |
| 594 | if (distanceM > basicTurnDistance) { |
| 595 | assert(flyOver); |
| 596 | assert(overflightCompensationAngle != 0.0); |
| 597 | double p = copysign(90, turnExitAngle); |
| 598 | |
| 599 | double distanceAlongCompensationTurn = distanceM - basicTurnDistance; |
| 600 | double theta = (distanceAlongCompensationTurn / turnRadius) * SG_RADIANS_TO_DEGREES; |
| 601 | |
| 602 | // compute the compensation turn center - twice the turn radius |
| 603 | // from turnCenter |
| 604 | SGGeod tc2 = SGGeodesy::direct(turnExitCenter, |
| 605 | legCourseTrue + turnExitAngle - p, |
| 606 | turnRadius * 2.0); |
| 607 | |
| 608 | theta = copysign(theta, overflightCompensationAngle); |
| 609 | return SGGeodesy::direct(tc2, |
| 610 | legCourseTrue + turnExitAngle + theta + p, turnRadius); |
| 611 | } |
| 612 | |
| 613 | double theta = (distanceM / turnRadius) * SG_RADIANS_TO_DEGREES; |
| 614 | theta = copysign(theta, turnExitAngle); |
| 615 | double inboundCourse = legCourseTrue + (flyOver ? 0.0 : turnExitAngle); |
| 616 | return pointOnExitTurnFromHeading(inboundCourse + theta); |
| 617 | } |
| 618 | |
| 619 | SGGeod pointAlongEntryPath(double distanceM) const |
| 620 | { |
| 621 | assert(hasEntry); |
| 622 | double theta = (distanceM / turnRadius) * SG_RADIANS_TO_DEGREES; |
| 623 | theta = copysign(theta, turnEntryAngle); |
| 624 | return pointOnEntryTurnFromHeading(legCourseTrue + theta); |
| 625 | } |
| 626 | |
| 627 | WayptRef wpt; |
| 628 | bool hasEntry, posValid, legCourseValid, skipped; |
| 629 | SGGeod pos, turnEntryPos, turnExitPos, turnEntryCenter, turnExitCenter; |
| 630 | double turnEntryAngle, turnExitAngle, turnRadius, legCourseTrue; |
| 631 | double pathDistanceM; |
| 632 | double turnPathDistanceM; // for flyBy, this is half the distance; for flyOver it's the complete distance |
| 633 | double overflightCompensationAngle; |
| 634 | bool flyOver; |
| 635 | }; |
| 636 | |
| 637 | bool isDescentWaypoint(const WayptRef& wpt) |
| 638 | { |
| 639 | return (wpt->flag(WPT_APPROACH) && !wpt->flag(WPT_MISS)) || wpt->flag(WPT_ARRIVAL); |
| 640 | } |
| 641 | |
| 642 | class RoutePath::RoutePathPrivate |
| 643 | { |
| 644 | public: |
| 645 | WayptDataVec waypoints; |
| 646 | |
| 647 | AircraftPerformance perf; |
| 648 | bool constrainLegCourses; |
| 649 | double maxFlyByTurnAngleDeg = 90.0; |
| 650 | |
| 651 | void computeDynamicPosition(int index) |
| 652 | { |
| 653 | auto previous(previousValidWaypoint(index)); |
| 654 | if ((previous == waypoints.end()) || !previous->posValid) { |
| 655 | SG_LOG(SG_NAVAID, SG_WARN, "couldn't compute position for dynamic waypoint: no preceeding valid waypoint"); |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | WayptRef wpt = waypoints[index].wpt; |
| 660 | |
| 661 | const std::string& ty(wpt->type()); |
| 662 | if (ty == "hdgToAlt") { |
| 663 | HeadingToAltitude* h = (HeadingToAltitude*)wpt.get(); |
| 664 | |
| 665 | double altFt = computeVNAVAltitudeFt(index - 1); |
| 666 | double distanceM = perf.distanceNmBetween(altFt, h->altitudeFt()) * SG_NM_TO_METER; |
| 667 | double hdg = h->headingDegMagnetic() + magVarFor(previous->pos); |
| 668 | waypoints[index].pos = SGGeodesy::direct(previous->turnExitPos, hdg, distanceM); |
| 669 | waypoints[index].posValid = true; |
| 670 | } else if (ty == "radialIntercept") { |
| 671 | // start from previous.turnExit |
| 672 | RadialIntercept* i = (RadialIntercept*)wpt.get(); |
| 673 | |
| 674 | SGGeoc prevGc = SGGeoc::fromGeod(previous->turnExitPos); |
| 675 | SGGeoc navid = SGGeoc::fromGeod(wpt->position()); |
| 676 | SGGeoc rGc; |
| 677 | double magVar = magVarFor(previous->pos); |
| 678 | |
| 679 | double radial = i->radialDegMagnetic() + magVar; |
| 680 | double track = i->courseDegMagnetic() + magVar; |
| 681 | bool ok = geocRadialIntersection(prevGc, track, navid, radial, rGc); |
| 682 | if (!ok) { |
| 683 | // try pulling backward along the radial in case we're too close. |
| 684 | // suggests bad procedure construction if this is happening! |
| 685 | SGGeoc navidAdjusted = SGGeodesy::advanceDegM(navid, radial, -10 * SG_NM_TO_METER); |
| 686 | |
| 687 | // try again |
| 688 | ok = geocRadialIntersection(prevGc, track, navidAdjusted, radial, rGc); |
| 689 | if (!ok) { |
| 690 | SG_LOG(SG_NAVAID, SG_WARN, "couldn't compute interception for radial:" << previous->turnExitPos << " / " << track << "/" << wpt->position() << "/" << radial); |
| 691 | waypoints[index].pos = wpt->position(); // horrible fallback |
| 692 | |
| 693 | } else { |
| 694 | waypoints[index].pos = SGGeod::fromGeoc(rGc); |
| 695 | } |
| 696 | } else { |
| 697 | waypoints[index].pos = SGGeod::fromGeoc(rGc); |
| 698 | } |
| 699 | |
| 700 | waypoints[index].posValid = true; |
| 701 | } else if (ty == "dmeIntercept") { |
| 702 | DMEIntercept* di = (DMEIntercept*)wpt.get(); |
| 703 | |
| 704 | SGGeoc prevGc = SGGeoc::fromGeod(previous->turnExitPos); |
| 705 | SGGeoc navid = SGGeoc::fromGeod(wpt->position()); |
| 706 | double distRad = di->dmeDistanceNm() * SG_NM_TO_RAD; |
| 707 | SGGeoc rGc; |
| 708 | |
| 709 | double crs = di->courseDegMagnetic() + magVarFor(wpt->position()); |
| 710 | SGGeoc bPt = SGGeodesy::advanceDegM(prevGc, crs, 1e5); |
| 711 | |
| 712 | double dNm = pointsKnownDistanceFromGC(prevGc, bPt, navid, distRad); |
| 713 | if (dNm < 0.0) { |
| 714 | SG_LOG(SG_NAVAID, SG_WARN, "dmeIntercept failed"); |
| 715 | waypoints[index].pos = wpt->position(); // horrible fallback |
| 716 | } else { |
| 717 | waypoints[index].pos = SGGeodesy::direct(previous->turnExitPos, crs, dNm * SG_NM_TO_METER); |
| 718 | } |
| 719 | |
| 720 | waypoints[index].posValid = true; |
| 721 | } else if (ty == "vectors") { |
| 722 | waypoints[index].legCourseTrue = SGGeodesy::courseDeg(previous->turnExitPos, waypoints[index].pos); |
| 723 | waypoints[index].legCourseValid = true; |
| 724 | // no turn data |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | double computeVNAVAltitudeFt(int index) |
| 729 | { |
| 730 | WayptRef w = waypoints[index].wpt; |
| 731 | if ((w->flag(WPT_APPROACH) && !w->flag(WPT_MISS)) || w->flag(WPT_ARRIVAL)) { |
| 732 | // descent |
| 733 | int next = findNextKnownAltitude(index); |
| 734 | if (next < 0) { |
| 735 | return 0.0; |
| 736 | } |
| 737 | |
| 738 | double fixedAlt = altitudeForIndex(next); |
| 739 | double distanceM = distanceBetweenIndices(index, next); |
| 740 | return perf.computePreviousAltitude(distanceM, fixedAlt); |
| 741 | } else { |
| 742 | // climb |
| 743 | int prev = findPreceedingKnownAltitude(index); |
| 744 | if (prev < 0) { |
| 745 | return 0.0; |
| 746 | } |
| 747 | |
| 748 | double fixedAlt = altitudeForIndex(prev); |
| 749 | double distanceM = distanceBetweenIndices(prev, index); |
| 750 | return perf.computeNextAltitude(distanceM, fixedAlt); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | int findPreceedingKnownAltitude(int index) const |
| 755 | { |
| 756 | const WayptData& w(waypoints[index]); |
| 757 | if (w.wpt->altitudeRestriction() == RESTRICT_AT) { |
| 758 | return index; |
| 759 | } |
| 760 | |
| 761 | // principal base case is runways. |
| 762 | const std::string& ty(w.wpt->type()); |
| 763 | if (ty == "runway") { |
| 764 | return index; // runway always has a known elevation |
| 765 | } |
| 766 | |
| 767 | if (index == 0) { |
| 768 | SG_LOG(SG_NAVAID, SG_WARN, "findPreceedingKnownAltitude: no preceding altitude value found"); |
| 769 | return -1; |
| 770 | } |
| 771 | |
| 772 | // recurse earlier in the route |
| 773 | return findPreceedingKnownAltitude(index - 1); |
| 774 | } |
| 775 | |
| 776 | int findNextKnownAltitude(unsigned int index) const |
| 777 | { |
| 778 | if (index >= waypoints.size()) { |
| 779 | SG_LOG(SG_NAVAID, SG_WARN, "findNextKnownAltitude: no next altitude value found"); |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | const WayptData& w(waypoints[index]); |
| 784 | if (w.wpt->altitudeRestriction() == RESTRICT_AT) { |
| 785 | return index; |
| 786 | } |
| 787 | |
| 788 | // principal base case is runways. |
| 789 | const std::string& ty(w.wpt->type()); |
| 790 | if (ty == "runway") { |
| 791 | return index; // runway always has a known elevation |
| 792 | } |
| 793 | |
| 794 | if (index == waypoints.size() - 1) { |
| 795 | SG_LOG(SG_NAVAID, SG_WARN, "findNextKnownAltitude: no next altitude value found"); |
| 796 | return -1; |
| 797 | } |
| 798 | |
| 799 | return findNextKnownAltitude(index + 1); |
| 800 | } |
| 801 | |
| 802 | double altitudeForIndex(int index) const |
| 803 | { |
| 804 | const WayptData& w(waypoints[index]); |
| 805 | if (w.wpt->altitudeRestriction() != RESTRICT_NONE) { |
| 806 | return w.wpt->altitudeFt(); |
| 807 | } |
| 808 | |
| 809 | const std::string& ty(w.wpt->type()); |
| 810 | if (ty == "runway") { |
| 811 | FGRunway* rwy = static_cast<RunwayWaypt*>(w.wpt.get())->runway(); |
| 812 | return rwy->threshold().getElevationFt(); |
| 813 | } |
| 814 | |
| 815 | SG_LOG(SG_NAVAID, SG_WARN, "altitudeForIndex: waypoint has no explicit altitude"); |
| 816 | return 0.0; |
| 817 | } |
| 818 | |
| 819 | double distanceBetweenIndices(int from, int to) const |
| 820 | { |
| 821 | double total = 0.0; |
| 822 | |
| 823 | for (int i=from+1; i<= to; ++i) { |
| 824 | total += waypoints[i].pathDistanceM; |
| 825 | } |
| 826 | |
| 827 | return total; |
| 828 | } |
| 829 | |
| 830 | WayptDataVec::iterator previousValidWaypoint(unsigned int index) |
| 831 | { |
| 832 | do { |
| 833 | if (index == 0) { |
| 834 | return waypoints.end(); |
| 835 | } |
| 836 | |
| 837 | --index; |
| 838 | } while (waypoints.at(index).skipped || (waypoints.at(index).wpt->type() == "discontinuity")); |
| 839 | |
| 840 | return waypoints.begin() + index; |
| 841 | } |
| 842 | |
| 843 | WayptDataVec::iterator previousValidWaypoint(WayptDataVec::iterator it) |
| 844 | { |
| 845 | return previousValidWaypoint(std::distance(waypoints.begin(), it)); |
| 846 | } |
| 847 | |
| 848 | |
| 849 | WayptDataVec::iterator nextValidWaypoint(int index) |
| 850 | { |
| 851 | return nextValidWaypoint(waypoints.begin() + index); |
| 852 | } |
| 853 | |
| 854 | WayptDataVec::iterator nextValidWaypoint(WayptDataVec::iterator it) |
| 855 | { |
| 856 | if (it == waypoints.end()) { |
| 857 | return it; |
| 858 | } |
| 859 | |
| 860 | ++it; |
| 861 | while ((it != waypoints.end()) && (it->skipped || (it->wpt->type() == "discontinuity"))) { |
| 862 | ++it; |
| 863 | } |
| 864 | return it; |
| 865 | } |
| 866 | }; // of RoutePathPrivate class |
| 867 | |
| 868 | RoutePath::RoutePath(const flightgear::FlightPlan* fp) : |
| 869 | d(new RoutePathPrivate) |
| 870 | { |
| 871 | for (int l=0; l<fp->numLegs(); ++l) { |
| 872 | WayptRef wpt = fp->legAtIndex(l)->waypoint(); |
| 873 | if (!wpt) { |
| 874 | SG_LOG(SG_NAVAID, SG_DEV_ALERT, "Waypoint " << l << " of " << fp->numLegs() << "is NULL"); |
| 875 | break; |
| 876 | } |
| 877 | d->waypoints.push_back(WayptData(wpt)); |
| 878 | } |
| 879 | |
| 880 | d->constrainLegCourses = fp->followLegTrackToFixes(); |
| 881 | commonInit(); |
| 882 | } |
| 883 | |
| 884 | |
| 885 | RoutePath::RoutePath(const RoutePath& other) |
| 886 | { |
| 887 | d.reset(new RoutePathPrivate(*other.d)); |
| 888 | } |
| 889 | |
| 890 | RoutePath& RoutePath::operator=(const RoutePath& other) |
| 891 | { |
| 892 | d.reset(new RoutePathPrivate(*other.d)); |
| 893 | return *this; |
| 894 | } |
| 895 | |
| 896 | RoutePath::~RoutePath() |
| 897 | { |
| 898 | } |
| 899 | |
| 900 | void RoutePath::commonInit() |
| 901 | { |
| 902 | for (auto& w : d->waypoints) { |
| 903 | w.initPass0(); |
| 904 | } |
| 905 | |
| 906 | for (unsigned int i=1; i<d->waypoints.size(); ++i) { |
| 907 | WayptData* nextPtr = ((i + 1) < d->waypoints.size()) ? &d->waypoints[i+1] : nullptr; |
| 908 | auto prev = d->previousValidWaypoint(i); |
| 909 | WayptData* prevPtr = (prev == d->waypoints.end()) ? nullptr : &(*prev); |
| 910 | d->waypoints[i].initPass1(prevPtr, nextPtr); |
| 911 | } |
| 912 | |
| 913 | for (unsigned int i=0; i<d->waypoints.size(); ++i) { |
| 914 | if (d->waypoints[i].skipped) { |
| 915 | continue; |
| 916 | } |
| 917 | |
| 918 | double alt = 0.0; // FIXME |
| 919 | double radiusM = d->perf.turnRadiusMForAltitude(alt); |
| 920 | |
| 921 | if (i > 0) { |
| 922 | auto prevIt = d->previousValidWaypoint(i); |
| 923 | WayptData* prevPtr = (prevIt == d->waypoints.end()) ? nullptr : &(*prevIt); |
| 924 | |
| 925 | d->waypoints[i].computeLegCourse(prevPtr, radiusM); |
| 926 | d->computeDynamicPosition(i); |
| 927 | } |
| 928 | |
| 929 | auto nextIt = d->nextValidWaypoint(i); |
| 930 | if (nextIt != d->waypoints.end()) { |
| 931 | nextIt->computeLegCourse(&(d->waypoints[i]), radiusM); |
| 932 | |
| 933 | if (nextIt->legCourseValid) { |
| 934 | d->waypoints[i].computeTurn(radiusM, d->constrainLegCourses, d->maxFlyByTurnAngleDeg, *nextIt); |
| 935 | } else { |
| 936 | // next waypoint has indeterminate course. Let's create a sharp turn |
| 937 | // this can happen when the following point is ATC vectors, for example. |
| 938 | d->waypoints[i].turnEntryPos = d->waypoints[i].pos; |
| 939 | d->waypoints[i].turnExitPos = d->waypoints[i].pos; |
| 940 | } |
| 941 | } else { |
| 942 | // final waypt, fix up some data |
| 943 | d->waypoints[i].turnExitPos = d->waypoints[i].pos; |
| 944 | d->waypoints[i].turnEntryPos = d->waypoints[i].pos; |
| 945 | } |
| 946 | |
| 947 | // now turn is computed, can resolve distances |
| 948 | d->waypoints[i].pathDistanceM = computeDistanceForIndex(i); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | SGGeodVec RoutePath::pathForIndex(int index) const |
| 953 | { |
| 954 | if ((index < 0) || (index >= static_cast<int>(d->waypoints.size()))) { |
| 955 | return {}; |
| 956 | } |
| 957 | |
| 958 | const WayptData& w(d->waypoints[index]); |
| 959 | const std::string& ty(w.wpt->type()); |
| 960 | SGGeodVec r; |
| 961 | |
| 962 | if (d->waypoints[index].skipped) { |
| 963 | return {}; |
| 964 | } |
| 965 | |
| 966 | // don't show any path |
| 967 | if (w.wpt->flag(WPT_HIDDEN)) { |
| 968 | return {}; |
| 969 | } |
| 970 | |
| 971 | // also hide the path if the prev WP is hidden |
| 972 | auto prevIt = d->previousValidWaypoint(index); |
| 973 | if (prevIt != d->waypoints.end()) { |
| 974 | if (prevIt->wpt->flag(WPT_HIDDEN)) { |
| 975 | return {}; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | if (ty == "vectors") { |
| 980 | // ideally we'd show a stippled line to connect the route? |
| 981 | return {}; |
| 982 | } |
| 983 | |
| 984 | if (ty == "discontinuity") { |
| 985 | return {}; // no points for a discontinuity of course |
| 986 | } |
| 987 | |
| 988 | if (ty == "via") { |
| 989 | return pathForVia(static_cast<Via*>(d->waypoints[index].wpt.get()), index); |
| 990 | } |
| 991 | |
| 992 | |
| 993 | if (prevIt != d->waypoints.end()) { |
| 994 | prevIt->turnExitPath(r); |
| 995 | |
| 996 | SGGeod from = prevIt->turnExitPos, |
| 997 | to = w.turnEntryPos; |
| 998 | |
| 999 | // compute rounding offset, we want to round towards the direction of travel |
| 1000 | // which depends on the east/west sign of the longitude change |
| 1001 | double lonDelta = to.getLongitudeDeg() - from.getLongitudeDeg(); |
| 1002 | if (fabs(lonDelta) > 0.5) { |
| 1003 | interpolateGreatCircle(from, to, r); |
| 1004 | } |
| 1005 | } // of have previous waypoint |
| 1006 | |
| 1007 | w.turnEntryPath(r); |
| 1008 | |
| 1009 | // hold is the normal leg and then the hold waypoints as well |
| 1010 | if (ty== "hold") { |
| 1011 | const auto h = static_cast<Hold*>(d->waypoints[index].wpt.get()); |
| 1012 | const auto holdPath = pathForHold(h); |
| 1013 | r.insert(r.end(), holdPath.begin(), holdPath.end()); |
| 1014 | } |
| 1015 | |
| 1016 | if (ty == "runway") { |
| 1017 | // runways get an extra point, at the end. this is particularly |
| 1018 | // important so missed approach segments draw correctly |
| 1019 | FGRunway* rwy = static_cast<RunwayWaypt*>(w.wpt.get())->runway(); |
| 1020 | r.push_back(rwy->end()); |
| 1021 | } |
| 1022 | |
| 1023 | return r; |
| 1024 | } |
| 1025 | |
| 1026 | void RoutePath::interpolateGreatCircle(const SGGeod& aFrom, const SGGeod& aTo, SGGeodVec& r) const |
| 1027 | { |
| 1028 | SGGeoc gcFrom = SGGeoc::fromGeod(aFrom), |
| 1029 | gcTo = SGGeoc::fromGeod(aTo); |
| 1030 | |
| 1031 | double lonDelta = gcTo.getLongitudeRad() - gcFrom.getLongitudeRad(); |
| 1032 | if (fabs(lonDelta) < 1e-3) { |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | lonDelta = SGMiscd::normalizeAngle(lonDelta); |
| 1037 | int steps = static_cast<int>(fabs(lonDelta) * SG_RADIANS_TO_DEGREES * 2); |
| 1038 | double lonStep = (lonDelta / steps); |
| 1039 | |
| 1040 | double lon = gcFrom.getLongitudeRad() + lonStep; |
| 1041 | for (int s=0; s < (steps - 1); ++s) { |
| 1042 | lon = SGMiscd::normalizeAngle(lon); |
| 1043 | double lat = latitudeForGCLongitude(gcFrom, gcTo, lon); |
| 1044 | r.push_back(SGGeod::fromGeoc(SGGeoc::fromRadM(lon, lat, SGGeodesy::EQURAD))); |
| 1045 | //SG_LOG(SG_GENERAL, SG_INFO, "lon:" << lon * SG_RADIANS_TO_DEGREES << " gives lat " << lat * SG_RADIANS_TO_DEGREES); |
| 1046 | lon += lonStep; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | SGGeod RoutePath::positionForIndex(int index) const |
| 1051 | { |
| 1052 | if ((index < 0) || (index >= static_cast<int>(d->waypoints.size()))) { |
| 1053 | return {}; |
| 1054 | } |
| 1055 | |
| 1056 | return d->waypoints[index].pos; |
| 1057 | } |
| 1058 | |
| 1059 | SGGeodVec RoutePath::pathForVia(Via* via, int index) const |
| 1060 | { |
| 1061 | // previous waypoint must be valid for a VIA |
| 1062 | auto prevIt = d->previousValidWaypoint(index); |
| 1063 | if (prevIt == d->waypoints.end()) { |
| 1064 | return SGGeodVec(); |
| 1065 | } |
| 1066 | |
| 1067 | WayptVec enrouteWaypoints = via->expandToWaypoints(prevIt->wpt); |
| 1068 | SGGeodVec r; |
| 1069 | |
| 1070 | WayptVec::const_iterator it; |
| 1071 | SGGeod legStart = prevIt->wpt->position(); |
| 1072 | for (it = enrouteWaypoints.begin(); it != enrouteWaypoints.end(); ++it) { |
| 1073 | // interpolate directly into the result vector |
| 1074 | interpolateGreatCircle(legStart, (*it)->position(), r); |
| 1075 | legStart = (*it)->position(); |
| 1076 | } |
| 1077 | |
| 1078 | return r; |
| 1079 | } |
| 1080 | |
| 1081 | SGGeodVec RoutePath::pathForHold(Hold* hold) const |
| 1082 | { |
| 1083 | int turnSteps = 16; |
| 1084 | double hdg = hold->inboundRadial(); |
| 1085 | double turnDelta = 180.0 / turnSteps; |
| 1086 | double altFt = 0.0; // FIXME |
| 1087 | double gsKts = d->perf.groundSpeedForAltitudeKnots(altFt); |
| 1088 | |
| 1089 | SGGeodVec r; |
| 1090 | double az2; |
| 1091 | double stepTime = turnDelta / 3.0; // 3.0 is degrees/sec for standard rate turn |
| 1092 | double stepDist = gsKts * SG_KT_TO_MPS * stepTime; |
| 1093 | double legDist = hold->isDistance() ? |
| 1094 | hold->timeOrDistance() |
| 1095 | : gsKts * (hold->timeOrDistance() / 3600.0); |
| 1096 | legDist *= SG_NM_TO_METER; |
| 1097 | |
| 1098 | if (hold->isLeftHanded()) { |
| 1099 | turnDelta = -turnDelta; |
| 1100 | } |
| 1101 | SGGeod pos = hold->position(); |
| 1102 | r.push_back(pos); |
| 1103 | |
| 1104 | // turn+leg sides are a mirror |
| 1105 | for (int j=0; j < 2; ++j) { |
| 1106 | // turn |
| 1107 | for (int i=0;i<turnSteps; ++i) { |
| 1108 | hdg += turnDelta; |
| 1109 | SGGeodesy::direct(pos, hdg, stepDist, pos, az2); |
| 1110 | r.push_back(pos); |
| 1111 | } |
| 1112 | |
| 1113 | // leg |
| 1114 | SGGeodesy::direct(pos, hdg, legDist, pos, az2); |
| 1115 | r.push_back(pos); |
| 1116 | } // of leg+turn duplication |
| 1117 | |
| 1118 | return r; |
| 1119 | } |
| 1120 | |
| 1121 | double RoutePath::computeDistanceForIndex(int index) const |
| 1122 | { |
| 1123 | if ((index < 0) || (index >= static_cast<int>(d->waypoints.size()))) { |
| 1124 | return 0.0; |
| 1125 | } |
| 1126 | |
| 1127 | auto it = d->waypoints.begin() + index; |
| 1128 | if ((index == 0) || it->skipped) { |
| 1129 | // first waypoint, distance is 0 |
| 1130 | return 0.0; |
| 1131 | } |
| 1132 | |
| 1133 | const auto ty = it->wpt->type(); |
| 1134 | if (ty == "via") { |
| 1135 | return distanceForVia(static_cast<Via*>(it->wpt.get()), index); |
| 1136 | } |
| 1137 | |
| 1138 | if (ty == "discontinuity") { |
| 1139 | return 0.0; |
| 1140 | } |
| 1141 | |
| 1142 | auto prevIt = d->previousValidWaypoint(index); |
| 1143 | if (prevIt == d->waypoints.end()) { |
| 1144 | return 0.0; |
| 1145 | } |
| 1146 | |
| 1147 | double dist = SGGeodesy::distanceM(prevIt->turnExitPos, it->turnEntryPos); |
| 1148 | dist += prevIt->turnDistanceM(); |
| 1149 | |
| 1150 | if (!it->flyOver) { |
| 1151 | // add entry distance |
| 1152 | dist += it->turnDistanceM(); |
| 1153 | } |
| 1154 | |
| 1155 | return dist; |
| 1156 | } |
| 1157 | |
| 1158 | double RoutePath::distanceForVia(Via* via, int index) const |
| 1159 | { |
| 1160 | auto prevIt = d->previousValidWaypoint(index); |
| 1161 | if (prevIt == d->waypoints.end()) { |
| 1162 | return 0.0; |
| 1163 | } |
| 1164 | |
| 1165 | WayptVec enrouteWaypoints = via->expandToWaypoints(prevIt->wpt); |
| 1166 | double dist = 0.0; |
| 1167 | |
| 1168 | SGGeod legStart = prevIt->wpt->position(); |
| 1169 | for (auto wp : enrouteWaypoints) { |
| 1170 | dist += SGGeodesy::distanceM(legStart, wp->position()); |
| 1171 | legStart = wp->position(); |
| 1172 | } |
| 1173 | |
| 1174 | return dist; |
| 1175 | } |
| 1176 | |
| 1177 | double RoutePath::trackForIndex(int index) const |
| 1178 | { |
| 1179 | const auto sz = static_cast<int>(d->waypoints.size()); |
| 1180 | if ((index < 0) || (index >= sz)) { |
| 1181 | return 0.0; |
| 1182 | } |
| 1183 | |
| 1184 | if (d->waypoints[index].skipped) |
| 1185 | return trackForIndex(index - 1); |
| 1186 | |
| 1187 | const WayptData& wd(d->waypoints[index]); |
| 1188 | if (!wd.legCourseValid) |
| 1189 | return 0.0; |
| 1190 | |
| 1191 | return wd.legCourseTrue; |
| 1192 | } |
| 1193 | |
| 1194 | double RoutePath::distanceForIndex(int index) const |
| 1195 | { |
| 1196 | const auto sz = static_cast<int>(d->waypoints.size()); |
| 1197 | if ((index < 0) || (index >= sz)) { |
| 1198 | return 0.0; |
| 1199 | } |
| 1200 | |
| 1201 | return d->waypoints[index].pathDistanceM; |
| 1202 | } |
| 1203 | |
| 1204 | double RoutePath::distanceBetweenIndices(int from, int to) const |
| 1205 | { |
| 1206 | if (from < 0) { |
| 1207 | from = 0; |
| 1208 | } |
| 1209 | |
| 1210 | const auto sz = static_cast<int>(d->waypoints.size()); |
| 1211 | if (to >= sz) { |
| 1212 | to = sz - 1; |
| 1213 | } |
| 1214 | |
| 1215 | return d->distanceBetweenIndices(from, to); |
| 1216 | } |
| 1217 | |
| 1218 | SGGeod RoutePath::positionForDistanceFrom(int index, double distanceM) const |
| 1219 | { |
| 1220 | int sz = (int) d->waypoints.size(); |
| 1221 | if (index < 0) { |
| 1222 | index = sz - 1; // map negative values to end of the route |
| 1223 | } |
| 1224 | |
| 1225 | if ((index < 0) || (index >= sz)) { |
| 1226 | throw sg_range_exception("waypt index out of range", |
| 1227 | "RoutePath::positionForDistanceFrom"); |
| 1228 | } |
| 1229 | |
| 1230 | // find the actual leg we're within |
| 1231 | if (distanceM < 0.0) { |
| 1232 | // scan backwards |
| 1233 | while ((index > 0) && (distanceM < 0.0)) { |
| 1234 | // we are looking at index n, say 4, but with a negative distance. |
| 1235 | // we want to look at index n-1 (so, 3), and see if this makes |
| 1236 | // distance positive. We need to offset by distance from 3 -> 4, |
| 1237 | // which is waypoint 4's path distance. |
| 1238 | |
| 1239 | // note pathDistanceM is 0 for skipped waypoints, so this works out |
| 1240 | distanceM += d->waypoints[index].pathDistanceM; |
| 1241 | --index; |
| 1242 | } |
| 1243 | |
| 1244 | if (distanceM < 0.0) { |
| 1245 | // still negative, return route start |
| 1246 | return d->waypoints[0].pos; |
| 1247 | } |
| 1248 | |
| 1249 | } else { |
| 1250 | // scan forwards |
| 1251 | int nextIndex = index + 1; |
| 1252 | while ((nextIndex < sz) && (d->waypoints[nextIndex].pathDistanceM < distanceM)) { |
| 1253 | distanceM -= d->waypoints[nextIndex].pathDistanceM; |
| 1254 | index = nextIndex++; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | auto nextIt = d->nextValidWaypoint(index); |
| 1259 | if (nextIt == d->waypoints.end()) { |
| 1260 | // past route end, just return final position |
| 1261 | return d->waypoints[sz - 1].pos; |
| 1262 | } |
| 1263 | |
| 1264 | // this is important so we start from a valid WP if we're |
| 1265 | // working either side of a DISCON |
| 1266 | auto curIt = d->previousValidWaypoint(nextIt); |
| 1267 | if (curIt == d->waypoints.end()) { |
| 1268 | SG_LOG(SG_NAVAID, SG_WARN, "Couldn't find valid preceeding waypoint " << index); |
| 1269 | return nextIt->pos; |
| 1270 | } |
| 1271 | |
| 1272 | const WayptData& wpt = *curIt; |
| 1273 | const WayptData& next = *nextIt; |
| 1274 | |
| 1275 | if (next.wpt->type() == "via") { |
| 1276 | return positionAlongVia(static_cast<Via*>(next.wpt.get()), index, distanceM); |
| 1277 | } |
| 1278 | |
| 1279 | if (wpt.turnPathDistanceM > distanceM) { |
| 1280 | // on the exit path of current wpt |
| 1281 | return wpt.pointAlongExitPath(distanceM); |
| 1282 | } else { |
| 1283 | distanceM -= wpt.turnPathDistanceM; |
| 1284 | } |
| 1285 | |
| 1286 | double corePathDistance = next.pathDistanceM - next.turnPathDistanceM; |
| 1287 | if (next.hasEntry && (distanceM > corePathDistance)) { |
| 1288 | // on the entry path of next waypoint |
| 1289 | return next.pointAlongEntryPath(distanceM - corePathDistance); |
| 1290 | } |
| 1291 | |
| 1292 | // linear between turn exit and turn entry points |
| 1293 | return SGGeodesy::direct(wpt.turnExitPos, next.legCourseTrue, distanceM); |
| 1294 | } |
| 1295 | |
| 1296 | SGGeod RoutePath::positionAlongVia(Via* via, int previousIndex, double distanceM) const |
| 1297 | { |
| 1298 | SG_LOG(SG_NAVAID, SG_ALERT, "RoutePath::positionAlongVia not implemented"); |
| 1299 | return SGGeod(); |
| 1300 | } |