./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
249 / 254
98.0%
Branches:
255 / 336
75.9%
Functions:
25 / 25
100.0%

src/layers/craters/crater_layer.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/crater_layer.h"
4
5 #include "layers/craters/generation/cell_cache.h"
6 #include "layers/craters/shaping/resolved_state.h"
7 #include "terraingen/distribution/equilibrium.h"
8 #include "terraingen/distribution/neukum.h"
9 #include "terraingen/distribution/power_law.h"
10 #include "terraingen/distribution/spliced.h"
11 #include "terraingen/distribution/summed.h"
12 #include "terraingen/geometry/cells.h"
13 #include "terraingen/layers/craters/ages/steady_state.h"
14 #include "terraingen/layers/craters/crater_fields.h"
15 #include "terraingen/layers/craters/degradation/diffusion.h"
16 #include "terraingen/layers/craters/degradation/diffusivities.h"
17 #include "terraingen/layers/craters/generation/spacing.h"
18 #include "terraingen/layers/craters/profiles/martin_parkes_dunstan.h"
19 #include "terraingen/layers/craters/shaping/morphometry.h"
20 #include "terraingen/layers/source_resolved.h"
21 #include "terraingen/random/stream.h"
22 #include "terraingen/raster/grid.h"
23 #include "util/numerics.h"
24
25 #include <algorithm>
26 #include <cmath>
27 #include <memory>
28 #include <optional>
29 #include <stdexcept>
30 #include <utility>
31 #include <vector>
32
33 namespace terraingen {
34
35 namespace {
36
37 /// The largest primary (m) searched for the smallest one throwing secondaries a map measures,
38 /// about where Singer et al.'s largest secondary stops growing with the primary.
39 constexpr double kLargestThrower = 5e5;
40
41 /// Sets the seed of `options` and the seeds of its apply options to `ReseedWord(seed, 0)` to
42 /// `ReseedWord(seed, 6)`.
43 50 void ReseedCraterOptions(CraterLayerOptions& options, std::uint64_t seed) {
44 50 options.seed = ReseedWord(seed, 0);
45 50 CraterApplyOptions& apply = options.apply;
46 50 apply.irregularity.seed = ReseedWord(seed, 1);
47 50 apply.noise.seed = ReseedWord(seed, 2);
48 50 apply.texture.seed = ReseedWord(seed, 3);
49 50 apply.slope.seed = ReseedWord(seed, 4);
50 50 apply.terraces.seed = ReseedWord(seed, 5);
51 50 apply.peaks.seed = ReseedWord(seed, 6);
52 50 }
53
54 } // namespace
55
56 986 CraterLayer::CraterLayer(CraterModels models, CraterLayerOptions options, TerrainBase base)
57 986 : m_models(std::move(models)), m_options(std::move(options)),
58
1/1
✓ Branch 1 taken 986 times.
986 m_given(std::make_shared<const CraterLayerOptions>(m_options)),
59
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 984 times.
1972 m_surface(std::move(base.surface)), m_source(std::move(base.source)) {
60
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 984 times.
986 if (!m_models.degradation) {
61
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A crater layer needs a degradation model");
62 }
63
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 978 times.
984 if (!m_models.ages) {
64
2/3
✓ Branch 1 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 m_models.ages = m_models.degradation->AgeModel();
65 }
66
5/6
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 502 times.
✓ Branch 2 taken 480 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 480 times.
984 if (m_options.generateRandom && (!m_models.density || !m_models.ages)) {
67
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A crater layer generating craters needs a density and ages");
68 }
69
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 973 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
982 if (m_options.densityMap && !m_models.density) {
70
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("A crater density map needs a density");
71 }
72
3/5
✓ Branch 1 taken 980 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 980 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 980 times.
984 m_models.profile = MatchedProfile(std::move(m_models.profile), *m_models.degradation);
73
1/1
✓ Branch 1 taken 979 times.
980 ValidateCraterSpacing(m_options.generation.spacing);
74
1/1
✓ Branch 1 taken 978 times.
979 ValidateCraterApplyOptions(m_options.apply);
75
1/1
✓ Branch 2 taken 978 times.
978 m_options.apply = SeededCraterApply(std::move(m_options.apply), m_options.seed);
76
1/1
✓ Branch 1 taken 976 times.
978 FollowSource();
77
1/1
✓ Branch 1 taken 976 times.
976 m_options.generation.marginRadii = CraterReachRadii(*m_models.profile, m_options.apply) *
78
1/1
✓ Branch 1 taken 976 times.
976 MaxOutlineScale(m_options.apply.irregularity);
79 976 const CraterList& manual = m_options.generation.manual;
80 976 double largestReplacing = 0.0;
81
2/2
✓ Branch 0 taken 769 times.
✓ Branch 1 taken 976 times.
1745 for (std::size_t n = 0; n < manual.Size(); ++n) {
82
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 718 times.
769 if (manual.Replace()[n] != 0) {
83 51 largestReplacing = std::fmax(largestReplacing, manual.Diameters()[n]);
84 }
85 }
86
1/1
✓ Branch 1 taken 976 times.
976 m_replacementMargin = ReplacementMargin(m_options.apply, *m_models.profile, largestReplacing);
87 976 m_options.generation.surface = m_surface;
88 976 m_options.apply.surface = m_surface;
89
2/3
✓ Branch 1 taken 976 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 976 times.
976 m_cells = MakeCells(m_surface);
90
2/2
✓ Branch 0 taken 975 times.
✓ Branch 1 taken 1 time.
976 if (m_options.cacheCraters > 0) {
91
2/3
✓ Branch 1 taken 975 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 975 times.
975 m_cache = std::make_shared<CraterCellCache>(m_options.cacheCraters);
92 }
93
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 976 times.
985 for (const SecondaryCraterOptions& secondaries : m_options.secondaries) {
94
1/1
✓ Branch 1 taken 9 times.
9 ValidateSecondaryCraterOptions(secondaries);
95 }
96 976 m_candidates = m_models.density;
97
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 967 times.
976 if (m_options.densityMap) {
98
1/1
✓ Branch 1 taken 8 times.
9 FollowDensityMap();
99 }
100
1/1
✓ Branch 1 taken 975 times.
975 SetUpBandCounts();
101
11/14
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 11 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 11 times.
✓ Branch 13 taken 11 times.
✗ Branch 14 not taken.
1023 }
102
103 975 void CraterLayer::SetUpBandCounts() {
104 975 const CraterGenerationOptions& generation = m_options.generation;
105
3/4
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 502 times.
✓ Branch 2 taken 473 times.
✗ Branch 3 not taken.
975 if (!m_options.generateRandom || !m_candidates) {
106 return;
107 }
108 473 const double maxDiameter = ResolveMaxDiameter(generation.maxDiameter, *m_candidates);
109
3/6
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 473 times.
✗ Branch 5 not taken.
473 if (std::isfinite(maxDiameter) && maxDiameter > generation.minDiameter &&
110 generation.minDiameter > 0.0) {
111 473 for (const CraterBand& band :
112
2/2
✓ Branch 1 taken 1870 times.
✓ Branch 2 taken 473 times.
2343 CraterBands(generation.minDiameter, maxDiameter, generation.cellSizeFactor)) {
113
1/1
✓ Branch 1 taken 1870 times.
1870 m_bandCounts.push_back(
114
1/1
✓ Branch 1 taken 1870 times.
1870 {band.low, band.high, m_candidates->ExpectedCount(1.0, band.low, band.high)});
115 473 }
116 }
117 }
118
119 25 LayerPtr CraterLayer::Reseeded(std::uint64_t seed) const {
120 25 auto layer = std::make_shared<CraterLayer>(*this);
121 25 ReseedCraterOptions(layer->m_options, seed);
122
1/1
✓ Branch 1 taken 25 times.
25 auto given = std::make_shared<CraterLayerOptions>(*m_given);
123 25 ReseedCraterOptions(*given, seed);
124 25 layer->m_given = std::move(given);
125
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (m_cache) {
126
2/5
✓ Branch 1 taken 25 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 25 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
25 layer->m_cache = std::make_shared<CraterCellCache>(m_options.cacheCraters);
127 }
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 return layer;
129 25 }
130
131 10 LayerPtr CraterLayer::Rebased(const TerrainBase& base) const {
132 10 return std::make_shared<CraterLayer>(m_models, *m_given, base);
133 }
134
135 134101 void CraterLayer::CountBands(CraterGenerationOptions& generation) const {
136
1/2
✓ Branch 0 taken 134101 times.
✗ Branch 1 not taken.
134101 if (m_bandCounts.empty()) {
137 return;
138 }
139 134109 generation.bandCount = [counts = &m_bandCounts,
140 134101 density = m_candidates.get()](const CraterBand& band) {
141
1/2
✓ Branch 0 taken 1031077 times.
✗ Branch 1 not taken.
1031077 for (const BandCount& entry : *counts) {
142
3/4
✓ Branch 0 taken 454753 times.
✓ Branch 1 taken 576324 times.
✓ Branch 2 taken 454928 times.
✗ Branch 3 not taken.
1031077 if (entry.low == band.low && entry.high == band.high) {
143 454928 return entry.count;
144 }
145 }
146 ✗ return density->ExpectedCount(1.0, band.low, band.high);
147 134101 };
148 }
149
150 979 void ValidateInheritedCraters(const InheritedCraters& inherited) {
151 979 CheckRanges(inherited);
152
6/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 973 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
977 if (inherited.minSourcePixels && inherited.maxSourcePixels &&
153
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 !(*inherited.maxSourcePixels > *inherited.minSourcePixels)) {
154 1 throw std::invalid_argument(
155
1/1
✓ Branch 2 taken 1 time.
1 "Inherited craters' max_source_px must be above their min_source_px");
156 }
157 976 }
158
159 978 void CraterLayer::FollowSource() {
160 978 ValidateSourceResolvesPixels(m_options.sourceResolvesPixels);
161
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 934 times.
978 if (auto limit = LayerResolvedLimit(m_source, m_options.sourceResolvesPixels, m_surface)) {
162 44 std::shared_ptr<const SurfaceField>& completeness = m_options.generation.completeness;
163
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
96 completeness = completeness ? std::make_shared<const MinimumField>(completeness, limit)
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 : std::move(limit);
165 }
166 978 ValidateInheritedCraters(m_options.inherited);
167 976 const InheritedCraters& inherited = m_options.inherited;
168
2/2
✓ Branch 0 taken 966 times.
✓ Branch 1 taken 10 times.
976 if (inherited.craters.Empty()) {
169 966 return;
170 }
171 10 const auto from = LayerResolvedLimit(m_source,
172
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 inherited.minSourcePixels ? inherited.minSourcePixels
173 : m_options.sourceResolvesPixels,
174
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 m_surface);
175
1/1
✓ Branch 1 taken 10 times.
10 const auto upTo = LayerResolvedLimit(m_source, inherited.maxSourcePixels, m_surface);
176
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const CraterList stamped = FilterCraters(inherited.craters, [&](std::size_t, const Crater& c) {
177
6/6
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 130 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 47 times.
228 return (!from || InSource(from.get(), c.diameter, c.x, c.y)) &&
178 49 !InSource(upTo.get(), c.diameter, c.x, c.y);
179
1/1
✓ Branch 1 taken 10 times.
10 });
180
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 10 times.
57 for (std::size_t n = 0; n < stamped.Size(); ++n) {
181
1/1
✓ Branch 1 taken 47 times.
47 Crater crater = stamped.At(n);
182
3/3
✓ Branch 1 taken 47 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 2 times.
47 if (!TakesRimHeight(*m_models.profile, crater.diameter)) {
183
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9 times.
45 crater.rimHeight.reset();
184 }
185
1/1
✓ Branch 1 taken 47 times.
47 m_options.generation.manual.Append(crater.Replace());
186 }
187
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
10 if (from && inherited.minSourcePixels) {
188 2 std::shared_ptr<const SurfaceField>& completeness = m_options.generation.completeness;
189 2 completeness =
190
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
8 completeness ? std::make_shared<const MinimumField>(completeness, from) : from;
191 }
192
4/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
21 }
193
194 82 CraterModels DefaultCraterModels() {
195 82 const EquilibriumSurface surface{
196
1/1
✓ Branch 2 taken 82 times.
246 std::make_shared<SplicedSfd>(ExtendedNeukumProductionFunction()),
197
1/3
✓ Branch 1 taken 82 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
164 std::make_shared<PowerLawSfd>(HartmannEquilibrium()), 3.5, NeukumLunarChronology};
198
1/1
✓ Branch 1 taken 82 times.
82 const auto ages = surface.Ages();
199
1/1
✓ Branch 1 taken 82 times.
82 const auto profile =
200
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
82 std::make_shared<DiffusedProfile>(std::make_shared<MartinParkesDunstanProfile>());
201 82 return {surface.Density(), ages,
202
5/11
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 82 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 82 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 82 times.
✓ Branch 10 taken 82 times.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
328 MatchedDiffusionDegradation(profile, LunarFreshCraterMorphometry, ages), profile};
203
6/15
✓ Branch 1 taken 82 times.
✓ Branch 3 taken 82 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 82 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 82 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 82 times.
✓ Branch 11 taken 82 times.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
492 }
204
205 49 std::shared_ptr<CraterLayer> CraterLayer::Manual(CraterList craters, CraterModels models,
206 CraterApplyOptions apply, TerrainBase base) {
207 49 CraterLayerOptions options;
208 49 options.generateRandom = false;
209 49 options.generation.manual = std::move(craters);
210 49 options.apply = std::move(apply);
211
1/1
✓ Branch 1 taken 48 times.
97 return std::make_shared<CraterLayer>(std::move(models), std::move(options), std::move(base));
212 49 }
213
214 9 void CraterLayer::FollowDensityMap() {
215
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 m_scale = std::make_shared<const DensityScale>(*m_options.densityMap, *m_models.density);
216
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (m_scale->Max() > 1.0) {
217 24 m_candidates = std::make_shared<SummedSfd>(
218
5/12
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
18 std::vector<WeightedSfd>{{m_scale->Max(), m_models.density}});
219 }
220
4/10
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
72 m_options.generation.keepShare = [scale = m_scale](const Crater& crater) {
221 13478 return scale->At(crater.x, crater.y) / scale->Max();
222
1/1
✓ Branch 1 taken 9 times.
9 };
223
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 const DensityMap& map = *m_options.densityMap;
224
3/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 6 times.
9 if (!map.total || map.measure == DensityMeasure::Scale) {
225 return;
226 }
227
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 for (const SecondaryCraterOptions& secondaries : m_options.secondaries) {
228 2 m_throwFrom = std::fmin(m_throwFrom, SmallestThrower(map, secondaries));
229 }
230
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
3 if (std::isfinite(m_throwFrom) && m_options.generation.maxCraters) {
231 1 throw std::invalid_argument("Taking secondaries out of a density map needs every crater, "
232
1/1
✓ Branch 2 taken 1 time.
1 "not the maxCraters largest");
233 }
234 2 m_options.generation.spacing.outrankFrom = m_throwFrom;
235
2/3
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
12 }
236
237 2 double CraterLayer::SmallestThrower(const DensityMap& map,
238 const SecondaryCraterOptions& secondaries) {
239 2 const double smallest = std::fmax(map.minDiameter, secondaries.minDiameter);
240 202 const auto throws = [&](double diameter) {
241 200 return LargestSecondary(diameter, secondaries) > smallest;
242 2 };
243
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!throws(kLargestThrower)) {
244 return std::numeric_limits<double>::infinity();
245 }
246 4 return std::fmax(BisectLog(1e-3, kLargestThrower, 100, 0.0, throws),
247 2 secondaries.selection.minDiameter);
248 }
249
250 3 CraterList CraterLayer::Throwers(const Bounds2d& region) const {
251 3 CraterGenerationOptions large = m_options.generation;
252 3 large.minDiameter = std::fmax(large.minDiameter, m_throwFrom);
253 3 large.minResolvedDiameter = 0.0;
254 3 CountBands(large);
255 3 double reach = 0.0;
256
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (const SecondaryCraterOptions& secondaries : m_options.secondaries) {
257
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 reach = std::fmax(reach, SecondaryOuterRange(MaxFeatureDiameter(), secondaries));
258 }
259
1/1
✓ Branch 1 taken 3 times.
3 const Bounds2d around = m_surface.Expand(region, reach);
260
2/3
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (large.minDiameter < ResolveMaxDiameter(large.maxDiameter, *m_candidates)) {
261 ✗ return GenerateCachedCraters(m_cache.get(), *m_cells, around, *m_candidates, *m_models.ages,
262
0/2
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ m_options.seed, large);
263 }
264
1/1
✓ Branch 1 taken 3 times.
3 return ManualCratersNear(around, 0.0);
265 3 }
266
267 28684 CraterList CraterLayer::ManualCratersNear(const Bounds2d& region, double marginRadii) const {
268 28684 CraterList craters;
269
2/2
✓ Branch 1 taken 28684 times.
✓ Branch 4 taken 28680 times.
28688 AppendManualCraters(m_options.generation.manual, craters, [&](const Crater& crater) {
270 76879 const Bounds2d near = marginRadii > 0.0
271
2/2
✓ Branch 0 taken 76876 times.
✓ Branch 1 taken 3 times.
76879 ? m_surface.Expand(region, 0.5 * marginRadii * crater.diameter)
272 3 : region;
273 76879 return m_surface.Contains(near, {crater.x, crater.y});
274 });
275 28680 return craters;
276 4 }
277
278 3 CraterKeepShare CraterLayer::KeepShare(const Bounds2d& region) const {
279 2 struct Thrower {
280 Vec2d center;
281 SecondaryDensity density;
282 };
283 3 auto thrown = std::make_shared<std::vector<Thrower>>();
284
1/1
✓ Branch 1 taken 3 times.
3 const CraterList throwers = Throwers(region);
285
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 for (std::size_t n = 0; n < throwers.Size(); ++n) {
286
1/1
✓ Branch 1 taken 2 times.
2 const Crater crater = throwers.At(n);
287
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (const SecondaryCraterOptions& secondaries : m_options.secondaries) {
288
1/1
✓ Branch 1 taken 2 times.
2 SecondaryDensity density(crater, secondaries);
289
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (density.Reach() > 0.0) {
290 4 thrown->push_back({{crater.x, crater.y}, std::move(density)});
291 }
292 2 }
293 }
294
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 const DensityMap& map = *m_options.densityMap;
295 3 const bool cover = map.measure == DensityMeasure::ArealFraction;
296
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
9 return [scale = m_scale, map, cover, thrown, from = m_throwFrom,
297 3 surface = m_surface](const Crater& crater) {
298
1/2
✓ Branch 1 taken 513850 times.
✗ Branch 2 not taken.
513850 const double share = scale->At(crater.x, crater.y) / scale->Max();
299
1/2
✓ Branch 0 taken 513850 times.
✗ Branch 1 not taken.
513850 if (crater.diameter >= from) {
300 return share;
301 }
302 513850 double value = MapValueAt(map, crater.x, crater.y);
303
1/2
✓ Branch 0 taken 513850 times.
✗ Branch 1 not taken.
513850 if (std::isnan(value)) {
304 return share;
305 }
306
2/2
✓ Branch 0 taken 509962 times.
✓ Branch 1 taken 513850 times.
1023812 for (const Thrower& thrower : *thrown) {
307 509962 const Vec2d offset = surface.Offset(thrower.center, {crater.x, crater.y});
308
2/2
✓ Branch 0 taken 279660 times.
✓ Branch 1 taken 230302 times.
509962 if (std::hypot(offset.x, offset.y) < thrower.density.Reach()) {
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279660 times.
559320 value -= cover ? thrower.density.CoverAt(offset, map.minDiameter, map.maxDiameter)
310 279660 : thrower.density.At(offset, map.minDiameter, map.maxDiameter);
311 }
312 }
313 513850 return scale->Scale(value) / scale->Max();
314
1/1
✓ Branch 1 taken 3 times.
3 };
315
2/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
8 }
316
317 162996 CraterList CraterLayer::Craters(const Bounds2d& region, double spacing, double minDiameter,
318 double maxDiameter, unsigned threads) const {
319 162996 CraterGenerationOptions generation = m_options.generation;
320 162832 generation.threads = threads;
321
2/2
✓ Branch 0 taken 134151 times.
✓ Branch 1 taken 28681 times.
162832 if (m_options.generateRandom) {
322
2/2
✓ Branch 0 taken 134093 times.
✓ Branch 1 taken 58 times.
134151 if (spacing > 0.0) {
323 134093 generation.minResolvedDiameter = m_options.apply.fade.startSamples * spacing;
324 }
325 134151 generation.minResolvedDiameter = std::fmax(generation.minResolvedDiameter, minDiameter);
326 134151 generation.maxBandDiameter = std::fmin(generation.maxBandDiameter, maxDiameter);
327
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 134148 times.
134151 if (std::isfinite(m_throwFrom)) {
328
1/1
✓ Branch 1 taken 3 times.
3 generation.keepShare = KeepShare(region);
329 }
330 134151 CountBands(generation);
331 268235 return GenerateCachedCraters(m_cache.get(), *m_cells, region, *m_candidates, *m_models.ages,
332
1/1
✓ Branch 2 taken 134348 times.
134205 m_options.seed, std::move(generation));
333 }
334
1/1
✓ Branch 1 taken 28677 times.
28681 return ManualCratersNear(region, generation.marginRadii);
335 163054 }
336
337 159092 void CraterLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const {
338
2/2
✓ Branch 0 taken 158806 times.
✓ Branch 1 taken 286 times.
159092 const Raster<float>* grid = samples.Grid();
339
4/4
✓ Branch 0 taken 158806 times.
✓ Branch 1 taken 286 times.
✓ Branch 2 taken 5787 times.
✓ Branch 3 taken 153019 times.
159092 if (grid == nullptr && m_options.apply.datum != CraterDatum::None) {
340 5787 RequireGround(context.ground, "Point queries of craters with a datum");
341 }
342
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 158800 times.
158805 if (grid == nullptr && m_replacementMargin > 0.0) {
343 5 RequireGround(context.ground, "Point queries of replacing craters");
344 }
345
4/4
✓ Branch 0 taken 158836 times.
✓ Branch 1 taken 254 times.
✓ Branch 2 taken 158835 times.
✓ Branch 3 taken 1 time.
159090 if (grid == nullptr && m_options.generation.maxCraters) {
346 1 throw std::invalid_argument(
347
1/1
✓ Branch 2 taken 1 time.
1 "Point queries cannot use maxCraters, which ranks craters over a whole grid");
348 }
349 159089 const double spacing = samples.ResolvedSpacing(context.spacing, m_surface);
350 158956 const Bounds2d bounds = samples.Bounds();
351 159297 const CraterSampling sampling = SamplingFrom(context, spacing, this);
352
6/6
✓ Branch 0 taken 158912 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 5786 times.
✓ Branch 3 taken 153126 times.
✓ Branch 4 taken 4130 times.
✓ Branch 5 taken 1656 times.
159017 if (grid == nullptr && m_options.apply.datum != CraterDatum::None && context.cache != nullptr &&
353
8/8
✓ Branch 1 taken 4130 times.
✓ Branch 4 taken 4130 times.
✓ Branch 6 taken 4130 times.
✓ Branch 7 taken 154887 times.
✓ Branch 9 taken 4130 times.
✓ Branch 10 taken 154887 times.
✓ Branch 12 taken 4016 times.
✓ Branch 13 taken 155001 times.
159017 ApplyCratersOnCachedDatums(samples, Craters(bounds, spacing), m_models.Shape(),
354 4130 m_options.apply, sampling)) {
355 4016 return;
356 }
357 155001 const CraterList craters =
358
2/2
✓ Branch 1 taken 155158 times.
✓ Branch 4 taken 155144 times.
155001 Craters(m_surface.Expand(bounds, GroundReach(spacing)), spacing, 0.0,
359
3/3
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 154636 times.
✓ Branch 3 taken 154735 times.
310145 std::numeric_limits<double>::infinity(), grid != nullptr ? context.threads : 1);
360
1/1
✓ Branch 2 taken 154788 times.
154735 ApplyCraters(samples, craters, m_models.Shape(), m_options.apply, sampling);
361 159260 }
362
363 155144 double CraterLayer::GroundReach(double /*spacing*/) const {
364 155138 return std::fmax(DatumMargin(m_options.apply, *m_models.profile, MaxFeatureDiameter()),
365 155144 m_replacementMargin);
366 }
367
368 115 CraterFeatures CraterLayer::CratersOver(const RasterGrid& grid, double spacing) const {
369
3/3
✓ Branch 1 taken 115 times.
✓ Branch 4 taken 115 times.
✓ Branch 7 taken 115 times.
230 return DescribeCraters(Craters(grid.Bounds(), ListedSpacing(grid, spacing, m_surface)),
370
1/1
✓ Branch 2 taken 115 times.
345 m_models.Shape());
371 }
372
373 158872 double CraterLayer::MaxFeatureDiameter() const {
374 158872 const CraterGenerationOptions& generation = m_options.generation;
375 158872 double largest = m_options.generateRandom
376
2/2
✓ Branch 0 taken 129999 times.
✓ Branch 1 taken 28873 times.
158872 ? ResolveMaxDiameter(generation.maxDiameter, *m_models.density)
377 : 0.0;
378
2/2
✓ Branch 0 taken 77071 times.
✓ Branch 1 taken 159097 times.
236168 for (std::size_t n = 0; n < generation.manual.Size(); ++n) {
379 77071 largest = std::fmax(largest, generation.manual.At(n).diameter);
380 }
381 159097 return largest;
382 }
383
384 } // namespace terraingen
385