./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
274 / 274
100.0%
Branches:
249 / 284
87.7%
Functions:
29 / 29, 1 excl
100.0%

src/layers/boulders/boulder_layer.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/boulders/boulder_layer.h"
4
5 #include "layers/boulders/boulder_clusters.h"
6 #include "layers/craters/generation/cell_cache.h"
7
8 #include "terraingen/geometry/cells.h"
9 #include "terraingen/layers/craters/ages/age_model.h"
10 #include "terraingen/layers/source_resolved.h"
11 #include "terraingen/noise/hash.h"
12 #include "terraingen/random/stream.h"
13 #include "terraingen/raster/grid.h"
14 #include "terraingen/util/bits.h"
15 #include "terraingen/util/parallel.h"
16 #include "terraingen/util/span.h"
17
18 #include <algorithm>
19 #include <cmath>
20 #include <limits>
21 #include <memory>
22 #include <optional>
23 #include <stdexcept>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27
28 namespace terraingen {
29
30 namespace {
31
32 /// The ground under one candidate: its heights at a few points, at one spacing, read up front
33 /// with every other candidate's; any other point or spacing is read from the ground beneath.
34 /// Gives the same heights either way.
35 325360 class CandidateGround final : public Ground {
36 public:
37 325360 CandidateGround(const Ground& ground, Span<const Vec2d> points, Span<const float> heights,
38 double spacing)
39 325360 : m_ground(ground), m_points(points), m_heights(heights), m_spacing(spacing) {}
40
41 protected:
42 686163 void FillSamples(SampleBlock& samples, double spacing, unsigned /*threads*/) const override {
43
2/2
✓ Branch 0 taken 1773495 times.
✓ Branch 1 taken 686163 times.
2459658 for (std::size_t i = 0; i < samples.Size(); ++i) {
44 1773495 const Vec2d p = samples.Position(i);
45 1773495 const std::optional<float> held = Held(p, spacing);
46
2/2
✓ Branch 0 taken 565795 times.
✓ Branch 1 taken 1207700 times.
1773495 samples.Height(i) = held ? *held : m_ground.HeightAt(p.x, p.y, spacing);
47 }
48 686163 }
49
50 private:
51 /// The height read up front at `p` and `spacing`, if any.
52 1773495 std::optional<float> Held(const Vec2d& p, double spacing) const {
53
1/2
✓ Branch 0 taken 1773495 times.
✗ Branch 1 not taken.
1773495 if (spacing == m_spacing) {
54
2/2
✓ Branch 0 taken 7231643 times.
✓ Branch 1 taken 1207700 times.
8439343 for (std::size_t n = 0; n < m_points.size(); ++n) {
55
2/2
✓ Branch 1 taken 753451 times.
✓ Branch 2 taken 6478192 times.
7231643 if (BitCast<std::uint64_t>(m_points[n].x) == BitCast<std::uint64_t>(p.x) &&
56
2/2
✓ Branch 0 taken 565795 times.
✓ Branch 1 taken 187656 times.
753451 BitCast<std::uint64_t>(m_points[n].y) == BitCast<std::uint64_t>(p.y)) {
57 565795 return m_heights[n];
58 }
59 }
60 }
61 1207700 return std::nullopt;
62 }
63
64 const Ground& m_ground;
65 Span<const Vec2d> m_points;
66 Span<const float> m_heights;
67 double m_spacing;
68 };
69
70 /// How far in radii a boulder shape can reach from its center: half the diagonal of its unit box
71 /// over half its longest axis, for a shape tilted or resting on a side.
72 constexpr double kShapeReachRadii = 1.7320508075688772;
73
74 } // namespace
75
76 1715337 RandomStream BoulderLayer::AttributeStream(std::uint64_t id, BoulderDraw draw) const {
77 1715337 const IdWords words = SplitId(id);
78 1715337 return RandomStream(m_options.seed, kBoulderAttributeStream, words.low, words.high,
79 1715337 static_cast<std::uint32_t>(draw));
80 }
81
82 250 BoulderLayer::BoulderLayer(BoulderLayerOptions options, std::shared_ptr<const CraterLayer> craters,
83 250 TerrainBase base)
84 : BoulderLayer(std::move(options),
85
13/15
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 198 times.
✓ Branch 3 taken 52 times.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 167 times.
✓ Branch 7 taken 49 times.
✓ Branch 8 taken 49 times.
✓ Branch 9 taken 49 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 31 times.
✓ Branch 13 taken 3 times.
✓ Branch 14 taken 3 times.
✓ Branch 15 taken 3 times.
✗ Branch 16 not taken.
604 craters ? std::vector<std::shared_ptr<const CraterLayer>>{std::move(craters)}
86 : std::vector<std::shared_ptr<const CraterLayer>>{},
87
2/2
✓ Branch 2 taken 52 times.
✓ Branch 6 taken 216 times.
784 std::move(base)) {}
88
89 360 BoulderLayer::BoulderLayer(BoulderLayerOptions options,
90 std::vector<std::shared_ptr<const CraterLayer>> craters,
91 360 TerrainBase base)
92
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 356 times.
360 : m_options(std::move(options)), m_craters(std::move(craters)),
93
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 356 times.
360 m_surface(std::move(base.surface)), m_source(std::move(base.source)),
94
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 356 times.
364 m_completeness(m_options.completeness) {
95
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 358 times.
451 for (const auto& layer : m_craters) {
96
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 92 times.
93 if (!layer) {
97
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A boulder layer's crater layers must not be null");
98 }
99
2/2
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 91 times.
92 if (!(layer->LayerBase()->surface == m_surface)) {
100 1 throw std::invalid_argument(
101
1/1
✓ Branch 2 taken 1 time.
1 "A boulder layer and its crater layer must share a surface");
102 }
103 }
104
1/1
✓ Branch 1 taken 324 times.
358 ValidateBoulderLayerOptions(m_options);
105
3/3
✓ Branch 1 taken 324 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 323 times.
324 if (auto limit = LayerResolvedLimit(m_source, m_options.sourceResolvesPixels, m_surface)) {
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 m_completeness = m_completeness
107
2/7
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
2 ? std::make_shared<const MinimumField>(m_completeness, limit)
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 : std::move(limit);
109 }
110
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 324 times.
405 for (const ManualBoulder& boulder : m_options.manual) {
111
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 41 times.
81 if (boulder.FixesAny()) {
112
2/2
✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
40 m_fixed.emplace(ManualBoulderId(boulder), boulder);
113 }
114 81 m_largestManual = std::fmax(m_largestManual, boulder.diameter);
115 }
116
2/3
✓ Branch 1 taken 324 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 324 times.
324 m_cells = MakeCells(m_surface);
117
1/1
✓ Branch 1 taken 324 times.
324 SetUpShapeCaches();
118
2/2
✓ Branch 0 taken 302 times.
✓ Branch 1 taken 22 times.
324 if (!m_options.generateRandom) {
119 return;
120 }
121
3/3
✓ Branch 1 taken 302 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 259 times.
302 m_minCraterDiameter = m_options.ejecta.ThrowingDiameter().value_or(0.0);
122
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 42 times.
302 if (m_minCraterDiameter > 0.0 && m_craters.empty()) {
123 1 throw std::invalid_argument(
124
1/1
✓ Branch 2 taken 1 time.
1 "Boulder ejecta need the crater layer whose craters throw them");
125 }
126
1/1
✓ Branch 1 taken 301 times.
301 SetUpAbundance();
127
1/2
✓ Branch 0 taken 301 times.
✗ Branch 1 not taken.
301 if (m_options.cacheBoulders > 0) {
128
2/3
✓ Branch 1 taken 301 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 301 times.
301 m_cache = std::make_shared<CraterCellCache>(m_options.cacheBoulders);
129 }
130
1/1
✓ Branch 1 taken 301 times.
301 m_exposure = LayerExposure(m_options.bedrock.exposure, m_options.groundSpacing,
131 301 "Boulders that read the exposure field");
132
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9 times.
301 if (m_exposure && m_options.densityMap) {
133
1/1
✓ Branch 1 taken 2 times.
2 SetUpBedrockMeans();
134 }
135
13/20
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 37 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 37 times.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 36 times.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 36 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 37 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 37 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 37 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 37 times.
77 }
136
137 18 std::shared_ptr<BoulderLayer> BoulderLayer::Manual(std::vector<ManualBoulder> boulders,
138 BoulderLayerOptions options, TerrainBase base) {
139
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 17 times.
18 if (!options.manual.empty()) {
140 1 throw std::invalid_argument(
141
1/1
✓ Branch 2 taken 1 time.
1 "BoulderLayer::Manual takes its boulders as an argument; leave options.manual empty");
142 }
143 17 options.generateRandom = false;
144 17 options.manual = std::move(boulders);
145
1/1
✓ Branch 1 taken 16 times.
17 return std::make_shared<BoulderLayer>(std::move(options), std::shared_ptr<const CraterLayer>{},
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 std::move(base));
147 }
148
149 13 LayerPtr BoulderLayer::Reseeded(std::uint64_t seed) const {
150 13 BoulderLayerOptions options = m_options;
151 13 options.seed = ReseedWord(seed, 0);
152 13 options.rendering.shapes.seed = ReseedWord(seed, 1);
153
1/1
✓ Branch 2 taken 13 times.
26 return std::make_shared<BoulderLayer>(std::move(options), m_craters, *LayerBase());
154 13 }
155
156 22 LayerPtr BoulderLayer::Rebound(const LayerReplacements& replaced) const {
157 22 std::vector<std::shared_ptr<const CraterLayer>> craters;
158
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 22 times.
40 for (const std::shared_ptr<const CraterLayer>& layer : m_craters) {
159
2/3
✓ Branch 2 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
18 craters.push_back(Replacement(layer, replaced));
160 }
161
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 12 times.
22 if (craters == m_craters) {
162 10 return nullptr;
163 }
164
1/1
✓ Branch 2 taken 12 times.
12 return std::make_shared<BoulderLayer>(m_options, std::move(craters), *LayerBase());
165 22 }
166
167 3 LayerPtr BoulderLayer::Rebased(const TerrainBase& base) const {
168 3 return std::make_shared<BoulderLayer>(m_options, m_craters, base);
169 }
170
171 65856 double BoulderLayer::MaxFeatureDiameter() const {
172
2/2
✓ Branch 0 taken 65855 times.
✓ Branch 1 taken 1 time.
65856 return std::fmax(m_options.generateRandom ? m_options.sizes.maxDiameter : 0.0, m_largestManual);
173 }
174
175 301 void BoulderLayer::SetUpAbundance() {
176 301 m_background.emplace(m_options);
177 301 m_clusterSeed = SubstreamSeed(m_options.seed, kBoulderClusterStream);
178 301 m_patchSeed = SubstreamSeed(m_options.seed, kBoulderPatchStream);
179
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 301 times.
301 m_candidates = BoulderCandidates(m_options, m_background->max);
180
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 298 times.
301 if (m_options.slopeMap) {
181 3 m_referenceCount =
182 3 m_options.sizes.distribution->Cumulative(m_options.slopeMap->referenceDiameter);
183 }
184
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 291 times.
301 if (m_options.clusters.share > 0.0) {
185 10 m_clusterMean = m_options.clusters.model == BoulderClusterModel::Thomas
186
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 ? MeanThomasField(m_options.clusters, m_surface.IsBody())
187 4 : MeanClusterBump(m_options.clusters);
188 }
189
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 259 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 301 times.
301 m_ejectaSizes = m_minCraterDiameter > 0.0 ? EjectaSizes(m_options) : nullptr;
190 301 }
191
192 2 void BoulderLayer::SetUpBedrockMeans() {
193 2 constexpr std::size_t kMeanCells = 1u << 16;
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 const double side = m_options.bedrock.meanSize / m_surface.UnitLength();
195 2 m_bedrockCellSize = {side, side};
196
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
2 if (const auto* raster = dynamic_cast<const RasterField*>(m_options.densityMap->field.get())) {
197 1 const GeoTransform& transform = raster->Values().Transform();
198 1 m_bedrockOrigin = {transform.originX, transform.originY};
199 1 m_bedrockCellSize = {transform.pixelWidth, transform.pixelHeight};
200 }
201 2 m_bedrockMeans = std::make_shared<LruCache<BedrockCell, double, BedrockCellHash>>(
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 kMeanCells, [](const double&) { return std::size_t{1}; });
203 2 }
204
205 324 void BoulderLayer::SetUpShapeCaches() {
206 324 const std::uint32_t prototypes = m_options.rendering.shapePrototypes;
207 972 m_shapes = std::make_shared<LruCache<std::uint64_t, BoulderShape>>(
208
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 321 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 324 times.
327 prototypes > 0 ? prototypes : 4096, [](const BoulderShape&) { return std::size_t{1}; });
209 972 m_fragments = std::make_shared<LruCache<std::uint64_t, std::vector<BoulderFragment>>>(
210 324 prototypes > 0 ? prototypes : 4096,
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 324 times.
324 [](const std::vector<BoulderFragment>&) { return std::size_t{1}; });
212 324 }
213
214 16886 std::shared_ptr<const BoulderShape> BoulderLayer::CachedShape(std::uint64_t shapeSeed) const {
215
1/1
✓ Branch 1 taken 16886 times.
16886 return m_shapes->Get(
216
1/1
✓ Branch 2 taken 16886 times.
34764 shapeSeed, [&] { return GenerateBoulderShape(shapeSeed, m_options.rendering.shapes); });
217 }
218
219 std::shared_ptr<const std::vector<BoulderFragment>>
220 5412 BoulderLayer::CachedFragments(std::uint64_t shapeSeed) const {
221
1/1
✓ Branch 1 taken 5412 times.
5412 return m_fragments->Get(shapeSeed, [&] {
222 377 const BoulderFragmentation& fragmentation = *m_options.fragmentation;
223 377 const auto span =
224 377 static_cast<std::uint32_t>(fragmentation.maxPieces - fragmentation.minPieces + 1);
225 377 RandomStream random = AttributeStream(shapeSeed, BoulderDraw::Fragments);
226 377 const int pieces = fragmentation.minPieces + static_cast<int>(random.NextU32() % span);
227 377 const BoulderCracks cracks = random.NextDouble() < fragmentation.slabShare
228
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 245 times.
377 ? BoulderCracks::Slabs
229 377 : BoulderCracks::Radial;
230
1/1
✓ Branch 2 taken 377 times.
754 return FragmentBoulderShape(*CachedShape(shapeSeed), pieces, shapeSeed, cracks);
231
1/1
✓ Branch 1 taken 5412 times.
10824 });
232 }
233
234 16960 std::shared_ptr<const BoulderShape> BoulderLayer::Shape(const Boulder& boulder) const {
235 16960 const std::uint64_t seed = ShapeSeed(boulder, m_options.rendering.shapePrototypes);
236
2/2
✓ Branch 0 taken 16509 times.
✓ Branch 1 taken 451 times.
16960 if (boulder.parent == 0) {
237 16509 return CachedShape(seed);
238 }
239 451 const auto fragments = CachedFragments(seed);
240
2/4
✓ Branch 0 taken 451 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 451 times.
✗ Branch 3 not taken.
902 return {fragments, &(*fragments)[boulder.piece].shape};
241 451 }
242
243 636597 double BoulderLayer::OwnMarginRadii() const {
244
2/2
✓ Branch 0 taken 388014 times.
✓ Branch 1 taken 248583 times.
636597 const double body = m_options.rendering.relief == BoulderRelief::Shape ? kShapeReachRadii : 1.0;
245 1273194 const double fillet =
246
2/2
✓ Branch 0 taken 194355 times.
✓ Branch 1 taken 442242 times.
636597 m_options.fillet.heightShare > 0.0 ? 1.0 + 2.0 * m_options.fillet.widthRatio : 1.0;
247 636597 return std::fmax(body, fillet);
248 }
249
250 142694 double BoulderLayer::BreakMarginRadii() const {
251 142694 const std::optional<BoulderFragmentation>& fragmentation = m_options.fragmentation;
252
2/2
✓ Branch 0 taken 7236 times.
✓ Branch 1 taken 135458 times.
142694 return fragmentation ? 2.0 * (fragmentation->gapRatio + fragmentation->spreadRatio) : 0.0;
253 }
254
255 4 double BoulderLayer::GroundReach(double spacing) const {
256
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 const double rim = m_options.rendering.relief == BoulderRelief::Dome ? 0.5 * spacing : 0.0;
257
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 const double roll = m_options.pose.rollDistance.value_or(0.0);
258 4 const double radius = 0.5 * MaxFeatureDiameter();
259 4 return rim + 2.0 * roll + radius * (OwnMarginRadii() + BreakMarginRadii()) +
260
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 std::fmax(radius, m_exposure.Reach());
261 }
262
263 139074 CraterList BoulderLayer::ScatterBoulderCandidates(const Bounds2d& region, double smallest) const {
264 139074 const double margin = OwnMarginRadii() + BreakMarginRadii();
265
2/2
✓ Branch 0 taken 9796 times.
✓ Branch 1 taken 129278 times.
139074 if (!m_options.generateRandom) {
266 9796 CraterList manual;
267
2/2
✓ Branch 0 taken 9819 times.
✓ Branch 1 taken 9796 times.
19615 for (const ManualBoulder& boulder : m_options.manual) {
268 9819 const Vec2d center{boulder.x, boulder.y};
269
3/3
✓ Branch 1 taken 9819 times.
✓ Branch 3 taken 9617 times.
✓ Branch 4 taken 202 times.
9819 if (m_surface.Contains(m_surface.Expand(region, 0.5 * margin * boulder.diameter),
270 center)) {
271
1/1
✓ Branch 1 taken 9617 times.
9617 manual.Append(
272
1/1
✓ Branch 1 taken 9617 times.
9617 {boulder.x, boulder.y, boulder.diameter, 0.0, ManualBoulderId(boulder), true});
273 }
274 }
275 9796 return manual;
276 9796 }
277 129278 const std::uint32_t seed = SubstreamSeed(m_options.seed, kBoulderCandidateStream);
278 129278 const FreshAges ages;
279 129278 CraterGenerationOptions generation;
280 129278 generation.minDiameter = m_options.sizes.minDiameter;
281 129278 generation.maxDiameter = m_options.sizes.maxDiameter;
282 129278 generation.marginRadii = margin;
283 129278 generation.surface = m_surface;
284
1/1
✓ Branch 1 taken 129278 times.
129278 generation.logName = "boulders";
285
2/2
✓ Branch 0 taken 129105 times.
✓ Branch 1 taken 173 times.
129278 if (smallest > 0.0) {
286 129105 generation.minResolvedDiameter = smallest;
287 }
288
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 129278 times.
129304 for (const ManualBoulder& boulder : m_options.manual) {
289 26 generation.manual.Append(
290
3/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
✓ Branch 3 taken 26 times.
29 {boulder.x, boulder.y, boulder.diameter, 0.0, boulder.id.value_or(0u), true});
291 }
292 129278 generation.completeness = m_completeness;
293
1/1
✓ Branch 1 taken 129278 times.
129278 return GenerateCachedCraters(m_cache.get(), *m_cells, region, *m_candidates, ages, seed,
294
1/1
✓ Branch 2 taken 129278 times.
129278 std::move(generation));
295 129278 }
296
297 139074 std::vector<Boulder> BoulderLayer::DrawBoulders(const CraterList& candidates, double spacing,
298 const Ground* ground, const EjectaIndex& ejecta,
299 unsigned threads) const {
300 139074 std::vector<ThinnedCandidate> thinned;
301
2/2
✓ Branch 0 taken 721034 times.
✓ Branch 1 taken 139074 times.
860108 for (std::size_t n = 0; n < candidates.Size(); ++n) {
302
4/4
✓ Branch 1 taken 721034 times.
✓ Branch 4 taken 721034 times.
✓ Branch 6 taken 485622 times.
✓ Branch 7 taken 235412 times.
721034 if (std::optional<ThinnedCandidate> one = Thin(candidates.At(n), ground, ejecta)) {
303
1/1
✓ Branch 1 taken 485622 times.
485622 thinned.push_back(*one);
304 }
305 }
306
1/1
✓ Branch 1 taken 139074 times.
139074 std::vector<Boulder> boulders;
307
1/1
✓ Branch 1 taken 139074 times.
139074 boulders.reserve(thinned.size());
308
2/2
✓ Branch 0 taken 126957 times.
✓ Branch 1 taken 12117 times.
139074 if (ground == nullptr) {
309
2/2
✓ Branch 0 taken 160262 times.
✓ Branch 1 taken 126957 times.
287219 for (const ThinnedCandidate& one : thinned) {
310
2/3
✓ Branch 1 taken 160262 times.
✓ Branch 3 taken 160262 times.
✗ Branch 4 not taken.
160262 if (std::optional<Boulder> boulder = PlaceCandidate(one, spacing, nullptr)) {
311
1/1
✓ Branch 1 taken 160262 times.
160262 boulders.push_back(*boulder);
312 }
313 }
314 return boulders;
315 }
316 12117 const BoulderPose& pose = m_options.pose;
317
4/4
✓ Branch 0 taken 12113 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 12101 times.
12117 const std::size_t each = pose.followSlope || pose.maxSlopeDegrees.has_value() ? 5 : 1;
318
1/1
✓ Branch 1 taken 12117 times.
12117 std::vector<Vec2d> points;
319
1/1
✓ Branch 1 taken 12117 times.
12117 points.reserve(thinned.size() * each);
320
2/2
✓ Branch 0 taken 325360 times.
✓ Branch 1 taken 12117 times.
337477 for (const ThinnedCandidate& one : thinned) {
321 325360 const Crater& c = one.candidate;
322
1/1
✓ Branch 1 taken 325360 times.
325360 points.push_back({c.x, c.y});
323
2/2
✓ Branch 0 taken 62117 times.
✓ Branch 1 taken 263243 times.
325360 if (each == 5) {
324
3/3
✓ Branch 1 taken 62117 times.
✓ Branch 3 taken 248468 times.
✓ Branch 4 taken 62117 times.
310585 for (const Vec2d& p : GradientPoints(m_surface, {c.x, c.y}, 0.5 * c.diameter)) {
325
1/1
✓ Branch 1 taken 248468 times.
248468 points.push_back(p);
326 }
327 }
328 }
329
1/1
✓ Branch 1 taken 12117 times.
12117 std::vector<float> heights(points.size());
330
1/1
✓ Branch 3 taken 12117 times.
12117 ground->HeightsAt(points, heights, m_options.groundSpacing, threads);
331
2/2
✓ Branch 0 taken 325360 times.
✓ Branch 1 taken 12117 times.
337477 for (std::size_t n = 0; n < thinned.size(); ++n) {
332 325360 const CandidateGround under(*ground, Span(points).subspan(n * each, each),
333
1/1
✓ Branch 6 taken 325360 times.
650720 Span(heights).subspan(n * each, each), m_options.groundSpacing);
334
3/3
✓ Branch 1 taken 325360 times.
✓ Branch 3 taken 323719 times.
✓ Branch 4 taken 1641 times.
325360 if (std::optional<Boulder> boulder = PlaceCandidate(thinned[n], spacing, &under)) {
335
1/1
✓ Branch 1 taken 323719 times.
323719 boulders.push_back(*boulder);
336 }
337 325360 }
338 12117 return boulders;
339 139074 }
340
341 std::optional<BoulderLayer::ThinnedCandidate>
342 721033 BoulderLayer::Thin(const Crater& candidate, const Ground* ground, const EjectaIndex& ejecta) const {
343
2/2
✓ Branch 0 taken 9617 times.
✓ Branch 1 taken 711416 times.
721033 if (!m_options.generateRandom) {
344 9617 return ThinnedCandidate{candidate, BoulderAbundance{}};
345 }
346 711416 ThinnedCandidate thinned{candidate, Abundance(candidate.x, candidate.y, ground, ejecta)};
347
2/2
✓ Branch 0 taken 711392 times.
✓ Branch 1 taken 25 times.
711417 if (!candidate.manual) {
348 711392 thinned.density = Density(candidate.diameter, thinned.abundance);
349 711392 RandomStream random = AttributeStream(candidate.id, BoulderDraw::Keep);
350 711392 if (random.NextDouble() >=
351
2/2
✓ Branch 1 taken 235412 times.
✓ Branch 2 taken 475980 times.
711392 thinned.density / m_candidates->Incremental(candidate.diameter)) {
352 235412 return std::nullopt;
353 }
354 }
355 476005 return thinned;
356 }
357
358 namespace {
359
360 62420 BoulderList ToBoulderList(const std::vector<Boulder>& boulders) {
361 62420 BoulderList list;
362
1/1
✓ Branch 1 taken 62420 times.
62420 list.Reserve(boulders.size());
363
2/2
✓ Branch 0 taken 376098 times.
✓ Branch 1 taken 62420 times.
438518 for (const Boulder& boulder : boulders) {
364
1/1
✓ Branch 1 taken 376098 times.
376098 list.Append(boulder);
365 }
366 62420 return list;
367 }
368
369 } // namespace
370
371 62362 BoulderList BoulderLayer::Boulders(const Bounds2d& region, double spacing,
372 const Ground* ground) const {
373
1/1
✓ Branch 2 taken 62361 times.
62362 return ToBoulderList(BoulderRecords(region, spacing, ground, 0));
374 }
375
376 139016 std::vector<Boulder> BoulderLayer::BoulderRecords(const Bounds2d& region, double spacing,
377 const Ground* ground, unsigned threads) const {
378
2/2
✓ Branch 0 taken 138889 times.
✓ Branch 1 taken 127 times.
139016 const double smallest = spacing > 0.0 ? m_options.fade.startSamples * spacing : 0.0;
379 139016 return PlaceBoulders(region, spacing, smallest, ground, threads);
380 }
381
382 139075 std::vector<Boulder> BoulderLayer::PlaceBoulders(const Bounds2d& region, double spacing,
383 double smallest, const Ground* ground,
384 unsigned threads) const {
385 139075 m_exposure.Require(ground);
386
4/4
✓ Branch 0 taken 12117 times.
✓ Branch 1 taken 126957 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 12109 times.
139074 const double roll = ground != nullptr ? m_options.pose.rollDistance.value_or(0.0) : 0.0;
387
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 139066 times.
139074 const Bounds2d from = roll > 0.0 ? m_surface.Expand(region, roll) : region;
388 617632 const auto rests = [&](const Boulder& boulder) {
389 478558 const double own = 0.5 * boulder.diameter * OwnMarginRadii();
390
2/2
✓ Branch 0 taken 58377 times.
✓ Branch 1 taken 420181 times.
478558 return roll == 0.0 ||
391 58377 m_surface.Contains(m_surface.Expand(region, own), {boulder.x, boulder.y});
392 139074 };
393 139074 const CraterList candidates = ScatterBoulderCandidates(from, smallest);
394
2/2
✓ Branch 0 taken 135458 times.
✓ Branch 1 taken 3616 times.
139074 if (!m_options.fragmentation) {
395 135458 std::vector<Boulder> boulders =
396
2/2
✓ Branch 1 taken 135458 times.
✓ Branch 4 taken 135458 times.
135458 DrawBoulders(candidates, spacing, ground, EjectaCraters(from), threads);
397
1/1
✓ Branch 1 taken 135458 times.
135458 boulders.erase(std::remove_if(boulders.begin(), boulders.end(),
398 478558 [&](const Boulder& boulder) { return !rests(boulder); }),
399
1/1
✓ Branch 1 taken 135458 times.
135458 boulders.end());
400 135458 return boulders;
401 135458 }
402 3616 const double reach = 0.5 * MaxFeatureDiameter() * (OwnMarginRadii() + BreakMarginRadii());
403
2/2
✓ Branch 1 taken 3616 times.
✓ Branch 4 taken 3616 times.
3616 const EjectaIndex ejecta = EjectaCraters(m_surface.Expand(from, reach));
404
1/1
✓ Branch 1 taken 3616 times.
3616 std::vector<Boulder> boulders;
405 3616 std::vector<std::size_t> pieces;
406
3/3
✓ Branch 1 taken 3616 times.
✓ Branch 3 taken 5423 times.
✓ Branch 4 taken 3616 times.
9039 for (const Boulder& parent : DrawBoulders(candidates, spacing, ground, ejecta, threads)) {
407
3/3
✓ Branch 1 taken 5423 times.
✓ Branch 3 taken 15345 times.
✓ Branch 4 taken 5423 times.
20768 for (const Boulder& boulder : Break(parent, spacing, smallest)) {
408 15345 const double own = 0.5 * boulder.diameter * OwnMarginRadii();
409
3/3
✓ Branch 1 taken 15345 times.
✓ Branch 3 taken 13491 times.
✓ Branch 4 taken 1854 times.
15345 if (m_surface.Contains(m_surface.Expand(region, own), {boulder.x, boulder.y})) {
410
2/2
✓ Branch 0 taken 13053 times.
✓ Branch 1 taken 438 times.
13491 if (boulder.parent != 0) {
411
1/1
✓ Branch 1 taken 13053 times.
13053 pieces.push_back(boulders.size());
412 }
413
1/1
✓ Branch 1 taken 13491 times.
13491 boulders.push_back(boulder);
414 }
415 5423 }
416 3616 }
417
1/1
✓ Branch 1 taken 3616 times.
3616 ReadGrounds(boulders, pieces, ground, threads);
418 3616 return boulders;
419 139074 }
420
421 59 BoulderFeatures BoulderLayer::BouldersOver(const RasterGrid& grid, double spacing,
422 const Ground* ground) const {
423 59 const std::vector<Boulder> boulders =
424 59 PlaceBoulders(grid.Bounds(), ListedSpacing(grid, spacing, m_surface), 0.0, ground, 0);
425 59 return {ToBoulderList(boulders), Models(boulders)};
426
2/2
✓ Branch 1 taken 59 times.
✓ Branch 4 taken 59 times.
59 }
427
428 } // namespace terraingen
429