./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
181 / 184
98.4%
Branches:
142 / 165
86.1%
Functions:
39 / 39
100.0%

src/config/layers.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "config/layers.h"
4
5 #include "terraingen/layers/noise_layer.h"
6 #include "terraingen/layers/sand_fill.h"
7 #include "terraingen/layers/sand_shadow.h"
8 #include "terraingen/layers/spectral_continuation.h"
9 #include "terraingen/layers/transforms.h"
10 #include "util/overloaded.h"
11
12 #include <type_traits>
13 #include <utility>
14
15 namespace terraingen {
16
17 233 std::vector<Fractal> NoiseLayerSpec::Fractals() const {
18 233 return StackFractals(stack);
19 }
20
21 7 std::vector<Fractal> NoiseLayerSpec::EstimatedFractals(const BaseRoughness& source) const {
22 7 std::vector<Fractal> fractals;
23
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7 times.
16 for (FractalSpec layer : stack) {
24
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (!layer.hurst) {
25 7 layer.hurst = static_cast<float>(source.hurst);
26 }
27
1/1
✓ Branch 1 taken 9 times.
9 Fractal effective = layer.EffectiveRelief();
28
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1 time.
9 if (!layer.amplitude && effective.enabled) {
29 7 BaseRoughness continued = source;
30
1/1
✓ Branch 1 taken 7 times.
7 continued.hurst = static_cast<double>(*layer.hurst);
31
1/1
✓ Branch 1 taken 7 times.
7 effective.amplitude = MatchedNoiseAmplitude(effective, continued);
32 }
33
1/1
✓ Branch 1 taken 9 times.
9 fractals.push_back(effective);
34 }
35 7 return fractals;
36 }
37
38 14 RoughnessOptions RoughnessLayerSpec::Options() const {
39 14 RoughnessOptions options = roughness;
40
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
14 options.rmsSlope = rmsSlope.value_or(roughness.rmsSlope);
41
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 options.hurst = hurst.value_or(roughness.hurst);
42 14 return options;
43 }
44
45 8 RoughnessOptions RoughnessLayerSpec::EstimatedOptions(BaseRoughness source) const {
46 8 RoughnessOptions options = Options();
47
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 source.hurst = hurst.value_or(source.hurst);
48 8 options.hurst = source.hurst;
49
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
8 options.rmsSlope = rmsSlope.value_or(source.RmsSlope(options.baseline));
50 8 return options;
51 }
52
53 257 std::string LayerType(const LayerSpec& layer) {
54 257 return std::visit(
55 257 [](const auto& spec) {
56 using Spec = std::decay_t<decltype(spec)>;
57
1/1
✓ Branch 16 taken 6 times.
257 std::string type(SpecName<Spec>::kName);
58 if constexpr (std::is_same_v<Spec, MaskedLayerSpec>) {
59
3/3
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
18 type += " (" + LayerType(*spec.layer) + ")";
60 }
61 6 return type;
62 ✗ },
63 257 layer);
64 }
65
66 namespace config {
67
68 /// @name Layer dispatch
69 /// Checks, builds or checks the built layer of any layer spec, through the overloads of its kind.
70 /// @{
71 void CheckLayer(const LayerSpec& spec, const std::string& path, LayerChecks& checks);
72 LayerPtr BuildLayer(const LayerSpec& spec, const std::string& path, const LayerInputs& inputs);
73 void CheckBuiltLayer(const LayerSpec& spec, const Layer& built, const std::string& path,
74 LayerChecks& checks);
75 /// @}
76
77 namespace {
78
79 /// The roughness of the source at its native resolution (`EstimateSourceRoughness`).
80 /// @throws std::invalid_argument without a source, or as `EstimateSourceRoughness`.
81 10 BaseRoughness SourceRoughness(const LayerInputs& inputs) {
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!inputs.source) {
83
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Estimating from the source needs a source");
84 }
85 10 return EstimateSourceRoughness(*inputs.source, inputs.base.surface);
86 }
87
88 255 void CheckSpec(const NoiseLayerSpec& spec, const std::string& path, LayerChecks& checks) {
89
1/1
✓ Branch 2 taken 255 times.
255 CheckNoiseStack(spec.stack, FieldPath(path, "stack"));
90 255 CheckEstimateSource(spec.estimateFromSource, path, checks);
91 254 }
92
93 11 void CheckSpec(const RoughnessLayerSpec& spec, const std::string& path, LayerChecks& checks) {
94 11 CheckEstimateSource(spec.estimateFromSource, path, checks);
95 8 }
96
97 7 void CheckSpec(const SpectralContinuationLayerSpec& spec, const std::string& path,
98 LayerChecks& checks) {
99
6/8
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
7 if (!checks.posts && !(spec.pixelSize && spec.slope && spec.octaveRms)) {
100 1 throw ConfigError(path, "without a source with posts to fit its spectrum to needs "
101
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 "pixel_size_m, slope and octave_rms_m");
102 }
103
1/1
✓ Branch 2 taken 5 times.
12 At(path, [&] { ValidateSpectrumFitBand(spec.fit); });
104 5 }
105
106 2 void CheckSpec(const SandShadowOptions& /*spec*/, const std::string& path, LayerChecks& checks) {
107
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (!checks.boulders) {
108
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError(path, "sand shadows need a boulder layer before this one");
109 }
110 1 }
111
112 20 void CheckSpec(const MaskedLayerSpec& spec, const std::string& path, LayerChecks& checks) {
113
2/2
✓ Branch 2 taken 15 times.
✓ Branch 7 taken 15 times.
60 At(FieldPath(path, "shape"), [&] { ValidateRegionShape(BuildRegionShape(spec.shape)); });
114 30 const bool empty = std::visit(
115 15 Overloaded{
116
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 [](const RectangleRegionSpec& r) { return !(r.maxX > r.minX && r.maxY > r.minY); },
117 11 [](const CircleRegionSpec& c) { return !(c.radius > 0.0); },
118 [](const PolygonRegionSpec&) { return false; },
119 },
120 15 spec.shape);
121
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (empty) {
122
1/1
✓ Branch 1 taken 2 times.
4 throw ConfigError(FieldPath(path, "shape"),
123 "required: the shape has no area; give the rectangle's bounds or the "
124
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
6 "circle's radius");
125 }
126 13 LayerChecks inner = checks;
127
2/2
✓ Branch 1 taken 13 times.
✓ Branch 4 taken 13 times.
26 CheckLayer(*spec.layer, FieldPath(path, "layer"), inner);
128 13 }
129
130 12 void CheckBuilt(const MaskedLayerSpec& spec, const Layer& built, const std::string& path,
131 LayerChecks& checks) {
132 12 LayerChecks inner = checks;
133
3/4
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 12 times.
12 CheckBuiltLayer(*spec.layer, dynamic_cast<const MaskedLayer&>(built).Inner(),
134
1/1
✓ Branch 1 taken 12 times.
24 FieldPath(path, "layer"), inner);
135 12 }
136
137 233 LayerPtr BuildSpec(const NoiseLayerSpec& spec, const std::string& /*path*/,
138 const LayerInputs& inputs) {
139 233 const std::vector<Fractal> fractals =
140
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 228 times.
233 spec.estimateFromSource ? spec.EstimatedFractals(SourceRoughness(inputs)) : spec.Fractals();
141
2/2
✓ Branch 1 taken 233 times.
✓ Branch 4 taken 233 times.
466 return std::make_shared<NoiseLayer>(NoiseLayerOptions{fractals, spec.sourceResolvesPixels});
142 233 }
143
144 8 LayerPtr BuildSpec(const RoughnessLayerSpec& spec, const std::string& /*path*/,
145 const LayerInputs& inputs) {
146 8 const RoughnessOptions options =
147
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 spec.estimateFromSource ? spec.EstimatedOptions(SourceRoughness(inputs)) : spec.Options();
148 8 return std::make_shared<RoughnessLayer>(options);
149 }
150
151 /// A spectral continuation layer, fitted to the source wherever the spec gives no slope or
152 /// amplitude; a valid configuration has a source whenever the spec leaves one out.
153 5 LayerPtr BuildSpec(const SpectralContinuationLayerSpec& spec, const std::string& /*path*/,
154 const LayerInputs& inputs) {
155 5 const Surface& surface = inputs.base.surface;
156 5 SpectralContinuationOptions options = spec.options;
157
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 const RasterGrid grid = inputs.source ? inputs.source->Grid() : RasterGrid{};
158
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 3 times.
5 options.pixelSize = spec.pixelSize.value_or(grid.PixelSize() * surface.UnitLength());
159 5 PowerLawSpectrum fit{options.slope, options.octaveRms};
160
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
5 if (!(spec.slope && spec.octaveRms)) {
161
1/1
✓ Branch 1 taken 2 times.
2 fit = EstimatePowerLawSpectrum(SampleSourceWindow(*inputs.source,
162 2 options.pixelSize / surface.UnitLength(),
163 2 grid.transform.PixelToWorld(0.5, 0.5)),
164 2 spec.fit);
165 }
166
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 options.slope = spec.slope.value_or(fit.slope);
167
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 options.octaveRms = spec.octaveRms.value_or(fit.octaveRms);
168 5 return std::make_shared<SpectralContinuationLayer>(options);
169 }
170
171 13 LayerPtr BuildSpec(const MaskedLayerSpec& spec, const std::string& path,
172 const LayerInputs& inputs) {
173
3/4
✓ Branch 1 taken 13 times.
✓ Branch 4 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
26 return std::make_shared<MaskedLayer>(BuildLayer(*spec.layer, FieldPath(path, "layer"), inputs),
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
37 BuildRegionShape(spec.shape), spec.feather, spec.blend);
175 }
176
177 8 LayerPtr BuildSpec(const DuneOptions& spec, const std::string& /*path*/, const LayerInputs&) {
178 8 return std::make_shared<DuneLayer>(spec);
179 }
180
181 5 LayerPtr BuildSpec(const BedformOptions& spec, const std::string& /*path*/, const LayerInputs&) {
182 5 return std::make_shared<BedformLayer>(spec);
183 }
184
185 2 LayerPtr BuildSpec(const PavementOptions& spec, const std::string& /*path*/, const LayerInputs&) {
186 2 return std::make_shared<PavementLayer>(spec);
187 }
188
189 2 LayerPtr BuildSpec(const StrataOptions& spec, const std::string& /*path*/, const LayerInputs&) {
190 2 return std::make_shared<StrataLayer>(spec);
191 }
192
193 1 LayerPtr BuildSpec(const SandShadowOptions& spec, const std::string& /*path*/,
194 const LayerInputs& inputs) {
195 1 return std::make_shared<SandShadowLayer>(inputs.boulders, spec);
196 }
197
198 1 LayerPtr BuildSpec(const SandFillOptions& spec, const std::string& /*path*/, const LayerInputs&) {
199 1 return std::make_shared<SandFillLayer>(spec);
200 }
201
202 4 LayerPtr BuildSpec(const ScaleLayerSpec& spec, const std::string& /*path*/, const LayerInputs&) {
203 4 return std::make_shared<ScaleLayer>(spec.scale, spec.offset);
204 }
205
206 5 LayerPtr BuildSpec(const ClampLayerSpec& spec, const std::string& /*path*/, const LayerInputs&) {
207 5 return std::make_shared<ClampLayer>(spec.min, spec.max);
208 }
209
210 /// The secondaries the secondary layers after the crater layer `layers[index]` throw from it, up
211 /// to the next crater layer; none when `layers[index]` is not a crater layer.
212 640 std::vector<SecondaryCraterOptions> ThrownSecondaries(const std::vector<LayerSpec>& layers,
213 std::size_t index) {
214
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 376 times.
640 std::vector<SecondaryCraterOptions> thrown;
215
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 376 times.
640 if (!std::holds_alternative<CraterLayerSpec>(layers[index])) {
216 return thrown;
217 }
218 264 for (std::size_t j = index + 1;
219
4/4
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 255 times.
✓ Branch 2 taken 118 times.
✓ Branch 3 taken 9 times.
382 j < layers.size() && !std::holds_alternative<CraterLayerSpec>(layers[j]); ++j) {
220
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
236 if (const auto* secondaries = std::get_if<SecondaryLayerSpec>(&layers[j])) {
221
1/1
✓ Branch 1 taken 7 times.
7 thrown.push_back(secondaries->secondaries);
222 }
223 }
224 return thrown;
225 }
226
227 } // namespace
228
229 712 void CheckLayer(const LayerSpec& spec, const std::string& path, LayerChecks& checks) {
230 1397 std::visit([&](const auto& layer) { CheckSpec(layer, path, checks); }, spec);
231 690 }
232
233 653 LayerPtr BuildLayer(const LayerSpec& spec, const std::string& path, const LayerInputs& inputs) {
234 1306 return At(path, [&] {
235 1306 return std::visit([&](const auto& layer) { return BuildSpec(layer, path, inputs); }, spec);
236 653 });
237 }
238
239 626 void CheckBuiltLayer(const LayerSpec& spec, const Layer& built, const std::string& path,
240 LayerChecks& checks) {
241 348 std::visit([&](const auto& layer) { CheckBuilt(layer, built, path, checks); }, spec);
242 623 }
243
244 266 void CheckEstimateSource(bool estimate, const std::string& path, const LayerChecks& checks) {
245
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 252 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 10 times.
266 if (estimate && !checks.posts) {
246
1/1
✓ Branch 1 taken 4 times.
8 throw ConfigError(FieldPath(path, "estimate_from_source"),
247 "needs a source with posts to estimate from, a raster or synthetic noise "
248
2/2
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
12 "with a pixel size; without one give the values instead");
249 }
250 262 }
251
252 36 RegionShape BuildRegionShape(const RegionShapeSpec& spec) {
253 36 return std::visit(
254 36 Overloaded{
255 5 [](const RectangleRegionSpec& s) { return RegionShape::Rectangle(s); },
256 22 [](const CircleRegionSpec& s) { return RegionShape::Circle({s.x, s.y}, s.radius); },
257 9 [](const PolygonRegionSpec& s) {
258 22 const auto vertices = [](const std::vector<std::array<double, 2>>& pairs) {
259
1/1
✓ Branch 1 taken 13 times.
13 std::vector<Vec2d> ring;
260
1/1
✓ Branch 1 taken 13 times.
13 ring.reserve(pairs.size());
261
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 13 times.
59 for (const std::array<double, 2>& p : pairs) {
262
1/1
✓ Branch 1 taken 46 times.
46 ring.push_back({p[0], p[1]});
263 }
264 13 return ring;
265 ✗ };
266
1/1
✓ Branch 1 taken 9 times.
9 std::vector<std::vector<Vec2d>> holes;
267
1/1
✓ Branch 1 taken 9 times.
9 holes.reserve(s.holes.size());
268
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 for (const auto& hole : s.holes) {
269
1/1
✓ Branch 1 taken 4 times.
8 holes.push_back(vertices(hole));
270 }
271
1/1
✓ Branch 1 taken 9 times.
18 return RegionShape::Polygon(vertices(s.points), std::move(holes));
272 9 },
273 },
274 36 spec);
275 }
276
277 396 BuiltLayers BuildLayers(const std::vector<LayerSpec>& layers,
278 const std::vector<OutputGrid>& outputs, const LayerSources& sources,
279 const std::filesystem::path& directory, const Surface& surface) {
280 396 LayerChecks checks{outputs};
281 396 checks.posts = sources.posts;
282
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 374 times.
1073 for (std::size_t i = 0; i < layers.size(); ++i) {
283
3/3
✓ Branch 1 taken 699 times.
✓ Branch 4 taken 699 times.
✓ Branch 7 taken 677 times.
1449 CheckLayer(layers[i], ItemPath("layers", i), checks);
284 }
285 374 BuiltLayers built;
286 374 std::vector<std::shared_ptr<const CraterLayer>> craters;
287 374 std::shared_ptr<const BoulderLayer> boulders;
288
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 345 times.
985 for (std::size_t i = 0; i < layers.size(); ++i) {
289
3/4
✓ Branch 1 taken 640 times.
✓ Branch 4 taken 640 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
1309 const std::string path = ItemPath("layers", i);
290
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 602 times.
640 const LayerInputs inputs{TerrainBase{surface, sources.measured},
291 directory,
292 craters,
293 boulders,
294 640 sources.given,
295
3/7
✓ Branch 3 taken 640 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 640 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 640 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
1309 ThrownSecondaries(layers, i)};
296
1/1
✓ Branch 1 taken 614 times.
640 const LayerPtr layer = BuildLayer(layers[i], path, inputs);
297
1/1
✓ Branch 1 taken 614 times.
614 built.layers.push_back(layer);
298
1/1
✓ Branch 1 taken 611 times.
614 CheckBuiltLayer(layers[i], *layer, path, checks);
299
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 359 times.
611 if (std::holds_alternative<CraterLayerSpec>(layers[i])) {
300
4/7
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 252 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 252 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
507 craters.push_back(std::static_pointer_cast<const CraterLayer>(layer));
301 }
302
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 535 times.
611 if (std::holds_alternative<BoulderLayerSpec>(layers[i])) {
303
2/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 76 times.
152 boulders = std::static_pointer_cast<const BoulderLayer>(layer);
304 }
305 1283 }
306
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 150 times.
345 if (!checks.expectedCraters.empty()) {
307
1/2
✓ Branch 0 taken 195 times.
✗ Branch 1 not taken.
390 built.expectedCraters =
308
1/2
✓ Branch 0 taken 195 times.
✗ Branch 1 not taken.
390 *std::max_element(checks.expectedCraters.begin(), checks.expectedCraters.end());
309 }
310
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 270 times.
345 return built;
311 403 }
312
313 } // namespace config
314
315 } // namespace terraingen
316