diff --git a/src/PlugIn.cpp b/src/PlugIn.cpp index 3344fd6..c8803af 100644 --- a/src/PlugIn.cpp +++ b/src/PlugIn.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "com/ZmqContext.h" #include "PlugIn.h" @@ -199,33 +200,59 @@ void PlugIn::OnRadarTargetPositionUpdate(EuroScopePlugIn::CRadarTarget radarTarg break; } - if (2 <= flightPlan.GetExtractedRoute().GetPointsNumber()) { - auto currentPosition = radarTarget.GetPosition().GetPosition(); - int lastIdx = flightPlan.GetExtractedRoute().GetPointsNumber() - 2; - auto iafPosition = flightPlan.GetExtractedRoute().GetPointPosition(lastIdx); - auto iafName = flightPlan.GetExtractedRoute().GetPointName(lastIdx); + auto currentPosition = radarTarget.GetPosition().GetPosition(); + EuroScopePlugIn::CPosition iafPosition; + std::string iafName; + + for (auto element = this->SectorFileElementSelectFirst(EuroScopePlugIn::SECTOR_ELEMENT_STAR); + true == element.IsValid(); + element = this->SectorFileElementSelectNext(element, EuroScopePlugIn::SECTOR_ELEMENT_STAR)) + { + auto split = String::splitString(element.GetName(), " "); + + /* find the correct star */ + if (0 != split.size() && destination == split[0]) { + /* get the IAF */ + EuroScopePlugIn::CPosition position; + if (true == element.GetPosition(&position, 0)) { + /* match the waypoints to get the name*/ + for (int i = 0; i < flightPlan.GetExtractedRoute().GetPointsNumber(); ++i) { + if (1.0f >= flightPlan.GetExtractedRoute().GetPointPosition(i).DistanceTo(position)) { + iafPosition = flightPlan.GetExtractedRoute().GetPointPosition(i); + iafName = flightPlan.GetExtractedRoute().GetPointName(i); + report.set_initialapproachfix(iafName); + break; + } + } + } + } + + if (0 != iafName.length()) + break; + } + + if (0 != iafName.length()) { auto iafDistance = currentPosition.DistanceTo(iafPosition); /* calculate the distance to the IAF */ - double distanceToIaf = 0.0f, lastDistance = 100000.0f; - for (int i = 0; i < flightPlan.GetPositionPredictions().GetPointsNumber(); ++i) { + double distanceToIaf = 0.0f, lastIafHeading = 100000.0f; + for (int i = 1; i < flightPlan.GetPositionPredictions().GetPointsNumber(); ++i) { double distance = flightPlan.GetPositionPredictions().GetPosition(i).DistanceTo(currentPosition); + double headingDelta = std::abs(radarTarget.GetPosition().GetReportedHeading() - flightPlan.GetPositionPredictions().GetPosition(i).DirectionTo(iafPosition)); /* * 1. no direct way to IAF -> some direct given -> stop after lateral passing * 2. passed the IAF on a way to a direct */ - if (distance > lastDistance || iafDistance < distance) + if ((90.0 < headingDelta && 270.0 > headingDelta) || iafDistance < distance) break; distanceToIaf += distance; - lastDistance = distance; + currentPosition = flightPlan.GetPositionPredictions().GetPosition(i); } - if (0.0f == distanceToIaf) + if (0.01f >= std::abs(distanceToIaf)) distanceToIaf = iafDistance; report.set_distancetoiaf(static_cast(std::round(distanceToIaf))); - - report.set_initialapproachfix(std::string(iafName)); } report.set_destination(std::string(destination)); report.set_allocated_aircraft(aircraft);