1#include "../Modules/Module.h"
2#include "../Rendering/FrameProfiler11.h"
3#include "../Viewscreen/GeometryBounds.h"
4#include <iostream>
5#include <memory>
6#include <chrono>
7#include <cstdlib>
8
20
22
23
24
25struct FixtureZone
26{
27 std::vector<GeometryBounds> obstacleBounds;
28 std::vector<float> obstacleTops;
29 std::vector<s_mesh_component> componentarray;
30 RiseMath::Vector3 terpos{};
31};
32class SceneBuildings
33{
34public:
35 std::vector<FixtureZone> buildzones;
36 void CollectTerrainObstacles(std::vector<TerrainAwareness::Obstacle>& result) const;
37};
41 uint64_t revision = 1;
44};
46{
47public:
53};
54#include "TerrainAwarenessScene.generated.h"
55
56static int checks = 0;
57static void Check(bool value, const char* message)
58{
59 ++checks;
60 if (!value) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
61}
63{
64 const double r = double(
radiusC)+height;
65 return {-x,-y,-float(std::sqrt(r*r-double(x)*x-double(y)*y))};
66}
67struct Fixture
68{
69 Logger logger;
70 std::unique_ptr<Bus> bus = std::make_unique<Bus>(&logger);
71 grid terrain;
72 SceneBuildings structures;
73 Viewscreen view{&terrain,&structures};
74 Gpws gpws{bus.get(),&logger,&view};
75 Fixture()
76 {
80
84 bus->RadioAltitudeKm = 1; bus->VerticalSpeedKms = 0; bus->WPtargetC = 0;
85 for (int u = 0; u < 64; ++u) for (int v = 0; v < 64; ++v)
86 {
88 cell.
tcp = {short(100+u),short(200+v)};
89 cell.pri_landform = 0; cell.elevationAvailable = true;
90 cell.position =
Surface((u-31)*.5f,(v-31)*.5f);
91 }
93 }
95 {
96
97 for (
auto& column :
insidearray)
for (
auto& row : column)
for (
auto& p : row) p = {};
98 for (short u = 16; u < 48; ++u) for (short v = 16; v < 48; ++v)
100 ++terrain.revision;
101 }
102 void Structure(float x, float y, float top, float halfWidth = .01f, unsigned char type = 1)
103 {
104 FixtureZone zone; GeometryBounds bounds;
106 bounds.
Include(RiseMath::Vector3(-halfWidth,-halfWidth,-top));
107 bounds.
Include(RiseMath::Vector3(halfWidth,halfWidth,0));
108 zone.obstacleBounds.push_back(bounds); zone.obstacleTops.push_back(top);
110 zone.componentarray.push_back(component); structures.buildzones.push_back(zone);
111 }
113 void Approach(float distance, float height, float field = 0, float speed = .166f, float sink = -.012f)
114 {
118 bus->RadioAltitudeKm = height; bus->VerticalSpeedKms = sink; bus->HeadingTrackDegrees = 0;
123 bus->waypoint = {
target}; bus->WPtargetC = 0;
124 }
125};
126#include "GpwsScenePerformance.h"
127#include "TerrainAlertStabilitySceneTests.h"
128int main(
int argc,
char** argv)
129{
130 if (argc == 2 && std::string(argv[1]) == "--benchmark") { BenchmarkGpwsScene(); return 0; }
131 TerrainAlertStabilitySceneRegressions();
133 Fixture f; f.Step();
134 Check(f.bus->GPWS.terrain.enabled && f.bus->GPWS.terrain.terrainComplete,"actual scene adapter reports full flat corridor");
135 Check(f.bus->GPWS.terrain.Alert() == Severity::None,"safe flat flight is quiet");
136 f.Structure(0,2.5f,1.2f); f.Step();
138 Check(f.bus->GPWS.terrain.altitudeKm == 1.05f,"each scheduled turn refreshes terrain without an independent 0.2-second gate");
140 Check(f.bus->GPWS.terrain.obstacle == Severity::Warning && f.bus->GPWS.Alert ==
Bus::Gpws::TA_Obstacle_Warning,
"streamed tower ahead triggers obstacle warning using ground-relative velocity");
141 Check(f.bus->GPWS.terrain.obstacles.size() == 1,"individual obstacle reaches map");
142 const unsigned oldRevision = f.bus->GPWS.terrainRevision;
144 Check(f.bus->GPWS.terrainRevision != oldRevision,"vehicle change invalidates old speech even when the new hazard is identical");
145 f.structures.buildzones.clear(); f.Structure(3,2,1.2f); f.Step();
146 Check(f.bus->GPWS.terrain.obstacle == Severity::None,"off-track structure stays quiet");
147 f.structures.buildzones.clear(); f.Structure(0,2,.001f,.5f); f.Step();
148 Check(f.bus->GPWS.terrain.obstacles.empty(),"ground-level pavement is not an obstacle");
149 f.structures.buildzones.clear(); f.Structure(0,2,1.2f,.1f,8); f.Step();
150 Check(f.bus->GPWS.terrain.obstacles.empty(),"non-solid crop components excluded");
151 f.structures.buildzones.clear();
153 f.Step(); Check(!f.bus->GPWS.terrain.obstaclesComplete,"unreceived colony layout is unknown");
155 for (int u = 29; u <= 34; ++u) for (int v = 36; v <= 37; ++v)
157 f.Inside(); f.Step();
158 Check(f.bus->GPWS.terrain.terrain == Severity::Warning,"surface triangles ahead detect a ridge");
159 Check(f.bus->GPWS.terrain.cells[31*63+36].severity == Severity::Warning,"the specific hazardous cell is highlighted");
160 for (
int u = 29; u <= 34; ++u)
gridarray[u][36].elevationAvailable =
false;
161 f.Step(); Check(!f.bus->GPWS.terrain.terrainComplete,"missing received elevation does not count as safe");
162 f.terrain.dataReady = false; f.Step();
163 Check(f.bus->GPWS.terrain.enabled && !f.bus->GPWS.terrain.terrainComplete && f.bus->GPWS.terrain.Alert() == Severity::None,"loading clears threats and shows unavailable coverage");
165 Check(!f.bus->GPWS.terrain.enabled && f.bus->GPWS.terrain.obstacles.empty(),"simulator admission clears production scene");
167 Check(f.bus->GPWS.terrain.enabled,"real-world scene reacquired after simulator");
168 for (
int u = 29; u <= 34; ++u)
gridarray[u][36].elevationAvailable =
true;
169 ++f.terrain.revision; f.terrain.geometryReady = false; f.Step();
170 Check(f.bus->GPWS.terrain.terrain == Severity::Warning,"GPU rebuild retains known hazards despite geometryReady being false");
171 f.terrain.geometryReady = true;
173 Check(f.bus->GPWS.terrain.enabled && f.bus->GPWS.terrain.Alert() == Severity::None,"hover retains map without invented forward collision");
174 const auto start = std::chrono::steady_clock::now();
175 for (int i = 0; i < 5; ++i) f.Step();
176 const auto elapsed = std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-start).count()/5;
177 std::cout << "PASS: " << checks << " production scene checks; " << elapsed << " ms per hover scan\n";
179 for (int i = 0; i < 200; ++i) f.Structure((i%20)*.1f-1,(i/20)*.1f+2,.2f);
180 const auto flightStart = std::chrono::steady_clock::now();
181 for (int i = 0; i < 5; ++i) f.Step();
182 std::cout << "200-building flight scan: " <<
183 std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-flightStart).count()/5 << " ms\n";
184
185 {
186 Fixture distant;
188 distant.Step();
189 Check(distant.bus->GPWS.terrain.terrainComplete,"coarse outer cells complete a corridor beyond the inner patch");
190 Check(distant.bus->GPWS.terrain.cells[31*63+60].valid,"outer elevation is valid without fine subdivisions");
191 for (int u = 29; u <= 34; ++u) for (int v = 54; v <= 55; ++v)
193 ++distant.terrain.revision; distant.terrain.geometryReady = false;
194 distant.gpws.FrameMove(.01f);
195 Check(distant.bus->GPWS.terrain.terrain == Severity::Warning,"high-speed 60-second / 8-nm warning scans new distant ridge is scanned immediately before graphics rebuild");
196 for (
int u = 0; u < 64; ++u)
for (
int v = 0; v < 64; ++v) ++
gridarray[u][v].tcp.u;
197 ++distant.terrain.revision; distant.gpws.FrameMove(.01f);
198 Check(distant.bus->GPWS.terrain.cells[31*63+54].u ==
gridarray[31][54].
tcp.
u &&
199 distant.bus->GPWS.terrain.cells[31*63+54].severity == Severity::Warning,
200 "changed grid identities rescan on the next module turn");
201 for (
int u = 0; u < 64; ++u)
gridarray[u][63].elevationAvailable =
false;
202 ++distant.terrain.revision; distant.gpws.FrameMove(.01f);
203 Check(distant.bus->GPWS.terrain.cells[31*63+54].valid && !distant.bus->GPWS.terrain.cells[31*63+62].valid,
204 "missing streamed row invalidates only dependent cells");
205 }
206 {
207 Fixture departure;
208 for (int u = 29; u <= 34; ++u) for (int v = 36; v <= 37; ++v)
210 departure.Inside(); departure.Structure(0,2,1.2f); departure.Step();
211 Check(departure.bus->GPWS.terrain.Alert() == Severity::Warning,"departure fixture has a mountain and tower on flight path");
214 departure.bus->RadioAltitudeKm = .01f;
215
216 departure.gpws.FrameMove(.01f);
217 const auto& state = departure.bus->GPWS.terrain;
218 Check(state.enabled && state.onGround && state.Alert() == Severity::None && !state.obstacles.empty(),
219 "ground transition retains terrain and building data but clears predictive threats immediately");
221 state.gearDown,state.cells[31*63+36].severity,true,state.displayRunwayKm,state.onGround) == 0xffb03030,
222 "mountain remains relative red while aircraft is on the ground");
223 Check(!departure.bus->GPWS.IsTerrainAlert(),"taxi speed cannot trigger airborne predictive warnings");
225 Check(!state.onGround && state.Alert() == Severity::Warning,"liftoff immediately re-enables the actual threat scan");
226 departure.view.ptrBuildings = nullptr; departure.Step();
227 Check(!state.obstaclesComplete,"missing building provider reports incomplete data without dereferencing null");
228 }
229 {
230 Fixture fine;
233 ++fine.terrain.revision; fine.terrain.geometryReady = false; fine.Step();
234 Check(fine.bus->GPWS.terrain.cells[31*63+36].topKm > 2.02f,
235 "fresh fine interpolation peak is used instead of coarse corner maximum");
237 ++fine.terrain.revision; fine.Step();
238 const auto expected = std::max({GeometricHeight(
gridarray[16][36].position),
241 Check(std::fabs(fine.bus->GPWS.terrain.cells[16*63+36].topKm-expected) < .001f,
242 "fine/coarse seam follows renderer midpoints");
243 }
244 {
245 Fixture approach;
246 approach.bus->VerticalSpeedKms = -.018f; approach.Structure(0,4,1.1f);
247 approach.Step();
248 const auto& state = approach.bus->GPWS.terrain;
249 Check(state.terrain == Severity::Caution && state.obstacle == Severity::Caution && state.obstacleTime < state.terrainTime,
250 "colony obstacle is encountered before terrain farther along the descent");
252 "equal-severity terrain retains guide priority in both map label and audio");
253 }
254 {
255 Fixture approach;
256 approach.Approach(9.319f,.6201f,.1f);
257 approach.bus->WPtargetC = -1; approach.Step();
258 Check(approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.terrain == Severity::None,
259 "Lancaster-like descent recognizes a received aligned runway without selecting it");
260 approach.bus->WPtargetC = 0; approach.Step();
261 Check(approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.terrain == Severity::None,
262 "selected runway limits look-down at 9.3 km instead of extrapolating through the field");
266 Check(approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.Alert() == Severity::None,
267 "normal hand-flown approach with gear still up receives the same runway-aware ribbon");
268 approach.Structure(0,6,.1f); approach.Step();
269 Check(approach.bus->GPWS.terrain.obstacle == Severity::None,
270 "low buildings below the approach ribbon do not generate nuisance cautions");
271 approach.Structure(0,6,.4f); approach.Step();
272 Check(approach.bus->GPWS.terrain.obstacle == Severity::Warning && approach.bus->GPWS.terrain.terrain == Severity::None &&
274 "tower penetrating a high-speed approach gets extended obstacle warning and matching map label");
275 approach.structures.buildzones.clear();
276 for (int u = 30; u <= 32; ++u) for (int v = 36; v <= 37; ++v)
278 approach.Inside(); approach.Step();
279 Check(approach.bus->GPWS.terrain.terrain == Severity::Warning,
280 "intervening ridge still warns during a recognized runway approach");
281 }
282 {
283 Fixture approach;
284 for (float distance : {18.52f,9.319f,5.1f,4.9f,1.f,.1f,-.1f})
285 {
286 const bool final = distance <= 1;
287 approach.Approach(distance,(std::max)(distance,0.f)*.0525f+.01f,0,
288 final ? .05f : .166f,final ? -.001f : -.008715f);
289 approach.Step();
290 Check(approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.Alert() == Severity::None,
291 "normal approach stays quiet from ten nautical miles through flare and runway threshold");
292 }
293 approach.Structure(0,.1f,.04f); approach.Step();
294 Check(approach.bus->GPWS.terrain.obstacle == Severity::Warning,
295 "an obstruction on the runway is not exempted with the landing surface");
296 approach.Approach(-6.f,.01f,0,.05f,-.001f); approach.bus->waypoint[0].length = 8;
297 approach.Step();
298 Check(approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.obstacle == Severity::Warning,
299 "runway length beyond five km cannot truncate protection before the actual runway end");
300 }
301 {
302 Fixture approach;
303 const auto rejected = [&](const char* message) {
304 approach.Step(); Check(!approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.terrain != Severity::None,message);
305 };
306 approach.Approach(9.319f,.6201f,.1f); approach.bus->waypoint[0].type = 0;
307 rejected("an arbitrary navigation pin cannot suppress the descending terrain conflict");
308 approach.Approach(9.319f,.6201f,.1f); approach.bus->waypoint[0].heading = 180;
309 rejected("wrong runway direction is rejected even with LOC and GS modes set");
310 approach.Approach(9.319f,.6201f,.1f); approach.bus->waypoint[0].location = -
Surface(2,9.319f,.1f);
311 rejected("off-course approach cannot gain a landing allowance");
312 approach.Approach(9.319f,.3f,.1f);
313 rejected("premature descent below the runway approach path restores terrain protection");
314 approach.Approach(9.319f,.6201f,.1f,.166f,-.03f);
315 rejected("excessive sink toward a selected runway cannot gain a landing allowance");
316 approach.Approach(9.319f,.6201f,.1f); approach.bus->WPtargetC = -1;
317 approach.gpws.FrameMove(2.1f);
318 Check(approach.bus->GPWS.terrain.runwayApproach && approach.bus->GPWS.terrain.terrain == Severity::None,
319 "aligned received runway is inferred even without a selected waypoint");
320 approach.Approach(9.319f,.6201f,.1f,.166f,.01f); approach.Step();
321 Check(!approach.bus->GPWS.terrain.runwayApproach,
322 "go-around removes the expected-landing limit");
323 approach.Approach(9.319f,.6201f,.1f); approach.Step();
325 Check(!approach.bus->GPWS.terrain.runwayApproach && !approach.bus->GPWS.terrain.enabled,
326 "simulator entry clears the runway-aware prediction state");
327 }
328 {
329 Fixture elevated;
330 for (int u = 0; u < 64; ++u) for (int v = 0; v < 64; ++v)
331 {
332 const float y = (v-31)*.5f;
334 5.874f+.1f*(std::clamp)(y/9.319f,0.f,1.f));
335 }
336 elevated.Inside(); elevated.Approach(9.319f,6.4941f,5.974f);
337 elevated.bus->RadioAltitudeKm = .6201f;
338 elevated.bus->WPtargetC = -1; elevated.Step();
339 Check(elevated.bus->GPWS.terrain.runwayApproach && elevated.bus->GPWS.terrain.terrain == Severity::None,
340 "Lancaster runway is inferred without an explicitly selected waypoint");
341 elevated.bus->WPtargetC = 0; elevated.Step();
342 Check(elevated.bus->GPWS.terrain.runwayApproach && elevated.bus->GPWS.terrain.Alert() == Severity::None,
343 "runway-relative ribbon also handles Lancaster's 5974 m field elevation");
344 }
345 {
346 Fixture high;
348 Check(!high.bus->GPWS.terrain.enabled,"100 km does not announce unavailable terrain data");
350 Check(high.bus->GPWS.terrain.enabled,"rapid descent re-arms above 17 km before terrain is loaded");
351 high.bus->VerticalSpeedKms = 0; high.Step();
352 Check(!high.bus->GPWS.terrain.enabled,"level flight above the global obstacle bound suspends prediction");
354 Check(high.bus->GPWS.terrain.enabled,"descending through the level-flight cutoff restores data annunciation");
356 Check(high.bus->GPWS.terrain.enabled,"hysteresis prevents chatter around the suspension boundary");
358 Check(!high.bus->GPWS.terrain.enabled,"climb beyond the hysteresis band suspends prediction");
359 }
360 {
361 Fixture approach;
362
363
364 float distance = 18.52f;
365 while (distance > .1f)
366 {
367 const float speed = distance > 6 ? .166f : distance > 2 ? .09f : .05f;
368 approach.Approach(distance,distance*.052408f+.01f,0,speed,-speed*.052408f);
369 approach.gpws.FrameMove(1);
370 Check(!*approach.bus->GPWS.AlertText(),"normal continuous 3-degree approach has no reactive, TCF/RFCF or predictive hazard warning");
371 distance -= speed;
372 }
373 }
374 {
376 Check(high.bus->GPWS.terrain.enabled && !high.bus->GPWS.terrain.predictionEnabled &&
377 high.bus->GPWS.terrain.cells[31*63+31].valid,"known high-altitude Peaks data remains while predictive corridor is suspended");
378 high.bus->GPWS.positionValid = false; high.Step();
379 Check(!high.bus->GPWS.terrain.enabled && (high.bus->GPWS.faults&4),"position invalidity inhibits TAD with an explicit input fault");
380 }
381
382
383 for (float degrees : {0.f,45.f,90.f,135.f,180.f,270.f})
384 {
385 Fixture bounds;
388 int towers = 0;
389 for (int u : {0,15,16,30,46,47,61}) for (int v : {0,15,16,30,46,47,61})
390 {
391 const float x = (u-31)*.5f+.25f, y = (v-31)*.5f+.25f;
392 bounds.Structure(x,y,.001f,.02f);
393 bounds.Structure(x,y,.3f,.02f);
394 ++towers;
395 }
396 bounds.Step();
397 Check(bounds.bus->GPWS.terrain.obstacles.size() == size_t(towers),"rotated track and coarse/fine/edge rows retain all towers and reject pavement");
398 for (const auto& obstacle : bounds.bus->GPWS.terrain.obstacles)
399 Check(obstacle.topKm == .3f,"row rejection never leaves pavement classified as an obstacle");
400 }
401 std::cout << "PASS: " << checks << " total production scene checks\n";
402}
void UpdatePredictiveWindshear(float seconds)
void FrameMove(float fElapsedTime) override
void Log(const char *msg, Level level=Info, int errorCode=0)
void AddToCallStack(const char *msg)
RiseMath::Vector3 QuadraticVectorInterpolation(RiseMath::Vector3 *vel0, RiseMath::Vector3 *vel1, RiseMath::Vector3 *vel2, float t) const
std::unique_ptr< grid > ptrGrid
std::unique_ptr< buildings > ptrBuildings
RiseMath::Vector3 forwardVector
uint64_t ElevationRevision() const
void CalculateInsideArray(short t, short s) const
s_network_objects playerships[MAX_SCAN]
RiseMath::Vector3 insidearray[63][63][3]
LOCALGRID2 gridarray[64][64]
bool Inside(Vec p, const Triangle &t)
uint32_t Color(float terrainKm, float altitudeKm, float verticalKms, bool gearDown, Severity severity, bool known=true, float displayRunwayKm=-100, bool onGround=false)
GpwsPredictiveWindshear::State predictiveWindshear
void Include(const Vector &p)
RiseMath::Vector3 position
std::vector< FlapSetting > flapSettings
RiseMath::Vector3 barycentric
RiseMath::Vector3 position
RiseMath::Vector3 velocity