Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/CloudVolumeCpuTests.h

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

1#pragma once
2#include <DirectXMath.h>
3#include "../Viewscreen/CloudBaseGeometry.h"
4
5// Shared by native and legacy fixtures; retain the established weather contracts.
6static void CpuTests()
7{
8 using namespace CloudVolume;
9 Parameters params;
10 float low=1,high=0;
11 for(int i=0;i<2000;++i)
12 {
13 const float x=float(i)*.043f;
14 Point p{x,params.radius+5.f,0};
15 const float d=Density(p,params);
16 low=(std::min)(low,d); high=(std::max)(high,d);
17 Check(d>=0 && d<=1,"bounded density");
18 Check(d==Density(p,params),"stationary density is deterministic");
19 p.x+=.001f;
20 Check(std::abs(d-Density(p,params))<.003f,"continuous density across spatial samples");
21 }
22 Check(high-low>.025f,"calm in-cloud density still varies with structure");
23 // Compare identical locations under weak/strong convection. A scalar lift
24 // of the same wrinkles would pass a height check but fail the relief test.
25 Parameters quiet=params,moderate=params,convective=params;
26 quiet.weather.current=ConstantWeather(25);
27 moderate.weather.current=ConstantWeather(153);
28 convective.weather.current=ConstantWeather(255);
29 double quietSlope=0,activeSlope=0,lift=0,moderateLift=0;
30 double quietBaseRelief=0,activeBaseRelief=0,quietBaseMean=0,activeBaseMean=0;
31 float quietLow=100,quietHigh=0,activeLow=100,activeHigh=0;
32 for(int z=0;z<32;++z) for(int x=0;x<32;++x)
33 {
34 const Point p{float(x*8),params.radius,float(z*8)};
35 const auto calm=Boundaries(p,quiet),active=Boundaries(p,convective);
36 const auto middle=Boundaries(p,moderate);
37 const Point next=Add(p,{.5f,0,0});
38 quietSlope+=std::abs(Boundaries(next,quiet).top-calm.top);
39 activeSlope+=std::abs(Boundaries(next,convective).top-active.top);
40 lift+=active.top-calm.top;
41 moderateLift+=middle.top-calm.top;
42 quietLow=(std::min)(quietLow,calm.top); quietHigh=(std::max)(quietHigh,calm.top);
43 activeLow=(std::min)(activeLow,active.top); activeHigh=(std::max)(activeHigh,active.top);
44 quietBaseRelief+=std::abs(calm.base-params.base);
45 activeBaseRelief+=std::abs(active.base-params.base);
46 quietBaseMean+=calm.base; activeBaseMean+=active.base;
47 Check(active.top>=calm.top && active.top<=params.top+MaxTopRiseKm,"active clusters rise within the marching shell");
48 Check(middle.top>=calm.top && middle.top<=active.top,"moderate convection develops between calm and strong profiles");
49 // Even a full calm-to-storm map update has no step as it interpolates.
50 Parameters evolving=quiet; evolving.weather.current=convective.weather.current;
51 evolving.weather.previous=quiet.weather.current;
52 float last=calm.top,lastBase=calm.base;
53 for(int frame=1;frame<=600;++frame)
54 {
55 evolving.weather.blend=Smooth(frame/600.f);
56 const auto evolvingColumn=Boundaries(p,evolving);
57 const float top=evolvingColumn.top;
58 Check(top>=last-.00001f && top-last<.13f,"weather blending grows formations continuously");
59 Check(std::abs(evolvingColumn.base-lastBase)<.005f,"weather transitions change the underside smoothly");
60 lastBase=evolvingColumn.base;
61 last=top;
62 }
63 }
64 std::cout<<"Calm/active top relief: "<<quietHigh-quietLow<<" / "<<activeHigh-activeLow
65 <<" km; mean storm lift "<<lift/1024<<" km; slope ratio "<<activeSlope/quietSlope<<std::endl;
66 Check(quietHigh-quietLow>.3f && quietHigh-quietLow<.65f,
67 "weak convection keeps shallow billows within a gently undulating stratus sheet");
68 Check(activeHigh-activeLow>1.f && lift/1024>1.f,"active convection forms broad raised clusters above the 10 km core");
69 std::cout<<"Strong convection top range: "<<activeLow<<" - "<<activeHigh<<" km MSL"<<std::endl;
70 Check(activeHigh>20 && activeHigh<22,"maximum convection supports 20-22 km storm peaks");
71 std::cout<<"60 percent convection mean lift: "<<moderateLift/1024<<" km"<<std::endl;
72 Check(moderateLift/1024>.7,"midrange live convection creates substantial growth too");
73 int towerSamples=0,clearSamples=0,highTowerSamples=0,highClearSamples=0;
74 for(int x=0;x<256;++x)
75 {
76 const Point radial=Scale({float(x),params.radius,37},1/Length({float(x),params.radius,37}));
77 for(const Parameters* field : {&quiet,&moderate,&convective})
78 Check(Density(Scale(radial,params.radius+10),*field)>.15f,"nominal 10 km level remains inside the stratus core");
79 const float d=Density(Scale(radial,params.radius+12),moderate);
80 towerSamples+=d>.1f; clearSamples+=d==0;
81 const float highDensity=Density(Scale(radial,params.radius+18),convective);
82 highTowerSamples+=highDensity>.1f; highClearSamples+=highDensity==0;
83 for(const Parameters* field : {&quiet,&moderate,&convective})
84 {
85 const auto before=Scale(radial,params.radius+9.999f),after=Scale(radial,params.radius+10.001f);
86 const float beforeLight=Transmittance(before,Scale(radial,params.radius+50),*field);
87 const float afterLight=Transmittance(after,Scale(radial,params.radius+50),*field);
88 Check(std::abs(beforeLight-afterLight)<.01f,"direct illumination cannot jump at the 10 km flight-mode boundary");
89 Check(std::abs(AmbientWeight(9.999f-Boundaries(before,*field).top)
90 -AmbientWeight(10.001f-Boundaries(after,*field).top))<.003f,
91 "scattered illumination stays continuous at the 10 km flight-mode boundary");
92 }
93 }
94 Check(towerSamples>5 && clearSamples>5,"at 12 km the aircraft can enter and leave moderate convective clusters");
95 Check(highTowerSamples>5 && highClearSamples>5,"at 18 km strong convection has both towers and clear gaps");
96 Check(activeSlope>quietSlope*2,"strong convection develops billows rather than only lifting a flat sheet");
97 std::cout<<"Calm/active base mean relief: "<<quietBaseRelief/1024<<" / "<<activeBaseRelief/1024<<" km"<<std::endl;
98 Check(activeBaseRelief>quietBaseRelief*2 && quietBaseRelief/1024<.03,
99 "weak-convection bases have less undulation than storm bases");
100 Check(std::abs(quietBaseMean/1024-params.base)<.025 && std::abs(activeBaseMean/1024-params.base)<.025,
101 "convection keeps mean base height centered on local ceiling");
102 Parameters beforeWrap=convective,afterWrap=convective;
103 beforeWrap.noiseOffset.x=3200.f; afterWrap.noiseOffset.x=-3200.f;
104 for(int x=0;x<128;++x)
105 {
106 const Point p{float(x),params.radius,37};
107 const auto before=Boundaries(p,beforeWrap),after=Boundaries(p,afterWrap);
108 Check(std::abs(before.top-after.top)<.005f && std::abs(before.base-after.base)<.005f,
109 "wind offset wrapping cannot move weather clusters or billows");
110 }
111 const auto regional=RegionalWeatherFixture();
112 params.weather.current=regional;
113 for(int i=0;i<128;++i)
114 {
115 Point p{float(i)*8.f,params.radius,37};
116 const auto column=Boundaries(p,params);
117 Check(column.top<=params.top+MaxTopRiseKm,"server convection towers stay inside marching shell");
118 Parameters moved=params; moved.noiseOffset={100,200,300};
119 Check(column.weather==Boundaries(p,moved).weather,"local wind cannot drift global weather twice");
120 }
121 Check(Density({0,params.radius+.2f,0},params)==0,"clear below layer");
122 Check(Density({0,params.radius+params.top+MaxTopRiseKm+.1f,0},params)==0,"clear above all towers");
123 Check(Density({0,params.radius+5,0},params,1)!=Density({0,params.radius+5,0},params,2),"seed changes formation");
124 Parameters storm=params; storm.weather.current=ConstantWeather(255);
125 for(int i=0;i<200;++i)
126 {
127 Point p{float(i),params.radius,0};
128 Check(Boundaries(p,storm).top>=Boundaries(p,params).top,"active weather raises tops");
129 }
130 for(float base : {.45f,1.8f,8.5f}) for(float activity : {0.f,1.f})
131 {
132 Parameters stratus=params; stratus.base=base; stratus.activity=activity;
133 for(int i=0;i<200;++i)
134 {
135 const auto column=Boundaries({float(i),params.radius,37},stratus);
136 Check(column.top<=params.top+MaxTopRiseKm,"minimum stratus thickness remains inside the marching shell");
137 }
138 }
139 // A cube edge has two TCP representations; both must sample the same field.
140 const auto a=CloudBaseGeometry::Position<DirectX::XMFLOAT3>(0,2038,1019,params.radius+5);
141 const auto b=CloudBaseGeometry::Position<DirectX::XMFLOAT3>(1,2038,1019,params.radius+5);
142 Check(std::abs(Density({a.x,a.y,a.z},params)-Density({b.x,b.y,b.z},params))<.001f,"TCP cube-edge continuity");
143 Check(AtmosphericVisibility(42.16f,0,0,10)==42.16f,"surface atmosphere retains weather visibility");
144 float previousVisibility=0;
145 for(int i=0;i<=1000;++i)
146 {
147 const float visibility=AtmosphericVisibility(42.16f,i*.01f,0,10);
148 Check(visibility>=previousVisibility && visibility<=ClearVisibilityKm,"visibility clears smoothly during ascent");
149 Check(visibility-previousVisibility<.2f || i==0,"visibility has no 10 km jump");
150 previousVisibility=visibility;
151 }
152 Check(AtmosphericVisibility(2,500,0,10)==ClearVisibilityKm,"clear atmosphere above the nominal deck");
153 Check(AtmosphericVisibility(2,14.5f,0,10)==168.64f,
154 "14.5 km uses the legacy clear-air override rather than the 42.16 km fallback");
155 Check(AtmosphericVisibility(2,1,1,10)==2,"elevated surface retains its visibility");
156 Check(ScreenOpacity(true,1,-1)==0 && ScreenOpacity(false,.7f,-1)==.7f,"volume replaces uniform tint while legacy keeps it");
157 Check(ScreenOpacity(true,1,1)==1,"explicit screen override is retained");
158 for(const Parameters* field : {&quiet,&moderate,&convective})
159 {
160 const float top=Boundaries({0,params.radius,0},*field).top;
161 float previous=-1,largestStep=0;
162 for(int i=-100;i<=100;++i)
163 {
164 const float altitude=top+i*.001f;
165 const Point eye{0,params.radius+altitude,0};
166 const float sunlight=Transmittance(eye,{0,params.radius+50,0},*field);
167 const float glare=SunGlareStrength(true,altitude-top,1,sunlight);
168 if(previous>=0) largestStep=(std::max)(largestStep,std::abs(glare-previous));
169 previous=glare;
170 }
171 std::cout<<"Glare change per 1 m across local top "<<top<<" km: "<<largestStep<<std::endl;
172 Check(largestStep<.03f,"sun glare follows cloud transmission without a local-top switch");
173 }
174 Check(SunGlareStrength(false,-.01f,1,1)==0 && SunGlareStrength(false,.01f,1,1)==1,
175 "simulator retains legacy sun glare gating");
176 const Point interior{0,params.radius+5,0};
177 const Point greenGlow{.125f,.375f,.25f};
178 for(float transmission : {0.f,.1f,1.f})
179 {
180 const auto lit=ScatteredSunlight({.94f,.96f,1},1,transmission);
181 const auto fog=WithAirglow(lit,greenGlow,1,5,1.8f,10);
182 Check(std::abs(fog.x-lit.x)<1e-6f && std::abs(fog.y-lit.y)<1e-6f && std::abs(fog.z-lit.z)<1e-6f,
183 "daylight suppresses airglow even in the darkest cloud pockets");
184 }
185 Check(AirglowVisibility(-.2f)==1 && AirglowVisibility(.1f)==0,
186 "airglow appears at night and is extinguished after sunrise");
187 float lastGlow=1;
188 for(int i=-200;i<=200;++i)
189 {
190 const float glow=AirglowVisibility(i*.001f);
191 Check(glow<=lastGlow && lastGlow-glow<.01f,"twilight airglow fades continuously with solar elevation");
192 lastGlow=glow;
193 }
194 const auto nightLow=WithAirglow({},greenGlow,-1,5,1.8f,10);
195 const auto nightHigh=WithAirglow({},greenGlow,-1,20,1.8f,10);
196 Check(nightLow.y>nightLow.x && nightHigh.y<nightLow.y*.1f,"green cloud emission falls off with altitude");
197 float thinTransmission=0,thickTransmission=1,thinRain=0,thickRain=0;
198 for(int i=0;i<800;++i)
199 {
200 const Point point{i*.02f,params.radius+5,0};
201 const float t=Transmittance(point,Add(point,{0,-.02f,0}),quiet);
202 const auto medium=SampleMedium(point,quiet);
203 if(t>thinTransmission) { thinTransmission=t; thinRain=RainDensity(medium); }
204 if(t<thickTransmission) { thickTransmission=t; thickRain=RainDensity(medium); }
205 }
206 std::cout<<"20 m wing transmission, thin/dense: "<<thinTransmission<<" / "<<thickTransmission
207 <<"; rain factors: "<<thinRain<<" / "<<thickRain<<std::endl;
208 Check(thinTransmission>.7f && thickTransmission>.13f && thickTransmission<.4f,
209 "dense pockets retain a faint 20 m wing outline while thin pockets reveal it");
210 Check(thickRain>thinRain+.4f,"the optically dense pockets also carry stronger rain");
211 // Follow a 180-knot flight through stationary cloud, measuring actual
212 // integrated visibility rather than the noise value at a single point.
213 for(float region : {0.f,37.f,129.f})
214 {
215 std::vector<float> visibility;
216 double mean=0;
217 for(int frame=0;frame<1200;++frame)
218 {
219 const Point p{frame*.05f*.0926f,params.radius+5,region};
220 const float t=Transmittance(p,Add(p,{0,-.02f,0}),quiet);
221 visibility.push_back(t); mean+=t/1200;
222 for(const Parameters* field : {&quiet,&moderate,&convective})
223 Check(Transmittance(p,Add(p,{0,-.02f,0}),*field)>.13f,
224 "20 m silhouettes survive the densest pockets at every convection level");
225 }
226 float halfCorrelation=5;
227 for(int lag=1;lag<=100;++lag)
228 {
229 double covariance=0,variance=0;
230 for(size_t i=lag;i<visibility.size();++i)
231 {
232 covariance+=(visibility[i]-mean)*(visibility[i-lag]-mean);
233 variance+=(visibility[i]-mean)*(visibility[i]-mean);
234 }
235 if(covariance<variance*.5) { halfCorrelation=lag*.05f; break; }
236 }
237 std::cout<<"180 kt visibility half-correlation, region "<<region<<": "<<halfCorrelation<<" s"<<std::endl;
238 Check(halfCorrelation>=.1f && halfCorrelation<1.f,
239 "flight visibility varies within a second without frame-scale flicker");
240 }
241 for(const Parameters* field : {&quiet,&moderate,&convective})
242 {
243 float lowLight=0,highLight=0,maxDrop=0,last=0;
244 for(int i=0;i<=220;++i)
245 {
246 const float altitude=i*.1f;
247 const float transmission=DiffuseSunTransmission({0,params.radius+altitude,0},{0,1,0},*field);
248 if(i>0) maxDrop=(std::max)(maxDrop,last-transmission);
249 if(i==20) lowLight=transmission;
250 if(i==220) highLight=transmission;
251 last=transmission;
252 }
253 std::cout<<"Solar column ascent bottom/top/max drop: "<<lowLight<<" / "<<highLight<<" / "<<maxDrop<<std::endl;
254 Check(highLight>.99f && lowLight<.4f,"sunlight brightens toward exposed cloud tops");
255 Check(maxDrop<.06f,"solar column integration has no major darkening step during ascent");
256 }
257 const float nearTransmission=Transmittance(interior,Add(interior,{.005f,0,0}),quiet);
258 const float farTransmission=Transmittance(interior,Add(interior,{1,0,0}),quiet);
259 Check(nearTransmission>.59f && farTransmission<.005f,"volume transmits a nearby lamp but hides distant lamps in the same cloud");
260 Check(Transmittance({0,params.radius+15,0},{0,params.radius+14,0},quiet)==1,
261 "clear observer and clear segment have no cloud extinction");
262 Check(Transmittance({0,params.radius+15,0},{0,params.radius+1,0},quiet)<.005f,
263 "cloud between two clear endpoints blocks the line of sight");
264 Check(Transmittance(interior,{0,params.radius+50,0},quiet)<.005f,
265 "sunlight is extinguished by the cloud depth above an interior point");
266 Parameters retained=params;
267 Point retainedWind{};
268 UpdateLocalWeather(retained,retainedWind,true,1.8f,1,{0,0,0},1);
269 Check(retained.activity>0 && retained.activity<.01f,"convection develops over minutes, not seconds");
270 const auto beforeClimb=retained;
271 for(int i=0;i<600;++i)
272 UpdateLocalWeather(retained,retainedWind,false,8.5f,0,{.1f,0,.1f},1);
273 Check(retained.base==beforeClimb.base && retained.activity==beforeClimb.activity
274 && retained.noiseOffset.x==beforeClimb.noiseOffset.x,"upper-air observations cannot reshape or accelerate the deck during ascent");
275 Parameters drift=params,substeps=params;
276 const Point wind{.01f,0,-.004f};
277 Advect(drift,wind,60);
278 for (int i=0;i<3600;++i) Advect(substeps,wind,1.0/60);
279 Check(std::abs(drift.noiseOffset.x-substeps.noiseOffset.x)<.0002f,"wind drift independent of frame rate");
280 Check(drift.noiseOffset.x>1 && drift.noiseOffset.z<0,"wind direction and cloud altitude speed multiplier");
281 const Point fixed{3,params.radius+5,7};
282 Check(std::abs(Density(fixed,params)-Density(fixed,drift))>.001f,"stationary observer encounters advected density");
283 const Point saved=drift.noiseOffset;
284 Advect(drift,{0,0,0},600);
285 Check(drift.noiseOffset.x==saved.x && drift.noiseOffset.z==saved.z,"calm wind does not move formations");
286 static const int cu[]={1019,3057,-1019,5095,1019,1019},cv[]={1019,1019,1019,1019,-1019,3057};
287 for(int cube=0;cube<6;++cube) for(int du : {-30,0,30}) for(int dv : {-30,0,30})
288 {
289 const auto p=CloudBaseGeometry::Position<DirectX::XMFLOAT3>(cube,cu[cube]+du,cv[cube]+dv,params.radius+params.base);
290 const auto map=ProjectGround(cube,cu[cube],cv[cube]);
291 const auto dot=[&](Point a) { return p.x*a.x+p.y*a.y+p.z*a.z; };
292 Check(std::abs(dot(map.east)/dot(map.normal)+map.u-(du+31.5f)/64)<.00001f,"city light U matches cloud grid on all faces");
293 Check(std::abs(dot(map.down)/dot(map.normal)+map.v-(dv+31.5f)/64)<.00001f,"city light V matches cloud grid on all faces");
294 }
295}
Vector Position(int cube, int u, int v, float radius)
float Smooth(float x)
Definition CloudVolume.h:51
GroundProjection ProjectGround(int cube, int u, int v)
constexpr float MaxTopRiseKm
Definition CloudVolume.h:23
Point Scale(Point p, float s)
Definition CloudVolume.h:93
float AirglowVisibility(float sunCosine)
Point Add(Point a, Point b)
Definition CloudVolume.h:95
void UpdateLocalWeather(Parameters &params, Point &deckWind, bool belowDeck, float base, float activity, Point observedWind, double elapsed, bool reset=false)
void Advect(Parameters &params, Point physicalWindKms, double elapsed)
Definition CloudVolume.h:96
float AmbientWeight(float altitudeAboveTop)
Definition CloudVolume.h:59
constexpr float ClearVisibilityKm
float Transmittance(Point from, Point to, const Parameters &params, uint32_t seed=DefaultSeed)
float DiffuseSunTransmission(Point p, Point towardSun, const Parameters &params, uint32_t seed=DefaultSeed)
Column Boundaries(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
Medium SampleMedium(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
float AtmosphericVisibility(float surfaceVisibility, float altitude, float surfaceAltitude, float clearAltitude)
float Density(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
float SunGlareStrength(bool volumeEnabled, float altitudeAboveTop, float visibleSun, float transmission)
float Length(Point p)
Definition CloudVolume.h:94
float RainDensity(const Medium &medium)
float ScreenOpacity(bool volumeEnabled, float cloudOpacity, float overrideOpacity)
Point ScatteredSunlight(Point incident, float sunCosine, float transmission)
Point WithAirglow(Point scattered, Point airglow, float sunCosine, float altitude, float base, float top)
CloudWeather::Frame weather
Definition CloudVolume.h:35