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

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

1#pragma once
2#include <algorithm>
3#include <array>
4#include <vector>
5
6namespace CloudLightningPlacementFixture
7{
10 inline Point At(Point normal,const Parameters& params,float altitude)
11 {return CloudVolume::Scale(normal,params.radius+altitude);}
12 inline Point AsPoint(const LightningChain::Vector3& p) {return {p.x,p.y,p.z};}
13 inline bool Near(Point a,Point b,float tolerance=.002f)
14 {return CloudVolume::Length({a.x-b.x,a.y-b.y,a.z-b.z})<=tolerance;}
15 inline float Moisture(Point p,const Parameters& params,uint32_t seed)
16 {return CloudVolume::Density(p,params,seed);}
17
18 struct UpperLobe
19 {
20 Point normal{};
21 float visibleTop=0,occupiedTop=0,occupiedBottom=0,oldSource=0;
22 };
23 inline bool FindThinUpperLobe(const Parameters& params,uint32_t seed,UpperLobe& result)
24 {
25 // Deterministic sphere coverage, with a fixed bound. The fixture
26 // selects a real overhang rather than mocking the production field.
27 constexpr unsigned columns=1024;
28 for(unsigned i=0;i<columns;++i)
29 {
30 const double z=1-2*(double(i)+.5)/columns;
31 const double azimuth=double(i)*2.399963229728653;
32 const double equator=std::sqrt(1-z*z);
33 const Point normal{float(equator*std::cos(azimuth)),float(equator*std::sin(azimuth)),float(z)};
34 const auto column=CloudVolume::Boundaries(At(normal,params,0),params,seed);
35 if(column.top<params.top+2)continue;
36 // Independently bracket the first occupied interval using 25 m
37 // probes. The obsolete kilometre-offset placement must skip it.
38 float top=column.top;
39 for(unsigned step=0;step<20 && Moisture(At(normal,params,top),params,seed)<.01f;++step)top-=.025f;
40 if(Moisture(At(normal,params,top),params,seed)<.01f)continue;
41 float bottom=top;
42 unsigned depthSteps=0;
43 while(depthSteps<64 && Moisture(At(normal,params,bottom),params,seed)>=.01f)
44 {bottom-=.025f;++depthSteps;}
45 if(depthSteps==64 || top-bottom<.075f)continue;
46 float old=column.top-1.6f;
47 if(old>=bottom-.05f || Moisture(At(normal,params,old),params,seed)>=.01f)continue;
48 for(unsigned step=0;step<440 && old>column.base+.25f
49 && Moisture(At(normal,params,old),params,seed)<.01f;++step)old-=.05f;
50 if(old>=bottom-.1f || Moisture(At(normal,params,old),params,seed)<.01f)continue;
51 result={normal,column.top,top,bottom,old};return true;
52 }
53 return false;
54 }
55
56 inline void CheckPlayback(const CloudLightning::Flash& fresh,double elapsed)
57 {
58 auto cloud=fresh;cloud.Update(elapsed);
59 LightningChain::Animation below;
60 below.Start(fresh.animation.seed,fresh.animation.path);below.Update(elapsed);
61 Check(cloud.animation.inuse==below.inuse && cloud.animation.activeSegment==below.activeSegment
62 && std::abs(cloud.animation.sequenceAge-below.sequenceAge)<1e-10
63 && std::abs(cloud.Brightness()-cloud.energy*below.flashBrightness)<.00001f,
64 "cloud-chain timing differs from the shared below-cloud sequence");
65 Check(Near(cloud.position,AsPoint(below.path.segments[below.activeSegment].start)),
66 "cloud light did not follow the active chain section");
67 }
68}
69
70static void CloudLightningPlacementTests()
71{
72 using namespace CloudLightningPlacementFixture;
73 constexpr uint32_t cloudSeed=CloudVolume::DefaultSeed;
74 Parameters params;params.organic=true;params.weather.current=ConstantWeather(255,128);params.weather.blend=1;
75 UpperLobe lobe;
76 Check(FindThinUpperLobe(params,cloudSeed,lobe),"thin upper-lobe regression fixture was not found within its bounded search");
77 for(float fraction:{0.f,.25f,.5f,.75f,1.f})
78 {
79 const auto source=CloudLightning::SourcePosition(lobe.normal,params,cloudSeed,fraction);
80 const float altitude=CloudVolume::Length(source)-params.radius;
81 Check(Moisture(source,params,cloudSeed)>=.0099f,"upper-lobe source is outside cloud moisture");
82 Check(altitude<=lobe.visibleTop+.005f && altitude>lobe.occupiedBottom-.025f,
83 "source skipped the highest occupied lobe and fell into a lower deck");
84 Check(altitude>lobe.oldSource+.1f,"upper-lobe regression still uses the old fixed kilometre offset");
85 }
86
87 const std::array<Point,8> rawNormals{{lobe.normal,{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1},{1,2,3}}};
88 unsigned chains=0,sections=0,multiSection=0;
89 for(const auto raw:rawNormals)
90 {
91 const auto normal=CloudVolume::Scale(raw,1/CloudVolume::Length(raw));
92 for(uint32_t eventSeed=0;eventSeed<32;++eventSeed)
93 {
95 flash.position=CloudLightning::SourcePosition(normal,params,cloudSeed,.5f);
96 flash.radius=12;flash.energy=2.3f;
97 flash.StartChain(params,cloudSeed,eventSeed);
98 const auto& animation=flash.animation;const auto& path=animation.path;
99 Check(path.count>=1 && path.count<=3,"cloud chain section count escaped its bound");
100 Check(animation.boltDuration>=LightningTiming::MinDurationSeconds
101 && animation.boltDuration<=LightningTiming::MaxDurationSeconds,
102 "cloud chain changed the shared single-bolt duration range");
103 Check(animation.Duration()==animation.boltDuration*path.count,"cloud chain duration is not an exact section multiple");
104 ++chains;multiSection+=path.count>1;
105 for(int section=0;section<path.count;++section)
106 {
107 const auto& segment=path.segments[section];++sections;
108 const auto start=AsPoint(segment.start),end=AsPoint(segment.end);
109 const Point delta{end.x-start.x,end.y-start.y,end.z-start.z};
110 const float distance=CloudVolume::Length(delta);
111 const float vertical=delta.x*normal.x+delta.y*normal.y+delta.z*normal.z;
112 const float tangent=std::sqrt((std::max)(0.f,distance*distance-vertical*vertical));
113 Check(std::isfinite(distance) && tangent<=LightningTiming::CloudHopMaxKm+.005f
114 && std::abs(vertical)<=LightningTiming::CloudHopMaxRiseKm+.005f,
115 "cloud-contained section exceeded the approved horizontal or vertical hop bounds");
116 Check(!segment.groundContact,"cloud-only lightning created a ground-contact section");
117 if(section)Check(Near(start,AsPoint(path.segments[section-1].end)),"cloud chain lost spatial continuity");
118 // An independent lattice also catches short clear pockets
119 // between the production containment probes.
120 for(unsigned sample=0;sample<=97;++sample)
121 {
122 const float t=sample/97.f;
123 const Point position{start.x+(end.x-start.x)*t,start.y+(end.y-start.y)*t,start.z+(end.z-start.z)*t};
124 Check(Moisture(position,params,cloudSeed)>=.0099f,
125 "cloud chain crosses a clear pocket between occupied endpoints");
126 Check(CloudVolume::Length(position)>params.radius+params.base
127 && CloudVolume::Length(position)<=params.radius+params.top+CloudVolume::MaxTopRiseKm+.005f,
128 "cloud chain left the established cloud altitude envelope");
129 }
130 }
131 // Re-evaluate every shared section boundary, peak, and tail. A
132 // single elapsed-time jump also covers stalls skipping sections.
133 std::vector<double> times{0,animation.Duration(),animation.Duration()+10};
134 for(int section=0;section<path.count;++section)
135 {
136 const double start=animation.SegmentStart(section);
137 if(section){times.push_back(start-1e-7);times.push_back(start);times.push_back(start+1e-7);}
138 LightningStrike::Animation pulse;pulse.Start(path.segments[section].seed,animation.boltDuration);
139 times.push_back(start+pulse.growthSeconds*.5);
140 for(int stroke=0;stroke<pulse.strokeCount;++stroke)
141 {
142 const auto& p=pulse.strokes[stroke];
143 times.push_back(start+p.start+p.attack+p.hold*.5);
144 times.push_back(start+p.End());
145 }
146 times.push_back(start+animation.boltDuration-1e-7);
147 }
148 for(const double time:times)CheckPlayback(flash,time);
149 for(int fps:{30,60,144})
150 {
151 auto advanced=flash;
152 while(advanced.animation.inuse)
153 {
154 advanced.Update(1./fps);
155 LightningChain::Animation jumped;jumped.Start(animation.seed,path);
156 jumped.Update(advanced.animation.sequenceAge);
157 Check(advanced.animation.activeSegment==jumped.activeSegment
158 && advanced.animation.inuse==jumped.inuse
159 && std::abs(advanced.Brightness()-advanced.energy*jumped.flashBrightness)<.00001f,
160 "cloud chain depends on frame segmentation");
161 Check(Near(advanced.position,AsPoint(path.segments[jumped.activeSegment].start)),
162 "frame-stepped illumination retained a previous section position");
163 }
164 }
165 }
166 }
167 Check(multiSection>0,"cloud chain containment fixture did not exercise a connected chain");
168 std::cout<<"Cloud placement: upper interval "<<lobe.occupiedBottom<<".."<<lobe.occupiedTop
169 <<" km; obsolete source "<<lobe.oldSource<<" km; "<<chains<<" chains / "<<sections<<" occupied sections\n";
170}
Point SourcePosition(const Point &normal, const CloudVolume::Parameters &params, uint32_t cloudSeed, float depthFraction)
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
Column Boundaries(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
float Density(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
float Length(Point p)
Definition CloudVolume.h:94
BasicParameters< RiseMath::Vector3, RiseMath::Color > Parameters
Definition ExteriorRain.h:7
constexpr float CloudHopMaxRiseKm
constexpr double MaxDurationSeconds
constexpr float CloudHopMaxKm
constexpr double MinDurationSeconds
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141
path
Definition erin.py:153
LightningChain::Animation animation
void StartChain(const CloudVolume::Parameters &params, uint32_t cloudSeed, uint32_t eventSeed, double elapsed=0)
void Update(double elapsed)
void Update(double elapsed)
void Start(uint32_t packetSeed, const Path &geometry)
std::array< Segment, LightningTiming::MaxChainedBolts+1 > segments
void Start(uint32_t strikeSeed, double durationSeconds=0)
std::array< Stroke, Timing::MaxStrokes > strokes