1#pragma once
2#include <DirectXMath.h>
3#include "../Viewscreen/CloudBaseGeometry.h"
4
5
6static void CpuTests()
7{
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
24
25 Parameters quiet=params,moderate=params,convective=params;
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)};
37 const Point next=
Add(p,{.5f,0,0});
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
52 float last=calm.top,lastBase=calm.base;
53 for(int frame=1;frame<=600;++frame)
54 {
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 {
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");
80 towerSamples+=d>.1f; clearSamples+=d==0;
82 highTowerSamples+=highDensity>.1f; highClearSamples+=highDensity==0;
83 for(
const Parameters* field : {&quiet,&moderate,&convective})
84 {
88 Check(std::abs(beforeLight-afterLight)<.01f,"direct illumination cannot jump at the 10 km flight-mode boundary");
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};
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();
113 for(int i=0;i<128;++i)
114 {
115 Point p{float(i)*8.f,params.
radius,37};
117 Check(column.top<=params.
top+
MaxTopRiseKm,
"server convection towers stay inside marching shell");
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");
125 for(int i=0;i<200;++i)
126 {
127 Point p{float(i),params.
radius,0};
129 }
130 for(float base : {.45f,1.8f,8.5f}) for(float activity : {0.f,1.f})
131 {
133 for(int i=0;i<200;++i)
134 {
136 Check(column.top<=params.
top+
MaxTopRiseKm,
"minimum stratus thickness remains inside the marching shell");
137 }
138 }
139
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");
144 float previousVisibility=0;
145 for(int i=0;i<=1000;++i)
146 {
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 }
154 "14.5 km uses the legacy clear-air override rather than the 42.16 km fallback");
157 Check(
ScreenOpacity(
true,1,1)==1,
"explicit screen override is retained");
158 for(
const Parameters* field : {&quiet,&moderate,&convective})
159 {
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};
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 }
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 {
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 }
186 "airglow appears at night and is extinguished after sunrise");
187 float lastGlow=1;
188 for(int i=-200;i<=200;++i)
189 {
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};
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
212
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};
221 visibility.push_back(t); mean+=t/1200;
222 for(
const Parameters* field : {&quiet,&moderate,&convective})
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;
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");
261 "clear observer and clear segment have no cloud extinction");
263 "cloud between two clear endpoints blocks the line of sight");
265 "sunlight is extinguished by the cloud depth above an interior point");
267 Point retainedWind{};
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)
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");
276 const Point wind{.01f,0,-.004f};
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");
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");
284 Advect(drift,{0,0,0},600);
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 {
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)
GroundProjection ProjectGround(int cube, int u, int v)
constexpr float MaxTopRiseKm
Point Scale(Point p, float s)
float AirglowVisibility(float sunCosine)
Point Add(Point a, Point b)
void UpdateLocalWeather(Parameters ¶ms, Point &deckWind, bool belowDeck, float base, float activity, Point observedWind, double elapsed, bool reset=false)
void Advect(Parameters ¶ms, Point physicalWindKms, double elapsed)
float AmbientWeight(float altitudeAboveTop)
constexpr float ClearVisibilityKm
float Transmittance(Point from, Point to, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float DiffuseSunTransmission(Point p, Point towardSun, const Parameters ¶ms, uint32_t seed=DefaultSeed)
Column Boundaries(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
Medium SampleMedium(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float AtmosphericVisibility(float surfaceVisibility, float altitude, float surfaceAltitude, float clearAltitude)
float Density(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float SunGlareStrength(bool volumeEnabled, float altitudeAboveTop, float visibleSun, float transmission)
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