./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
153 / 155
98.7%
Branches:
176 / 191
92.1%
Functions:
24 / 24
100.0%

src/layers/boulders/boulder_options.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/boulders/boulder_options.h"
4
5 #include "terraingen/distribution/sp8023.h"
6 #include "terraingen/layers/boulders/boulder_abundance.h"
7 #include "terraingen/layers/boulders/boulder_fields.h"
8 #include "terraingen/util/bits.h"
9 #include "util/checks.h"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <stdexcept>
14 #include <string>
15 #include <unordered_set>
16
17 namespace terraingen {
18
19 namespace {
20
21 /// Whether `distribution` may be evaluated from `low` to `high` meters.
22 173 bool Covers(const SizeFrequencyDistribution& distribution, double low, double high) {
23
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 25 times.
173 return distribution.AllowsExtrapolation() ||
24
3/4
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 146 times.
✗ Branch 3 not taken.
148 (distribution.ValidRange().min <= low && distribution.ValidRange().max >= high);
25 }
26
27 /// Throws unless `distribution` is a density valid over the boulder sizes.
28 175 void CheckDistribution(const SizeFrequencyDistribution& distribution, const BoulderSizes& sizes,
29 const char* what) {
30
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 173 times.
175 if (distribution.Kind() != SfdKind::Density) {
31
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
6 throw std::invalid_argument(std::string(what) + " must be a density, not a rate");
32 }
33
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 171 times.
173 if (!Covers(distribution, sizes.minDiameter, sizes.maxDiameter)) {
34
1/1
✓ Branch 1 taken 2 times.
4 throw std::invalid_argument(std::string(what) +
35 " must be valid from minDiameter to maxDiameter, or allow "
36
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
6 "extrapolation");
37 }
38 171 }
39
40 /// Checks what boulder sizes tie together, their ranges checked: min < max diameter, and a density
41 /// distribution covering them or a coverage in (0, 1).
42 463 void CheckBoulderSizes(const BoulderSizes& sizes) {
43
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 461 times.
463 if (!(sizes.maxDiameter > sizes.minDiameter)) {
44
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Boulder diameters must satisfy minDiameter < maxDiameter");
45 }
46
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 314 times.
461 if (sizes.distribution) {
47 147 CheckDistribution(*sizes.distribution, sizes, "The boulder distribution");
48
5/6
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 313 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 313 times.
590 } else if (!(sizes.Coverage() > 0.0 && sizes.Coverage() < 1.0)) {
49
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder coverage must be between 0 and 1");
50 }
51 458 }
52
53 /// Rejects Golombek-Rapp terms that a size distribution would leave unused.
54 458 void CheckGolombekRappTerms(const BoulderLayerOptions& options) {
55 458 const BoulderSizes& sizes = options.sizes;
56 458 const BoulderEjecta& ejecta = options.ejecta;
57
4/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 313 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 143 times.
458 if (sizes.distribution && sizes.coverage) {
58 2 throw std::invalid_argument(
59 "Boulder coverage is only used by Golombek-Rapp sizes; set sizes.distribution to null "
60
1/1
✓ Branch 2 taken 2 times.
2 "for them or leave the coverage unset");
61 }
62
4/4
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 295 times.
✓ Branch 2 taken 160 times.
✓ Branch 3 taken 1 time.
456 if (ejecta.distribution && ejecta.coverage) {
63 1 throw std::invalid_argument(
64 "Boulder ejecta coverage is only used by Golombek-Rapp blocks; set ejecta.distribution "
65
1/1
✓ Branch 2 taken 1 time.
1 "to null for them or leave the coverage unset");
66 }
67
6/6
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 313 times.
✓ Branch 2 taken 131 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 130 times.
✓ Branch 5 taken 1 time.
455 if (sizes.distribution && ejecta.distribution &&
68
3/4
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 130 times.
131 (sizes.qConstant || sizes.qCoverageCoefficient)) {
69 1 throw std::invalid_argument(
70 "The Golombek-Rapp qConstant and qCoverageCoefficient are only used by Golombek-Rapp "
71
1/1
✓ Branch 2 taken 1 time.
1 "sizes or blocks; leave them unset with size distributions");
72 }
73 454 }
74
75 /// Checks what crater blocks that craters throw tie together, their ranges checked: a
76 /// distribution that covers `sizes`, or else a coverage.
77 446 void CheckBoulderEjecta(const BoulderEjecta& ejecta, const BoulderSizes& sizes) {
78
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 383 times.
446 if (!ejecta.ThrowingDiameter()) {
79 return;
80 }
81
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 35 times.
63 if (ejecta.distribution) {
82 28 CheckDistribution(*ejecta.distribution, sizes, "The boulder ejecta distribution");
83
4/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 35 times.
49 } else if (!(ejecta.Coverage() > 0.0 && ejecta.Coverage() < 1.0)) {
84
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Boulder ejecta coverage must be between 0 and 1");
85 }
86 }
87
88 /// Checks what a boulder pose ties together, its ranges checked.
89 471 void CheckBoulderPose(const BoulderPose& pose) {
90
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 470 times.
471 if (!(pose.maxHeightRatio >= pose.minHeightRatio)) {
91
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder height ratios must satisfy min <= max");
92 }
93
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 469 times.
470 if (!(pose.maxBurial >= pose.minBurial)) {
94
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder burial must satisfy min <= max");
95 }
96
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 468 times.
469 if (!(pose.sideShare + pose.endShare <= 1.0)) {
97
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder sideShare and endShare must sum to at most 1");
98 }
99
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 463 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 4 times.
468 if (pose.rollDistance && !pose.maxSlopeDegrees) {
100
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder rollDistance needs a maxSlopeDegrees");
101 }
102 467 }
103
104 /// Checks what the drawing of boulders ties together, its ranges checked.
105 465 void CheckBoulderRendering(const BoulderRendering& rendering) {
106 465 const BoulderReliefRange& range = rendering.reliefRange;
107
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 463 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
465 if (range.maxDiameter && !(*range.maxDiameter > range.minDiameter)) {
108
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder relief maxDiameter must exceed its minDiameter");
109 }
110 464 ValidateBoulderShapeOptions(rendering.shapes);
111 464 }
112
113 } // namespace
114
115 500520 bool BoulderModelThreshold::IsModel(double diameter, double spacing) const {
116
6/6
✓ Branch 0 taken 200084 times.
✓ Branch 1 taken 300436 times.
✓ Branch 2 taken 193750 times.
✓ Branch 3 taken 6334 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 494180 times.
500520 const bool large = (minDiameter && diameter >= *minDiameter) ||
117
6/6
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 494180 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 time.
494186 (minPixels && spacing > 0.0 && diameter >= *minPixels * spacing);
118
6/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6335 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 6334 times.
6337 return large && !(maxDiameter && diameter >= *maxDiameter) &&
119
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6334 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
6336 !(maxPixels && spacing > 0.0 && diameter >= *maxPixels * spacing);
120 }
121
122 1691 std::shared_ptr<const SizeFrequencyDistribution> DefaultBoulderSizes() {
123 1691 static const std::shared_ptr<const SizeFrequencyDistribution> sizes =
124
4/5
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 1515 times.
✓ Branch 3 taken 176 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 176 times.
1867 std::make_shared<TabulatedSfd>(Sp8023IntercraterBlocks(SpecificationCurve::Nominal));
125
1/2
✓ Branch 0 taken 1691 times.
✗ Branch 1 not taken.
3382 return sizes;
126 }
127
128 1357 std::optional<double> BoulderEjecta::ThrowingDiameter() const {
129
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1341 times.
1357 const double excavated = regolithThickness ? *regolithThickness / excavationDepthRatio : 0.0;
130
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1157 times.
1357 const double smallest = std::fmax(minCraterDiameter.value_or(0.0), excavated);
131
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 1155 times.
1357 return smallest > 0.0 ? std::optional<double>(smallest) : std::nullopt;
132 }
133
134 9 double BoulderThrowDistance(double craterDiameter, double regolithThickness, double gravity) {
135
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 7 times.
32 for (const double value : {craterDiameter, regolithThickness, gravity}) {
136 25 RequirePositive(value, "Crater diameter, regolith thickness and gravity");
137 }
138 7 return 138.5 / gravity * std::pow(craterDiameter / regolithThickness, 0.7086);
139 }
140
141 3 void ValidateBoulderPatches(const BoulderPatches& patches) {
142 3 CheckRanges(patches);
143 1 }
144
145 3 void ValidateBoulderFillet(const BoulderFillet& fillet) {
146 3 CheckRanges(fillet);
147 }
148
149 482 void ValidateBoulderClusters(const BoulderClusters& clusters) {
150 482 CheckRanges(clusters);
151
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 474 times.
482 if (clusters.model == BoulderClusterModel::Thomas) {
152
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 7 times.
8 if (!(clusters.radius <= 2.0 * clusters.centerSpacing)) {
153
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Thomas boulder clusters must satisfy radius <= 2 spacing");
154 }
155
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 472 times.
474 } else if (!(clusters.radius < clusters.centerSpacing)) {
156
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Boulder clusters must satisfy radius < spacing");
157 }
158 479 }
159
160 15 void ValidateBoulderFragmentation(const BoulderFragmentation& fragmentation) {
161 15 CheckRanges(fragmentation);
162
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 11 times.
12 if (!(fragmentation.rubbleWear >= fragmentation.splitWear)) {
163
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder fragmentation must satisfy splitWear <= rubbleWear");
164 }
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!(fragmentation.maxPieces >= fragmentation.minPieces)) {
166
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Boulder fragments must satisfy minPieces <= maxPieces");
167 }
168 11 }
169
170 476 void ValidateBoulderVentifacts(const BoulderVentifacts& ventifacts) {
171 476 CheckRanges(ventifacts);
172
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 467 times.
469 if (!(ventifacts.minAngleDegrees <= ventifacts.maxAngleDegrees)) {
173 2 throw std::invalid_argument(
174
1/1
✓ Branch 2 taken 2 times.
2 "Boulder ventifact facet angles must satisfy minAngleDegrees <= maxAngleDegrees");
175 }
176 467 }
177
178 2 BoulderVentifacts GaleVentifacts() {
179 2 BoulderVentifacts ventifacts;
180 2 ventifacts.share = 0.5 * 0.78;
181 2 ventifacts.keelShare = 12.0 / 39.0;
182 2 return ventifacts;
183 }
184
185 50389 double FilletReach(const Boulder& boulder, const BoulderFillet& fillet) {
186 50389 return (0.5 + fillet.widthRatio) * boulder.diameter;
187 }
188
189 11 FilletContact DomeFilletContact(const Boulder& boulder, const BoulderFillet& fillet) {
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 const double share = std::clamp(fillet.heightShare, 0.0, 1.0);
191 11 return {0.5 * boulder.diameter * std::sqrt(1.0 - share * share),
192 11 share * boulder.ExposedHeight()};
193 }
194
195 207717 double FilletHeight(const Boulder& boulder, double distance, const BoulderFillet& fillet,
196 const FilletContact& contact) {
197
3/4
✓ Branch 0 taken 170027 times.
✓ Branch 1 taken 37690 times.
✓ Branch 2 taken 170027 times.
✗ Branch 3 not taken.
207717 if (distance < contact.distance || !(contact.height > 0.0)) {
198 return 0.0;
199 }
200 170027 const double reach =
201 170027 std::fmax(0.5 * boulder.diameter, contact.distance) + fillet.widthRatio * boulder.diameter;
202 170027 const double t = (distance - contact.distance) / (reach - contact.distance);
203
2/2
✓ Branch 0 taken 117339 times.
✓ Branch 1 taken 52688 times.
170027 return t >= 1.0 ? 0.0 : contact.height * (1.0 - t) * (1.0 - t);
204 }
205
206 9 double FilletHeight(const Boulder& boulder, double distance, const BoulderFillet& fillet) {
207 9 return FilletHeight(boulder, distance, fillet, DomeFilletContact(boulder, fillet));
208 }
209
210 139 void ValidateManualBoulder(const ManualBoulder& boulder) {
211 139 CheckRanges(boulder);
212 114 }
213
214 9785 std::uint64_t ManualBoulderId(const ManualBoulder& boulder) {
215
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 9746 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
9785 return boulder.id.value_or(0u) != 0 ? *boulder.id
216 9746 : ManualCraterId(boulder.x, boulder.y, boulder.diameter);
217 }
218
219 536 void ValidateBoulderLayerOptions(const BoulderLayerOptions& options) {
220 536 CheckRanges(options);
221 506 std::unordered_set<std::uint64_t> ids;
222
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 490 times.
603 for (const ManualBoulder& boulder : options.manual) {
223
1/1
✓ Branch 1 taken 101 times.
113 ValidateManualBoulder(boulder);
224
4/4
✓ Branch 1 taken 101 times.
✓ Branch 4 taken 101 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 97 times.
143 if (!ids.insert(ManualBoulderId(boulder)).second) {
225
1/1
✓ Branch 2 taken 4 times.
4 throw std::invalid_argument("Manual boulders must not share an ID");
226 }
227 }
228
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 27 times.
490 if (options.generateRandom) {
229
1/1
✓ Branch 1 taken 458 times.
463 CheckBoulderSizes(options.sizes);
230
1/1
✓ Branch 1 taken 454 times.
458 CheckGolombekRappTerms(options);
231 }
232
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 473 times.
481 if (options.densityMap) {
233
1/1
✓ Branch 1 taken 8 times.
8 ValidateDensityMap(*options.densityMap);
234 }
235
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 472 times.
481 if (options.slopeMap) {
236
1/1
✓ Branch 1 taken 9 times.
9 ValidateSizeSlopeMap(*options.slopeMap);
237
3/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 8 times.
9 if (options.generateRandom && !options.sizes.distribution) {
238 1 throw std::invalid_argument(
239
1/1
✓ Branch 2 taken 1 time.
1 "A boulder size slope map needs a size distribution, not Golombek-Rapp sizes");
240 }
241 }
242
1/1
✓ Branch 1 taken 478 times.
480 ValidateBoulderClusters(options.clusters);
243
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 466 times.
478 if (options.fragmentation) {
244
1/1
✓ Branch 1 taken 11 times.
12 ValidateBoulderFragmentation(*options.fragmentation);
245 }
246
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 459 times.
477 if (options.bedrock.exposure) {
247
1/1
✓ Branch 1 taken 18 times.
18 ValidateExposureParams(*options.bedrock.exposure);
248 }
249
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 27 times.
477 if (options.generateRandom) {
250
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 141 times.
450 if (!options.sizes.distribution &&
251
5/5
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 34 times.
✓ Branch 3 taken 309 times.
✓ Branch 5 taken 305 times.
✓ Branch 6 taken 4 times.
584 options.sizes.Coverage() * MaxBackgroundAbundance(options) >= 1.0) {
252 4 throw std::invalid_argument("Boulder coverage times the patch spread, cluster and "
253
1/1
✓ Branch 2 taken 4 times.
4 "bedrock abundance must stay below 1");
254 }
255
1/1
✓ Branch 1 taken 444 times.
446 CheckBoulderEjecta(options.ejecta, options.sizes);
256 }
257
1/1
✓ Branch 1 taken 467 times.
471 CheckBoulderPose(options.pose);
258
1/1
✓ Branch 1 taken 466 times.
467 ValidateBoulderVentifacts(options.ventifacts);
259
1/1
✓ Branch 1 taken 465 times.
466 ValidateResolutionFade(options.fade);
260
1/1
✓ Branch 1 taken 464 times.
465 CheckBoulderRendering(options.rendering);
261 464 }
262 } // namespace terraingen
263