./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
113 / 113
100.0%
Branches:
129 / 143
90.2%
Functions:
21 / 21
100.0%

src/config/boulders.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "config/boulders.h"
4
5 #include "terraingen/distribution/power_law.h"
6 #include "terraingen/distribution/tabulated.h"
7 #include "terraingen/layers/boulders/boulder_abundance.h"
8 #include "terraingen/mesh/io/mesh_io.h"
9 #include "util/overloaded.h"
10
11 #include <cmath>
12 #include <cstdint>
13 #include <unordered_map>
14 #include <utility>
15
16 namespace terraingen::config {
17
18 namespace {
19
20 /// The distribution a boulder size spec names, or null for the layer's Golombek-Rapp boulders.
21 196 std::shared_ptr<SizeFrequencyDistribution> BuildBoulderSizes(const BoulderSizesSpec& spec) {
22 196 using Result = std::shared_ptr<SizeFrequencyDistribution>;
23 217 const auto density = [](const DensitySpec& named) {
24
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
41 return std::const_pointer_cast<SizeFrequencyDistribution>(BuildDensity(named));
25 };
26 196 return std::visit(
27 391 Overloaded{
28 94 [](const GolombekRappBouldersSpec&) -> Result { return nullptr; },
29 70 [](const Sp8023IntercraterBouldersSpec& s) -> Result {
30 140 return std::make_shared<TabulatedSfd>(Sp8023IntercraterBlocks(s.curve));
31 },
32 10 [](const Sp8023CraterBouldersSpec& s) -> Result {
33 20 return std::make_shared<TabulatedSfd>(Sp8023CraterBlocks(s.curve));
34 },
35 1 [](const SynthterrainBouldersSpec& s) -> Result {
36 1 return std::make_shared<PowerLawSfd>(SynthterrainBoulderDistribution(s.model));
37 },
38
1/1
✓ Branch 1 taken 16 times.
32 [&](const PowerLawSpec& s) -> Result { return density(s); },
39
1/1
✓ Branch 2 taken 1 time.
2 [&](const PiecewisePowerLawSpec& s) -> Result { return density(s); },
40
1/1
✓ Branch 2 taken 1 time.
3 [&](const TabulatedSpec& s) -> Result { return density(s); },
41
1/1
✓ Branch 2 taken 2 times.
4 [&](const PiecewiseUniformSpec& s) -> Result { return density(s); },
42 },
43 196 spec);
44 }
45
46 258 bool IsGolombekRapp(const BoulderSizesSpec& sizes) {
47 258 return std::holds_alternative<GolombekRappBouldersSpec>(sizes);
48 }
49
50 /// @throws ConfigError at a Golombek-Rapp term that is set, such as "coverage", when no size model
51 /// of the layer is Golombek-Rapp and so none would use it.
52 88 void CheckGolombekRappTerms(const BoulderLayerSpec& spec, const std::string& path) {
53 88 const BoulderSizes& sizes = spec.boulders.sizes;
54 88 const bool layer = IsGolombekRapp(spec.sizes);
55
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 79 times.
176 const bool ejecta = IsGolombekRapp(spec.ejectaSizes.value_or(spec.sizes));
56 431 const auto check = [](bool used, const std::optional<double>& value, const std::string& at,
57 const char* fix) {
58
4/4
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 182 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 155 times.
343 if (!used && value) {
59
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
18 throw ConfigError(at, std::string("is only used by Golombek-Rapp sizes; set ") + fix +
60
3/3
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
✓ Branch 8 taken 6 times.
24 " or leave it out");
61 }
62 337 };
63 88 const char* const layerFix = R"("sizes": {"type": "golombek_rapp"})";
64
1/1
✓ Branch 2 taken 86 times.
90 check(layer, sizes.coverage, FieldPath(path, "coverage"), layerFix);
65
1/1
✓ Branch 2 taken 85 times.
87 check(layer || ejecta, sizes.qConstant, FieldPath(path, "qa"), layerFix);
66
1/1
✓ Branch 2 taken 84 times.
86 check(layer || ejecta, sizes.qCoverageCoefficient, FieldPath(path, "qb"), layerFix);
67
2/2
✓ Branch 2 taken 84 times.
✓ Branch 5 taken 82 times.
168 check(ejecta, spec.boulders.ejecta.coverage, FieldPath(FieldPath(path, "ejecta"), "coverage"),
68 R"("ejecta_sizes": {"type": "golombek_rapp"})");
69 82 }
70
71 /// @throws ConfigError at the Golombek-Rapp terms, a missing coverage, or ejecta without a crater
72 /// layer before; the layer checks the rest of its options as it is built.
73 89 void CheckBoulderOptions(const BoulderLayerSpec& spec, const BoulderLayerOptions& options,
74 const std::string& path, bool cratersBefore) {
75
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 1 time.
89 if (!options.generateRandom) {
76 return;
77 }
78 88 CheckGolombekRappTerms(spec, path);
79
6/6
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 43 times.
125 if (IsGolombekRapp(spec.sizes) && !(options.sizes.Coverage() > 0.0)) {
80
1/1
✓ Branch 1 taken 1 time.
2 throw ConfigError(FieldPath(path, "coverage"),
81 "must be above 0 to generate random boulders; set generate_random to "
82
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
3 "false to place only the manual ones");
83 }
84
4/4
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 2 times.
81 if (options.ejecta.ThrowingDiameter() && !cratersBefore) {
85
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 throw ConfigError(FieldPath(FieldPath(path, "ejecta"), "min_crater_diameter_m"),
86
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
6 "ejecta need a crater layer before this one");
87 }
88 }
89
90 /// The distribution a boulder size spec at `path` names, extrapolated when `extrapolate` is set,
91 /// or null for Golombek-Rapp sizes.
92 /// @throws ConfigError at `path` for an invalid distribution.
93 196 std::shared_ptr<SizeFrequencyDistribution> BuildSizes(const BoulderSizesSpec& spec,
94 bool extrapolate, const std::string& path) {
95
1/1
✓ Branch 2 taken 195 times.
392 auto built = At(path, [&] { return BuildBoulderSizes(spec); });
96
4/4
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 89 times.
195 if (built && extrapolate) {
97 12 built->SetAllowExtrapolation(true);
98 }
99 195 return built;
100 }
101
102 /// The rock meshes of a layer's shape files, read in order.
103 /// @throws ConfigError at `path`[i] for an unreadable mesh.
104 std::vector<std::shared_ptr<const SceneMesh>>
105 97 ReadShapeFiles(const std::vector<std::string>& files, const std::string& path,
106 const std::filesystem::path& directory) {
107 97 std::vector<std::shared_ptr<const SceneMesh>> meshes;
108
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 96 times.
98 for (std::size_t i = 0; i < files.size(); ++i) {
109
3/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
4 meshes.push_back(AtFile(ItemPath(path, i), [&] {
110
1/1
✓ Branch 2 taken 1 time.
4 return std::make_shared<const SceneMesh>(ReadMesh(Resolve(files[i], directory)));
111 }));
112 }
113 96 return meshes;
114 1 }
115
116 } // namespace
117
118 99 BoulderLayerOptions BuildBoulderOptions(const BoulderLayerSpec& spec, const std::string& path,
119 const std::filesystem::path& directory) {
120 99 BoulderLayerOptions options = spec.boulders;
121
6/7
✓ Branch 1 taken 99 times.
✓ Branch 3 taken 98 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 98 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 98 times.
296 if (options.sizes.distribution != DefaultBoulderSizes() ||
122
3/7
✓ Branch 1 taken 98 times.
✓ Branch 3 taken 98 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 99 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
197 options.ejecta.distribution != DefaultBoulderSizes()) {
123
1/1
✓ Branch 1 taken 1 time.
2 throw ConfigError(FieldPath(path, "sizes"),
124 "gives the boulder sizes; leave boulders.sizes.distribution and "
125 "boulders.ejecta.distribution at their defaults and set sizes and "
126
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
3 "ejecta_sizes instead");
127 }
128
3/4
✓ Branch 1 taken 98 times.
✓ Branch 4 taken 98 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 98 times.
98 options.sizes.distribution = BuildSizes(spec.sizes, spec.extrapolate, FieldPath(path, "sizes"));
129
2/2
✓ Branch 1 taken 98 times.
✓ Branch 4 taken 97 times.
294 options.ejecta.distribution = BuildSizes(spec.ejectaSizes.value_or(spec.sizes),
130
4/5
✓ Branch 1 taken 98 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 86 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 97 times.
196 spec.extrapolate, FieldPath(path, "ejecta_sizes"));
131
1/1
✓ Branch 1 taken 97 times.
97 std::vector<std::shared_ptr<const SceneMesh>> meshes =
132
2/2
✓ Branch 1 taken 97 times.
✓ Branch 4 taken 96 times.
98 ReadShapeFiles(spec.shapeFiles, FieldPath(path, "shape_files"), directory);
133 96 std::vector<std::shared_ptr<const SceneMesh>>& library = options.rendering.shapes.library;
134
1/1
✓ Branch 1 taken 96 times.
96 library.insert(library.end(), meshes.begin(), meshes.end());
135
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 94 times.
96 if (spec.densityMap) {
136
3/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 options.densityMap = BuildMap(*spec.densityMap, FieldPath(path, "density_map"), directory);
137 }
138
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 93 times.
96 if (spec.slopeMap) {
139
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
4 options.slopeMap = BuildMap(*spec.slopeMap, FieldPath(path, "slope_map"), directory);
140 }
141
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 93 times.
95 if (spec.completeness) {
142
1/1
✓ Branch 1 taken 2 times.
2 options.completeness =
143
3/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 BuildField(*spec.completeness, FieldPath(path, "completeness"), directory);
144 }
145 95 return options;
146 100 }
147
148 93 void CheckSpec(const BoulderLayerSpec& spec, const std::string& path, LayerChecks& checks) {
149 93 const std::string manualPath = FieldPath(path, "manual");
150 93 std::unordered_map<std::uint64_t, std::size_t> ids;
151
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 91 times.
101 for (std::size_t n = 0; n < spec.boulders.manual.size(); ++n) {
152
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
20 At(ItemPath(manualPath, n), [&] { ValidateManualBoulder(spec.boulders.manual[n]); });
153
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
12 const auto added = ids.emplace(ManualBoulderId(spec.boulders.manual[n]), n);
154
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (!added.second) {
155
1/1
✓ Branch 1 taken 2 times.
4 throw ConfigError(ItemPath(manualPath, n),
156
3/3
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
6 "shares its ID with " + ItemPath(manualPath, added.first->second));
157 }
158 }
159 91 checks.boulders = true;
160 93 }
161
162 91 LayerPtr BuildSpec(const BoulderLayerSpec& spec, const std::string& path,
163 const LayerInputs& inputs) {
164 91 BoulderLayerOptions options = BuildBoulderOptions(spec, path, inputs.directory);
165
1/1
✓ Branch 1 taken 80 times.
89 CheckBoulderOptions(spec, options, path, !inputs.craters.empty());
166
5/5
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 79 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 63 times.
80 const bool throws = options.generateRandom && options.ejecta.ThrowingDiameter().has_value();
167 78 return std::make_shared<BoulderLayer>(
168 std::move(options),
169
1/1
✓ Branch 1 taken 16 times.
238 throws ? inputs.craters : std::vector<std::shared_ptr<const CraterLayer>>{}, inputs.base);
170 89 }
171
172 78 void CheckBuilt(const BoulderLayerSpec& /*spec*/, const Layer& built, const std::string& path,
173 LayerChecks& checks) {
174
3/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 1 time.
78 const BoulderLayerOptions& boulders = dynamic_cast<const BoulderLayer&>(built).Options();
175
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 1 time.
78 if (!boulders.generateRandom) {
176 return;
177 }
178 77 const BoulderSizes& sizes = boulders.sizes;
179
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 76 times.
207 for (const OutputGrid& output : checks.outputs) {
180
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 123 times.
131 if (!output.grid) {
181 8 continue;
182 }
183 246 const double minDiameter =
184 123 output.listed
185
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 72 times.
123 ? sizes.minDiameter
186 72 : std::fmax(sizes.minDiameter, boulders.fade.startSamples * output.spacing);
187
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 122 times.
123 if (minDiameter >= sizes.maxDiameter) {
188 1 continue;
189 }
190 122 const double expected =
191
2/3
✓ Branch 2 taken 122 times.
✓ Branch 4 taken 122 times.
✗ Branch 5 not taken.
122 BoulderCandidates(boulders)->ExpectedCount(output.area, minDiameter, sizes.maxDiameter);
192
2/2
✓ Branch 1 taken 122 times.
✓ Branch 4 taken 121 times.
122 CheckExpectedCount(expected, "candidate boulders", "raise min_diameter_m or lower coverage",
193
1/1
✓ Branch 1 taken 122 times.
244 FieldPath(path, "min_diameter_m"));
194 }
195 }
196
197 } // namespace terraingen::config
198