1#pragma once
2#include <array>
3
4namespace CloudOrganicGrowthFixture
5{
7 inline Point Normal(unsigned index)
8 {
9
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)
19
20}
21
22static void CloudOrganicGrowthTests()
23{
24 using namespace CloudOrganicGrowthFixture;
25
26
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;
32 Check(std::isfinite(a) && std::abs(a)<1,"bounded 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}};
41
42
43 for(unsigned byte=0;byte<256;++byte)
44 {
46 for(unsigned column=0;column<16;++column)
47 {
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;
55 for(Point offset:{Point{},Point{123.5f,-91.25f,37.f}})
56 {
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 {
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;
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);
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
100
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 {
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;
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
134
135
137 std::array<std::array<float,25>,16> calmPrevious{};
138 for(unsigned byte=0;byte<=20;++byte)
139 {
141 for(unsigned column=0;column<16;++column)
142 {
143 const auto normal=
Normal(column);
145 for(unsigned level=0;level<25;++level)
146 {
147 const float altitude=params.
top-.5f+.05f*level;
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 ¶ms, uint32_t seed=DefaultSeed)
Sample Evaluate(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed, const Region *prepared=nullptr)
float Evaluate(float x, float y, float z)
constexpr float MaxTopRiseKm
Point Scale(Point p, float s)
constexpr uint32_t DefaultSeed
Medium SampleMedium(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
Medium LegacySampleMedium(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
CloudWeather::Frame weather