Rise
The Vieneo Province
Loading...
Searching...
No Matches
TerrainAwareness.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstdint>
7#include <vector>
8
9// Rise advisory approximation of Honeywell 060-4314-000 Rev C, pp. 29-38.
10// Distances are km, times seconds, heights geometric MSL. These are instrument
11// margins, not Earth aerodynamic constants or a model of escape performance.
12namespace TerrainAwareness
13{
14 constexpr float CautionSeconds = 60, WarningSeconds = 30;
15 constexpr float ClearanceKm = .1524f, MaxRangeKm = 60;
16 // Published Vieneo peak plus Jason's maximum structure height. The extra
17 // kilometre and 250 m hysteresis are Rise operational margins, not physics.
18 constexpr float HighestTerrainKm = 9.525f, TallestStructureKm = .915f;
19 constexpr float SuspensionMarginKm = 1.f, SuspensionHysteresisKm = .25f;
20 constexpr int GridSize = 63;
21 inline bool NeedsPrediction(float altitudeKm, float verticalKms, bool active)
22 {
23 if (!std::isfinite(altitudeKm) || !std::isfinite(verticalKms)) return true;
24 const float lowestKm = altitudeKm + (std::min)(verticalKms,0.f)*CautionSeconds;
26 (active ? SuspensionHysteresisKm : 0.f);
27 }
28 enum class Severity : uint8_t { None, Caution, Warning };
29 struct Point { float x = 0, y = 0, z = 0; };
30 inline Point Mix(Point a, Point b, float t)
31 { return {a.x + (b.x-a.x)*t, a.y + (b.y-a.y)*t, a.z + (b.z-a.z)*t}; }
32 inline float Cross(Point a, Point b, Point c)
33 { return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x); }
34 inline float Distance(Point a, Point b)
35 { return std::hypot(a.x-b.x, a.y-b.y); }
36 inline Severity Max(Severity a, Severity b) { return a > b ? a : b; }
37 inline bool Finite(Point p) { return std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z); }
38 inline bool Allowed(bool simulator, bool groundVehicle, bool airborne, float power)
39 { return !simulator && !groundVehicle && airborne && std::isfinite(power) && power >= .5f; }
40 inline bool DisplayAllowed(bool simulator, bool groundVehicle, float power)
41 { return !simulator && !groundVehicle && std::isfinite(power) && power >= .5f; }
42
52 struct Cell
53 {
54 int u = 0, v = 0; // Stored TCP identity survives grid shifts.
55 bool valid = false, obstaclesKnown = true;
56 float topKm = 0;
58 };
59 struct Obstacle
60 {
61 // Absolute render-space corners, used by both the predictor and MFD.
62 std::array<Point, 8> corners{};
63 float topKm = 0;
65 };
66 struct State
67 {
68 bool enabled = false, terrainComplete = false, obstaclesComplete = false;
69 bool predictionEnabled = false;
70 float altitudeKm = 0, verticalKms = 0;
71 bool gearDown = false, runwayApproach = false, onGround = false;
72 float displayRunwayKm = -100;
75 std::array<Cell, GridSize*GridSize> cells{};
76 std::vector<Obstacle> obstacles;
77 Severity Alert() const { return Max(terrain, obstacle); }
78 // Pilot Guide p.40: terrain precedes obstacle at equal severity.
79 // Predicted-time jitter must not reorder the voice or moving-map label.
80 bool ObstacleAlert() const { return obstacle > terrain; }
90 {
93 runwayApproach = false;
94 for (auto& cell : cells) cell.severity = Severity::None;
95 for (auto& item : obstacles) item.severity = Severity::None;
96 }
97 };
98
99 // Convex clipping preserves height interpolation and catches narrow ridges
100 // and towers between prediction steps. No center-point-only height samples.
101 struct Polygon
102 {
103 std::array<Point, 16> p{};
104 int size = 0;
105 template<class Plane> void Clip(Plane plane)
106 {
107 Polygon out;
108 for (int i = 0; i < size; ++i)
109 {
110 const Point a = p[i], b = p[(i+1)%size];
111 const float da = plane(a), db = plane(b);
112 if (da >= 0) out.p[out.size++] = a;
113 if ((da >= 0) != (db >= 0)) out.p[out.size++] = Mix(a,b,da/(da-db));
114 }
115 *this = out;
116 }
117 float Area() const
118 {
119 float area = 0;
120 for (int i = 1; i+1 < size; ++i) area += Cross(p[0],p[i],p[i+1]);
121 return std::fabs(area)*.5f;
122 }
123 };
124 struct Segment
125 {
127 float t0 = 0, t1 = 0, width0 = 0, width1 = 0, coveredArea = 0;
128 float dx = 0, dy = 1, length = 0;
129 Polygon Intersect(const Point (&triangle)[3]) const
130 {
131 Polygon p;
132 for (auto v : triangle)
133 {
134 const float x = v.x-start.x, y = v.y-start.y;
135 p.p[p.size++] = {x*dx+y*dy, x*dy-y*dx, v.z};
136 }
137 p.Clip([](Point v) { return v.x; });
138 p.Clip([&](Point v) { return length-v.x; });
139 p.Clip([&](Point v) { return width0+(width1-width0)*v.x/length-v.y; });
140 p.Clip([&](Point v) { return width0+(width1-width0)*v.x/length+v.y; });
141 return p;
142 }
143 };
144
146 {
148 float lowX = 0, highX = 0, lowY = 0, highY = 0;
149 public:
150 std::vector<Segment> segments;
151 bool rangeLimited = false;
152 explicit Predictor(Flight input) : flight(input)
153 {
154 // A stationary VTOL is still covered by reactive GPWS. The forward
155 // corridor is meaningful above 1 m/s; never invent a travel speed.
156 if (!std::isfinite(input.speedKms) || input.speedKms < .001f) return;
157 const float duration = (std::min)(CautionSeconds, MaxRangeKm/input.speedKms);
158 rangeLimited = duration < CautionSeconds;
159 const int steps = (std::min)(240, (std::max)(1, int(std::ceil((std::max)(duration,input.speedKms*duration/.25f)))));
160 const float dt = duration/steps;
161 // Continue the measured turn for ten seconds, then its tangent.
162 // This bounds extrapolation without deriving turn rate from Earth g.
163 const float rate = (std::clamp)(input.turnRadiansPerSecond,-.10472f,.10472f);
164 Point p{};
165 for (int i = 0; i < steps; ++i)
166 {
167 Segment s; s.start = p; s.t0 = i*dt; s.t1 = (i+1)*dt;
168 const float angle = rate*(std::min)((s.t0+s.t1)*.5f,10.f);
169 s.dx = std::sin(angle); s.dy = std::cos(angle); s.length = input.speedKms*dt;
170 p.x += s.dx*s.length; p.y += s.dy*s.length; s.end = p;
171 // Guide: 1/4 nm initial width, widening at 3 degrees each side.
172 s.width0 = .2315f + input.radiusKm + input.speedKms*s.t0*.052408f;
173 s.width1 = .2315f + input.radiusKm + input.speedKms*s.t1*.052408f;
174 const float reach = s.length+s.width1;
175 lowX = (std::min)(lowX,s.start.x-reach); highX = (std::max)(highX,s.start.x+reach);
176 lowY = (std::min)(lowY,s.start.y-reach); highY = (std::max)(highY,s.start.y+reach);
177 segments.push_back(s);
178 }
179 }
180 Severity Scan(const Point (&triangle)[3], bool obstacle, bool coverage = false, float* firstThreatTime = nullptr)
181 {
182 for (auto p : triangle) if (!Finite(p)) return Severity::None;
183 const float triLowX = (std::min)({triangle[0].x,triangle[1].x,triangle[2].x});
184 const float triHighX = (std::max)({triangle[0].x,triangle[1].x,triangle[2].x});
185 const float triLowY = (std::min)({triangle[0].y,triangle[1].y,triangle[2].y});
186 const float triHighY = (std::max)({triangle[0].y,triangle[1].y,triangle[2].y});
187 if (triLowX > highX || triHighX < lowX || triLowY > highY || triHighY < lowY)
188 return Severity::None;
189 Severity result = Severity::None;
190 for (auto& s : segments)
191 {
192 // Cheap conservative rejection before polygon clipping.
193 const float reach = s.length+s.width1;
194 if (triLowX > s.start.x+reach || triHighX < s.start.x-reach ||
195 triLowY > s.start.y+reach || triHighY < s.start.y-reach) continue;
196 Polygon p = s.Intersect(triangle);
197 if (coverage) s.coveredArea += p.Area();
198 p.Clip([&](Point v) {
199 const float t = s.t0+(s.t1-s.t0)*v.x/s.length;
201 });
202 // Guide p.29 limits look-down using height above the runway.
203 // Bound the descending ribbon by the inbound runway profile;
204 // do not extrapolate a normal approach through the landing field.
205 if (flight.landing)
206 {
208 {
209 const auto approachHeight = [&](Point v) {
210 const float worldY = s.start.y+s.dy*v.x-s.dx*v.y;
212 (1-worldY/(std::max)(flight.landingSite.y,.15f));
213 };
214 // Same clearance taper as low-altitude flight, applied
215 // along the runway-relative profile. Separate planes
216 // keep clipping exact where the clearance taper changes.
217 p.Clip([&](Point v) { return v.z-flight.landingSite.z-.003f; });
218 p.Clip([&](Point v) { return v.z-flight.landingSite.z-approachHeight(v)*.5f; });
219 p.Clip([&](Point v) { return v.z-flight.landingSite.z-approachHeight(v)+flight.clearanceKm; });
220 }
221 else
222 {
223 // Retain the existing close touchdown/pad allowance.
224 bool landingSurface = !obstacle;
225 if (obstacle)
226 landingSurface = Distance(triangle[0],flight.landingSite) < .15f &&
227 Distance(triangle[1],flight.landingSite) < .15f &&
228 Distance(triangle[2],flight.landingSite) < .15f;
229 if (landingSurface) p.Clip([&](Point v) { return v.z-flight.landingSite.z-.003f; });
230 }
231 // Stop extrapolating the descent beyond the landing site.
232 p.Clip([&](Point v) {
233 const float worldY = s.start.y+s.dy*v.x-s.dx*v.y;
234 return flight.landingSite.y+flight.landingLengthKm+.15f-worldY;
235 });
236 }
237 if (!p.size) continue;
238 if (firstThreatTime)
239 for (int i = 0; i < p.size; ++i)
240 *firstThreatTime = (std::min)(*firstThreatTime,s.t0+(s.t1-s.t0)*p.p[i].x/s.length);
241 result = Max(result,Severity::Caution);
242 p.Clip([&](Point v) { return flight.warningSeconds-(s.t0+(s.t1-s.t0)*v.x/s.length); });
243 if (p.size) result = Severity::Warning;
244 }
245 return result;
246 }
247 bool Covered() const
248 {
249 if (rangeLimited || segments.empty()) return false;
250 for (const auto& s : segments)
251 if (s.coveredArea+.00001f < s.length*(s.width0+s.width1)*.999f) return false;
252 return true;
253 }
254 bool Intersects(const Point (&triangle)[3]) const
255 {
256 for (const auto& s : segments) if (s.Intersect(triangle).Area() > 0) return true;
257 return false;
258 }
259 };
260
261 inline uint32_t Color(float terrainKm, float altitudeKm, float verticalKms,
262 bool gearDown, Severity severity, bool known = true, float displayRunwayKm = -100, bool onGround = false)
263 {
264 if (!known) return 0xffb030b0;
265 if (severity == Severity::Warning) return 0xffff2020;
266 if (severity == Severity::Caution) return 0xffffff00;
267 // p.32: declutter terrain within 400 ft of the nearest runway elevation.
268 // Threat highlighting always takes precedence over this display filter.
269 if (std::fabs(terrainKm-displayRunwayKm) <= .12192f || (onGround && terrainKm < altitudeKm)) return 0xff000000;
270 // Descent >1,000 ft/min previews thirty seconds of lost height (p.33).
271 if (verticalKms < -.00508f) altitudeKm += verticalKms*30;
272 const float height = terrainKm-altitudeKm;
273 if (height > .6096f) return 0xffb03030;
274 if (height > .3048f) return 0xffd8bb00;
275 if (height >= (gearDown ? -.0762f : -.1524f)) return 0xff8c8200;
276 if (height >= -.3048f) return 0xff168438;
277 if (height >= -.6096f) return 0xff104620;
278 return 0xff000000;
279 }
280}
@ Caution
Definition Message.h:8
@ Warning
Definition Message.h:7
@ None
Definition ServerTypes.h:97
Severity Scan(const Point(&triangle)[3], bool obstacle, bool coverage=false, float *firstThreatTime=nullptr)
bool Intersects(const Point(&triangle)[3]) const
std::vector< Segment > segments
char area[3]
Definition globals.cpp:87
constexpr float CautionSeconds
constexpr float WarningSeconds
constexpr int GridSize
constexpr float HighestTerrainKm
constexpr float SuspensionHysteresisKm
float Distance(Point a, Point b)
Severity Max(Severity a, Severity b)
uint32_t Color(float terrainKm, float altitudeKm, float verticalKms, bool gearDown, Severity severity, bool known=true, float displayRunwayKm=-100, bool onGround=false)
float Cross(Point a, Point b, Point c)
bool NeedsPrediction(float altitudeKm, float verticalKms, bool active)
constexpr float ClearanceKm
constexpr float MaxRangeKm
bool DisplayAllowed(bool simulator, bool groundVehicle, float power)
Point Mix(Point a, Point b, float t)
constexpr float SuspensionMarginKm
bool Finite(Point p)
bool Allowed(bool simulator, bool groundVehicle, bool airborne, float power)
constexpr float TallestStructureKm
std::array< Point, 8 > corners
std::array< Point, 16 > p
Polygon Intersect(const Point(&triangle)[3]) const
std::array< Cell, GridSize *GridSize > cells
std::vector< Obstacle > obstacles