./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
250 / 269
92.9%
Branches:
206 / 249
82.7%
Functions:
38 / 41
92.7%

src/pipeline/terrain.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/pipeline/terrain.h"
4
5 #include "terraingen/layers/evaluation_cache.h"
6 #include "terraingen/random/stream.h"
7 #include "terraingen/util/bits.h"
8 #include "terraingen/util/log.h"
9 #include "terraingen/util/parallel.h"
10 #include "util/checks.h"
11
12 #include <algorithm>
13 #include <cmath>
14 #include <cstdint>
15 #include <memory>
16 #include <optional>
17 #include <stdexcept>
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 namespace terraingen {
23
24 namespace {
25
26 /// Values the point queries of a terrain and its copies keep, such as fitted crater datums.
27 constexpr std::size_t kPointQueryValues = std::size_t{1} << 17U;
28
29 /// Points evaluated together, sharing the features they prepare.
30 constexpr std::size_t kBatch = 256;
31
32 /// The side in cells of the tiles a grid read of the ground is evaluated in, `kBatch` cells each.
33 constexpr std::size_t kTileSide = 16;
34
35 /// The 64-bit seed of part `part` of a terrain reseeded with `seed`: 0 the source, n + 1 layer n.
36 66 std::uint64_t PartSeed(std::uint64_t seed, std::size_t part) {
37 66 const auto word = static_cast<std::uint32_t>(2 * part);
38 66 return JoinWords(ReseedWord(seed, word), ReseedWord(seed, word + 1));
39 }
40
41 /// The sample spacing in meters `options` evaluate `grid` at on `surface`.
42 /// @throws std::invalid_argument for a negative or non-finite spacing.
43 842 double EvaluatedSpacing(const RasterGrid& grid, const EvaluationOptions& options,
44 const Surface& surface) {
45 842 RequireNonNegative(options.spacing, "Sample spacing");
46 840 return ResolvedSpacing(grid, options.spacing, surface);
47 }
48
49 void ReadStack(const Terrain& terrain, std::size_t layers, SampleBlock& samples, double spacing,
50 unsigned threads, EvaluationCache* cache);
51
52 /// The ground beneath layer `layers` of a terrain during one evaluation, whose reads share
53 /// `cache`.
54 2508480 class StackGround final : public Ground {
55 public:
56 2505853 StackGround(const Terrain& terrain, std::size_t layers, EvaluationCache* cache)
57 57 : m_terrain(terrain), m_layers(layers), m_cache(cache) {}
58
59 2504900 LayerContext Context(double spacing, unsigned threads) const override {
60 ✗ LayerContext context = m_terrain.Context(spacing, threads);
61 2503879 context.ground = this;
62 2503879 return context;
63 }
64
65 protected:
66 43811 void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const override {
67 43811 ReadStack(m_terrain, m_layers, samples, spacing, threads, m_cache);
68 43812 }
69
70 private:
71 const Terrain& m_terrain;
72 std::size_t m_layers;
73 EvaluationCache* m_cache;
74 };
75
76 /// Sets `heights` to the terrain's source at `points`, then applies its first `layers` layers to
77 /// them all at once, each on the ground beneath it, which shares `cache`.
78 2366249 void EvaluateStack(const Terrain& terrain, std::size_t layers, Span<const Vec2d> points,
79 Span<float> heights, double spacing, EvaluationCache* cache) {
80
2/2
✓ Branch 0 taken 2029756 times.
✓ Branch 1 taken 336493 times.
2366249 if (terrain.Source()) {
81
2/2
✓ Branch 0 taken 3549 times.
✓ Branch 1 taken 2026207 times.
2033305 terrain.Source()->HeightsAt(points, heights, spacing / terrain.Surface().UnitLength());
82 } else {
83 336493 std::fill(heights.begin(), heights.end(), 0.0f);
84 }
85
2/2
✓ Branch 0 taken 2505140 times.
✓ Branch 1 taken 2375631 times.
4880771 for (std::size_t n = 0; n < layers; ++n) {
86 2505140 const StackGround ground(terrain, n, cache);
87 2505140 SampleBlock samples(points, heights);
88 2504244 LayerContext context = ground.Context(spacing, 1);
89 2503223 context.cache = cache;
90 2503223 terrain.Layers()[n]->Apply(samples, context);
91 2507824 }
92 2375631 }
93
94 /// The Morton code of `point` on a 2^16 lattice over `bounds`.
95 1281699 std::uint32_t MortonKey(const Vec2d& point, const Bounds2d& bounds) {
96 3845097 const auto quantize = [](double value, double low, double high) {
97
1/2
✓ Branch 0 taken 2563398 times.
✗ Branch 1 not taken.
2563398 const double share = high > low ? (value - low) / (high - low) : 0.0;
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2563398 times.
2563398 return static_cast<std::uint32_t>(std::clamp(share, 0.0, 1.0) * 65535.0);
99 };
100 3845097 const auto spread = [](std::uint32_t v) {
101 2563398 v = (v | (v << 8u)) & 0x00FF00FFu;
102 2563398 v = (v | (v << 4u)) & 0x0F0F0F0Fu;
103 2563398 v = (v | (v << 2u)) & 0x33333333u;
104 2563398 v = (v | (v << 1u)) & 0x55555555u;
105 2563398 return v;
106 };
107 1281699 return spread(quantize(point.x, bounds.minX, bounds.maxX)) |
108 1281699 (spread(quantize(point.y, bounds.minY, bounds.maxY)) << 1u);
109 }
110
111 /// The indices of `points`, fewer than 2^32, sorted by their Morton codes over the points'
112 /// bounds, ties by index.
113 95 std::vector<std::size_t> MortonOrder(Span<const Vec2d> points) {
114 95 Bounds2d bounds{points[0].x, points[0].y, points[0].x, points[0].y};
115
2/2
✓ Branch 0 taken 1281699 times.
✓ Branch 1 taken 95 times.
1281794 for (const Vec2d& p : points) {
116 1281699 bounds = {std::fmin(bounds.minX, p.x), std::fmin(bounds.minY, p.y),
117 1281699 std::fmax(bounds.maxX, p.x), std::fmax(bounds.maxY, p.y)};
118 }
119 95 std::vector<std::uint64_t> keyed(points.size());
120
2/2
✓ Branch 0 taken 1281699 times.
✓ Branch 1 taken 95 times.
1281794 for (std::size_t i = 0; i < points.size(); ++i) {
121 1281699 keyed[i] = (std::uint64_t{MortonKey(points[i], bounds)} << 32U) | std::uint64_t{i};
122 }
123 95 std::sort(keyed.begin(), keyed.end());
124
1/1
✓ Branch 1 taken 95 times.
95 std::vector<std::size_t> order(points.size());
125
2/2
✓ Branch 0 taken 1281699 times.
✓ Branch 1 taken 95 times.
1281794 for (std::size_t i = 0; i < points.size(); ++i) {
126 1281699 order[i] = static_cast<std::size_t>(keyed[i] & 0xFFFFFFFFU);
127 }
128 95 return order;
129 95 }
130
131 /// `EvaluateStack` over `points` in batches on `threads` threads, the points taken in Morton order
132 /// so each batch covers a small area and prepares only the features there.
133 2365136 void EvaluatePoints(const Terrain& terrain, std::size_t layers, Span<const Vec2d> points,
134 Span<float> heights, double spacing, unsigned threads, EvaluationCache* cache) {
135
2/2
✓ Branch 0 taken 2365041 times.
✓ Branch 1 taken 95 times.
2365136 if (points.size() <= kBatch) {
136 2365041 EvaluateStack(terrain, layers, points, heights, spacing, cache);
137 2371049 return;
138 }
139 95 const std::vector<std::size_t> order = MortonOrder(points);
140
1/1
✓ Branch 1 taken 95 times.
95 std::vector<Vec2d> sorted(points.size());
141
2/2
✓ Branch 0 taken 1281699 times.
✓ Branch 1 taken 95 times.
1281794 for (std::size_t i = 0; i < points.size(); ++i) {
142 1281699 sorted[i] = points[order[i]];
143 }
144
1/1
✓ Branch 1 taken 95 times.
95 std::vector<float> values(points.size());
145
2/2
✓ Branch 1 taken 95 times.
✓ Branch 4 taken 95 times.
95 ParallelFor(CeilDiv(points.size(), kBatch), threads, [&](std::size_t batch) {
146 5051 const std::size_t begin = batch * kBatch;
147
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4963 times.
5051 const std::size_t count = std::min(kBatch, points.size() - begin);
148 10108 EvaluateStack(terrain, layers, Span<const Vec2d>(sorted).subspan(begin, count),
149 5053 Span<float>(values).subspan(begin, count), spacing, cache);
150 10104 });
151
2/2
✓ Branch 0 taken 1281699 times.
✓ Branch 1 taken 95 times.
1281794 for (std::size_t i = 0; i < points.size(); ++i) {
152 1281699 heights[order[i]] = values[i];
153 }
154 95 }
155
156 /// `EvaluateStack` over the cells of the grid block `samples` in tiles of `kTileSide` squared
157 /// cells, which are already near each other, on `threads` threads.
158 ✗ void EvaluateTiles(const Terrain& terrain, std::size_t layers, const SampleBlock& samples,
159 double spacing, unsigned threads, EvaluationCache* cache) {
160 ✗ const std::size_t width = samples.Grid()->Width();
161 ✗ const std::size_t height = samples.Grid()->Height();
162 ✗ const std::size_t across = CeilDiv(width, kTileSide);
163
0/1
✗ Branch 2 not taken.
✗ ParallelFor(across * CeilDiv(height, kTileSide), threads, [&](std::size_t tile) {
164 ✗ const std::size_t col = (tile % across) * kTileSide;
165 ✗ const std::size_t row = (tile / across) * kTileSide;
166
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const std::size_t cols = std::min(kTileSide, width - col);
167
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const std::size_t rows = std::min(kTileSide, height - row);
168 ✗ std::vector<Vec2d> points(cols * rows);
169
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::size_t k = 0; k < points.size(); ++k) {
170
0/1
✗ Branch 1 not taken.
✗ points[k] = samples.Position((row + k / cols) * width + col + k % cols);
171 }
172
0/1
✗ Branch 1 not taken.
✗ std::vector<float> values(points.size());
173
0/1
✗ Branch 3 not taken.
✗ EvaluateStack(terrain, layers, points, values, spacing, cache);
174
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::size_t k = 0; k < points.size(); ++k) {
175 ✗ samples.Height((row + k / cols) * width + col + k % cols) = values[k];
176 }
177 ✗ });
178 }
179
180 /// Sets `samples` to the terrain's source and first `layers` layers on `threads` threads; the
181 /// reads share `cache`, or one of their own when it is null.
182 2362504 void ReadStack(const Terrain& terrain, std::size_t layers, SampleBlock& samples, double spacing,
183 unsigned threads, EvaluationCache* cache) {
184 2362504 std::optional<EvaluationCache> own;
185
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2362504 times.
✗ Branch 3 not taken.
2362504 EvaluationCache* shared = cache != nullptr ? cache : &own.emplace();
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2362504 times.
2362504 if (samples.IsGrid()) {
187
0/1
✗ Branch 1 not taken.
✗ EvaluateTiles(terrain, layers, samples, spacing, threads, shared);
188 } else {
189
1/1
✓ Branch 1 taken 2370559 times.
2362504 EvaluatePoints(terrain, layers, samples.Points(), samples.Heights(), spacing, threads,
190 shared);
191 }
192 2372025 }
193
194 /// Checks that `source` can be the source of a terrain on `surface`.
195 719 void ValidateSource(const Surface& surface, const Source* source) {
196
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 678 times.
719 if (source == nullptr) {
197 41 return;
198 }
199 678 const GeoKeys keys = source->Keys();
200
5/5
✓ Branch 1 taken 678 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 671 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 4 times.
678 if (keys.IsGeographic() && !surface.IsBody()) {
201 3 throw std::invalid_argument(
202 "The source is in longitude and latitude (a geographic CRS); craters are sized in "
203 "meters, so reproject it first, for example with gdalwarp -t_srs, or set the "
204
1/1
✓ Branch 2 taken 3 times.
3 "terrain's surface to a sphere or ellipsoid");
205 }
206
7/7
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 592 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 78 times.
✓ Branch 5 taken 5 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 1 time.
675 if (surface.IsBody() && !keys.Empty() && !keys.IsGeographic()) {
207 1 throw std::invalid_argument(
208
1/1
✓ Branch 2 taken 1 time.
1 "On a body the source must be in longitude and latitude (a geographic CRS)");
209 }
210 678 }
211
212 /// Checks that `layers` on `surface` over `source` can be evaluated.
213 719 void Validate(const Surface& surface, const std::shared_ptr<const Source>& source,
214 const std::vector<LayerPtr>& layers) {
215 719 ValidateSource(surface, source.get());
216
2/2
✓ Branch 1 taken 1088 times.
✓ Branch 2 taken 702 times.
1794 for (std::size_t i = 0; i < layers.size(); ++i) {
217
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1086 times.
1088 if (!layers[i]) {
218
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A terrain's layers must not be null");
219 }
220 1086 const std::optional<TerrainBase> base = layers[i]->LayerBase();
221
4/4
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 480 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 602 times.
1086 if (base && !(base->surface == surface)) {
222
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
8 throw std::invalid_argument("Layer " + std::to_string(i) +
223
1/1
✓ Branch 2 taken 4 times.
12 " was built on another surface than the terrain's");
224 }
225
6/6
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 480 times.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 482 times.
✓ Branch 4 taken 116 times.
✓ Branch 5 taken 4 times.
1082 if (base && base->source && base->source != source) {
226
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
8 throw std::invalid_argument("Layer " + std::to_string(i) +
227
1/1
✓ Branch 2 taken 4 times.
12 " was built on another source than the terrain's");
228 }
229
1/1
✓ Branch 1 taken 1078 times.
1078 const auto before = layers.begin() + static_cast<std::ptrdiff_t>(i);
230
3/3
✓ Branch 1 taken 1078 times.
✓ Branch 3 taken 67 times.
✓ Branch 4 taken 1075 times.
1142 for (const Layer* dependency : layers[i]->Dependencies()) {
231
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 64 times.
67 if (std::none_of(layers.begin(), before,
232
5/14
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 32 times.
✓ Branch 12 taken 61 times.
✓ Branch 13 taken 1 time.
99 [dependency](const LayerPtr& l) { return l.get() == dependency; })) {
233 3 throw std::invalid_argument(
234
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 "Layer " + std::to_string(i) +
235
1/1
✓ Branch 2 taken 3 times.
9 " reads a layer that does not come before it in the terrain's layers");
236 }
237 1078 }
238 1075 }
239 702 }
240
241 } // namespace
242
243
2/5
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
16 Terrain::Terrain() : Terrain(terraingen::Surface{}, nullptr, {}) {}
244
245 654 Terrain::Terrain(terraingen::Surface surface, std::shared_ptr<const terraingen::Source> source,
246 654 std::vector<LayerPtr> layers)
247
1/1
✓ Branch 1 taken 654 times.
654 : m_surface(std::move(surface)), m_source(std::move(source)), m_layers(std::move(layers)),
248
1/1
✓ Branch 1 taken 654 times.
654 m_cache(std::make_shared<EvaluationCache>(kPointQueryValues)) {
249
1/1
✓ Branch 1 taken 641 times.
654 Validate(m_surface, m_source, m_layers);
250
4/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
691 }
251
252 3 double Terrain::GroundReach(double spacing) const {
253 3 double reach = 0.0;
254
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 for (const LayerPtr& layer : m_layers) {
255 5 reach += layer->GroundReach(spacing);
256 }
257 3 return reach;
258 }
259
260 207 std::optional<RasterGrid> Terrain::Grid() const {
261
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 9 times.
207 return m_source ? BoundedGrid(*m_source) : std::nullopt;
262 }
263
264 157 RasterGrid Terrain::SourceGrid() const {
265 157 const std::optional<RasterGrid> grid = Grid();
266
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 148 times.
157 if (!grid) {
267 9 throw std::invalid_argument(
268 "A terrain without a source, or with a source without bounds such as synthetic "
269
1/1
✓ Branch 2 taken 9 times.
9 "noise, needs a grid");
270 }
271 148 return *grid;
272 }
273
274 118 GeoKeys Terrain::Keys() const {
275
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 2 times.
118 GeoKeys keys = m_source ? m_source->Keys() : GeoKeys{};
276
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 1 time.
118 if (m_surface.IsBody() && keys.Empty()) {
277
1/1
✓ Branch 1 taken 12 times.
12 keys = GeographicSphereKeys(m_surface.Radius());
278 }
279 118 return keys;
280 }
281
282 2506575 LayerContext Terrain::Context(double spacing, unsigned threads) const {
283 2506575 LayerContext context = Ground::Context(spacing, threads);
284 2510879 context.surface = &m_surface;
285 2510879 context.source = m_source.get();
286 2510879 return context;
287 }
288
289 391 Raster<float> Terrain::Evaluate(const RasterGrid& grid, const EvaluationOptions& options) const {
290 391 Raster<float> heights(grid);
291
1/1
✓ Branch 1 taken 388 times.
391 Fill(grid, heights.Values(), options);
292 388 return heights;
293 3 }
294
295 126 Raster<float> Terrain::Evaluate(const EvaluationOptions& options) const {
296 126 return Evaluate(SourceGrid(), options);
297 }
298
299 413 void Terrain::Fill(const RasterGrid& grid, Span<float> heights,
300 const EvaluationOptions& options) const {
301
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 411 times.
413 if (heights.size() != grid.width * grid.height) {
302
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 throw std::invalid_argument("The grid needs " + std::to_string(grid.width * grid.height) +
303
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
8 " heights, not " + std::to_string(heights.size()));
304 }
305 411 Raster<float> view(grid, heights);
306
1/1
✓ Branch 1 taken 409 times.
411 const double spacing = EvaluatedSpacing(grid, options, m_surface);
307
2/2
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 14 times.
409 if (m_source) {
308
3/3
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 365 times.
✓ Branch 3 taken 395 times.
425 m_source->Fill(view, spacing / m_surface.UnitLength(), options.threads);
309 } else {
310 14 std::fill(heights.begin(), heights.end(), 0.0f);
311 }
312
1/1
✓ Branch 1 taken 408 times.
409 ApplyFrom(0, view, options);
313 408 }
314
315 432 void Terrain::ApplyFrom(std::size_t first, Raster<float>& heights,
316 const EvaluationOptions& options) const {
317
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 431 times.
432 if (first > m_layers.size()) {
318
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
2 throw std::invalid_argument("The terrain has " + std::to_string(m_layers.size()) +
319
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
4 " layers, so it has none from layer " + std::to_string(first));
320 }
321 431 const double spacing = EvaluatedSpacing(heights.Grid(), options, m_surface);
322
2/2
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 315 times.
547 const Backend backend = ResolveBackend(options.backend.value_or(DefaultBackend()));
323
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 430 times.
1086 for (std::size_t n = first; n < m_layers.size(); ++n) {
324 656 const Stopwatch stopwatch;
325 656 EvaluationCache cache;
326
1/1
✓ Branch 1 taken 656 times.
656 const StackGround ground(*this, n, &cache);
327
1/1
✓ Branch 1 taken 656 times.
656 LayerContext context = ground.Context(spacing, options.threads);
328 656 context.backend = backend;
329
1/1
✓ Branch 1 taken 655 times.
656 m_layers[n]->Apply(heights, context);
330
2/2
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 429 times.
655 if (options.onLayer) {
331
1/1
✓ Branch 2 taken 226 times.
226 options.onLayer(n, stopwatch.Milliseconds());
332 }
333 656 }
334 430 }
335
336 2323739 void Terrain::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const {
337 2323739 RequirePositive(spacing, "Sample spacing");
338 2319438 ReadStack(*this, m_layers.size(), samples, spacing, threads, m_cache.get());
339 2327259 }
340
341 116 CraterFeatures Terrain::Craters(const RasterGrid& grid, double spacing) const {
342 116 const double resolved = ListedSpacing(grid, spacing, m_surface);
343 116 CraterFeatures placed;
344
1/1
✓ Branch 1 taken 116 times.
116 ForEachLayer<CraterPlacer>(*this, [&](const CraterPlacer& placer, std::size_t) {
345
1/1
✓ Branch 2 taken 123 times.
123 placed.Append(placer.CratersOver(grid, resolved));
346 123 });
347 116 return placed;
348 }
349
350 14 CraterFeatures Terrain::Craters(double spacing) const {
351 14 return Craters(SourceGrid(), spacing);
352 }
353
354 58 BoulderFeatures Terrain::Boulders(const RasterGrid& grid, double spacing) const {
355 58 const double resolved = ListedSpacing(grid, spacing, m_surface);
356 58 BoulderFeatures placed;
357
1/1
✓ Branch 1 taken 58 times.
58 ForEachLayer<BoulderPlacer>(*this, [&](const BoulderPlacer& placer, std::size_t n) {
358 57 const StackGround ground(*this, n, m_cache.get());
359
1/1
✓ Branch 2 taken 57 times.
114 placed.Append(placer.BouldersOver(grid, resolved, &ground));
360 57 });
361 58 return placed;
362 }
363
364 17 BoulderFeatures Terrain::Boulders(double spacing) const {
365 17 return Boulders(SourceGrid(), spacing);
366 }
367
368 20169 Terrain Terrain::Beneath(std::size_t layers) const {
369
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20167 times.
20169 if (layers > m_layers.size()) {
370
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 throw std::invalid_argument("The terrain has " + std::to_string(m_layers.size()) +
371
1/1
✓ Branch 1 taken 2 times.
4 " layers, so it has no ground beneath layer " +
372
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
6 std::to_string(layers));
373 }
374 20167 Terrain prefix = *this;
375
1/1
✓ Branch 1 taken 20167 times.
20167 prefix.m_layers.resize(layers);
376 20167 return prefix;
377 }
378
379 65 void Terrain::Replace(std::shared_ptr<const terraingen::Source> source,
380 const LayerReplacer& replace) {
381 65 std::vector<LayerPtr> layers = m_layers;
382
4/6
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 65 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 65 times.
127 const TerrainBase base{m_surface, source};
383 65 LayerReplacements replaced;
384
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 65 times.
196 for (std::size_t n = 0; n < layers.size(); ++n) {
385
1/1
✓ Branch 1 taken 131 times.
131 LayerPtr layer = replace(n, layers[n]);
386
4/6
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 131 times.
188 layer = layer ? std::move(layer) : layers[n];
387
1/1
✓ Branch 1 taken 131 times.
131 const std::optional<TerrainBase> built = layer->LayerBase();
388
6/6
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 6 times.
131 if (source != m_source && built && built->source == m_source) {
389
2/3
✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
11 if (LayerPtr rebased = layer->Rebased(base)) {
390 11 layer = std::move(rebased);
391 11 }
392 }
393
5/5
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 32 times.
✓ Branch 3 taken 32 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 123 times.
131 if (LayerPtr rebound = replaced.empty() ? nullptr : layer->Rebound(replaced)) {
394 8 layer = std::move(rebound);
395 131 }
396
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 49 times.
131 if (layer != layers[n]) {
397
1/1
✓ Branch 1 taken 82 times.
82 replaced.emplace_back(layers[n].get(), layer);
398 82 layers[n] = std::move(layer);
399 }
400
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 82 times.
180 }
401
1/1
✓ Branch 1 taken 61 times.
65 Validate(m_surface, source, layers);
402 61 m_source = std::move(source);
403 61 m_layers = std::move(layers);
404
2/3
✓ Branch 1 taken 61 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 61 times.
61 m_cache = std::make_shared<EvaluationCache>(kPointQueryValues);
405 73 }
406
407 36 void Terrain::SetLayer(std::size_t index, LayerPtr layer) {
408
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34 times.
36 if (index >= m_layers.size()) {
409
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 throw std::invalid_argument("The terrain has " + std::to_string(m_layers.size()) +
410
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
8 " layers, so it has no layer " + std::to_string(index));
411 }
412
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 33 times.
34 if (!layer) {
413
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A terrain's layers must not be null");
414 }
415
5/7
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 30 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 42 times.
✓ Branch 9 taken 33 times.
✗ Branch 10 not taken.
174 Replace(m_source, [&](std::size_t n, const LayerPtr&) { return n == index ? layer : nullptr; });
416 30 }
417
418 8 void Terrain::SetSource(std::shared_ptr<const terraingen::Source> source) {
419
1/1
✓ Branch 1 taken 7 times.
16 Replace(std::move(source), [](std::size_t, const LayerPtr&) { return nullptr; });
420 7 }
421
422 24 Terrain Terrain::Reseed(std::uint64_t seed) const {
423 24 Terrain reseeded = *this;
424 24 std::shared_ptr<const terraingen::Source> source =
425
3/3
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 23 times.
24 m_source ? m_source->Reseeded(PartSeed(seed, 0)) : nullptr;
426
3/5
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 24 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
62 reseeded.Replace(source ? std::move(source) : m_source,
427
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 15 times.
24 [seed](std::size_t n, const LayerPtr& layer) {
428 43 return layer->Reseeded(PartSeed(seed, n + 1));
429 });
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 return reseeded;
431 24 }
432
433 } // namespace terraingen
434