./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
410 / 411
99.8%
Branches:
251 / 288
87.2%
Functions:
24 / 24
100.0%

src/layers/craters/shaping/crater_kernel.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "layers/craters/shaping/crater_kernel.h"
4
5 #include "terraingen/geometry/geometry.h"
6 #include "terraingen/layers/craters/degradation/diffusion.h"
7 #include "terraingen/layers/craters/profiles/complex.h"
8 #include "terraingen/layers/craters/profiles/fassett_thomson.h"
9 #include "terraingen/layers/craters/profiles/martin_parkes_dunstan.h"
10 #include "terraingen/layers/craters/shaping/apply.h"
11 #include "terraingen/layers/fade.h"
12 #include "terraingen/random/stream.h"
13 #include "terraingen/random/streams.h"
14 #include "util/numerics.h"
15
16 #include <algorithm>
17 #include <array>
18 #include <cmath>
19 #include <map>
20 #include <stdexcept>
21
22 namespace terraingen {
23
24 namespace {
25
26 /// Share of the wall the benches cover from the full terrace diameter: even fully terraced
27 /// craters have terraces of limited length, up to about half the rim in craters 20-130 km across
28 /// (Du et al. 2024, LPSC 55, abstract 1382).
29 constexpr double kFullTerraceCover = 0.9;
30
31 /// Width in harmonic units over which a bench's arc fades in and out, wide enough that a bench
32 /// tapers into the unstepped wall over tens of degrees rather than ending in a radial cliff.
33 constexpr double kTerraceArcWidth = 0.3;
34
35 /// Samples of a crater's wall height across the wall, for splitting it into steps by height.
36 constexpr int kTerraceWallSamples = 256;
37
38 /// Narrowest step across the wall, as a share of an even step's width.
39 constexpr double kNarrowestTerraceStep = 0.25;
40
41 /// Share of a half-scarp over which its slope rounds into its brink or toe (`KernelTerraceEase`).
42 constexpr double kTerraceScarpRounding = 0.4;
43
44 /// Most share of a step its scarps take, so even steep steps keep a narrow bench.
45 constexpr double kMostTerraceScarpShare = 0.9;
46
47 /// Share of a step its scarps would need to stay at the scarp slope from which its bench fades,
48 /// gone where the step is as steep as the scarps: walls too steep for a bench keep their slope.
49 constexpr double kSteepTerraceStep = 0.7;
50
51 /// Headroom on the scarp slope for scallops and block offsets, which narrow some steps.
52 constexpr double kTerraceScarpSqueeze = 0.75;
53
54 /// Samples of a step for its mean height, which its bench level keeps.
55 constexpr int kTerraceMeanSamples = 64;
56
57 /// Bisection steps for the bench level that keeps a step's mean height.
58 constexpr int kTerraceLevelIterations = 30;
59
60 /// Nearest a bench's level comes to the bottom or top of its step, as a share of the step.
61 constexpr double kTerraceLevelMargin = 0.1;
62
63 /// A scarp's room, as a share of the narrower of its two steps.
64 constexpr double kTerraceScarpRoom = 0.4;
65
66 /// Most a scarp may move, scallops, wobble and block offset together, in units of its room: with
67 /// the room 0.4 of the narrower step, two neighboring scarps stay 4% of a step apart.
68 constexpr double kTerraceScarpReach = 1.2;
69
70 /// Fewest and most scallops a scarp has around the rim, as slump blocks scallop the walls of
71 /// fresh complex craters.
72 constexpr double kMinTerraceScallops = 6.0;
73 constexpr double kMaxTerraceScallops = 12.0;
74
75 /// Height of the rim crest's scallops, in radii, per unit of `CraterTerraces::rimVariation`.
76 constexpr double kTerraceRimScallop = 0.5;
77
78 /// How far a central peak's massifs may reach before warping, in peak base radii; the kernel
79 /// reads them out to 2.6.
80 constexpr double kPeakRoom = 2.3;
81
82 /// Radii of the main and of a typical lesser massif of a two-massif peak, in peak base radii;
83 /// with more massifs they shrink with the square root of their number, so a cluster of
84 /// separate mountains covers about the peak's base.
85 constexpr double kMainMassifRadius = 1.0;
86 constexpr double kLesserMassifRadius = 0.6;
87
88 /// Furthest a massif's summit sits off its center, in units of its footprint, at full
89 /// lopsidedness.
90 constexpr double kMaxSummitPull = 0.45;
91
92 /// Whether the simple profile shapes `crater` in `state`: not where the table or the complex
93 /// form does.
94 161349 bool ShapesSimply(const CraterShapeKernel& kernel, double diameter, const CraterState& state) {
95 161349 const CraterProfileKernel& p = kernel.profile;
96
4/4
✓ Branch 0 taken 95653 times.
✓ Branch 1 taken 65696 times.
✓ Branch 2 taken 54939 times.
✓ Branch 3 taken 40714 times.
161349 if (p.diffused == 1u && state.diffusion > 0.0) {
97 return false;
98 }
99
4/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 120544 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 78 times.
120635 return !(p.complex == 1u && diameter >= static_cast<double>(p.transitionDiameter));
100 }
101
102 /// `profile` as the kernel evaluates it in closed form, or nothing for a profile it does not
103 /// know, which it tabulates.
104 239135 std::optional<CraterProfileKernel> ClosedFormProfile(const CraterProfile& profile) {
105 239135 CraterProfileKernel p;
106 239135 const CraterProfile* fresh = &profile;
107
2/2
✓ Branch 0 taken 36503 times.
✓ Branch 1 taken 202632 times.
239135 if (const auto* diffused = dynamic_cast<const DiffusedProfile*>(&profile)) {
108 36503 p = diffused->Kernel();
109 36503 fresh = &diffused->Fresh();
110 }
111
3/4
✓ Branch 0 taken 239135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31215 times.
✓ Branch 3 taken 207920 times.
239135 if (const auto* complex = dynamic_cast<const ComplexCraterProfile*>(fresh)) {
112 31215 SetKernelComplexShape(p, complex->Shape());
113 31215 fresh = &complex->Simple();
114 }
115
4/4
✓ Branch 0 taken 239115 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 108855 times.
✓ Branch 3 taken 130260 times.
239135 if (const auto* ft = dynamic_cast<const FassettThomsonProfile*>(fresh)) {
116 108855 p.simple = ft->Kernel().simple;
117 108855 p.rim = ft->Kernel().rim;
118
3/4
✓ Branch 0 taken 130280 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79838 times.
✓ Branch 3 taken 50442 times.
130280 } else if (const auto* mpd = dynamic_cast<const MartinParkesDunstanProfile*>(fresh)) {
119 79838 p.simple = mpd->Kernel().simple;
120 } else {
121 50442 return std::nullopt;
122 }
123 188693 return p;
124 }
125
126 /// `profile` tabulated per crater (`CraterProfileKind::Tabulated`).
127 239061 CraterProfileKernel TabulatedProfile(const CraterProfile& profile) {
128 239061 CraterProfileKernel p;
129 239061 p.simple = CraterProfileKind::Tabulated;
130 239061 p.tableExtent = static_cast<float>(0.5 * profile.ReachRadii());
131 239100 p.tableCells = kProfileTableCells;
132 239100 return p;
133 }
134
135 /// Sets how `options` shape craters on `k`: reach, blend, rim smoothing, noise and texture.
136 239086 void SetShapeOptions(CraterShapeKernel& k, const CraterProfile& profile,
137 const CraterApplyOptions& options) {
138 239086 k.reachRadii = static_cast<float>(CraterReachRadii(profile, options));
139 239079 k.profileReachRadii = static_cast<float>(profile.ReachRadii());
140 239083 k.blendStart = static_cast<float>(options.blend.startRadii);
141 239083 k.blendEnd = static_cast<float>(options.blend.endRadii);
142 239083 k.blendShape = static_cast<std::uint32_t>(options.blend.shape);
143 239083 k.interiorReplacement = static_cast<float>(options.interiorReplacement);
144 239083 k.replaceStart = static_cast<float>(options.replacement.startRadii);
145
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 239075 times.
239083 k.replaceEnd = static_cast<float>(options.replacement.EndRadii(profile.ReachRadii()));
146 239076 k.mantling = static_cast<float>(options.texture.mantling);
147 239076 k.ejectaReachRadii = static_cast<float>(options.texture.ejectaReachRadii);
148 239076 const RimSmoothing& rim = options.rimSmoothing;
149 239076 k.rimSigma = static_cast<float>(rim.sigmaRadii);
150 239076 k.rimInner = static_cast<float>(rim.innerRadii);
151 239076 k.rimOuter = static_cast<float>(rim.outerRadii);
152 239076 k.rimStrength = static_cast<float>(rim.strength);
153 239076 k.rimShape = static_cast<std::uint32_t>(rim.shape);
154 239076 const CraterNoise& noise = options.noise;
155 239076 k.noiseAmplitude = static_cast<float>(noise.amplitude);
156 239076 k.noiseWavelength = static_cast<float>(noise.wavelengthRadii);
157 239076 k.noiseOctaves = noise.octaves;
158 239076 k.noiseGain = static_cast<float>(noise.gain);
159 239076 k.noiseRimAmplitude = static_cast<float>(noise.rimAmplitude);
160 239076 k.noiseRimWavelength = static_cast<float>(noise.rimWavelengthRadii);
161 239076 k.noiseRimInner = static_cast<float>(noise.rimInnerRadii);
162 239076 k.noiseRimOuter = static_cast<float>(noise.rimOuterRadii);
163 239076 const CraterTexture& texture = options.texture;
164 239076 k.wallStreaks = static_cast<float>(texture.wallStreaks);
165 239076 k.wallStreakWidth = static_cast<float>(texture.wallStreakWidthRadii);
166 239076 k.ejecta = static_cast<float>(texture.ejecta);
167 239076 k.ejectaLineation = static_cast<float>(texture.ejectaLineationRadii);
168 239076 k.minFreshness = static_cast<float>(texture.minFreshness);
169 239076 }
170
171 /// Checks the state a Martin, Parkes and Dunstan profile shapes a crater in.
172 /// @throws std::invalid_argument for a state the profile rejects.
173 343588 void CheckProfileState(const CraterShapeKernel& kernel, double diameter, const CraterState& state) {
174
2/2
✓ Branch 0 taken 161366 times.
✓ Branch 1 taken 182222 times.
343588 if (kernel.profile.simple != CraterProfileKind::MartinParkesDunstan ||
175
2/2
✓ Branch 0 taken 120639 times.
✓ Branch 1 taken 40727 times.
161366 !ShapesSimply(kernel, diameter, state)) {
176 return;
177 }
178
2/2
✓ Branch 0 taken 54939 times.
✓ Branch 1 taken 65700 times.
120639 const CraterMorphometry& shaped = kernel.profile.diffused == 1u ? state.fresh : state.current;
179
2/4
✓ Branch 0 taken 120639 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 120639 times.
✗ Branch 3 not taken.
120639 if (!(shaped.depthRatio > 0.0 && shaped.rimHeightRatio >= 0.0 &&
180
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 120638 times.
120639 shaped.rimHeightRatio <= state.fresh.rimHeightRatio)) {
181 1 throw std::invalid_argument("Crater state needs a positive depth and 0 <= rim "
182
1/1
✓ Branch 2 taken 1 time.
1 "height <= fresh rim height");
183 }
184 }
185
186 /// The record of prepared crater `p`, without its outline and profile tables.
187 343603 CraterRecord BaseRecord(const PreparedCrater& p, const CraterApplyOptions& options) {
188 343603 const CraterState& state = p.state;
189
2/2
✓ Branch 0 taken 109293 times.
✓ Branch 1 taken 234310 times.
343603 CraterRecord r = KernelStateRecord(p.crater.diameter, state);
190 343603 r.resolution = static_cast<float>(p.resolution);
191
2/2
✓ Branch 0 taken 109293 times.
✓ Branch 1 taken 234310 times.
343603 if (p.datum) {
192 109293 r.hasDatum = 1u;
193 109293 r.datumA = static_cast<float>(p.datum->a);
194 109293 r.datumB = static_cast<float>(p.datum->b);
195 109293 r.datumC = static_cast<float>(p.datum->c);
196 }
197
2/2
✓ Branch 0 taken 343516 times.
✓ Branch 1 taken 87 times.
343603 r.replace = p.crater.replace ? 1u : 0u;
198 343603 r.shiftX = static_cast<float>(p.slope.shift.x);
199 343603 r.shiftY = static_cast<float>(p.slope.shift.y);
200 343603 r.upslopeX = static_cast<float>(p.slope.upslope.x);
201 343603 r.upslopeY = static_cast<float>(p.slope.upslope.y);
202
4/4
✓ Branch 0 taken 164285 times.
✓ Branch 1 taken 179318 times.
✓ Branch 2 taken 164231 times.
✓ Branch 3 taken 179372 times.
507888 r.noiseKey = CraterKey(p.crater.id, options.noise.seed.value_or(0u));
203 687206 r.textureKey = CraterKey(
204
2/2
✓ Branch 0 taken 164231 times.
✓ Branch 1 taken 179372 times.
343603 p.crater.id, SubstreamSeed(options.texture.seed.value_or(0u), kCraterTextureStream));
205 343603 return r;
206 }
207
208 /// The profile tables of a batch, each appended to its data once.
209 class ProfileTables {
210 public:
211 239474 ProfileTables(const CraterShapeKernel& kernel, const CraterProfile& profile)
212 239474 : m_kernel(kernel), m_profile(profile),
213 239474 m_diffused(dynamic_cast<const DiffusedProfile*>(&profile)) {}
214
215 /// Appends the ages of a diffused profile's tables to `data`, which must be empty.
216 239505 void AppendAges(std::vector<float>& data) const {
217
3/4
✓ Branch 0 taken 36224 times.
✓ Branch 1 taken 203281 times.
✓ Branch 2 taken 36224 times.
✗ Branch 3 not taken.
239505 if (m_kernel.profile.diffused == 1u && m_diffused != nullptr) {
218
2/2
✓ Branch 1 taken 5832011 times.
✓ Branch 2 taken 36224 times.
5868235 for (const double age : m_diffused->Ages()) {
219 5832011 data.push_back(static_cast<float>(age));
220 }
221 }
222 239505 }
223
224 /// Points `r` at the table crater `p` is shaped from, appending it to `data` when new.
225 343578 void Append(const PreparedCrater& p, CraterRecord& r, std::vector<float>& data) {
226 343578 const CraterState& state = p.state;
227 343578 const double diameter = p.crater.diameter;
228
2/2
✓ Branch 0 taken 50732 times.
✓ Branch 1 taken 292846 times.
343578 if (m_kernel.profile.simple == CraterProfileKind::Tabulated) {
229 50732 const std::array<double, 6> key{diameter,
230 50732 state.fresh.depthRatio,
231 50732 state.fresh.rimHeightRatio,
232 50732 state.current.depthRatio,
233 50732 state.current.rimHeightRatio,
234 50732 state.diffusion};
235 101464 const auto [at, added] =
236
2/2
✓ Branch 1 taken 50731 times.
✓ Branch 2 taken 1 time.
50732 m_tabulated.try_emplace(key, static_cast<std::uint32_t>(data.size()));
237
2/2
✓ Branch 0 taken 50731 times.
✓ Branch 1 taken 1 time.
50732 if (added) {
238 50731 AppendRow(diameter, state, data);
239 }
240 50732 r.profileOffset = at->second;
241 50732 return;
242 }
243
5/6
✓ Branch 0 taken 129485 times.
✓ Branch 1 taken 163361 times.
✓ Branch 2 taken 129485 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 71038 times.
✓ Branch 5 taken 58447 times.
292846 if (m_kernel.profile.diffused == 1u && m_diffused != nullptr && state.diffusion > 0.0) {
244 71038 const std::shared_ptr<const std::vector<float>> table =
245 71038 m_diffused->HeightTable(state.fresh, diameter);
246 142083 const auto [at, added] =
247
3/3
✓ Branch 1 taken 71042 times.
✓ Branch 3 taken 30177 times.
✓ Branch 4 taken 40865 times.
71041 diffused_tables_.try_emplace(table.get(), static_cast<std::uint32_t>(data.size()));
248
2/2
✓ Branch 0 taken 30177 times.
✓ Branch 1 taken 40865 times.
71042 if (added) {
249
1/3
✓ Branch 1 taken 30175 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
30177 data.insert(data.end(), table->begin(), table->end());
250 }
251
1/2
✓ Branch 0 taken 71040 times.
✗ Branch 1 not taken.
71040 r.profileOffset = at->second;
252 71045 }
253 }
254
255 private:
256 /// Appends the profile's heights over the diameter along one row for a crater of `diameter`
257 /// meters in `state`.
258 50731 void AppendRow(double diameter, const CraterState& state, std::vector<float>& data) const {
259 50731 const double extent = 0.5 * m_profile.ReachRadii();
260 50731 const double step = extent / static_cast<double>(m_kernel.profile.tableCells);
261
2/2
✓ Branch 0 taken 26025003 times.
✓ Branch 1 taken 50731 times.
26075734 for (std::uint32_t i = 0; i <= m_kernel.profile.tableCells; ++i) {
262 26025003 const double distance = static_cast<double>(i) * step * diameter;
263 52050006 data.push_back(
264 26025003 static_cast<float>(m_profile.Height(distance, diameter, state) / diameter));
265 }
266 50731 }
267
268 const CraterShapeKernel& m_kernel;
269 const CraterProfile& m_profile;
270 const DiffusedProfile* m_diffused;
271 std::map<const std::vector<float>*, std::uint32_t> diffused_tables_;
272 std::map<std::array<double, 6>, std::uint32_t> m_tabulated;
273 };
274
275 } // namespace
276
277 238832 CraterShapeKernel MakeCraterShapeKernel(const CraterProfile& profile,
278 const CraterApplyOptions& options) {
279 238832 CraterShapeKernel k;
280 238832 k.profile = ClosedFormProfile(profile).value_or(TabulatedProfile(profile));
281 239079 SetShapeOptions(k, profile, options);
282 239046 return k;
283 }
284
285 81909 CraterFrame MakeCraterFrame(const Surface& surface, const Crater& crater) {
286 81909 const Vec2d center{crater.x, crater.y};
287 81909 const Vec3d position = surface.Position(center);
288 81907 const LocalFrame frame = surface.Frame(center);
289 81913 CraterFrame f;
290
2/2
✓ Branch 0 taken 3216 times.
✓ Branch 1 taken 78697 times.
81913 SplitCoordinate(position.x, f.x, f.xLow);
291 81913 SplitCoordinate(position.y, f.y, f.yLow);
292 81913 SplitCoordinate(position.z, f.z, f.zLow);
293 81913 f.eastX = static_cast<float>(frame.east.x);
294 81913 f.eastY = static_cast<float>(frame.east.y);
295 81913 f.eastZ = static_cast<float>(frame.east.z);
296 81913 f.northX = static_cast<float>(frame.north.x);
297 81913 f.northY = static_cast<float>(frame.north.y);
298 81913 f.northZ = static_cast<float>(frame.north.z);
299
2/2
✓ Branch 0 taken 3216 times.
✓ Branch 1 taken 78697 times.
81913 f.sphereRadius = surface.IsSphere() ? static_cast<float>(surface.Radius()) : 0.0f;
300 81913 return f;
301 }
302
303 7186915 SplitPoint MakeSplitPoint(const Surface& surface, const Vec2d& p) {
304
2/2
✓ Branch 0 taken 2465251 times.
✓ Branch 1 taken 4721664 times.
7186915 const Vec3d at = surface.IsBody() ? surface.Position(p) : Vec3d{p.x, p.y, 0.0};
305 7189309 SplitPoint split{};
306 7189309 SplitCoordinate(at.x, split.x, split.xLow);
307 7189309 SplitCoordinate(at.y, split.y, split.yLow);
308 7189309 SplitCoordinate(at.z, split.z, split.zLow);
309 7189309 return split;
310 }
311
312 343293 void AppendKernelTerraces(const PreparedCrater& p, const CraterTerraces& terraces,
313 const CraterProfile& profile, CraterRecord& record,
314 std::vector<float>& data) {
315 343293 const double diameter = p.crater.diameter;
316
4/4
✓ Branch 0 taken 144770 times.
✓ Branch 1 taken 198523 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 144743 times.
343293 if (!terraces.enabled || diameter < terraces.onsetDiameter) {
317 343267 return;
318 }
319 27 const CraterState& state = p.state;
320 54 const double freshness =
321
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 state.fresh.depthRatio > 0.0 ? state.current.depthRatio / state.fresh.depthRatio : 1.0;
322 27 const double strength =
323 27 Smoothstep((freshness - terraces.minFreshness) / (1.0 - terraces.minFreshness));
324
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 time.
27 if (!(strength > 0.0)) {
325 return;
326 }
327 26 KilometerPowerLaw floor{0.187, 1.249};
328
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 6 times.
26 if (const auto* complex = dynamic_cast<const ComplexCraterProfile*>(&profile)) {
329 20 floor = complex->Shape().floorDiameter;
330 }
331
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 const double inner = std::clamp(floor.At(diameter) / diameter, 0.2, 0.9);
332 26 const double radius = 0.5 * diameter;
333 26 const double drop =
334 26 profile.Height(radius, diameter, state) - profile.Height(inner * radius, diameter, state);
335 26 const double meanSlope = drop / ((1.0 - inner) * radius);
336 26 const double benchSlope = std::tan(terraces.benchSlopeDegrees * kDegree);
337 26 const double scarpSlope = std::tan(terraces.scarpSlopeDegrees * kDegree) *
338 (1.0 - 0.5 * kTerraceScarpRounding) * kTerraceScarpSqueeze;
339
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
52 const double slopeShare = meanSlope > 0.0 ? std::clamp(benchSlope / meanSlope, 0.02, 0.5) : 0.5;
340 52 const double size = std::clamp((diameter - terraces.onsetDiameter) /
341 26 (terraces.fullDiameter - terraces.onsetDiameter),
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 0.0, 1.0);
343
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 const IdWords id = SplitId(p.crater.id);
344
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
52 RandomStream random(terraces.seed.value_or(0u), kCraterTerraceStream, id.low, id.high);
345 26 const double spread = terraces.maxBenches - terraces.minBenches;
346 26 const int benches =
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 std::clamp(static_cast<int>(std::lround(terraces.minBenches + spread * size +
348 26 random.NextDouble() - 0.5)),
349 26 terraces.minBenches, terraces.maxBenches);
350 52 const auto harmonics = [&](int first, int count) {
351 26 std::vector<double> pairs;
352 26 double total = 0.0;
353
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 26 times.
156 for (int k = first; k < first + count; ++k) {
354 130 total += 1.0 / k;
355 }
356
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 26 times.
156 for (int k = first; k < first + count; ++k) {
357
1/1
✓ Branch 1 taken 130 times.
130 pairs.push_back(1.0 / k / total);
358
2/2
✓ Branch 1 taken 130 times.
✓ Branch 4 taken 130 times.
130 pairs.push_back(2.0 * kPi * random.NextDouble());
359 }
360 26 return pairs;
361 ✗ };
362 26 const std::vector<double> arcs = harmonics(2, 5);
363 26 const double cover =
364 26 terraces.arcShare >= 1.0
365
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 19 times.
26 ? 1.0
366
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 : terraces.arcShare + std::max(0.0, kFullTerraceCover - terraces.arcShare) * size;
367 26 double cut = -2.0;
368
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
26 if (cover < 1.0) {
369 19 constexpr std::size_t kSamples = 360;
370
1/1
✓ Branch 1 taken 19 times.
19 std::vector<double> values(kSamples);
371
2/2
✓ Branch 0 taken 6840 times.
✓ Branch 1 taken 19 times.
6859 for (std::size_t n = 0; n < kSamples; ++n) {
372 6840 const double theta = 2.0 * kPi * static_cast<double>(n) / double{kSamples};
373 6840 double sum = 0.0;
374
2/2
✓ Branch 0 taken 34200 times.
✓ Branch 1 taken 6840 times.
41040 for (std::size_t k = 0; k < 5; ++k) {
375 34200 sum += arcs[2 * k] *
376 34200 std::cos((static_cast<double>(k) + 2.0) * theta + arcs[2 * k + 1]);
377 }
378 6840 values[n] = sum;
379 }
380 19 const auto at =
381 19 values.begin() + static_cast<std::ptrdiff_t>((1.0 - cover) * double{kSamples - 1});
382 19 std::nth_element(values.begin(), at, values.end());
383 19 cut = *at;
384 19 }
385 26 record.terraceCount = static_cast<std::uint32_t>(benches);
386 26 record.terraceOffset = static_cast<std::uint32_t>(data.size());
387 338 for (const double value :
388
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 26 times.
182 {inner, slopeShare, strength, kTerraceScarpRounding, cut, kTerraceArcWidth}) {
389
1/1
✓ Branch 1 taken 156 times.
156 data.push_back(static_cast<float>(value));
390 }
391
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 26 times.
286 for (const double value : arcs) {
392
1/1
✓ Branch 1 taken 260 times.
260 data.push_back(static_cast<float>(value));
393 }
394 109 const auto scallopCount = [&random] {
395 83 return kMinTerraceScallops +
396 83 std::floor((kMaxTerraceScallops - kMinTerraceScallops + 1.0) * random.NextDouble());
397 26 };
398 26 const double rimScallop =
399
1/1
✓ Branch 1 taken 26 times.
26 kTerraceRimScallop * terraces.rimVariation * strength * (0.6 + 0.8 * random.NextDouble());
400 26 for (const double value :
401
4/4
✓ Branch 1 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 6 taken 104 times.
✓ Branch 7 taken 26 times.
130 {rimScallop, scallopCount(), random.NextDouble(), terraces.blockGaps}) {
402
1/1
✓ Branch 1 taken 104 times.
104 data.push_back(static_cast<float>(value));
403 }
404
1/1
✓ Branch 1 taken 26 times.
26 std::vector<double> wall(kTerraceWallSamples + 1);
405
2/2
✓ Branch 0 taken 6682 times.
✓ Branch 1 taken 26 times.
6708 for (std::size_t n = 0; n < wall.size(); ++n) {
406 6682 const double t = static_cast<double>(n) / kTerraceWallSamples;
407
1/1
✓ Branch 1 taken 6682 times.
6682 const double height = profile.Height((inner + t * (1.0 - inner)) * radius, diameter, state);
408
4/4
✓ Branch 0 taken 6656 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 6604 times.
6734 wall[n] = n == 0 ? height : std::max(height, wall[n - 1]);
409 }
410
1/1
✓ Branch 1 taken 26 times.
26 const double rise = wall.back() - wall.front();
411 135 const auto across = [&](double share) {
412
1/2
✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
109 if (!(rise > 0.0)) {
413 return share;
414 }
415 109 const double target = wall.front() + share * rise;
416 109 const auto above = std::lower_bound(wall.begin(), wall.end(), target);
417
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 26 times.
109 if (above == wall.begin()) {
418 return 0.0;
419 }
420
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (above == wall.end()) {
421 return 1.0;
422 }
423
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 const auto n = static_cast<double>(above - wall.begin());
424
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 const double gap = *above - *(above - 1);
425
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 const double part = gap > 0.0 ? (*above - target) / gap : 0.0;
426 83 return (n - part) / kTerraceWallSamples;
427 26 };
428
1/1
✓ Branch 1 taken 26 times.
26 std::vector<double> heights{0.0};
429 26 double total = 0.0;
430
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 26 times.
109 for (int i = 0; i < benches; ++i) {
431
1/1
✓ Branch 1 taken 83 times.
83 total += 0.5 + random.NextDouble();
432
1/1
✓ Branch 1 taken 83 times.
83 heights.push_back(total);
433 }
434 26 std::vector<double> bases;
435
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 26 times.
135 for (double& height : heights) {
436 109 height /= total;
437
1/1
✓ Branch 2 taken 109 times.
109 bases.push_back(across(height));
438 }
439 26 bases.back() = 1.0;
440 26 const double narrowest = kNarrowestTerraceStep / benches;
441
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 26 times.
83 for (std::size_t k = 1; k + 1 < bases.size(); ++k) {
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 bases[k] = std::max(bases[k], bases[k - 1] + narrowest);
443 }
444
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 26 times.
83 for (std::size_t k = bases.size() - 2; k > 0; --k) {
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 bases[k] = std::min(bases[k], bases[k + 1] - narrowest);
446 }
447 26 const double variation = terraces.rimVariation / (1.0 - inner);
448
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 26 times.
83 for (std::size_t k = 1; k < bases.size() - 1; ++k) {
449 57 const double room =
450
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 18 times.
57 kTerraceScarpRoom * std::min(bases[k] - bases[k - 1], bases[k + 1] - bases[k]);
451
2/3
✓ Branch 1 taken 57 times.
✓ Branch 3 taken 57 times.
✗ Branch 4 not taken.
57 double scallop = std::min(variation * (0.6 + 0.8 * random.NextDouble()), 0.9 * room);
452
1/1
✓ Branch 1 taken 57 times.
57 const double scallops = scallopCount();
453
1/1
✓ Branch 1 taken 57 times.
57 const double phase = random.NextDouble();
454
3/3
✓ Branch 1 taken 57 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 44 times.
57 double wobble = std::min(0.5 * variation * random.NextDouble(), 0.4 * room);
455
1/1
✓ Branch 1 taken 57 times.
57 const double wobblePhase = 2.0 * kPi * random.NextDouble();
456 57 double block = terraces.blockOffset * room;
457 57 const double reach = 2.0 / 3.0 * scallop + wobble + block;
458
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 3 times.
57 if (reach > kTerraceScarpReach * room) {
459 54 const double fit = kTerraceScarpReach * room / reach;
460 54 scallop *= fit;
461 54 wobble *= fit;
462 54 block *= fit;
463 }
464
1/1
✓ Branch 1 taken 57 times.
57 const double key = static_cast<double>(random.NextU32() >> 8u);
465 969 for (const double value :
466
2/2
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 57 times.
513 {bases[k], scallop, scallops, phase, wobble, wobblePhase, block, key}) {
467
1/1
✓ Branch 1 taken 456 times.
456 data.push_back(static_cast<float>(value));
468 }
469 }
470 164864 const auto wallAt = [&](double t) {
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164838 times.
164838 const double at = std::clamp(t, 0.0, 1.0) * kTerraceWallSamples;
472
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 164812 times.
164838 const auto n = std::min(static_cast<std::size_t>(at), wall.size() - 2);
473 164838 return wall[n] + (at - static_cast<double>(n)) * (wall[n + 1] - wall[n]);
474 26 };
475 159386 const auto stairAt = [&](double within, double level, double scarp) {
476 159360 const auto rounding = static_cast<float>(kTerraceScarpRounding);
477 159360 const double lower = scarp * level;
478 159360 const double upper = scarp * (1.0 - level);
479 159360 double stair = level;
480
2/2
✓ Branch 0 taken 54736 times.
✓ Branch 1 taken 104624 times.
159360 if (within < lower) {
481 54736 stair = level * (1.0 - double{KernelTerraceEase(
482
2/2
✓ Branch 0 taken 21530 times.
✓ Branch 1 taken 33206 times.
54736 static_cast<float>(1.0 - within / lower), rounding)});
483
2/2
✓ Branch 0 taken 46035 times.
✓ Branch 1 taken 58589 times.
104624 } else if (within > 1.0 - upper) {
484 92070 stair =
485 46035 level + (1.0 - level) *
486 46035 double{KernelTerraceEase(
487
2/2
✓ Branch 0 taken 17954 times.
✓ Branch 1 taken 28081 times.
46035 static_cast<float>((within - (1.0 - upper)) / upper), rounding)};
488 }
489 159360 return slopeShare * within + (1.0 - slopeShare) * stair;
490 26 };
491 2599 const auto meanHeight = [&](double from, double to, double level, double scarp) {
492 2573 double weighted = 0.0;
493 2573 double weights = 0.0;
494
2/2
✓ Branch 0 taken 164672 times.
✓ Branch 1 taken 2573 times.
167245 for (int n = 0; n < kTerraceMeanSamples; ++n) {
495 164672 const double within = (n + 0.5) / kTerraceMeanSamples;
496 164672 const double r = inner + (from + within * (to - from)) * (1.0 - inner);
497
2/2
✓ Branch 0 taken 159360 times.
✓ Branch 1 taken 5312 times.
164672 const double shaped = level < 0.0 ? within : stairAt(within, level, scarp);
498 164672 weighted += r * wallAt(from + shaped * (to - from));
499 164672 weights += r;
500 }
501 2573 return weighted / weights;
502 26 };
503
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 26 times.
109 for (std::size_t k = 0; k + 1 < bases.size(); ++k) {
504
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 const double from = bases[k];
505 83 const double to = bases[k + 1];
506 83 const double stepSlope =
507 83 (wallAt(to) - wallAt(from)) / ((to - from) * (1.0 - inner) * radius);
508 166 const double needed =
509
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 scarpSlope > benchSlope ? (stepSlope - benchSlope) / (scarpSlope - benchSlope) : 1.0;
510
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 65 times.
83 const double scarp = std::clamp(needed, terraces.scarpShare, kMostTerraceScarpShare);
511 83 const double kept =
512
1/1
✓ Branch 1 taken 83 times.
83 1.0 - Smoothstep((needed - kSteepTerraceStep) / (1.0 - kSteepTerraceStep));
513 83 const double target = meanHeight(from, to, -1.0, scarp);
514 83 const BisectionInterval level =
515 83 Bisect(kTerraceLevelMargin, 1.0 - kTerraceLevelMargin, kTerraceLevelIterations, 0.0,
516 2573 [&](double l) { return !(meanHeight(from, to, l, scarp) < target); });
517
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 83 times.
332 for (const double value : {0.5 * (level.low + level.high), scarp, kept}) {
518
1/1
✓ Branch 1 taken 249 times.
249 data.push_back(static_cast<float>(value));
519 }
520 }
521 26 }
522
523 343485 void AppendKernelPeaks(const PreparedCrater& p, const CraterPeaks& peaks,
524 const CraterProfile& profile, CraterRecord& record,
525 std::vector<float>& data) {
526 343485 const auto* complex = dynamic_cast<const ComplexCraterProfile*>(&profile);
527
4/4
✓ Branch 0 taken 144658 times.
✓ Branch 1 taken 198827 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 144645 times.
343485 if (!peaks.enabled || complex == nullptr ||
528
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 time.
13 p.crater.diameter < complex->Shape().transitionDiameter) {
529 343473 return;
530 }
531 12 const CraterState& state = p.state;
532 12 const double freshness =
533 12 state.fresh.depthRatio > 0.0
534
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
24 ? std::clamp(state.current.depthRatio / state.fresh.depthRatio, 0.0, 1.0)
535 12 : 1.0;
536
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 const IdWords id = SplitId(p.crater.id);
537
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
24 RandomStream random(peaks.seed.value_or(0u), kCraterPeakStream, id.low, id.high);
538 12 const int massifs =
539 12 peaks.minMassifs +
540 24 std::min(peaks.maxMassifs - peaks.minMassifs,
541
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
12 static_cast<int>((peaks.maxMassifs - peaks.minMassifs + 1) * random.NextDouble()));
542 12 record.peakCount = static_cast<std::uint32_t>(massifs);
543 12 record.peakOffset = static_cast<std::uint32_t>(data.size());
544 12 const double key = static_cast<double>(random.NextU32() >> 8u);
545 156 for (const double value : {peaks.roughness * freshness, peaks.roughnessWavelength, peaks.warp,
546
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 12 times.
84 key, peaks.heightScale, peaks.widthScale}) {
547 72 data.push_back(static_cast<float>(value));
548 }
549 12 const double chain = 2.0 * kPi * random.NextDouble();
550 12 const double room = kPeakRoom - peaks.warp;
551
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 12 times.
38 for (int n = 0; n < massifs; ++n) {
552 26 const bool main = n == 0;
553
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
26 const double side = n % 2 == 0 ? kPi : 0.0;
554 18 const double bearing =
555
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 main ? 2.0 * kPi * random.NextDouble()
556 14 : chain + side + peaks.chainSpreadDegrees * kDegree * (random.NextDouble() - 0.5);
557
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
26 const double reach = peaks.spreadRadii * (main ? 0.25 * random.NextDouble()
558 14 : 0.35 + 0.65 * random.NextDouble());
559 26 const double aspect = 1.0 + (peaks.elongation - 1.0) * random.NextDouble();
560 26 const double stretch = std::sqrt(aspect);
561
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 6 times.
26 const double crowding = massifs == 1 ? 1.0 : std::sqrt(2.0 / massifs);
562
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
26 double radius = crowding * (main ? kMainMassifRadius
563 14 : kLesserMassifRadius * (0.5 + random.NextDouble()));
564
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 radius = std::max(0.1, std::min(radius, (room - reach) / stretch));
565 26 const double heading =
566
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
26 main ? 2.0 * kPi * random.NextDouble() : bearing + 0.5 * (random.NextDouble() - 0.5);
567
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
26 const double height = main ? 1.0 : 1.0 - peaks.heightVariation * random.NextDouble();
568 26 const double exponent =
569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 std::clamp((2.0 - 1.5 * peaks.sharpness) * (0.8 + 0.4 * random.NextDouble()), 0.4, 2.5);
570 26 const double pull = kMaxSummitPull * peaks.lopsidedness * std::sqrt(random.NextDouble());
571 26 const double pullBearing = 2.0 * kPi * random.NextDouble();
572 598 for (const double value :
573 26 {reach * std::cos(bearing), reach * std::sin(bearing), radius, stretch,
574 26 std::cos(heading), std::sin(heading), height, exponent, pull * std::cos(pullBearing),
575
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 26 times.
312 pull * std::sin(pullBearing), 2.0 - peaks.sharpness}) {
576 286 data.push_back(static_cast<float>(value));
577 }
578 }
579 }
580
581 239518 CraterKernelBatch MakeCraterKernelBatch(const CraterShapeKernel& kernel,
582 const CraterProfile& profile,
583 const std::vector<PreparedCrater>& prepared,
584 const std::vector<std::size_t>& craters,
585 const CraterApplyOptions& options) {
586 239518 CraterKernelBatch batch;
587 239518 ProfileTables tables(kernel, profile);
588
1/1
✓ Branch 1 taken 239528 times.
239510 tables.AppendAges(batch.data);
589
1/1
✓ Branch 1 taken 239540 times.
239528 batch.records.reserve(craters.size());
590
2/2
✓ Branch 0 taken 343584 times.
✓ Branch 1 taken 239566 times.
583150 for (const std::size_t k : craters) {
591
1/1
✓ Branch 1 taken 343605 times.
343584 const PreparedCrater& p = prepared[k];
592
1/1
✓ Branch 1 taken 343605 times.
343584 CheckProfileState(kernel, p.crater.diameter, p.state);
593 343605 CraterRecord r = BaseRecord(p, options);
594
1/1
✓ Branch 1 taken 343615 times.
343605 SplitCoordinate(p.crater.x, r.x, r.xLow);
595 343605 SplitCoordinate(p.crater.y, r.y, r.yLow);
596
1/1
✓ Branch 1 taken 343615 times.
343605 AppendKernelOutline(p.outline, r, batch.data);
597
1/1
✓ Branch 1 taken 343405 times.
343615 AppendKernelTerraces(p, options.terraces, profile, r, batch.data);
598
1/1
✓ Branch 1 taken 343544 times.
343405 AppendKernelPeaks(p, options.peaks, profile, r, batch.data);
599
1/1
✓ Branch 1 taken 343632 times.
343544 tables.Append(p, r, batch.data);
600
1/1
✓ Branch 1 taken 343610 times.
343632 batch.records.push_back(r);
601 }
602
2/2
✓ Branch 0 taken 148064 times.
✓ Branch 1 taken 91502 times.
239566 if (batch.data.empty()) {
603
1/1
✓ Branch 1 taken 148052 times.
148064 batch.data.push_back(0.0f);
604 }
605 239565 return batch;
606 239555 }
607
608 77468 std::vector<std::uint32_t> FlattenKernelLists(const std::vector<std::vector<std::uint32_t>>& lists,
609 std::vector<std::uint32_t>& heads) {
610 77468 std::vector<std::uint32_t> entries;
611
2/2
✓ Branch 0 taken 1046127 times.
✓ Branch 1 taken 77454 times.
1123581 for (const std::vector<std::uint32_t>& list : lists) {
612
1/1
✓ Branch 1 taken 1046193 times.
1046127 heads.push_back(static_cast<std::uint32_t>(entries.size()));
613
1/1
✓ Branch 1 taken 1046209 times.
1046193 heads.push_back(static_cast<std::uint32_t>(list.size()));
614
1/1
✓ Branch 1 taken 1046113 times.
1046209 entries.insert(entries.end(), list.begin(), list.end());
615 }
616
2/2
✓ Branch 0 taken 15326 times.
✓ Branch 1 taken 62128 times.
77454 if (entries.empty()) {
617
1/1
✓ Branch 1 taken 15333 times.
15326 entries.push_back(0);
618 }
619 77461 return entries;
620 }
621
622 } // namespace terraingen
623