./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
68 / 68
100.0%
Branches:
48 / 50
96.0%
Functions:
10 / 10
100.0%

src/pipeline/materials.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/pipeline/materials.h"
4
5 #include "terraingen/layers/fade.h"
6 #include "terraingen/pipeline/materials_fields.h"
7 #include "terraingen/util/log.h"
8 #include "terraingen/util/parallel.h"
9 #include "util/checks.h"
10
11 #include <cstddef>
12 #include <optional>
13 #include <utility>
14
15 namespace terraingen {
16
17 namespace {
18
19 /// Width of the patch noise band over which dust comes in.
20 constexpr double kDustEdge = 0.2;
21
22 /// The layers before the first that lays a material: the bare ground's.
23 13523 std::size_t BareLayers(const Terrain& terrain) {
24 13523 const std::vector<LayerPtr>& layers = terrain.Layers();
25 13523 std::size_t n = 0;
26
5/6
✓ Branch 0 taken 17041 times.
✓ Branch 1 taken 10005 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17041 times.
✓ Branch 4 taken 13523 times.
✓ Branch 5 taken 3518 times.
27046 while (n < layers.size() && dynamic_cast<const MaterialSource*>(layers[n].get()) == nullptr) {
27 13523 ++n;
28 }
29 13523 return n;
30 }
31
32 /// `options`, after checking them.
33 13523 const MaterialOptions& Checked(const MaterialOptions& options) {
34 13523 ValidateMaterialOptions(options);
35 13523 return options;
36 }
37
38 /// The shares of a terrain before any material layer: soil, bedrock where the bare ground is
39 /// exposed and dust patches.
40 13523 class GroundCover {
41 public:
42 /// @throws std::invalid_argument as `ValidateMaterialOptions`.
43 13523 GroundCover(const Terrain& terrain, const MaterialOptions& options)
44 13523 : m_surface(terrain.Surface()), m_options(Checked(options)),
45
2/2
✓ Branch 2 taken 13522 times.
✓ Branch 3 taken 1 time.
13523 m_bare(terrain.Beneath(BareLayers(terrain))) {
46
2/2
✓ Branch 0 taken 13522 times.
✓ Branch 1 taken 1 time.
13523 if (options.exposure) {
47
1/1
✓ Branch 1 taken 13522 times.
13522 m_field.emplace(*options.exposure, m_surface);
48 }
49 13523 }
50
51 /// The shares at surface point (`x`, `y`), reading the ground at `spacing` meters.
52 108104 MaterialShares At(double x, double y, double spacing) const {
53 108104 MaterialShares shares = SoilShares();
54
1/2
✓ Branch 0 taken 108115 times.
✗ Branch 1 not taken.
108107 if (m_field) {
55 108115 CoverWith(shares, MaterialClass::Bedrock, m_field->Exposure(m_bare, x, y, spacing));
56 }
57
2/2
✓ Branch 0 taken 98011 times.
✓ Branch 1 taken 10100 times.
108111 if (m_options.dustCover > 0.0) {
58 98011 const double noise = PatchNoise(m_surface, x, y, m_options.dustPatch, m_options.seed);
59 98002 const double threshold = 1.0 - 2.0 * m_options.dustCover;
60 98002 CoverWith(shares, MaterialClass::Dust, Smoothstep((noise - threshold) / kDustEdge));
61 }
62 108100 return shares;
63 }
64
65 private:
66 const Surface& m_surface;
67 const MaterialOptions& m_options;
68 Terrain m_bare;
69 std::optional<ExposureField> m_field;
70 };
71
72 } // namespace
73
74 13535 void ValidateMaterialOptions(const MaterialOptions& options) {
75 13535 CheckRanges(options);
76
2/2
✓ Branch 0 taken 13526 times.
✓ Branch 1 taken 6 times.
13532 if (options.exposure) {
77 13526 ValidateExposureParams(*options.exposure);
78 }
79 13532 }
80
81 13512 MaterialShares MaterialsAt(const Terrain& terrain, const MaterialOptions& options, double x,
82 double y, double spacing) {
83 13512 const GroundCover ground(terrain, options);
84
1/1
✓ Branch 1 taken 13511 times.
13512 RequirePositive(spacing, "Material spacing");
85
1/1
✓ Branch 1 taken 13511 times.
13511 MaterialShares shares = ground.At(x, y, spacing);
86
1/1
✓ Branch 1 taken 13511 times.
13512 ForEachLayer<MaterialSource>(terrain, [&](const MaterialSource& source, std::size_t n) {
87 6581 const Terrain beneath = terrain.Beneath(n);
88
3/3
✓ Branch 1 taken 6581 times.
✓ Branch 4 taken 6581 times.
✓ Branch 7 taken 6581 times.
6581 CoverWith(shares, source.Material(),
89
1/1
✓ Branch 1 taken 6581 times.
6581 source.MaterialCoverAt(x, y, beneath.Context(spacing, 1)));
90 6581 });
91 13511 return shares;
92 13511 }
93
94 11 std::vector<Raster<float>> EvaluateMaterials(const Terrain& terrain, const MaterialOptions& options,
95 const RasterGrid& grid,
96 const EvaluationOptions& evaluation) {
97 11 const GroundCover ground(terrain, options);
98
1/1
✓ Branch 1 taken 10 times.
11 RequireNonNegative(evaluation.spacing, "Material spacing");
99
1/1
✓ Branch 1 taken 10 times.
10 const double resolved = ResolvedSpacing(grid, evaluation.spacing, terrain.Surface());
100
4/4
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 8 times.
✓ Branch 6 taken 10 times.
12 const Backend backend = ResolveBackend(evaluation.backend.value_or(DefaultBackend()));
101
1/1
✓ Branch 1 taken 10 times.
10 std::vector<std::pair<MaterialClass, Raster<double>>> covers;
102
1/1
✓ Branch 1 taken 10 times.
10 ForEachLayer<MaterialSource>(terrain, [&](const MaterialSource& source, std::size_t n) {
103 13 const Stopwatch stopwatch;
104 13 const Terrain beneath = terrain.Beneath(n);
105
1/1
✓ Branch 1 taken 13 times.
13 LayerContext context = beneath.Context(resolved, evaluation.threads);
106 13 context.backend = backend;
107
3/3
✓ Branch 1 taken 13 times.
✓ Branch 4 taken 13 times.
✓ Branch 7 taken 13 times.
13 covers.emplace_back(source.Material(), source.MaterialCoverOver(grid, context));
108
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
13 if (evaluation.onLayer) {
109
1/1
✓ Branch 2 taken 2 times.
2 evaluation.onLayer(n, stopwatch.Milliseconds());
110 }
111 13 });
112
3/3
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
20 std::vector<Raster<float>> bands(kMaterialClasses, Raster<float>(grid));
113
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
10 ParallelFor(grid.height, evaluation.threads, [&](std::size_t row) {
114
2/2
✓ Branch 0 taken 94584 times.
✓ Branch 1 taken 641 times.
95225 for (std::size_t col = 0; col < grid.width; ++col) {
115 94584 const Vec2d center = bands[0].CellCenter(col, row);
116 94584 MaterialShares shares = ground.At(center.x, center.y, resolved);
117
2/2
✓ Branch 1 taken 98295 times.
✓ Branch 2 taken 94596 times.
192891 for (const auto& [material, cover] : covers) {
118 98295 CoverWith(shares, material, cover(col, row));
119 }
120
2/2
✓ Branch 0 taken 469032 times.
✓ Branch 1 taken 94584 times.
563616 for (std::size_t k = 0; k < kMaterialClasses; ++k) {
121 469032 bands[k](col, row) = static_cast<float>(shares[k]);
122 }
123 }
124 641 });
125 20 return bands;
126 20 }
127
128 } // namespace terraingen
129