Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/NightVisionExposureTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Viewscreen/NightVisionExposure.h"
2#include "../Viewscreen/NightVisionGain.h"
3#include "../Viewscreen/CloudVolume.h"
4#include "../Viewscreen/CockpitLighting.h"
5#include <cmath>
6#include <cstdlib>
7#include <iostream>
8
9static void Check(bool condition, const char* message)
10{
11 if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
12}
13
14static bool Near(float left, float right)
15{
16 return std::fabs(left - right) < 0.0001f;
17}
18
19#include "CloudNightVisionTests.h"
20
21int main()
22{
23 TestCloudNightVision();
24 // The cockpit must retain the optical response when its ambient source
25 // changes from cloud scattering to sky. The cloud input is already gained.
26 const auto night=NightVisionGain::Calculate(true,false,.0025f);
27 Check(CockpitLighting::DisplayStrength(night)>.99f,"display optics use bounded night response");
29 "display optics fade toward daylight without a threshold pop");
30 const CloudVolume::Point redCabin{64.f/255,0,0};
31 const auto assistedCabin=CockpitLighting::LocalLight(redCabin,night);
32 Check(assistedCabin.x>redCabin.x && assistedCabin.x<=1 && assistedCabin.y==0 && assistedCabin.z==0,
33 "NVG enhances existing red cabin illumination without inventing other light channels");
34 for(const auto reset:{NightVisionGain::Calculate(false,false,.0025f),
35 NightVisionGain::Calculate(true,true,.0025f),NightVisionGain::Calculate(true,false,1)})
36 {
37 const auto local=CockpitLighting::LocalLight(redCabin,reset);
38 Check(local.x==redCabin.x && local.y==0 && local.z==0,"local light resets for equipment/camera, simulator and daylight");
39 Check(CockpitLighting::DisplayStrength(reset)==0,"display optics clear for equipment/camera, simulator and daylight");
40 }
41 const auto unpowered=CockpitLighting::LocalLight({0,0,0},night);
42 Check(unpowered.x==0 && unpowered.y==0 && unpowered.z==0,"unpowered cabin cannot acquire a synthetic light source");
43 const auto dimCabin=CockpitLighting::LocalLight({.0001f,0,0},night);
44 Check(dimCabin.x>0 && dimCabin.x<assistedCabin.x,"cabin power dimming still reduces the assisted signal");
45 const CloudVolume::Point cloud{CloudVolume::Saturate(.0025f*night.red),
46 CloudVolume::Saturate(.0025f*night.green),CloudVolume::Saturate(.0025f*night.blue)};
47 const CloudVolume::Point sky{.0015f,.002f,.0025f};
48 const auto below=CockpitLighting::Ambient(cloud,sky,-1,night);
49 Check(below.x==cloud.x && below.y==cloud.y && below.z==cloud.z,"below-deck cockpit light is not amplified twice");
50 const auto above=CockpitLighting::Ambient(cloud,sky,20,night);
51 Check(above.x>.2f && above.y>.3f && above.z>.4f,"night sky lights cockpit surfaces above the deck");
52 const auto bare=CloudVolume::VehicleAmbient(cloud,sky,20);
53 Check(above.y>bare.y*100,"above-deck cockpit does not fall back to unassisted sky light");
54 for(float height:{-10.f,-.5f,0.f,.25f,.5f,1.f,20.f,100.f,1.f,0.f,-.5f,-10.f})
55 {
56 const auto light=CockpitLighting::Ambient(cloud,sky,height,night);
57 Check(light.y>=above.y && light.y<=below.y,"ascent/descent blends continuously between enhanced light sources");
58 const auto off=CockpitLighting::Ambient(cloud,sky,height,NightVisionGain::Calculate(false,false,.0025f));
59 const auto normal=CloudVolume::VehicleAmbient(cloud,sky,height);
60 Check(off.x==normal.x && off.y==normal.y && off.z==normal.z,"unequipped/external view uses baseline sky fill");
61 const auto simulator=CockpitLighting::Ambient(cloud,sky,height,NightVisionGain::Calculate(true,true,.0025f));
62 Check(simulator.x==normal.x && simulator.y==normal.y && simulator.z==normal.z,"simulator does not acquire enhanced sky fill");
63 const auto day=CockpitLighting::Ambient(cloud,sky,height,NightVisionGain::Calculate(true,false,1));
64 Check(day.x==normal.x && day.y==normal.y && day.z==normal.z,"daylight cockpit stays unchanged");
65 const auto resumed=CockpitLighting::Ambient(cloud,sky,height,night);
66 Check(resumed.x==light.x && resumed.y==light.y && resumed.z==light.z,"camera/equipment/simulator round trip restores cockpit light immediately");
67 }
68 const auto zero=CockpitLighting::Ambient({0,0,0},{0,0,0},100,night);
69 Check(zero.x==0 && zero.y==0 && zero.z==0,"orbital darkness does not create a new light source");
70 const auto clipped=CockpitLighting::Ambient(cloud,{1,1,1},20,night);
71 Check(clipped.x==1 && clipped.y==1 && clipped.z==1,"surface fill is bounded before texture modulation");
72 const auto boundaryA=CockpitLighting::Ambient(cloud,sky,.2499f,night);
73 const auto boundaryB=CockpitLighting::Ambient(cloud,sky,.2501f,night);
74 Check(std::abs(boundaryA.y-boundaryB.y)<.001f,"cloud-top crossing does not introduce a lighting step");
75 std::cout << "PASS: cockpit NVG above/below deck, round trips, daylight, zero light, bounded fill and simulator baseline.\n";
76
77 // Use the actual cloud column and display attenuation across a round trip,
78 // including the low-sun ~.0025 input which previously hit the 24x limit.
79 CloudVolume::Parameters clouds; clouds.base=.6f;
80 for (float sun : {.02f,.05f,.10f,.20f,1.f})
81 {
82 float start=0;
83 for (float altitude : {.5f,1.2f,2.6f,5.f,11.f,.5f})
84 {
85 const float transmission=CloudVolume::DiffuseSunTransmission({0,clouds.radius+altitude,0},
86 {std::sqrt(1-sun*sun),sun,0},clouds);
87 const float incident=std::exp(-.5f*std::exp(-clouds.top/7.5f));
88 const float light=CloudVolume::ScatteredSunlight({incident,incident,incident},(std::max)(sun,.05f),transmission).x;
89 const auto response=NightVisionGain::Calculate(true,false,light);
90 const float amplified=(std::min)(1.f,light*response.green);
91 if (sun<=.20f && altitude<10) Check(amplified>.85f,"NVG low-sun cloud/ground brightness remains readable");
92 if (sun==1) Check(response.red==1 && response.green==1 && response.blue==1 && !response.clouds,"daylight gain is identity");
93 if (altitude==.5f && start==0) start=amplified;
94 else if (altitude==.5f) Check(amplified==start,"return below deck restores the same gain without re-equipping");
95 }
96 }
97 for (float luma : {0.f,1e-7f,.001f,.0025f,.01f,.04f,.1f,.3f,.35f,1.f})
98 {
99 auto response=NightVisionGain::Calculate(true,false,luma);
100 Check(std::isfinite(response.green) && response.green<=990.001f,"gain is finite at zero and subthreshold signals");
101 if (luma<1e-6f) Check(luma*response.green<.001f,"absent light is not replaced with a brightness floor");
102 response=NightVisionGain::Calculate(false,false,luma);
103 Check(response.red==1 && response.green==1 && response.blue==1 && !response.clouds,"unequipped/external camera clears enhanced gain");
104 response=NightVisionGain::Calculate(true,true,luma);
105 const float blend=std::clamp((.35f-luma)/.30f,0.f,1.f);
106 const float target=(std::min)(24.f,.9f/(std::max)(luma,.01f));
107 const float original=1.f+((std::max)(1.f,target)-1.f)*blend;
108 Check(!response.clouds && Near(response.green,original*(1.f+.10f*blend)),"simulator entry preserves original gain and suppresses enhanced clouds");
109 const auto resumed=NightVisionGain::Calculate(true,false,luma);
110 if (luma==.0025f) Check(resumed.green>response.green*10,"simulator exit recomputes real-flight gain");
111 }
112 std::cout << "PASS: NVG low-sun climb/descent, signal floor, daylight, equipment/camera and simulator transitions.\n";
113 for (int hz : {15, 30, 60, 144})
114 {
115 const float dt = 1.0f / hz;
116 float exposure = 0.0f, hold = 0.0f;
117 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
118 Check(Near(exposure, 1.0f) && Near(hold, 0.65f), "nighttime lightning still produces a strong flash");
119
120 // On leaving the clouds no lightning illuminates the scene. The old
121 // above-cloud animation may continue, but it must not feed this input.
122 for (int frame = 0; frame < hz * 5; ++frame)
123 NightVisionExposure::Update(true, 0.0f, 0.35f, dt, exposure, hold);
124 Check(exposure == 0.0f && hold == 0.0f, "cloud-top exit recovers without switching off NVG");
125 for (int frame = 0; frame < hz * 60; ++frame)
126 NightVisionExposure::Update(true, 0.0f, 0.02f, dt, exposure, hold);
127 Check(exposure == 0.0f && hold == 0.0f, "no phantom exposure during prolonged flight above clouds");
128
129 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
130 for (int frame = 0; frame < hz * 5; ++frame)
131 NightVisionExposure::Update(true, 0.10f, 0.02f, dt, exposure, hold);
132 Check(Near(exposure, 0.175f), "continuous weaker flashes cannot latch an earlier full whiteout");
133
134 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
135 for (int frame = 0; frame < hz * 5; ++frame)
136 NightVisionExposure::Update(true, 1.0f, 0.35f, dt, exposure, hold);
137 Check(Near(exposure, 0.15f), "daylight flashes cannot preserve nighttime saturation");
138 for (int frame = 0; frame < hz; ++frame)
139 NightVisionExposure::Update(true, 0.0f, 0.35f, dt, exposure, hold);
140 Check(exposure == 0.0f && hold == 0.0f, "daytime flash clears within a second");
141
142 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
143 for (int frame = 0; frame < hz; ++frame)
144 NightVisionExposure::Update(true, 0.0f, 0.02f, dt, exposure, hold);
145 Check(exposure > 0.8f && exposure < 0.95f, "nighttime recovery retains the gradual effect");
146 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
147 Check(Near(exposure, 1.0f) && Near(hold, 0.65f), "a new strong strike can retrigger saturation");
148
149 NightVisionExposure::Update(false, 1.0f, 0.02f, dt, exposure, hold);
150 Check(exposure == 0.0f && hold == 0.0f, "external camera or unequipping NVG immediately clears state");
151 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
152 const bool equipped=true, simulator=true;
153 NightVisionExposure::Update(equipped && !simulator, 1.0f, 0.02f, dt, exposure, hold);
154 Check(exposure == 0.0f && hold == 0.0f, "simulator entry clears an active real-flight afterimage even while equipped");
155 NightVisionExposure::Update(true, 0.0f, 0.02f, dt, exposure, hold);
156 Check(exposure == 0.0f && hold == 0.0f, "simulator exit cannot replay the cleared afterimage");
157 NightVisionExposure::Update(true, 0.03f, 0.02f, dt, exposure, hold);
158 Check(exposure == 0.0f && hold == 0.0f, "subthreshold flashes do not accumulate");
159
160 NightVisionExposure::Update(true, 1.0f, 0.02f, dt, exposure, hold);
161 NightVisionExposure::Update(true, 0.0f, 0.02f, 0.0f, exposure, hold);
162 Check(Near(exposure, 1.0f) && Near(hold, 0.65f), "zero elapsed time does not advance recovery");
163 NightVisionExposure::Update(true, 0.0f, 0.02f, 4.0f, exposure, hold);
164 Check(exposure == 0.0f && hold == 0.0f, "long frames consume both hold and fade time");
165 }
166 std::cout << "PASS: NVG flash recovery, cloud-top input cessation, weaker flashes, daylight sensitivity, and reset at 15/30/60/144 Hz.\n";
167}
Point VehicleAmbient(Point cloudLight, Point skyLight, float altitudeAboveTop)
Definition CloudVolume.h:84
float DiffuseSunTransmission(Point p, Point towardSun, const Parameters &params, uint32_t seed=DefaultSeed)
float Saturate(float x)
Definition CloudVolume.h:49
Point ScatteredSunlight(Point incident, float sunCosine, float transmission)
float DisplayStrength(const NightVisionGain::Response &vision)
CloudVolume::Point Ambient(CloudVolume::Point cloudLight, CloudVolume::Point skyLight, float altitudeAboveTop, const NightVisionGain::Response &vision)
CloudVolume::Point LocalLight(CloudVolume::Point light, const NightVisionGain::Response &vision)
void Update(bool active, float flashStimulus, float ambientLuminance, float elapsedSeconds, float &exposure, float &holdSeconds)
Response Calculate(bool active, bool simulator, float luminance)
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141