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

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

1#pragma once
2#include <array>
3
4namespace CloudOrganicGrowthFixture
5{
6 using namespace CloudVolume;
7 inline Point Normal(unsigned index)
8 {
9 // Include poles and widely separated longitudes, not one favorable tower.
10 if(index==0)return {0,0,1};
11 if(index==1)return {0,0,-1};
12 const double z=1-2*(double(index-2)+.5)/14;
13 const double angle=double(index)*2.399963229728653;
14 const double r=std::sqrt(1-z*z);
15 return {float(r*std::cos(angle)),float(r*std::sin(angle)),float(z)};
16 }
17 inline Point At(Point normal,const Parameters& params,float altitude)
18 {return Scale(normal,params.radius+altitude);}
19
20}
21
22static void CloudOrganicGrowthTests()
23{
24 using namespace CloudOrganicGrowthFixture;
25 // Gradient noise is deterministic, signed and periodic. Wind wrapping
26 // must preserve its value, including at a lattice boundary.
27 float perlinLow=1,perlinHigh=-1;
28 for(unsigned i=0;i<1024;++i)
29 {
30 const float x=float(i)*.013f,y=float(i)*.021f,z=float(i)*.037f;
31 const float a=CloudPerlin::Evaluate(x,y,z);
32 Check(std::isfinite(a) && std::abs(a)<1,"bounded Perlin gradient field");
33 Check(std::abs(a-CloudPerlin::Evaluate(x+8,y-8,z+8))<.00001f,"periodic Perlin gradient field");
34 perlinLow=(std::min)(perlinLow,a);perlinHigh=(std::max)(perlinHigh,a);
35 }
36 Check(perlinLow<-.1f && perlinHigh>.1f,"Perlin detail contains both signs");
37 const auto quiet=ConstantWeather(0),weak=ConstantWeather(64),middle=ConstantWeather(127),
38 active=ConstantWeather(192),strong=ConstantWeather(255);
39 const std::array<CloudWeather::MapPtr,5> maps{{quiet,weak,middle,active,strong}};
40 Parameters params;params.organic=true;params.weather.blend=1;
41 // All byte values decode monotonically, retain exact endpoints, and are
42 // independent of regional procedural shape (a permitted height envelope).
43 for(unsigned byte=0;byte<256;++byte)
44 {
45 params.weather.current=ConstantWeather(uint8_t(byte));
46 for(unsigned column=0;column<16;++column)
47 {
48 const auto region=CloudOrganicShape::Regional(At(Normal(column),params,0),params);
49 Check(std::abs(region.top-(10.f+12.f*std::sqrt(float(byte)/255)))<.00001f,
50 "cloud envelope changed byte mapping or endpoints");
51 }
52 }
53 unsigned tallColumns=0,clearSamples=0,occupiedSamples=0;
54 for(uint32_t seed:{DefaultSeed,DefaultSeed^0xa511e9b3u})
55 for(Point offset:{Point{},Point{123.5f,-91.25f,37.f}})
56 {
57 params.noiseOffset=offset;
58 for(unsigned column=0;column<16;++column)
59 {
60 const auto normal=Normal(column),surface=At(normal,params,0);
61 std::array<float,49> previous{};
62 float previousTop=params.top;
63 for(const auto& map:maps)
64 {
65 params.weather.current=map;
66 const auto region=CloudOrganicShape::Regional(surface,params,seed);
67 Check(region.top>=previousTop && region.top<=params.top+MaxTopRiseKm,
68 "convection envelope must grow monotonically within 22 km");
69 if(map==quiet)
70 Check(region.top<params.top+.001f,"weak convection must retain the shallow calm deck");
71 previousTop=region.top;
72 for(unsigned level=0;level<previous.size();++level)
73 {
74 const float altitude=params.top+.25f*level;
75 const auto sample=CloudOrganicShape::Evaluate(At(normal,params,altitude),params,seed,&region);
76 Check(std::isfinite(sample.density) && sample.density>=0 && sample.density<=1,
77 "finite bounded cloud density");
78 Check(sample.density+1e-5f>=previous[level],
79 "more convection cannot erase a pre-existing lobe or wisp");
80 if(map==quiet && altitude>params.top+.001f)
81 Check(sample.density==0,"quiet weather produced a tall tower");
82 previous[level]=sample.density;
83 occupiedSamples+=sample.density>.01f;clearSamples+=sample.density==0;
84 if(map==strong && altitude>params.top+8 && sample.density>.01f)++tallColumns;
85 }
86 for(float altitude:{params.base+.5f,5.f,9.49f})
87 {
88 const auto point=At(normal,params,altitude);
89 const auto actual=SampleMedium(point,params,seed),legacy=LegacySampleMedium(point,params,seed);
90 Check(actual.density==legacy.density && actual.extinction==legacy.extinction,
91 "upper-cloud refinement changed the preserved below-deck medium");
92 }
93 }
94 }
95 }
96 Check(occupiedSamples>100 && clearSamples>100,"weather checks must sample occupied lobes and clear gaps");
97 Check(tallColumns>0,"strong convection must still support towers above 18 km");
98
99 // Exercise the actual previous/current map blend. Use a fixed altitude
100 // grid so changed envelope search spacing cannot manufacture height jumps.
101 params.noiseOffset={};params.weather.previous=quiet;params.weather.current=strong;
102 unsigned developedColumns=0;
103 for(unsigned column=0;column<16;++column)
104 {
105 const auto normal=Normal(column),surface=At(normal,params,0);
106 std::array<float,81> previousDensity{};
107 float previousEnvelope=0,previousVisible=0,firstVisible=0;
108 for(unsigned step=0;step<=16;++step)
109 {
110 params.weather.blend=float(step)/16;
111 const auto region=CloudOrganicShape::Regional(surface,params);
112 Check(region.top+1e-5f>=previousEnvelope && region.top<=params.top+MaxTopRiseKm,
113 "blended convection lowered the regional envelope or escaped the maximum height");
114 float visible=params.top;
115 for(unsigned level=0;level<previousDensity.size();++level)
116 {
117 const float altitude=params.top+.15f*level;
118 const auto sample=CloudOrganicShape::Evaluate(At(normal,params,altitude),params,DefaultSeed,&region);
119 Check(sample.density+1e-5f>=previousDensity[level],
120 "increasing blended convection removed an existing cloud billow");
121 Check(std::isfinite(sample.density) && sample.density>=0 && sample.density<=1,
122 "blended convection produced invalid density");
123 previousDensity[level]=sample.density;
124 if(sample.density>.01f)visible=altitude;
125 }
126 Check(visible>=previousVisible,"increasing blended convection lowered the sampled visible top");
127 if(step==0)firstVisible=visible;
128 previousEnvelope=region.top;previousVisible=visible;
129 }
130 developedColumns+=previousVisible>firstVisible+.3f;
131 }
132 Check(developedColumns>4,"weather-height monotonicity comparison contained too few growing formations");
133 // The calm-sheet treatment reaches its full strength near byte 16.
134 // Probe every byte around that boundary, including the sheet below the
135 // nominal top; the coarser tower grid above cannot detect its regressions.
136 params.weather.previous.reset();params.weather.blend=1;
137 std::array<std::array<float,25>,16> calmPrevious{};
138 for(unsigned byte=0;byte<=20;++byte)
139 {
140 params.weather.current=ConstantWeather(uint8_t(byte));
141 for(unsigned column=0;column<16;++column)
142 {
143 const auto normal=Normal(column);
144 const auto region=CloudOrganicShape::Regional(At(normal,params,0),params);
145 for(unsigned level=0;level<25;++level)
146 {
147 const float altitude=params.top-.5f+.05f*level;
148 const auto sample=CloudOrganicShape::Evaluate(At(normal,params,altitude),params,DefaultSeed,&region);
149 Check(std::isfinite(sample.density) && sample.density>=0 && sample.density<=1,
150 "calm sheet produced invalid density");
151 Check(sample.density+1e-5f>=calmPrevious[column][level],
152 "calm sheet lost density while convection increased");
153 if(altitude>region.top+.001f)
154 Check(sample.density==0,"calm sheet escaped its weather envelope");
155 calmPrevious[column][level]=sample.density;
156 }
157 }
158 }
159 std::cout<<"Organic fixed-endpoint growth: calm zero convection, bounded strong towers, "
160 <<developedColumns<<"/16 monotonic blended-weather columns developed; lower deck preserved\n";
161}
Region Regional(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
Sample Evaluate(Point p, const Parameters &params, uint32_t seed=DefaultSeed, const Region *prepared=nullptr)
float Evaluate(float x, float y, float z)
constexpr float MaxTopRiseKm
Definition CloudVolume.h:23
Point Scale(Point p, float s)
Definition CloudVolume.h:93
constexpr uint32_t DefaultSeed
Definition CloudVolume.h:19
Medium SampleMedium(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
Medium LegacySampleMedium(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
constexpr double Normal
Definition FlightLoads.h:14
CloudWeather::Frame weather
Definition CloudVolume.h:35