-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflightplan.cpp
93 lines (82 loc) · 2.67 KB
/
flightplan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Created by Ian Parker on 24/11/2024.
//
#include "flightplan.h"
#include <cfloat>
#include "flightconverter.h"
using namespace std;
void FlightPlan::removeDuplicates()
{
bool changed = true;
while (changed)
{
changed = false;
for (auto it = m_waypoints.begin(); it != m_waypoints.end() && (it + 1) != m_waypoints.end(); ++it)
{
if (it->id == (it + 1)->id)
{
m_waypoints.erase(it + 1);
changed = true;
break;
}
}
}
}
void FlightPlan::updateWaypoints(std::shared_ptr<FlightConverter> flightConnector)
{
auto navData = flightConnector->getNavData();
for (auto& wp : m_waypoints)
{
printf("updateWaypoints: %s: %ls\n", wp.id.c_str(), wp.coordinate.toString().c_str());
auto navaids = navData->findById(wp.id);
bool validNavAid = false;
if (!navaids.empty())
{
shared_ptr<UFC::NavAid> nearestNavAid = nullptr;
double distance = DBL_MAX;
for (const auto& navaid : navaids)
{
printf("updateWaypoints: -> Found navaid: %s (%ls)\n", navaid->getId().c_str(), navaid->getLocation().toString().c_str());
double d = UFC::GeoUtils::distance(navaid->getLocation(), wp.coordinate);
if (d < distance)
{
distance = d;
nearestNavAid = navaid;
}
}
if (nearestNavAid && distance < 0.1f)
{
validNavAid = true;
}
}
else
{
printf("updateWaypoints: -> No navaids found!\n");
}
if (!validNavAid)
{
printf("updateWaypoints: Way point is invalid, searching for suitable replacement...\n");
auto nearest = navData->findNearest(wp.coordinate);
bool validReplacement = false;
if (nearest != nullptr)
{
auto distance = UFC::GeoUtils::distance(wp.coordinate, nearest->getLocation());
// This should probably depend on altitude
validReplacement = (distance < 2.0f);
}
if (validReplacement)
{
printf("updateWaypoints: %s: Replacing with %s\n", wp.id.c_str(), nearest->getId().c_str());
wp.id = nearest->getId();
wp.coordinate = nearest->getLocation();
wp.type = nearest->getType();
}
else
{
wp.type = UFC::NavAidType::WAY_POINT;
wp.via = "";
}
}
}
removeDuplicates();
}