./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
256 / 257
99.6%
Branches:
232 / 259
89.6%
Functions:
30 / 30
100.0%

src/layers/craters/generation/population.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/generation/population.h"
4
5 #include "terraingen/layers/craters/generation/spacing.h"
6 #include "terraingen/random/philox.h"
7 #include "terraingen/random/samplers.h"
8 #include "terraingen/util/log.h"
9 #include "terraingen/util/parallel.h"
10 #include "util/checks.h"
11 #include "util/numerics.h"
12
13 #include <algorithm>
14 #include <cmath>
15 #include <memory>
16 #include <stdexcept>
17 #include <string>
18 #include <utility>
19
20 namespace terraingen {
21
22 namespace {
23
24 2696222 std::uint64_t CraterId(std::uint32_t seed, const CraterBand& band, const CellIndex& cell,
25 std::uint32_t index) {
26 2696222 const U32x4 block =
27 2696222 Philox4x32({static_cast<std::uint32_t>(cell.i), static_cast<std::uint32_t>(cell.j),
28 2696222 static_cast<std::uint32_t>(band.exponent), index},
29 {seed, kCraterIdStream});
30 2696222 return JoinWords(block.x, block.y);
31 }
32
33 } // namespace
34
35 264189 void ValidateCraterGenerationOptions(const CraterGenerationOptions& options) {
36 264189 ValidateCraterSpacing(options.spacing);
37 264655 RequirePositive(options.minDiameter, "minDiameter");
38
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 264515 times.
264517 if (!(options.maxDiameter > options.minDiameter)) {
39
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("maxDiameter must exceed minDiameter");
40 }
41 264515 RequireNonNegative(options.marginRadii, "marginRadii");
42 264640 RequirePositive(options.cellSizeFactor, "cellSizeFactor");
43
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 264628 times.
264630 if (!(options.maxExpectedCraters >= 0.0)) {
44
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("maxExpectedCraters must not be negative");
45 }
46 264628 RequireNonNegative(options.minResolvedDiameter, "minResolvedDiameter");
47
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 264643 times.
264644 if (!(options.maxBandDiameter > 0.0)) {
48
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("maxBandDiameter must be positive");
49 }
50 264643 }
51
52 namespace {
53
54 /// One band of a generation: the cells drawn and the craters they are expected to hold.
55 710948 struct BandPlan {
56 CraterBand band;
57 std::vector<CellIndex> cells;
58 double expected = 0.0;
59 };
60
61 /// What a generation keeps and drops, for its log line.
62 struct GenerationCounts {
63 std::size_t bands = 0;
64 double expected = 0.0;
65 std::size_t removed = 0; ///< By spacing.
66 std::size_t capped = 0; ///< Over `maxCraters`.
67 };
68
69 /// One call of `GenerateCraters`, in steps.
70 class Generation {
71 public:
72 264704 Generation(const SurfaceCells& cells, const Bounds2d& region,
73 const SizeFrequencyDistribution& density, const CraterAgeModel& ages,
74 std::uint32_t seed, const CraterGenerationOptions& options)
75 264704 : m_cells(cells), m_region(region), m_density(density), m_ages(ages), m_seed(seed),
76 264704 m_options(options), m_surface(options.surface) {
77
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 264703 times.
264704 if (density.Kind() != SfdKind::Density) {
78
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Craters are generated from a Density distribution");
79 }
80 264703 m_maxDiameter = ResolveMaxDiameter(options.maxDiameter, density);
81
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 264702 times.
264703 if (std::isinf(m_maxDiameter)) {
82
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An unbounded density needs a finite maxDiameter");
83 }
84
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 264701 times.
264702 if (!(m_maxDiameter > options.minDiameter)) {
85
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("maxDiameter must exceed minDiameter");
86 }
87 264701 }
88
89 264264 CraterList Run(GenerationCounts& counts) const {
90 264264 const std::vector<BandPlan> plan = PlanBands();
91 264749 counts.bands = plan.size();
92
2/2
✓ Branch 0 taken 711180 times.
✓ Branch 1 taken 264749 times.
975929 for (const BandPlan& entry : plan) {
93 711180 counts.expected += entry.expected;
94 }
95
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 264748 times.
264749 if (counts.expected > m_options.maxExpectedCraters) {
96
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
2 throw std::invalid_argument("Expected crater count " + std::to_string(counts.expected) +
97
1/1
✓ Branch 2 taken 1 time.
3 " exceeds maxExpectedCraters");
98 }
99
1/1
✓ Branch 1 taken 264696 times.
264748 CraterList craters = DrawPlannedCells(plan, counts.expected);
100
1/1
✓ Branch 1 taken 264509 times.
264696 const std::size_t generated = craters.Size();
101
1/1
✓ Branch 1 taken 264509 times.
264699 AppendManualCraters(m_options.manual, craters);
102
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 264780 times.
264816 if (m_options.spacing.Enabled()) {
103
1/1
✓ Branch 1 taken 36 times.
36 craters = RemoveSpacingConflicts(craters, counts.removed);
104
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 264728 times.
264780 } else if (m_options.manual.Size() > 0) {
105 52 craters = CropToRegion(craters, generated);
106 }
107
1/1
✓ Branch 1 taken 264531 times.
529582 return KeepLargest(craters, counts.capped);
108 264538 }
109
110 264707 void Log(std::size_t kept, const GenerationCounts& counts, const Stopwatch& stopwatch) const {
111
2/2
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 264791 times.
264707 if (!LogEnabled(LogLevel::Debug)) {
112 return;
113 }
114 37 const bool spaced = m_options.spacing.Enabled();
115 37 const std::size_t manual = m_options.manual.Size();
116 74 LogDebug(m_options.logName, "Generated ", kept, " ", m_options.logName, " (",
117 37 m_options.minDiameter, "-", m_maxDiameter, " m, ", counts.bands, " bands, ",
118
1/1
✓ Branch 1 taken 37 times.
37 static_cast<long long>(std::llround(counts.expected)), " expected",
119
10/14
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 36 times.
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 13 taken 36 times.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 36 times.
✓ Branch 17 taken 1 time.
✓ Branch 18 taken 36 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
76 spaced ? ", " + std::to_string(counts.removed) + " removed by spacing" : "",
120
10/14
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 36 times.
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 13 taken 36 times.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 36 times.
✓ Branch 17 taken 1 time.
✓ Branch 18 taken 36 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
76 manual > 0 ? ", " + std::to_string(manual) + " manual supplied" : "",
121 37 counts.capped > 0
122
10/14
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 36 times.
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 13 taken 36 times.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 36 times.
✓ Branch 17 taken 1 time.
✓ Branch 18 taken 36 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
76 ? ", " + std::to_string(counts.capped) + " over maxCraters dropped"
123 : "",
124 74 ") in ", FormatDuration(stopwatch.Milliseconds()));
125 }
126
127 private:
128 /// How far beyond the region a crater `diameter` across is drawn: its margin, and with spacing
129 /// as far again as the craters it can conflict with reach.
130 2458757 double CandidateMargin(double diameter) const {
131 2458757 const CraterSpacing& spacing = m_options.spacing;
132
2/2
✓ Branch 0 taken 2454030 times.
✓ Branch 1 taken 4727 times.
2458757 if (!spacing.Enabled()) {
133 2454030 return 0.5 * m_options.marginRadii * diameter;
134 }
135 4727 const double neighbor = std::fmin(spacing.diameterRatio * diameter, m_maxDiameter);
136 4727 return SpacingReach(diameter, m_maxDiameter, spacing) +
137 4727 0.5 * m_options.marginRadii * neighbor;
138 }
139
140 /// Whether `crater` is centered in the region or within its margin of it.
141 357713 bool InRegion(const Crater& crater, double margin) const {
142 357713 return m_surface.Contains(m_surface.Expand(m_region, margin), {crater.x, crater.y});
143 }
144
145 264450 std::vector<BandPlan> PlanBands() const {
146 264450 const CraterSpacing& spacing = m_options.spacing;
147 264450 const double skipBelow =
148
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 264414 times.
264450 m_options.minResolvedDiameter / (spacing.Enabled() ? spacing.diameterRatio : 1.0);
149
1/1
✓ Branch 1 taken 264859 times.
264450 std::vector<BandPlan> plan;
150 1043632 for (const CraterBand& band :
151
3/3
✓ Branch 1 taken 264859 times.
✓ Branch 3 taken 778866 times.
✓ Branch 4 taken 264766 times.
1043223 CraterBands(m_options.minDiameter, m_maxDiameter, m_options.cellSizeFactor)) {
152
2/2
✓ Branch 0 taken 778212 times.
✓ Branch 1 taken 654 times.
778866 const bool tooLarge =
153
4/6
✓ Branch 0 taken 778212 times.
✓ Branch 1 taken 654 times.
✓ Branch 2 taken 778559 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 778634 times.
✗ Branch 5 not taken.
778866 !spacing.Enabled() && !m_options.maxCraters && band.low > m_options.maxBandDiameter;
154
4/4
✓ Branch 0 taken 711709 times.
✓ Branch 1 taken 67157 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 711699 times.
778866 if (band.high <= skipBelow || tooLarge) {
155 67167 continue;
156 }
157
1/1
✓ Branch 1 taken 710360 times.
711699 const double margin = CandidateMargin(band.high);
158 710360 BandPlan entry{
159
2/2
✓ Branch 1 taken 711518 times.
✓ Branch 4 taken 712234 times.
710360 band, m_cells.CellsCovering(m_surface.Expand(m_region, margin), band.cellSize)};
160
2/2
✓ Branch 0 taken 455138 times.
✓ Branch 1 taken 257096 times.
712234 const double countPerArea = m_options.bandCount
161
3/3
✓ Branch 0 taken 455138 times.
✓ Branch 1 taken 257096 times.
✓ Branch 3 taken 454813 times.
712234 ? m_options.bandCount(band)
162
1/1
✓ Branch 1 taken 257095 times.
257096 : m_density.ExpectedCount(1.0, band.low, band.high);
163
2/2
✓ Branch 0 taken 1918139 times.
✓ Branch 1 taken 711688 times.
2629827 for (const CellIndex& cell : entry.cells) {
164
1/1
✓ Branch 1 taken 1917919 times.
1918139 entry.expected += countPerArea * m_cells.CellArea(cell, band.cellSize);
165 }
166 711688 LogDebug(m_options.logName, "Band [", band.low, ", ", band.high,
167 1 ") m: ", entry.cells.size(), " cells of ", band.cellSize, " m, ",
168
1/1
✓ Branch 1 taken 711478 times.
711688 entry.expected, " ", m_options.logName, " expected");
169
1/1
✓ Branch 1 taken 711339 times.
711478 plan.push_back(std::move(entry));
170 976374 }
171 264725 return plan;
172 2 }
173
174 264772 CraterList DrawPlannedCells(const std::vector<BandPlan>& plan, double expected) const {
175 264772 CraterList craters;
176
1/1
✓ Branch 1 taken 264873 times.
264706 craters.Reserve(static_cast<std::size_t>(expected));
177 264873 std::vector<BandRegion> regions;
178 264873 std::vector<std::pair<std::size_t, CellIndex>> cells;
179
2/2
✓ Branch 0 taken 711684 times.
✓ Branch 1 taken 264790 times.
976474 for (std::size_t b = 0; b < plan.size(); ++b) {
180
1/1
✓ Branch 1 taken 711614 times.
711684 regions.push_back(BandBounds(plan[b].band));
181
2/2
✓ Branch 0 taken 1703038 times.
✓ Branch 1 taken 711601 times.
2414639 for (const CellIndex& cell : plan[b].cells) {
182
1/1
✓ Branch 1 taken 1703032 times.
1703038 cells.emplace_back(b, cell);
183 }
184 }
185
4/4
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 264540 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 248 times.
264790 if (m_options.threads == 1 || cells.size() < 2) {
186
3/3
✓ Branch 1 taken 1700755 times.
✓ Branch 3 taken 1700612 times.
✓ Branch 4 taken 264685 times.
1965154 for (const auto& [b, cell] : cells) {
187
1/1
✓ Branch 1 taken 1700755 times.
1700612 DrawKept(plan[b].band, regions[b], cell, craters);
188 }
189 return craters;
190 }
191
1/1
✓ Branch 1 taken 248 times.
248 std::vector<CraterList> kept(cells.size());
192
2/2
✓ Branch 1 taken 248 times.
✓ Branch 4 taken 248 times.
248 ParallelFor(cells.size(), m_options.threads, [&](std::size_t n) {
193 5219 const auto& [b, cell] = cells[n];
194 5219 DrawKept(plan[b].band, regions[b], cell, kept[n]);
195 5223 });
196
2/2
✓ Branch 0 taken 5223 times.
✓ Branch 1 taken 248 times.
5471 for (const CraterList& some : kept) {
197
1/1
✓ Branch 1 taken 5223 times.
5223 craters.Append(some);
198 }
199 248 return craters;
200 264933 }
201
202 /// The region expanded by the candidate margins of a band's smallest and largest craters,
203 /// which bound every margin in between since `Expand` and `CandidateMargin` only grow.
204 struct BandRegion {
205 CraterBand band;
206 Bounds2d inner;
207 Bounds2d outer;
208 };
209
210 /// Appends to `craters` those of `cell` of `band` kept in the band's `region`, in the order
211 /// drawn.
212 1705445 void DrawKept(const CraterBand& band, const BandRegion& region, const CellIndex& cell,
213 CraterList& craters) const {
214 1705445 const std::shared_ptr<const CellCraters> drawn = DrawCell(band, cell);
215
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1704141 times.
1704161 const CraterList& list = drawn->craters;
216 1704161 const Bounds2d reach = drawn->maxDiameter > band.high
217
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1704141 times.
✓ Branch 3 taken 20 times.
✓ Branch 6 taken 20 times.
1704161 ? m_surface.Expand(m_region, CandidateMargin(drawn->maxDiameter))
218 1704141 : region.outer;
219
3/3
✓ Branch 1 taken 1699653 times.
✓ Branch 3 taken 6456966 times.
✓ Branch 4 taken 1699691 times.
8161165 for (const std::uint32_t n : drawn->Rows(reach.minY, reach.maxY)) {
220
2/2
✓ Branch 0 taken 3871855 times.
✓ Branch 1 taken 2585111 times.
6456966 if (OutsideBand(region, list.Diameters()[n], list.X()[n], list.Y()[n])) {
221 3871855 continue;
222 }
223
1/1
✓ Branch 1 taken 2585081 times.
2585111 const Crater crater = list.At(n);
224
6/6
✓ Branch 1 taken 2585224 times.
✓ Branch 3 taken 2362140 times.
✓ Branch 4 taken 223084 times.
✓ Branch 6 taken 2362125 times.
✓ Branch 8 taken 2325651 times.
✓ Branch 9 taken 36474 times.
2585081 if (InBandRegion(region, crater) && Kept(crater)) {
225
1/1
✓ Branch 1 taken 2325591 times.
2325651 craters.Append(crater);
226 }
227 1699691 }
228 1706152 }
229
230 711371 BandRegion BandBounds(const CraterBand& band) const {
231 711539 return {band, m_surface.Expand(m_region, CandidateMargin(band.low)),
232 1422917 m_surface.Expand(m_region, CandidateMargin(band.high))};
233 }
234
235 /// Whether a crater of the band lies outside its outer region, which `InBandRegion` rejects
236 /// first; tested on the list's columns before the crater is copied out.
237 6450258 bool OutsideBand(const BandRegion& region, double diameter, double x, double y) const {
238
2/4
✓ Branch 0 taken 6453103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6454031 times.
✗ Branch 3 not taken.
6450258 return diameter >= region.band.low && diameter <= region.band.high &&
239
2/2
✓ Branch 0 taken 2585042 times.
✓ Branch 1 taken 3868989 times.
6454031 !m_surface.Contains(region.outer, {x, y});
240 }
241
242 /// `InRegion` with the crater's candidate margin, expanding the region only for craters of the
243 /// band between its inner and outer regions.
244 2585120 bool InBandRegion(const BandRegion& region, const Crater& crater) const {
245
3/4
✓ Branch 0 taken 2585120 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 2585079 times.
2585120 if (!(crater.diameter >= region.band.low && crater.diameter <= region.band.high)) {
246 41 return InRegion(crater, CandidateMargin(crater.diameter));
247 }
248 2585079 const Vec2d center{crater.x, crater.y};
249
1/2
✓ Branch 0 taken 2585079 times.
✗ Branch 1 not taken.
2585079 if (!m_surface.Contains(region.outer, center)) {
250 return false;
251 }
252
2/2
✓ Branch 0 taken 328264 times.
✓ Branch 1 taken 2256815 times.
2585079 return m_surface.Contains(region.inner, center) ||
253 328264 InRegion(crater, CandidateMargin(crater.diameter));
254 }
255
256 /// Whether a generated crater stays: below the catalog's completeness limit at its center, and
257 /// within the keep share there by a uniform keyed to its ID.
258 2362095 bool Kept(const Crater& crater) const {
259
2/2
✓ Branch 0 taken 429926 times.
✓ Branch 1 taken 1342 times.
431268 if (m_options.completeness &&
260
2/2
✓ Branch 0 taken 431206 times.
✓ Branch 1 taken 1930889 times.
2362095 crater.diameter >= m_options.completeness->ValueAt(crater.x, crater.y)) {
261 return false;
262 }
263
2/2
✓ Branch 0 taken 543683 times.
✓ Branch 1 taken 1817132 times.
2360815 if (!m_options.keepShare) {
264 return true;
265 }
266 543683 const IdWords id = SplitId(crater.id);
267 543683 RandomStream random(m_seed, kCraterKeepStream, id.low, id.high);
268 543683 return random.NextDouble() < m_options.keepShare(crater);
269 }
270
271 1705243 std::shared_ptr<const CellCraters> DrawCell(const CraterBand& band,
272 const CellIndex& cell) const {
273
2/2
✓ Branch 0 taken 1686378 times.
✓ Branch 1 taken 18865 times.
1705243 if (m_options.cellCraters) {
274 1686378 return m_options.cellCraters(band, cell);
275 }
276 18865 return std::make_shared<const CellCraters>(
277 37729 GenerateCellCraters(m_cells, band, cell, m_density, m_ages, m_seed));
278 }
279
280 /// The craters within their margin of the region, the first `generated` of which already are.
281 52 CraterList CropToRegion(const CraterList& craters, std::size_t generated) const {
282 52 return FilterCraters(craters, [&](std::size_t n, const Crater& crater) {
283
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 514071 times.
514195 return n < generated || InRegion(crater, 0.5 * m_options.marginRadii * crater.diameter);
284
1/1
✓ Branch 1 taken 52 times.
52 });
285 }
286
287 /// The craters within their margin of the region that spacing keeps, counting those removed.
288 36 CraterList RemoveSpacingConflicts(const CraterList& craters, std::size_t& removed) const {
289 36 const std::vector<bool> conflicts = SpacingConflicts(craters, m_options.spacing, m_surface);
290 36 return FilterCraters(craters, [&](std::size_t n, const Crater& crater) {
291
2/2
✓ Branch 1 taken 24798 times.
✓ Branch 2 taken 4476 times.
29274 if (!InRegion(crater, 0.5 * m_options.marginRadii * crater.diameter)) {
292 return false;
293 }
294
2/2
✓ Branch 0 taken 8922 times.
✓ Branch 1 taken 15876 times.
24798 if (conflicts[n]) {
295 8922 ++removed;
296 8922 return false;
297 }
298 return true;
299
1/1
✓ Branch 1 taken 36 times.
36 });
300 36 }
301
302 /// The manual craters and the `maxCraters` largest generated ones (ties to the smaller ID),
303 /// counting the generated craters dropped.
304 264799 CraterList KeepLargest(const CraterList& craters, std::size_t& capped) const {
305
2/2
✓ Branch 0 taken 264796 times.
✓ Branch 1 taken 3 times.
264799 if (!m_options.maxCraters) {
306 264796 return craters;
307 }
308 3 const std::size_t limit = *m_options.maxCraters;
309 3 std::vector<std::size_t> generated;
310
2/2
✓ Branch 0 taken 4711 times.
✓ Branch 1 taken 3 times.
4714 for (std::size_t n = 0; n < craters.Size(); ++n) {
311
2/2
✓ Branch 0 taken 4710 times.
✓ Branch 1 taken 1 time.
4711 if (craters.Manual()[n] == 0) {
312
1/1
✓ Branch 1 taken 4710 times.
4710 generated.push_back(n);
313 }
314 }
315
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 if (generated.size() <= limit) {
316
1/1
✓ Branch 1 taken 1 time.
1 return craters;
317 }
318 2 const auto& diameters = craters.Diameters();
319 2 const auto& ids = craters.Ids();
320 2 std::nth_element(generated.begin(), generated.begin() + static_cast<std::ptrdiff_t>(limit),
321 4026 generated.end(), [&](std::size_t a, std::size_t b) {
322
1/2
✓ Branch 0 taken 4026 times.
✗ Branch 1 not taken.
4026 if (diameters[a] != diameters[b]) {
323 4026 return diameters[a] > diameters[b];
324 }
325 ✗ return ids[a] < ids[b];
326 });
327
1/1
✓ Branch 1 taken 2 times.
2 std::vector<bool> keep(craters.Size(), false);
328
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
62 for (std::size_t k = 0; k < limit; ++k) {
329 60 keep[generated[k]] = true;
330 }
331
1/1
✓ Branch 1 taken 2 times.
2 capped = generated.size() - limit;
332 2 return FilterCraters(
333
3/3
✓ Branch 1 taken 2 times.
✓ Branch 5 taken 2362 times.
✓ Branch 6 taken 60 times.
2424 craters, [&](std::size_t n, const Crater& crater) { return keep[n] || crater.manual; });
334 3 }
335
336 const SurfaceCells& m_cells;
337 const Bounds2d& m_region;
338 const SizeFrequencyDistribution& m_density;
339 const CraterAgeModel& m_ages;
340 std::uint32_t m_seed;
341 const CraterGenerationOptions& m_options;
342 const Surface& m_surface;
343 double m_maxDiameter = 0.0;
344 };
345
346 } // namespace
347
348 264773 std::vector<CraterBand> CraterBands(double minDiameter, double maxDiameter, double cellSizeFactor) {
349 264773 std::vector<CraterBand> bands;
350
2/2
✓ Branch 0 taken 780155 times.
✓ Branch 1 taken 265352 times.
1045507 for (int k = std::ilogb(minDiameter); std::ldexp(1.0, k) < maxDiameter; ++k) {
351 780155 const double low = std::fmax(minDiameter, std::ldexp(1.0, k));
352 780155 const double high = std::fmin(maxDiameter, std::ldexp(1.0, k + 1));
353
1/1
✓ Branch 1 taken 780734 times.
780155 bands.push_back({k, low, high, cellSizeFactor * std::ldexp(1.0, k + 1)});
354 }
355 265352 return bands;
356 }
357
358
1/1
✓ Branch 2 taken 128637 times.
128633 CellCraters::CellCraters(CraterList list) : craters(std::move(list)), byY(craters.Size()) {
359 128637 const std::vector<double>& y = craters.Y();
360
2/2
✓ Branch 0 taken 2699295 times.
✓ Branch 1 taken 128637 times.
2827932 for (std::uint32_t n = 0; n < byY.size(); ++n) {
361 2699295 byY[n] = n;
362 }
363 128637 std::stable_sort(byY.begin(), byY.end(),
364
12/18
✓ Branch 0 taken 1520506 times.
✓ Branch 1 taken 2009202 times.
✓ Branch 2 taken 1842678 times.
✓ Branch 3 taken 2346952 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2076642 times.
✓ Branch 9 taken 1585780 times.
✓ Branch 10 taken 531720 times.
✓ Branch 11 taken 508286 times.
✓ Branch 12 taken 749442 times.
✓ Branch 13 taken 747412 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 613378 times.
✓ Branch 17 taken 1585786 times.
16117784 [&y](std::uint32_t a, std::uint32_t b) { return y[a] < y[b]; });
365
2/2
✓ Branch 0 taken 2698999 times.
✓ Branch 1 taken 128635 times.
2827634 for (const double diameter : craters.Diameters()) {
366 2698999 maxDiameter = std::fmax(maxDiameter, diameter);
367 }
368 128635 }
369
370 1703827 std::vector<std::uint32_t> CellCraters::Rows(double minY, double maxY) const {
371 1703827 const std::vector<double>& y = craters.Y();
372 1703827 const auto first = std::lower_bound(
373
2/2
✓ Branch 0 taken 2549968 times.
✓ Branch 1 taken 4888840 times.
7438808 byY.begin(), byY.end(), minY, [&y](std::uint32_t n, double value) { return y[n] < value; });
374 1701592 const auto last = std::upper_bound(
375
2/2
✓ Branch 0 taken 2075730 times.
✓ Branch 1 taken 3004885 times.
5080615 first, byY.end(), maxY, [&y](double value, std::uint32_t n) { return value < y[n]; });
376 1700175 std::vector<std::uint32_t> rows(first, last);
377 1700366 std::sort(rows.begin(), rows.end());
378 1699627 return rows;
379 }
380
381 128205 CraterList GenerateCellCraters(const SurfaceCells& cells, const CraterBand& band,
382 const CellIndex& cell, const SizeFrequencyDistribution& density,
383 const CraterAgeModel& ages, std::uint32_t seed) {
384 128205 const auto k = static_cast<std::uint32_t>(band.exponent);
385 128205 const auto i = static_cast<std::uint32_t>(cell.i);
386 128205 const auto j = static_cast<std::uint32_t>(cell.j);
387 128205 RandomStream geometry(seed, kCraterStream, i, j, k);
388 128209 RandomStream ageStream(seed, kCraterAgeStream, i, j, k);
389 128206 const double countPerArea = density.ExpectedCount(1.0, band.low, band.high);
390 128205 const SurfaceCells::Sampling sampling = cells.CellSampling(cell, band.cellSize);
391 128205 const std::uint64_t count = SamplePoisson(geometry, countPerArea * sampling.area);
392 128209 CraterList craters;
393
1/1
✓ Branch 1 taken 128211 times.
128207 craters.Reserve(static_cast<std::size_t>(count));
394
2/2
✓ Branch 0 taken 2803814 times.
✓ Branch 1 taken 128209 times.
2932023 for (std::uint64_t index = 0; index < count; ++index) {
395
1/1
✓ Branch 1 taken 2803446 times.
2803814 const double diameter = density.SampleDiameter(geometry, band.low, band.high);
396
1/1
✓ Branch 1 taken 2803674 times.
2803446 const double u = geometry.NextDouble();
397
1/1
✓ Branch 1 taken 2803988 times.
2803674 const double v = geometry.NextDouble();
398
1/1
✓ Branch 1 taken 2803696 times.
2803988 const Vec2d center = cells.SamplePoint(u, v, cell, band.cellSize);
399
4/4
✓ Branch 0 taken 210816 times.
✓ Branch 1 taken 2592880 times.
✓ Branch 2 taken 106569 times.
✓ Branch 3 taken 104262 times.
3014527 if (sampling.bound > 0.0 &&
400
2/2
✓ Branch 1 taken 210845 times.
✓ Branch 4 taken 210831 times.
210816 geometry.NextDouble() * sampling.bound >= cells.Weight(center)) {
401 106569 continue;
402 }
403
1/1
✓ Branch 1 taken 2696174 times.
2697142 const double ageGyr = ages.SampleAgeGyr(ageStream, diameter);
404 2697243 craters.Append({center.x, center.y, diameter, ageGyr,
405
1/1
✓ Branch 1 taken 2697243 times.
2696174 CraterId(seed, band, cell, static_cast<std::uint32_t>(index))});
406 }
407 128209 return craters;
408 }
409
410 394642 double ResolveMaxDiameter(double maxDiameter, const SizeFrequencyDistribution& density) {
411
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 394637 times.
394642 return std::isinf(maxDiameter) ? density.ValidRange().max : maxDiameter;
412 }
413
414 264730 CraterList GenerateCraters(const SurfaceCells& cells, const Bounds2d& region,
415 const SizeFrequencyDistribution& density, const CraterAgeModel& ages,
416 std::uint32_t seed, const CraterGenerationOptions& options) {
417 264730 ValidateCraterGenerationOptions(options);
418 264647 const Stopwatch stopwatch;
419 264836 const Generation generation(cells, region, density, ages, seed, options);
420 264898 GenerationCounts counts;
421 264898 CraterList craters = generation.Run(counts);
422
1/1
✓ Branch 1 taken 264821 times.
264599 generation.Log(craters.Size(), counts, stopwatch);
423 264821 return craters;
424 }
425
426 6 double CountLimitDiameter(const SizeFrequencyDistribution& density, double area, double count,
427 double minDiameter, double maxDiameter) {
428 6 RequirePositive(count, "Count");
429 5 RequirePositive(area, "Area");
430
4/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
8 if (!(IsPositive(minDiameter) && maxDiameter > minDiameter && std::isfinite(maxDiameter))) {
431
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Diameter interval must satisfy 0 < minDiameter < maxDiameter");
432 }
433
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
3 if (density.ExpectedCount(area, minDiameter, maxDiameter) <= count) {
434 return minDiameter;
435 }
436 2 return BisectLog(minDiameter, maxDiameter, 200, 1e-12, [&](double diameter) {
437 86 return !(density.ExpectedCount(area, diameter, maxDiameter) > count);
438 });
439 }
440
441 } // namespace terraingen
442