./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
415 / 417
99.5%
Branches:
285 / 307
92.8%
Functions:
47 / 47
100.0%

src/layers/craters/shaping/datum_fit.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "layers/craters/shaping/datum_fit.h"
4
5 #include "gpu/kernel.h"
6 #include "layers/craters/shaping/resolved_state.h"
7 #include "terraingen/layers/craters/band_buckets.h"
8 #include "terraingen/raster/resample.h"
9 #include "terraingen/util/log.h"
10 #include "terraingen/util/parallel.h"
11
12 #include <algorithm>
13 #include <array>
14 #include <cmath>
15 #include <cstdint>
16 #include <limits>
17 #include <map>
18 #include <optional>
19 #include <utility>
20
21 namespace terraingen {
22
23 namespace {
24
25 /// The size band of `crater`: its diameter stretched by its outline, as a power of two.
26 13648 int ReliefBand(const PreparedCrater& crater) {
27 13648 return std::ilogb(crater.crater.diameter * crater.outline.MaxScale());
28 }
29
30 /// The datum fit footprint of a crater, in meters from its center.
31 23245 double FitRadius(const Crater& crater, const CraterApplyOptions& options) {
32 23245 return options.datumFitRadii * 0.5 * crater.diameter;
33 }
34
35 /// The surface points of the datum lattices of some craters, a row of `kDatumLatticePoints` per
36 /// crater, with `inside` 1 for the points inside the fit footprint.
37 482 struct DatumLattice {
38 std::vector<Vec2d> points;
39 std::vector<std::uint8_t> inside;
40 };
41
42 /// The datum lattices of craters `fitted` of `prepared`, located on `threads` threads.
43 482 DatumLattice LocateDatumLattice(const std::vector<PreparedCrater>& prepared,
44 const std::vector<std::size_t>& fitted,
45 const CraterApplyOptions& options, unsigned threads) {
46 482 DatumLattice lattice{std::vector<Vec2d>(kDatumLatticePoints * fitted.size()),
47
2/2
✓ Branch 2 taken 482 times.
✓ Branch 5 taken 482 times.
482 std::vector<std::uint8_t>(kDatumLatticePoints * fitted.size(), 0)};
48
2/2
✓ Branch 1 taken 482 times.
✓ Branch 4 taken 482 times.
482 ParallelFor(fitted.size(), threads, [&](std::size_t n) {
49 6368 const Crater& c = prepared[fitted[n]].crater;
50 6368 ForEachDatumPoint(FitRadius(c, options), [&](std::size_t point, double dx, double dy) {
51 234906 const std::size_t at = n * kDatumLatticePoints + point;
52 234906 lattice.points[at] = options.surface.Locate({c.x, c.y}, {dx, dy});
53 235171 lattice.inside[at] = 1;
54 235171 });
55 6368 });
56 482 return lattice;
57 }
58
59 /// The ground under `lattice`, a row per crater, read in one pass by `batchGround` when given,
60 /// else by `ground` on `threads` threads; NaN, the no-data value, outside the footprint and where
61 /// the ground has no data.
62 482 Raster<float> ReadGround(const DatumLattice& lattice, const HeightSampler& ground,
63 const HeightsSampler& batchGround, unsigned threads) {
64 482 constexpr float kNaN = std::numeric_limits<float>::quiet_NaN();
65 482 Raster<float> heights(kDatumLatticePoints, lattice.points.size() / kDatumLatticePoints, kNaN);
66
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 40 times.
482 heights.SetNoData(kNaN);
67
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 40 times.
482 if (batchGround) {
68 442 std::vector<Vec2d> read;
69 442 std::vector<std::size_t> slots;
70
2/2
✓ Branch 0 taken 42189 times.
✓ Branch 1 taken 442 times.
42631 for (std::size_t at = 0; at < lattice.points.size(); ++at) {
71
2/2
✓ Branch 0 taken 31857 times.
✓ Branch 1 taken 10332 times.
42189 if (lattice.inside[at] != 0) {
72
1/1
✓ Branch 1 taken 31857 times.
31857 read.push_back(lattice.points[at]);
73
1/1
✓ Branch 1 taken 31857 times.
31857 slots.push_back(at);
74 }
75 }
76
1/1
✓ Branch 1 taken 442 times.
442 std::vector<float> values(read.size());
77
1/1
✓ Branch 3 taken 442 times.
442 batchGround(read, values);
78
2/2
✓ Branch 0 taken 31857 times.
✓ Branch 1 taken 442 times.
32299 for (std::size_t i = 0; i < read.size(); ++i) {
79 31857 heights.Values()[slots[i]] = values[i];
80 }
81 442 return heights;
82 442 }
83
2/2
✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
40 ParallelFor(heights.Height(), threads, [&](std::size_t row) {
84
2/2
✓ Branch 0 taken 269843 times.
✓ Branch 1 taken 5507 times.
275350 for (std::size_t at = row * kDatumLatticePoints; at < (row + 1) * kDatumLatticePoints;
85 ++at) {
86
2/2
✓ Branch 0 taken 203759 times.
✓ Branch 1 taken 66084 times.
269843 if (lattice.inside[at] != 0) {
87 203759 heights.Values()[at] = ground(lattice.points[at].x, lattice.points[at].y);
88 }
89 }
90 5507 });
91 40 return heights;
92 }
93
94 /// 1 for each point of `ground` holding data, 0 elsewhere.
95 482 std::vector<std::uint8_t> ValidPoints(const Raster<float>& ground) {
96 482 std::vector<std::uint8_t> valid(ground.Size());
97
2/2
✓ Branch 0 taken 312032 times.
✓ Branch 1 taken 482 times.
312514 for (std::size_t at = 0; at < ground.Size(); ++at) {
98
2/2
✓ Branch 1 taken 193887 times.
✓ Branch 2 taken 118145 times.
505919 valid[at] = std::isnan(ground.Values()[at]) ? 0 : 1;
99 }
100 482 return valid;
101 }
102
103 /// Fits the datums of craters `fitted` of `prepared` to their rows of `lattice`.
104 482 void SolveDatums(std::vector<PreparedCrater>& prepared, const std::vector<std::size_t>& fitted,
105 const Raster<float>& lattice, const std::vector<std::uint8_t>& valid,
106 const CraterApplyOptions& options, unsigned threads) {
107
1/1
✓ Branch 2 taken 482 times.
482 ParallelFor(fitted.size(), threads, [&](std::size_t n) {
108 6368 PlaneFit fit;
109 6368 const Crater& c = prepared[fitted[n]].crater;
110 6368 ForEachDatumPoint(FitRadius(c, options), [&](std::size_t point, double dx, double dy) {
111 235416 const std::size_t at = n * kDatumLatticePoints + point;
112
2/2
✓ Branch 0 taken 193830 times.
✓ Branch 1 taken 41586 times.
235416 if (valid[at] != 0) {
113 193830 fit.Add(dx, dy, static_cast<double>(lattice.Values()[at]));
114 }
115 235419 });
116 6368 prepared[fitted[n]].datum = FittedDatum(fit, options.datum);
117 6368 });
118 482 }
119
120 /// Craters indexed for `KernelDatumRelief`, with the kernel that reads them. Positions are split
121 /// (`SplitDifference`) and cells are absolute, so the relief at a point depends only on the craters
122 /// reaching it. On a plane only.
123 struct DatumKernelIndex {
124 CraterDatumKernel kernel;
125 CraterKernelBatch batch;
126 /// Per size band: its cell size, which is its largest reach, and its first cell's column and
127 /// row.
128 std::vector<float> bandFloats;
129 /// Per size band: its cells across and down and its first cell.
130 std::vector<std::uint32_t> bandCells;
131 /// Per cell: its first entry and its entry count.
132 std::vector<std::uint32_t> cells;
133 /// Indices into the records, ascending within each cell.
134 std::vector<std::uint32_t> entries;
135 /// The record of each prepared crater, for the craters indexed.
136 std::vector<std::uint32_t> slot;
137 };
138
139 /// Most cells `IndexDatumRelief` lays out, and the largest cell column or row a float holds
140 /// exactly enough, before datums are left to `ReliefIndex`.
141 constexpr std::size_t kMaxReliefCells = std::size_t{1} << 24;
142 constexpr float kMaxReliefCellIndex = 8388608.0f;
143
144 /// The box holding the datum footprints of craters `fitted` of `prepared`.
145 454 Bounds2d FitRegion(const std::vector<PreparedCrater>& prepared,
146 const std::vector<std::size_t>& fitted, const CraterApplyOptions& options) {
147 454 constexpr double kInfinity = std::numeric_limits<double>::infinity();
148 454 Bounds2d region{kInfinity, kInfinity, -kInfinity, -kInfinity};
149
2/2
✓ Branch 0 taken 4946 times.
✓ Branch 1 taken 454 times.
5400 for (const std::size_t k : fitted) {
150 4946 const Crater& c = prepared[k].crater;
151 4946 const double fitRadius = FitRadius(c, options);
152 4946 region = {std::fmin(region.minX, c.x - fitRadius), std::fmin(region.minY, c.y - fitRadius),
153 4946 std::fmax(region.maxX, c.x + fitRadius), std::fmax(region.maxY, c.y + fitRadius)};
154 }
155 454 return region;
156 }
157
158 /// The craters of `prepared` up to the last of `fitted` that are fitted or reach their footprints,
159 /// in order, noting each one's record in `slot`.
160 454 std::vector<std::size_t> ReliefCraters(const std::vector<PreparedCrater>& prepared,
161 const std::vector<std::size_t>& fitted,
162 const CraterProfile& profile,
163 const CraterApplyOptions& options,
164 std::vector<std::uint32_t>& slot) {
165 454 const Bounds2d region = FitRegion(prepared, fitted, options);
166 454 std::vector<bool> isFitted(prepared.size(), false);
167
2/2
✓ Branch 0 taken 4946 times.
✓ Branch 1 taken 454 times.
5400 for (const std::size_t k : fitted) {
168 4946 isFitted[k] = true;
169 }
170
1/2
✓ Branch 0 taken 454 times.
✗ Branch 1 not taken.
454 const std::size_t last = *std::max_element(fitted.begin(), fitted.end());
171
1/1
✓ Branch 1 taken 454 times.
454 slot.assign(prepared.size(), 0);
172 454 std::vector<std::size_t> kept;
173
2/2
✓ Branch 0 taken 7733 times.
✓ Branch 1 taken 454 times.
8187 for (std::size_t n = 0; n <= last; ++n) {
174
1/1
✓ Branch 1 taken 7733 times.
7733 const PreparedCrater& p = prepared[n];
175
2/2
✓ Branch 1 taken 7733 times.
✓ Branch 4 taken 7733 times.
7733 const double reach = CraterReach(p, profile.ReachRadii());
176
6/6
✓ Branch 0 taken 2787 times.
✓ Branch 1 taken 4946 times.
✓ Branch 2 taken 1976 times.
✓ Branch 3 taken 811 times.
✓ Branch 4 taken 1243 times.
✓ Branch 5 taken 733 times.
7733 if (isFitted[n] || (p.crater.x + reach > region.minX && p.crater.x - reach < region.maxX &&
177
6/6
✓ Branch 0 taken 2787 times.
✓ Branch 1 taken 4946 times.
✓ Branch 2 taken 1078 times.
✓ Branch 3 taken 165 times.
✓ Branch 4 taken 726 times.
✓ Branch 5 taken 352 times.
8976 p.crater.y + reach > region.minY && p.crater.y - reach < region.maxY)) {
178
1/1
✓ Branch 1 taken 5672 times.
5672 slot[n] = static_cast<std::uint32_t>(kept.size());
179
1/1
✓ Branch 1 taken 5672 times.
5672 kept.push_back(n);
180 }
181 }
182 454 return kept;
183 454 }
184
185 /// The records of craters `kept` of `prepared`, grouped by size band.
186 454 std::map<int, std::vector<std::uint32_t>> BandRecords(const std::vector<PreparedCrater>& prepared,
187 const std::vector<std::size_t>& kept) {
188 454 std::map<int, std::vector<std::uint32_t>> bands;
189
2/2
✓ Branch 0 taken 5672 times.
✓ Branch 1 taken 454 times.
6126 for (std::size_t m = 0; m < kept.size(); ++m) {
190
3/3
✓ Branch 1 taken 5672 times.
✓ Branch 4 taken 5672 times.
✓ Branch 7 taken 5672 times.
5672 bands[ReliefBand(prepared[kept[m]])].push_back(static_cast<std::uint32_t>(m));
191 }
192 454 return bands;
193 }
194
195 /// The cells of side `size` that records `members` of `index` fall in: the first one's column
196 /// and row, and the cells across and down.
197 struct CellExtent {
198 float minI = 0.0f;
199 float minJ = 0.0f;
200 float maxI = 0.0f;
201 float maxJ = 0.0f;
202
203 1210 std::size_t Across() const {
204 1210 return static_cast<std::size_t>(maxI - minI) + 1;
205 }
206 1210 std::size_t Down() const {
207 1210 return static_cast<std::size_t>(maxJ - minJ) + 1;
208 }
209 };
210
211 605 CellExtent RecordCells(const DatumKernelIndex& index, float size,
212 const std::vector<std::uint32_t>& members) {
213 605 const float infinity = std::numeric_limits<float>::infinity();
214 605 CellExtent extent{infinity, infinity, -infinity, -infinity};
215
2/2
✓ Branch 0 taken 5672 times.
✓ Branch 1 taken 605 times.
6277 for (const std::uint32_t m : members) {
216 5672 const float i = std::floor(index.batch.records[m].x / size);
217 5672 const float j = std::floor(index.batch.records[m].y / size);
218 5672 extent.minI = std::fmin(extent.minI, i);
219 5672 extent.minJ = std::fmin(extent.minJ, j);
220 5672 extent.maxI = std::fmax(extent.maxI, i);
221 5672 extent.maxJ = std::fmax(extent.maxJ, j);
222 }
223 605 return extent;
224 }
225
226 /// Lays out `extent`'s cells from cell `first` and sorts records `members` into them.
227 605 void FillCells(DatumKernelIndex& index, float size, const CellExtent& extent, std::size_t first,
228 const std::vector<std::uint32_t>& members) {
229 605 const std::size_t across = extent.Across();
230 605 const std::size_t count = across * extent.Down();
231 605 index.cells.resize(2 * (first + count), 0);
232 605 std::vector<std::uint32_t> cellIndex(members.size());
233
2/2
✓ Branch 0 taken 5672 times.
✓ Branch 1 taken 605 times.
6277 for (std::size_t m = 0; m < members.size(); ++m) {
234 5672 const CraterRecord& r = index.batch.records[members[m]];
235 5672 const auto i = static_cast<std::size_t>(std::floor(r.x / size) - extent.minI);
236 5672 const auto j = static_cast<std::size_t>(std::floor(r.y / size) - extent.minJ);
237 5672 cellIndex[m] = static_cast<std::uint32_t>(first + j * across + i);
238 5672 ++index.cells[2 * cellIndex[m] + 1];
239 }
240 605 auto start = static_cast<std::uint32_t>(index.entries.size());
241
2/2
✓ Branch 0 taken 68589 times.
✓ Branch 1 taken 605 times.
69194 for (std::size_t cell = first; cell < first + count; ++cell) {
242 68589 index.cells[2 * cell] = start;
243 68589 start += index.cells[2 * cell + 1];
244 }
245
1/1
✓ Branch 1 taken 605 times.
605 index.entries.resize(start);
246
1/1
✓ Branch 1 taken 605 times.
605 std::vector<std::uint32_t> filled(count, 0);
247
2/2
✓ Branch 0 taken 5672 times.
✓ Branch 1 taken 605 times.
6277 for (std::size_t m = 0; m < members.size(); ++m) {
248 5672 const std::uint32_t cell = cellIndex[m];
249 5672 index.entries[index.cells[2 * cell] + filled[cell - first]++] = members[m];
250 }
251 605 }
252
253 /// Lays out the cells of one size band of `index`, `size` meters across, holding records
254 /// `members`; false when they spread too far for float cell indices or the cell budget.
255 605 bool AddReliefBand(DatumKernelIndex& index, float size, const std::vector<std::uint32_t>& members) {
256 605 const CellExtent extent = RecordCells(index, size, members);
257
1/2
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
605 const std::size_t first = index.cells.size() / 2;
258
2/4
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 605 times.
✗ Branch 3 not taken.
605 if (std::fmax(std::fabs(extent.minI), std::fabs(extent.maxI)) >= kMaxReliefCellIndex ||
259
2/4
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 605 times.
✗ Branch 3 not taken.
605 std::fmax(std::fabs(extent.minJ), std::fabs(extent.maxJ)) >= kMaxReliefCellIndex ||
260
1/2
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
605 first + extent.Across() * extent.Down() > kMaxReliefCells) {
261 return false;
262 }
263 605 index.bandFloats.insert(index.bandFloats.end(), {size, extent.minI, extent.minJ});
264 1210 index.bandCells.insert(index.bandCells.end(), {static_cast<std::uint32_t>(extent.Across()),
265 605 static_cast<std::uint32_t>(extent.Down()),
266 static_cast<std::uint32_t>(first)});
267 605 FillCells(index, size, extent, first, members);
268 return true;
269 }
270
271 /// Indexes the craters of `prepared` that can reach the datum footprint of any of `fitted`, in
272 /// size bands of cells as wide as each band's largest reach; nothing for craters spread too far.
273 454 std::optional<DatumKernelIndex> IndexDatumRelief(const std::vector<PreparedCrater>& prepared,
274 const std::vector<std::size_t>& fitted,
275 const CraterProfile& profile,
276 const CraterShapeKernel& shape,
277 const CraterApplyOptions& options) {
278 454 DatumKernelIndex index;
279 454 const std::vector<std::size_t> kept =
280
1/1
✓ Branch 1 taken 454 times.
454 ReliefCraters(prepared, fitted, profile, options, index.slot);
281
1/1
✓ Branch 1 taken 454 times.
454 index.batch = MakeCraterKernelBatch(shape, profile, prepared, kept, options);
282
1/1
✓ Branch 1 taken 454 times.
454 const std::map<int, std::vector<std::uint32_t>> bands = BandRecords(prepared, kept);
283
3/3
✓ Branch 1 taken 605 times.
✓ Branch 3 taken 605 times.
✓ Branch 4 taken 454 times.
1059 for (const auto& [band, members] : bands) {
284
1/1
✓ Branch 1 taken 605 times.
605 const auto size = static_cast<float>(1.0001 * profile.ReachRadii() * std::ldexp(1.0, band));
285
2/3
✓ Branch 1 taken 605 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 605 times.
605 if (!AddReliefBand(index, size, members)) {
286 ✗ return std::nullopt;
287 }
288 }
289 454 index.kernel.shape = shape;
290 454 index.kernel.fitRadii = static_cast<float>(options.datumFitRadii);
291 454 index.kernel.latticeSide = static_cast<std::uint32_t>(kDatumLatticeSide);
292 454 index.kernel.bandCount = static_cast<std::uint32_t>(bands.size());
293 454 return index;
294 454 }
295
296 /// Adds to `lattice` the relief of the craters before each of `fitted`, by `KernelDatumRelief`
297 /// on GPU `backend` when it can take it and on CPU threads otherwise; the backend it ran on.
298 470 Backend AddDatumRelief(Raster<float>& lattice, const std::vector<std::uint8_t>& valid,
299 const std::vector<std::size_t>& fitted, const DatumKernelIndex& index,
300 Backend backend, unsigned threads) {
301 470 std::vector<std::uint32_t> targets(fitted.size());
302
2/2
✓ Branch 0 taken 4927 times.
✓ Branch 1 taken 470 times.
5397 for (std::size_t n = 0; n < fitted.size(); ++n) {
303 4927 targets[n] = index.slot[fitted[n]];
304 }
305 470 const CraterKernelBatch& batch = index.batch;
306 470 const std::array<KernelBuffer, 7> extra{
307 470 MakeKernelBuffer(batch.records), MakeKernelBuffer(batch.data),
308 470 MakeKernelBuffer(index.bandFloats), MakeKernelBuffer(index.bandCells),
309 470 MakeKernelBuffer(index.cells), MakeKernelBuffer(index.entries),
310 470 MakeKernelBuffer(targets)};
311
4/4
✓ Branch 1 taken 470 times.
✓ Branch 4 taken 470 times.
✓ Branch 6 taken 464 times.
✓ Branch 7 taken 6 times.
470 if (RunKernel(backend, CompiledKernel<CraterDatumKernel>(), AsBytes(Span(&index.kernel, 1)),
312 lattice, {0.0, 0.0}, extra)) {
313 return backend;
314 }
315
2/2
✓ Branch 1 taken 464 times.
✓ Branch 4 taken 464 times.
464 ParallelFor(fitted.size(), threads, [&](std::size_t n) {
316
2/2
✓ Branch 0 taken 147179 times.
✓ Branch 1 taken 3004 times.
150183 for (std::size_t point = 0; point < kDatumLatticePoints; ++point) {
317 147179 const std::size_t at = n * kDatumLatticePoints + point;
318
2/2
✓ Branch 0 taken 90814 times.
✓ Branch 1 taken 56365 times.
147179 if (valid[at] != 0) {
319 90813 lattice.Values()[at] += KernelDatumRelief(
320 90814 index.kernel, batch.Data(), batch.records.data(), index.bandFloats.data(),
321 90814 index.bandCells.data(), index.cells.data(), index.entries.data(), targets[n],
322 static_cast<std::uint32_t>(point));
323 }
324 }
325 3004 });
326 464 return Backend::Cpu;
327 470 }
328
329 /// Fits the datums of craters `fitted` of `prepared`, all indexed in `index`: the ground under
330 /// each lattice point read, the relief of the craters before each crater added, and the fit solved
331 /// in double.
332 470 void FitDatumsWithKernel(std::vector<PreparedCrater>& prepared,
333 const std::vector<std::size_t>& fitted, const HeightSampler& ground,
334 const HeightsSampler& batchGround, const DatumKernelIndex& index,
335 const CraterApplyOptions& options, Backend backend, unsigned threads) {
336 470 Raster<float> lattice = ReadGround(LocateDatumLattice(prepared, fitted, options, threads),
337
1/1
✓ Branch 1 taken 470 times.
470 ground, batchGround, threads);
338
1/1
✓ Branch 1 taken 470 times.
470 const std::vector<std::uint8_t> valid = ValidPoints(lattice);
339
1/1
✓ Branch 1 taken 470 times.
470 const Backend ran = AddDatumRelief(lattice, valid, fitted, index, backend, threads);
340
1/1
✓ Branch 1 taken 470 times.
470 SolveDatums(prepared, fitted, lattice, valid, options, threads);
341
2/2
✓ Branch 1 taken 470 times.
✓ Branch 4 taken 470 times.
470 LogDebug("craters", "Fitted ", fitted.size(), " crater datums on ", BackendName(ran));
342 470 }
343
344 /// Prepared craters bucketed by surface position, to sum with `CraterRelief` the relief of the
345 /// craters before any one of them at a point, on any surface, each read at the kernel's split
346 /// offset (`PlanarSplitOffset`, `BodySplitOffset`). A point sums in an order fixed by the craters'
347 /// positions, so the relief depends only on the craters reaching it.
348 class ReliefIndex {
349 public:
350 /// Indexes `prepared`, which must outlive the index; their datums are not read.
351 136 ReliefIndex(const std::vector<PreparedCrater>& prepared, const CraterShapeKernel& shape,
352 const CraterProfile& profile, const CraterApplyOptions& options)
353 136 : m_prepared(&prepared), m_shape(shape), m_surface(options.surface),
354
2/2
✓ Branch 1 taken 136 times.
✓ Branch 4 taken 136 times.
136 m_profileReach(profile.ReachRadii()), m_buckets(m_surface) {
355
1/1
✓ Branch 1 taken 136 times.
136 std::vector<std::size_t> all(prepared.size());
356
2/2
✓ Branch 0 taken 7976 times.
✓ Branch 1 taken 136 times.
8112 for (std::size_t n = 0; n < all.size(); ++n) {
357 7976 all[n] = n;
358 }
359
1/1
✓ Branch 1 taken 136 times.
136 m_batch = MakeCraterKernelBatch(shape, profile, prepared, all, options);
360 136 std::map<int, std::vector<std::size_t>> members;
361
2/2
✓ Branch 0 taken 7976 times.
✓ Branch 1 taken 136 times.
8112 for (std::size_t n = 0; n < prepared.size(); ++n) {
362
1/1
✓ Branch 1 taken 7976 times.
7976 const int band = ReliefBand(prepared[n]);
363
1/1
✓ Branch 1 taken 7976 times.
7976 const Crater& crater = prepared[n].crater;
364
1/1
✓ Branch 1 taken 7976 times.
7976 m_buckets.Add(band, m_profileReach * std::ldexp(1.0, band + 1), {crater.x, crater.y},
365 n);
366
2/2
✓ Branch 1 taken 7976 times.
✓ Branch 4 taken 7976 times.
7976 members[band].push_back(n);
367
2/2
✓ Branch 0 taken 2982 times.
✓ Branch 1 taken 4994 times.
7976 if (m_surface.IsBody()) {
368
1/1
✓ Branch 1 taken 2982 times.
5964 m_frames.push_back(MakeCraterFrame(m_surface, crater));
369 }
370 }
371
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 84 times.
136 if (m_frames.empty()) {
372
1/1
✓ Branch 1 taken 52 times.
52 m_frames.push_back(CraterFrame{});
373 }
374
3/3
✓ Branch 1 taken 330 times.
✓ Branch 3 taken 330 times.
✓ Branch 4 taken 136 times.
466 for (auto& [band, craters] : members) {
375
1/1
✓ Branch 1 taken 330 times.
330 m_bands.push_back(band);
376
1/1
✓ Branch 1 taken 330 times.
330 m_members.push_back(std::move(craters));
377 }
378
0/2
✗ Branch 7 not taken.
✗ Branch 8 not taken.
136 }
379
380 ReliefIndex(const ReliefIndex&) = delete;
381 ReliefIndex& operator=(const ReliefIndex&) = delete;
382
383 /// The records of every crater, in order.
384 const CraterKernelBatch& Batch() const {
385 12 return m_batch;
386 }
387
388 /// Every crater's frame on a body, in order; a placeholder on a plane.
389 12 const std::vector<CraterFrame>& Frames() const {
390 12 return m_frames;
391 }
392
393 /// How far (m) crater `n` reaches, stretched by its outline, with a little slack: the bound
394 /// `CraterRelief` takes.
395 3590493 float Bound(std::size_t n) const {
396 3590493 return static_cast<float>(1.0001 * CraterReach((*m_prepared)[n], m_profileReach));
397 }
398
399 /// Calls `visit(n)` for each crater n before crater `k` whose center may lie within `radius`
400 /// meters plus its reach of surface `point`, and perhaps for others before `k`, in no fixed
401 /// order.
402 template <typename Visit>
403 1441 void ForEachNear(const Vec2d& point, double radius, std::size_t k, const Visit& visit) const {
404 14879 const auto before = [&](const std::vector<std::size_t>& craters) {
405
2/2
✓ Branch 0 taken 176767 times.
✓ Branch 1 taken 167 times.
176934 for (const std::size_t n : craters) {
406
2/2
✓ Branch 0 taken 170215 times.
✓ Branch 1 taken 6552 times.
176767 if (n >= k) {
407 break;
408 }
409 170215 visit(n);
410 }
411 };
412
2/2
✓ Branch 0 taken 6719 times.
✓ Branch 1 taken 1441 times.
8160 for (std::size_t b = 0; b < m_bands.size(); ++b) {
413 6719 const auto span =
414 6719 static_cast<long long>(std::ceil(radius / m_buckets.Size(m_bands[b]))) + 1;
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6719 times.
6719 const long long depth = m_surface.IsBody() ? span : 0;
416 6719 const auto buckets = static_cast<double>((2 * span + 1) * (2 * span + 1)) *
417 6719 static_cast<double>(2 * depth + 1);
418
1/2
✓ Branch 0 taken 6719 times.
✗ Branch 1 not taken.
6719 if (buckets >= static_cast<double>(m_members[b].size())) {
419 6719 before(m_members[b]);
420 } else {
421 ✗ m_buckets.ForEachAround(m_bands[b], point, span, before);
422 }
423 }
424 1441 }
425
426 /// The relief in meters at surface `point` of the craters before crater `k`.
427 55263 float ReliefBefore(const Vec2d& point, std::size_t k) const {
428 55263 float relief = 0.0f;
429 55263 const CraterData data = m_batch.Data();
430 55263 const SplitPoint at = MakeSplitPoint(m_surface, point);
431 55259 const bool onBody = m_surface.IsBody();
432
2/2
✓ Branch 0 taken 248838 times.
✓ Branch 1 taken 55247 times.
304085 for (const int band : m_bands) {
433 248838 m_buckets.ForEachAround(band, point, 1, [&](const std::vector<std::size_t>& bucket) {
434
2/2
✓ Branch 0 taken 4205612 times.
✓ Branch 1 taken 170768 times.
4376380 for (const std::size_t n : bucket) {
435
2/2
✓ Branch 0 taken 3418897 times.
✓ Branch 1 taken 786715 times.
4205612 if (n >= k) {
436 break;
437 }
438
2/2
✓ Branch 0 taken 3400409 times.
✓ Branch 1 taken 18488 times.
3418897 const KernelOffset offset = onBody ? BodySplitOffset(m_frames[n], at)
439 18488 : PlanarSplitOffset(m_batch.records[n], at);
440 3418891 relief += CraterRelief(m_shape, data, m_batch.records[n], Bound(n), offset.dx,
441 3418897 offset.dy);
442 }
443 957483 });
444 }
445 55247 return relief;
446 }
447
448 private:
449 const std::vector<PreparedCrater>* m_prepared;
450 CraterShapeKernel m_shape;
451 Surface m_surface;
452 double m_profileReach = 1.0;
453 CraterKernelBatch m_batch;
454 std::vector<CraterFrame> m_frames;
455 BandBuckets m_buckets;
456 std::vector<int> m_bands;
457 /// The craters of each of `m_bands`, in order.
458 std::vector<std::vector<std::size_t>> m_members;
459 };
460
461 /// The ground crater `k` fits its datum to: `ground` plus the relief of the craters before it in
462 /// `index`; NaN where `ground` has no data. Both must outlive the result.
463 1632 HeightSampler GroundBefore(const HeightSampler& ground, const ReliefIndex& index, std::size_t k) {
464 60581 return [&ground, &index, k](double x, double y) {
465 57317 const float height = ground(x, y);
466
2/2
✓ Branch 0 taken 55257 times.
✓ Branch 1 taken 2060 times.
57317 if (std::isnan(height)) {
467 return height;
468 }
469 55257 return static_cast<float>(static_cast<double>(height) +
470 55257 static_cast<double>(index.ReliefBefore({x, y}, k)));
471 1632 };
472 }
473
474 /// How far beyond the sum of their reach and footprint craters are taken as reaching a datum
475 /// footprint on a body, so local frames, which shorten distances on a tangent plane, miss none.
476 constexpr double kBodyReachSlack = 1.25;
477
478 /// The craters whose relief datums on a body are fitted to, as `KernelBodyDatumRelief` reads
479 /// them.
480 12 struct BodyReliefCraters {
481 std::vector<float> bounds; ///< Per crater, `ReliefIndex::Bound`.
482 /// Per crater fitted, its first entry and its entry count.
483 std::vector<std::uint32_t> lists;
484 /// The craters before each crater fitted that can reach its footprint, ascending per crater.
485 std::vector<std::uint32_t> entries;
486 };
487
488 /// The craters before crater `k` of `prepared` that can reach its datum footprint on a body,
489 /// ascending.
490 1441 std::vector<std::uint32_t> CratersNear(const std::vector<PreparedCrater>& prepared, std::size_t k,
491 const ReliefIndex& index,
492 const CraterApplyOptions& options) {
493
1/1
✓ Branch 1 taken 1441 times.
1441 const Crater& c = prepared[k].crater;
494 1441 const double fitRadius = FitRadius(c, options);
495
1/1
✓ Branch 1 taken 1441 times.
1441 std::vector<std::uint32_t> near;
496
1/1
✓ Branch 1 taken 1441 times.
1441 index.ForEachNear({c.x, c.y}, kBodyReachSlack * fitRadius, k, [&](std::size_t n) {
497 170215 const Crater& other = prepared[n].crater;
498 170215 const double reach = static_cast<double>(index.Bound(n)) + fitRadius;
499
2/2
✓ Branch 1 taken 19555 times.
✓ Branch 2 taken 150660 times.
170215 if (options.surface.Distance({c.x, c.y}, {other.x, other.y}) < kBodyReachSlack * reach) {
500 19555 near.push_back(static_cast<std::uint32_t>(n));
501 }
502 170215 });
503 1441 std::sort(near.begin(), near.end());
504 1441 return near;
505 }
506
507 /// The craters of `prepared` in `index` as the relief datums of craters `fitted` are fitted to on
508 /// a body, on `threads` threads.
509 12 BodyReliefCraters MakeBodyReliefCraters(const std::vector<PreparedCrater>& prepared,
510 const std::vector<std::size_t>& fitted,
511 const ReliefIndex& index, const CraterApplyOptions& options,
512 unsigned threads) {
513
1/1
✓ Branch 1 taken 12 times.
12 BodyReliefCraters craters;
514
1/1
✓ Branch 1 taken 12 times.
12 craters.bounds.resize(prepared.size());
515
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 12 times.
1453 for (std::size_t n = 0; n < prepared.size(); ++n) {
516
1/1
✓ Branch 1 taken 1441 times.
1441 craters.bounds[n] = index.Bound(n);
517 }
518
1/1
✓ Branch 1 taken 12 times.
12 std::vector<std::vector<std::uint32_t>> near(fitted.size());
519
1/1
✓ Branch 1 taken 12 times.
12 ParallelFor(fitted.size(), threads,
520
1/1
✓ Branch 1 taken 12 times.
1453 [&](std::size_t n) { near[n] = CratersNear(prepared, fitted[n], index, options); });
521
1/1
✓ Branch 1 taken 12 times.
12 craters.entries = FlattenKernelLists(near, craters.lists);
522 12 return craters;
523 12 }
524
525 /// The points of `lattice` inside the footprint as kernels read them (`MakeSplitPoint`), zero
526 /// elsewhere, set on `threads` threads.
527 12 std::vector<SplitPoint> LatticePositions(const DatumLattice& lattice, const Surface& surface,
528 unsigned threads) {
529
1/1
✓ Branch 2 taken 12 times.
12 std::vector<SplitPoint> positions(lattice.points.size(), SplitPoint{});
530
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
12 ParallelFor(lattice.points.size() / kDatumLatticePoints, threads, [&](std::size_t row) {
531
2/2
✓ Branch 0 taken 70609 times.
✓ Branch 1 taken 1441 times.
72050 for (std::size_t at = row * kDatumLatticePoints; at < (row + 1) * kDatumLatticePoints;
532 ++at) {
533
2/2
✓ Branch 0 taken 53317 times.
✓ Branch 1 taken 17292 times.
70609 if (lattice.inside[at] != 0) {
534 53317 positions[at] = MakeSplitPoint(surface, lattice.points[at]);
535 }
536 }
537 1441 });
538 12 return positions;
539 }
540
541 /// Fits the datums of craters `fitted` of `prepared` on a body with the relief of the craters
542 /// before each from `index`, added by `KernelBodyDatumRelief` on GPU `sampling.backend`; false,
543 /// fitting nothing, when the backend is the CPU or cannot take the lattice.
544 37 bool FitBodyDatumsOnGpu(std::vector<PreparedCrater>& prepared,
545 const std::vector<std::size_t>& fitted, const HeightSampler& ground,
546 const HeightsSampler& batchGround, const ReliefIndex& index,
547 const CraterShapeKernel& shape, const CraterApplyOptions& options,
548 const CraterSampling& sampling) {
549
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
37 if (sampling.backend == Backend::Cpu || fitted.empty()) {
550 return false;
551 }
552 12 const DatumLattice points = LocateDatumLattice(prepared, fitted, options, sampling.threads);
553 12 const std::vector<SplitPoint> positions =
554
1/1
✓ Branch 1 taken 12 times.
12 LatticePositions(points, options.surface, sampling.threads);
555
1/1
✓ Branch 1 taken 12 times.
12 Raster<float> lattice = ReadGround(points, ground, batchGround, sampling.threads);
556
1/1
✓ Branch 1 taken 12 times.
12 const std::vector<std::uint8_t> valid = ValidPoints(lattice);
557 12 const BodyReliefCraters craters =
558
1/1
✓ Branch 1 taken 12 times.
12 MakeBodyReliefCraters(prepared, fitted, index, options, sampling.threads);
559 12 const CraterKernelBatch& batch = index.Batch();
560 12 const CraterBodyDatumKernel kernel{shape};
561 12 const std::array<KernelBuffer, 7> extra{
562 12 MakeKernelBuffer(batch.records), MakeKernelBuffer(batch.data),
563 12 MakeKernelBuffer(index.Frames()), MakeKernelBuffer(craters.bounds),
564 12 MakeKernelBuffer(craters.lists), MakeKernelBuffer(craters.entries),
565 12 MakeKernelBuffer(positions, 1)};
566
3/4
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
12 if (!RunKernel(sampling.backend, CompiledKernel<CraterBodyDatumKernel>(),
567 AsBytes(Span(&kernel, 1)), lattice, {0.0, 0.0}, extra)) {
568 return false;
569 }
570
1/1
✓ Branch 1 taken 12 times.
12 SolveDatums(prepared, fitted, lattice, valid, options, sampling.threads);
571
1/1
✓ Branch 1 taken 12 times.
12 LogDebug("craters", "Fitted ", fitted.size(), " crater datums on ",
572
1/1
✓ Branch 1 taken 12 times.
12 BackendName(sampling.backend));
573 12 return true;
574 24 }
575
576 /// Gives every crater of `prepared` its shape (`SlopeShape`) and wear (`SlopeWearRate`) on the
577 /// gradient of `ground`, the ground beneath the layer, across its datum footprint; before any
578 /// datum is fitted, so each crater's slope depends on no other crater of its layer.
579 1740 void ShapeOnSlopes(std::vector<PreparedCrater>& prepared, const HeightSampler& ground,
580 const HeightsSampler& batch, const CraterShapeModels& models,
581 const CraterApplyOptions& options) {
582 1740 const CraterSlopeResponse& response = options.slope;
583
4/4
✓ Branch 0 taken 1727 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1724 times.
✓ Branch 3 taken 3 times.
1740 if (!response.asymmetric && !response.criticalGradient) {
584 1724 return;
585 }
586
1/1
✓ Branch 1 taken 16 times.
16 std::vector<Vec2d> points;
587
1/1
✓ Branch 1 taken 16 times.
16 points.reserve(4 * prepared.size());
588
2/2
✓ Branch 0 taken 2061 times.
✓ Branch 1 taken 16 times.
2077 for (const PreparedCrater& crater : prepared) {
589 2061 const Crater& c = crater.crater;
590 2061 const std::array<Vec2d, 4> around =
591
1/1
✓ Branch 1 taken 2061 times.
2061 GradientPoints(options.surface, {c.x, c.y}, FitRadius(c, options));
592
1/1
✓ Branch 1 taken 2061 times.
2061 points.insert(points.end(), around.begin(), around.end());
593 }
594
1/1
✓ Branch 1 taken 16 times.
16 std::vector<float> heights(points.size());
595
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 15 times.
16 if (batch) {
596
1/1
✓ Branch 3 taken 1 time.
1 batch(points, heights);
597 } else {
598
2/2
✓ Branch 0 taken 8196 times.
✓ Branch 1 taken 15 times.
8211 for (std::size_t i = 0; i < points.size(); ++i) {
599
1/1
✓ Branch 1 taken 8196 times.
8196 heights[i] = ground(points[i].x, points[i].y);
600 }
601 }
602
2/2
✓ Branch 0 taken 2061 times.
✓ Branch 1 taken 16 times.
2077 for (std::size_t k = 0; k < prepared.size(); ++k) {
603
1/1
✓ Branch 1 taken 2061 times.
2061 PreparedCrater& crater = prepared[k];
604 2061 const Crater& c = crater.crater;
605
1/1
✓ Branch 1 taken 2061 times.
2061 const std::optional<Vec2d> gradient = GradientFromHeights(
606
1/1
✓ Branch 1 taken 2061 times.
2061 Span<const float>(heights.data() + 4 * k, 4), FitRadius(c, options));
607
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 1841 times.
2061 if (!gradient) {
608 220 continue;
609 }
610
1/1
✓ Branch 1 taken 1841 times.
1841 crater.slope = SlopeShape(c, gradient->x, gradient->y, response);
611
1/1
✓ Branch 1 taken 1841 times.
1841 const double rate = SlopeWearRate(response, gradient->x, gradient->y);
612
2/2
✓ Branch 0 taken 599 times.
✓ Branch 1 taken 1242 times.
1841 if (rate > 1.0) {
613
1/1
✓ Branch 1 taken 599 times.
599 crater.state = ResolvedCraterState(models, c, rate);
614 }
615 }
616 16 }
617
618 /// Whether datums are fitted with `DatumKernelIndex`: on a plane, for fitted datums.
619 597 bool FitsOnKernelIndex(const CraterApplyOptions& options) {
620
4/4
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 470 times.
597 return !options.surface.IsBody() && options.datum != CraterDatum::CenterHeight;
621 }
622
623 /// Fits the datums of craters `fitted` of `prepared` to `ground`: with the crater datum kernel
624 /// where it can take them, on a body with the body datum kernel on a GPU, else through
625 /// `ReliefIndex`.
626 111 void FitDatums(std::vector<PreparedCrater>& prepared, const std::vector<std::size_t>& fitted,
627 const HeightSampler& ground, const HeightsSampler& batchGround,
628 const CraterProfile& profile, const CraterShapeKernel& shape,
629 const CraterApplyOptions& options, const CraterSampling& sampling) {
630
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 55 times.
111 if (FitsOnKernelIndex(options)) {
631 112 if (const std::optional<DatumKernelIndex> index =
632
1/2
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
56 IndexDatumRelief(prepared, fitted, profile, shape, options)) {
633 56 FitDatumsWithKernel(prepared, fitted, ground, batchGround, *index, options,
634
1/1
✓ Branch 1 taken 56 times.
56 sampling.backend, sampling.threads);
635 56 return;
636 56 }
637 }
638 55 const ReliefIndex index(prepared, shape, profile, options);
639
6/6
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 25 times.
92 if (options.surface.IsBody() && options.datum != CraterDatum::CenterHeight &&
640
1/1
✓ Branch 1 taken 37 times.
37 FitBodyDatumsOnGpu(prepared, fitted, ground, batchGround, index, shape, options,
641 sampling)) {
642 12 return;
643 }
644
2/2
✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
43 ParallelFor(fitted.size(), sampling.threads, [&](std::size_t n) {
645 1529 const std::size_t k = fitted[n];
646 1529 prepared[k].datum =
647
1/1
✓ Branch 2 taken 1529 times.
1529 FitCraterDatum(prepared[k].crater, options, GroundBefore(ground, index, k));
648 1529 });
649 55 }
650
651 } // namespace
652
653 111 void FitGridDatums(std::vector<PreparedCrater>& prepared, const std::vector<std::size_t>& fitted,
654 const Raster<float>& raster, const CraterShapeModels& models,
655 const CraterShapeKernel& kernel, const CraterApplyOptions& options,
656 const CraterSampling& sampling) {
657 111 const HeightSampler ground =
658
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 45 times.
111 sampling.ground ? sampling.ground : HeightSampler([&raster](double x, double y) {
659 263745 return SampleRaster(raster, x, y, ResampleMethod::Bilinear);
660 111 });
661
3/3
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 45 times.
✓ Branch 3 taken 66 times.
111 const HeightsSampler batch = sampling.ground ? sampling.groundBatch : HeightsSampler{};
662
1/1
✓ Branch 1 taken 111 times.
111 ShapeOnSlopes(prepared, ground, batch, models, options);
663
1/1
✓ Branch 1 taken 111 times.
111 FitDatums(prepared, fitted, ground, batch, *models.profile, kernel, options, sampling);
664 111 }
665
666 464 void FitPointDatums(std::vector<PreparedCrater>& prepared, const std::vector<std::size_t>& fitted,
667 const CraterShapeModels& models, const CraterShapeKernel& kernel,
668 const CraterApplyOptions& options, const CraterSampling& sampling) {
669 464 ShapeOnSlopes(prepared, sampling.ground, sampling.groundBatch, models, options);
670 464 const CraterProfile& profile = *models.profile;
671 464 std::optional<DatumKernelIndex> kernelIndex;
672 464 bool indexed = false;
673 464 std::optional<ReliefIndex> index;
674 950 const auto fit = [&](std::size_t k) -> std::optional<Plane> {
675
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 72 times.
486 if (FitsOnKernelIndex(options)) {
676
2/2
✓ Branch 0 taken 398 times.
✓ Branch 1 taken 16 times.
414 if (!indexed) {
677 398 kernelIndex = IndexDatumRelief(prepared, fitted, profile, kernel, options);
678 398 indexed = true;
679 }
680
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 if (kernelIndex) {
681
1/1
✓ Branch 1 taken 414 times.
414 FitDatumsWithKernel(prepared, {k}, sampling.ground, sampling.groundBatch,
682 414 *kernelIndex, options, Backend::Cpu, 1);
683 414 return prepared[k].datum;
684 }
685 }
686
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 6 times.
72 if (!index) {
687 66 index.emplace(prepared, kernel, profile, options);
688 }
689
1/1
✓ Branch 1 taken 72 times.
72 return FitCraterDatum(prepared[k].crater, options,
690 144 GroundBefore(sampling.ground, *index, k));
691 464 };
692
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 464 times.
978 for (const std::size_t k : fitted) {
693
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 356 times.
514 if (sampling.cache != nullptr) {
694 158 prepared[k].datum = *sampling.cache->Get<std::optional<Plane>>(
695
2/3
✓ Branch 2 taken 158 times.
✓ Branch 4 taken 158 times.
✗ Branch 5 not taken.
288 sampling.owner, prepared[k].crater.id, sampling.spacing, [&] { return fit(k); });
696 } else {
697
1/1
✓ Branch 1 taken 356 times.
356 prepared[k].datum = fit(k);
698 }
699 }
700 464 }
701
702 76224 void FitReplacementDatums(std::vector<PreparedCrater>& prepared,
703 const std::vector<std::size_t>& replacing, const Raster<float>* raster,
704 const CraterShapeModels& models, const CraterShapeKernel& kernel,
705 const CraterApplyOptions& options, const CraterSampling& sampling) {
706
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 76209 times.
76224 if (replacing.empty()) {
707 76210 return;
708 }
709 15 const HeightSampler ground =
710
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
15 sampling.ground || raster == nullptr
711
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
15 ? sampling.ground
712 6 : HeightSampler([raster](double x, double y) {
713 432 return SampleRaster(*raster, x, y, ResampleMethod::Bilinear);
714 15 });
715
1/1
✓ Branch 1 taken 15 times.
15 const CraterProfile& profile = *models.profile;
716
1/1
✓ Branch 1 taken 15 times.
15 const ReliefIndex index(prepared, kernel, profile, options);
717 46 const auto fit = [&](std::size_t k) {
718
3/3
✓ Branch 1 taken 31 times.
✓ Branch 4 taken 31 times.
✓ Branch 7 taken 31 times.
31 return FitReplacementDatum(prepared[k].crater, options, profile.ReachRadii(),
719
1/1
✓ Branch 2 taken 31 times.
62 prepared[k].outline.MaxScale(), GroundBefore(ground, index, k));
720 15 };
721
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
15 if (raster == nullptr && sampling.cache != nullptr) {
722
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 for (const std::size_t k : replacing) {
723 1 prepared[k].datum = *sampling.cache->Get<std::optional<Plane>>(
724
2/3
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 sampling.owner, prepared[k].crater.id, sampling.spacing, [&] { return fit(k); });
725 }
726 1 return;
727 }
728
1/1
✓ Branch 1 taken 14 times.
14 ParallelFor(replacing.size(), sampling.threads,
729
1/1
✓ Branch 1 taken 14 times.
44 [&](std::size_t n) { prepared[replacing[n]].datum = fit(replacing[n]); });
730 15 }
731
732 1279 bool UseCachedDatums(std::vector<PreparedCrater>& prepared, const std::vector<std::size_t>& fitted,
733 const CraterShapeModels& models, const CraterApplyOptions& options,
734 const CraterSampling& sampling) {
735
1/2
✓ Branch 0 taken 1279 times.
✗ Branch 1 not taken.
1279 if (sampling.cache == nullptr) {
736 return false;
737 }
738 1279 std::vector<PreparedCrater> found;
739
2/2
✓ Branch 0 taken 1137 times.
✓ Branch 1 taken 1165 times.
2302 for (const std::size_t k : fitted) {
740 1137 const auto datum = sampling.cache->Find<std::optional<Plane>>(
741
1/1
✓ Branch 1 taken 1137 times.
1137 sampling.owner, prepared[k].crater.id, sampling.spacing);
742
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 1023 times.
1137 if (!datum) {
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 return false;
744 }
745
1/1
✓ Branch 1 taken 1023 times.
1023 found.push_back(prepared[k]);
746
1/2
✓ Branch 0 taken 1023 times.
✗ Branch 1 not taken.
1023 found.back().datum = *datum;
747 1137 }
748
1/1
✓ Branch 1 taken 1165 times.
1165 ShapeOnSlopes(found, sampling.ground, sampling.groundBatch, models, options);
749
2/2
✓ Branch 0 taken 1013 times.
✓ Branch 1 taken 1165 times.
2178 for (std::size_t n = 0; n < fitted.size(); ++n) {
750 1013 prepared[fitted[n]] = std::move(found[n]);
751 }
752 return true;
753 1279 }
754
755 } // namespace terraingen
756