./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
227 / 229
99.1%
Branches:
185 / 201
92.0%
Functions:
22 / 22
100.0%

src/layers/craters/shaping/apply.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/shaping/apply.h"
4
5 #include "gpu/kernel.h"
6 #include "layers/craters/shaping/crater_kernel.h"
7 #include "layers/craters/shaping/datum_fit.h"
8 #include "terraingen/raster/cell_loops.h"
9 #include "terraingen/raster/grid.h"
10 #include "terraingen/util/bits.h"
11 #include "terraingen/util/log.h"
12 #include "terraingen/util/parallel.h"
13 #include "util/checks.h"
14
15 #include <algorithm>
16 #include <array>
17 #include <cmath>
18 #include <cstdint>
19 #include <stdexcept>
20 #include <utility>
21 #include <vector>
22
23 namespace terraingen {
24
25 namespace {
26
27 /// The box a crater reaching `reachRadii` radii can change, in surface coordinates.
28 508969 Bounds2d Footprint(const PreparedCrater& prepared, double reachRadii, const Surface& surface) {
29 508969 const Crater& crater = prepared.crater;
30 508969 return surface.Expand({crater.x, crater.y, crater.x, crater.y},
31 509235 CraterReach(prepared, reachRadii));
32 }
33
34 /// Craters `indices` of `prepared` split into those that replace the terrain beneath them and the
35 /// others, each in order.
36 77532 struct ReplacingSplit {
37 std::vector<std::size_t> replacing;
38 std::vector<std::size_t> others;
39 };
40
41 77548 ReplacingSplit SplitReplacing(const std::vector<PreparedCrater>& prepared,
42 const std::vector<std::size_t>& indices) {
43 77548 ReplacingSplit split;
44
2/2
✓ Branch 0 taken 168602 times.
✓ Branch 1 taken 77546 times.
246148 for (const std::size_t k : indices) {
45
3/3
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 168571 times.
✓ Branch 3 taken 168600 times.
168602 (prepared[k].crater.replace ? split.replacing : split.others).push_back(k);
46 }
47 77546 return split;
48 }
49
50 /// A prepared crater with the box its profile can reach.
51 struct ReachingCrater {
52 PreparedCrater* prepared;
53 Bounds2d footprint;
54 };
55
56 /// The craters of `prepared` reaching `reachRadii` radii that reach any of `samples`, in order.
57 76949 std::vector<ReachingCrater> CratersReaching(const SampleBlock& samples,
58 std::vector<PreparedCrater>& prepared,
59 double reachRadii, const Surface& surface) {
60 76949 std::vector<ReachingCrater> reaching;
61
2/2
✓ Branch 0 taken 480046 times.
✓ Branch 1 taken 77045 times.
557091 for (PreparedCrater& crater : prepared) {
62 480046 const Crater& c = crater.crater;
63
1/1
✓ Branch 1 taken 480385 times.
480046 const ReachingCrater candidate{&crater, Footprint(crater, reachRadii, surface)};
64
1/1
✓ Branch 1 taken 480398 times.
480385 const KernelOutline outline(crater.outline, c.diameter);
65 480398 const double reach = reachRadii * 0.5 * c.diameter;
66
2/2
✓ Branch 0 taken 42998439 times.
✓ Branch 1 taken 334249 times.
43332688 for (std::size_t i = 0; i < samples.Size(); ++i) {
67
1/1
✓ Branch 1 taken 42997710 times.
42998439 const Vec2d p = samples.Position(i);
68
4/4
✓ Branch 0 taken 535086 times.
✓ Branch 1 taken 42462624 times.
✓ Branch 2 taken 145989 times.
✓ Branch 3 taken 389666 times.
43533365 if (surface.Contains(candidate.footprint, p) &&
69
2/2
✓ Branch 1 taken 534733 times.
✓ Branch 4 taken 535655 times.
43387376 outline.ProfileDistance(surface.Offset({c.x, c.y}, p)) < reach) {
70
1/1
✓ Branch 1 taken 145981 times.
145989 reaching.push_back(candidate);
71 145981 break;
72 }
73 }
74 480142 }
75 77045 return reaching;
76 }
77
78 /// The frames (`KernelFrameOffset`) of craters `indices` of `prepared` on a body; a placeholder
79 /// on a plane.
80 77430 std::vector<CraterFrame> MakeCraterFrames(const std::vector<PreparedCrater>& prepared,
81 const std::vector<std::size_t>& indices,
82 const Surface& surface) {
83
4/4
✓ Branch 0 taken 4929 times.
✓ Branch 1 taken 72501 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 4919 times.
77430 if (!surface.IsBody() || indices.empty()) {
84 72511 return {CraterFrame{}};
85 }
86 4919 std::vector<CraterFrame> frames(indices.size());
87
2/2
✓ Branch 0 taken 78919 times.
✓ Branch 1 taken 4919 times.
83838 for (std::size_t n = 0; n < indices.size(); ++n) {
88
1/1
✓ Branch 1 taken 78919 times.
78919 frames[n] = MakeCraterFrame(surface, prepared[indices[n]].crater);
89 }
90 4919 return frames;
91 4919 }
92
93 /// The height after craters `entries[first]` to `entries[first + count - 1]` at surface point
94 /// `p`, as the GPU shapes it.
95 6211553 float ShapeAt(const CraterShapeKernel& kernel, const CraterKernelBatch& batch,
96 const std::vector<CraterFrame>& frames, const std::uint32_t* entries,
97 std::uint32_t first, std::uint32_t count, float height, const Vec2d& p,
98 const Surface& surface) {
99 6211553 const SplitPoint at = MakeSplitPoint(surface, p);
100
2/2
✓ Branch 0 taken 1660487 times.
✓ Branch 1 taken 4551940 times.
6212427 if (surface.IsBody()) {
101 1660487 return ShapeCratersOnBody(kernel, batch.Data(), batch.records.data(), frames.data(),
102 1659548 entries, first, count, height, at);
103 }
104 4551940 return ShapeCraters(kernel, batch.Data(), batch.records.data(), entries, first, count, height,
105 4554009 at);
106 }
107
108 /// Side of the tiles craters are binned into, in cells.
109 constexpr std::size_t kTileCells = 16;
110
111 /// Each tile's craters as `CraterTileHead` reads them: tiles across, side and 1 on a body, then
112 /// each tile's first entry and count; and the entries.
113 481 struct CraterTiles {
114 std::vector<std::uint32_t> heads;
115 std::vector<std::uint32_t> entries;
116 };
117
118 /// The tiles of `kTileCells` of `raster` each crater's `windows` cover, craters in order.
119 481 CraterTiles TileLists(const Raster<float>& raster,
120 const std::vector<std::vector<PixelWindow>>& windows, bool onBody) {
121 481 const std::size_t tilesX = CeilDiv(raster.Width(), kTileCells);
122 481 const std::size_t tilesY = CeilDiv(raster.Height(), kTileCells);
123 481 std::vector<std::vector<std::uint32_t>> lists(tilesX * tilesY);
124
2/2
✓ Branch 0 taken 22591 times.
✓ Branch 1 taken 481 times.
23072 for (std::size_t n = 0; n < windows.size(); ++n) {
125
2/2
✓ Branch 0 taken 22591 times.
✓ Branch 1 taken 22591 times.
45182 for (const PixelWindow& window : windows[n]) {
126 22591 ForEachTileIn(window, kTileCells, kTileCells,
127
1/1
✓ Branch 1 taken 22591 times.
22591 [&](std::size_t tx, std::size_t ty, const PixelWindow&) {
128
2/2
✓ Branch 0 taken 272624 times.
✓ Branch 1 taken 29325 times.
301949 std::vector<std::uint32_t>& list = lists[ty * tilesX + tx];
129
3/4
✓ Branch 0 taken 272624 times.
✓ Branch 1 taken 29325 times.
✓ Branch 2 taken 272624 times.
✗ Branch 3 not taken.
301949 if (list.empty() || list.back() != n) {
130 301949 list.push_back(static_cast<std::uint32_t>(n));
131 }
132 301949 });
133 }
134 }
135
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 91 times.
481 CraterTiles tiles;
136 1443 tiles.heads = {static_cast<std::uint32_t>(tilesX), static_cast<std::uint32_t>(kTileCells),
137
3/3
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 91 times.
✓ Branch 3 taken 481 times.
871 onBody ? 1u : 0u};
138
1/1
✓ Branch 1 taken 481 times.
481 tiles.entries = FlattenKernelLists(lists, tiles.heads);
139 481 return tiles;
140 481 }
141
142 /// What the GPU reads of each cell's position beyond `RunKernel`'s float columns and rows: on a
143 /// plane the remainders of the columns' x then the rows' y, on a body each cell's split position.
144 43 struct CellPositions {
145 std::vector<float> lows{0.0f};
146 std::vector<SplitPoint> points{SplitPoint{}};
147 };
148
149 43 CellPositions MakeCellPositions(const Raster<float>& raster, const Surface& surface,
150 unsigned threads) {
151 43 CellPositions cells;
152
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 22 times.
43 if (surface.IsBody()) {
153
1/1
✓ Branch 1 taken 21 times.
21 cells.points.resize(raster.Size());
154
2/2
✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
21 ParallelFor(raster.Height(), threads, [&](std::size_t row) {
155
2/2
✓ Branch 0 taken 700416 times.
✓ Branch 1 taken 3456 times.
703872 for (std::size_t col = 0; col < raster.Width(); ++col) {
156 700416 cells.points[row * raster.Width() + col] =
157 700416 MakeSplitPoint(surface, raster.CellCenter(col, row));
158 }
159 3456 });
160 21 return cells;
161 }
162 22 cells.lows.clear();
163
2/2
✓ Branch 1 taken 3639 times.
✓ Branch 2 taken 22 times.
3661 for (std::size_t col = 0; col < raster.Width(); ++col) {
164
1/1
✓ Branch 1 taken 3639 times.
7278 cells.lows.push_back(MakeSplitPoint(surface, raster.CellCenter(col, 0)).xLow);
165 }
166
2/2
✓ Branch 0 taken 3463 times.
✓ Branch 1 taken 22 times.
3485 for (std::size_t row = 0; row < raster.Height(); ++row) {
167
1/1
✓ Branch 1 taken 3463 times.
6926 cells.lows.push_back(MakeSplitPoint(surface, raster.CellCenter(0, row)).yLow);
168 }
169 return cells;
170 }
171
172 /// Shapes `raster` on GPU `backend`; false, leaving it unchanged, when the backend is the CPU or
173 /// cannot take the grid (`RunKernel`).
174 481 bool ShapeTilesOnGpu(Raster<float>& raster, Backend backend, unsigned threads,
175 const CraterShapeKernel& kernel, const CraterKernelBatch& batch,
176 const std::vector<CraterFrame>& frames, const CraterTiles& tiles,
177 const Surface& surface) {
178
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 438 times.
481 if (backend == Backend::Cpu) {
179 return false;
180 }
181 43 const CellPositions cells = MakeCellPositions(raster, surface, threads);
182 43 const std::array<KernelBuffer, 7> extra{
183 43 MakeKernelBuffer(batch.records),
184 43 MakeKernelBuffer(batch.data),
185 43 MakeKernelBuffer(tiles.heads),
186 43 MakeKernelBuffer(tiles.entries),
187
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 21 times.
43 MakeKernelBuffer(frames),
188
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 21 times.
65 MakeKernelBuffer(cells.points, surface.IsBody() ? std::size_t{1} : std::size_t{0}),
189 43 MakeKernelBuffer(cells.lows)};
190
2/2
✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
43 if (!RunKernel(backend, CompiledKernel<CraterShapeKernel>(), AsBytes(Span(&kernel, 1)), raster,
191
2/3
✓ Branch 1 taken 43 times.
✓ Branch 3 taken 43 times.
✗ Branch 4 not taken.
86 Vec2d{}, extra)) {
192 return false;
193 }
194
2/2
✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
43 LogDebug("craters", "Shaped ", batch.records.size(), " craters on ", BackendName(backend));
195 43 return true;
196 43 }
197
198 /// Shapes `raster` on `threads` threads, each tile of cells running its craters as the GPU does.
199 438 void ShapeTilesOnCpu(Raster<float>& raster, unsigned threads, const CraterShapeKernel& kernel,
200 const CraterKernelBatch& batch, const std::vector<CraterFrame>& frames,
201 const CraterTiles& tiles, const Surface& surface) {
202 438 ForEachTile(
203 438 raster.Width(), raster.Height(), kTileCells, threads, [&](const PixelWindow& cells) {
204 31274 const std::uint32_t head =
205 31274 CraterTileHead(tiles.heads.data(), static_cast<std::uint32_t>(cells.colBegin),
206
2/2
✓ Branch 0 taken 23522 times.
✓ Branch 1 taken 7752 times.
31274 static_cast<std::uint32_t>(cells.rowBegin));
207
2/2
✓ Branch 0 taken 23522 times.
✓ Branch 1 taken 7752 times.
31274 const std::uint32_t first = tiles.heads[head];
208 31274 const std::uint32_t count = tiles.heads[head + 1];
209
2/2
✓ Branch 0 taken 23522 times.
✓ Branch 1 taken 7752 times.
31274 if (count == 0) {
210 return;
211 }
212
2/2
✓ Branch 0 taken 356191 times.
✓ Branch 1 taken 23524 times.
379715 for (std::size_t row = cells.rowBegin; row < cells.rowEnd; ++row) {
213
2/2
✓ Branch 0 taken 5361726 times.
✓ Branch 1 taken 356193 times.
5717919 for (std::size_t col = cells.colBegin; col < cells.colEnd; ++col) {
214 5361726 float& cell = raster(col, row);
215
4/4
✓ Branch 0 taken 1984 times.
✓ Branch 1 taken 5356344 times.
✓ Branch 2 taken 1982 times.
✓ Branch 3 taken 2 times.
5360312 if (!raster.IsNoData(cell)) {
216 10720052 cell = ShapeAt(kernel, batch, frames, tiles.entries.data(), first, count,
217 5358326 cell, raster.CellCenter(col, row), surface);
218 }
219 }
220 }
221 });
222 438 }
223
224 /// The craters of `prepared` whose footprints reach `raster`, with the windows of it they cover.
225 486 std::vector<std::size_t> CratersOnRaster(const Raster<float>& raster,
226 const std::vector<PreparedCrater>& prepared,
227 double reachRadii, const Surface& surface,
228 std::vector<std::vector<PixelWindow>>& windows) {
229 486 const RasterGrid grid = raster.Grid();
230 486 std::vector<std::size_t> onRaster;
231
2/2
✓ Branch 0 taken 28886 times.
✓ Branch 1 taken 486 times.
29372 for (std::size_t k = 0; k < prepared.size(); ++k) {
232
1/1
✓ Branch 1 taken 28886 times.
28886 const Bounds2d box = Footprint(prepared[k], reachRadii, surface);
233
1/1
✓ Branch 1 taken 28886 times.
28886 std::vector<PixelWindow> around = PixelWindowsIn(grid, box, surface);
234
2/2
✓ Branch 0 taken 22591 times.
✓ Branch 1 taken 6295 times.
28886 if (!around.empty()) {
235
1/1
✓ Branch 1 taken 22591 times.
22591 onRaster.push_back(k);
236
1/1
✓ Branch 1 taken 22591 times.
22591 windows.push_back(std::move(around));
237 }
238 28886 }
239 486 return onRaster;
240 }
241
242 /// Shapes the craters `reaching`, craters `indices` of `prepared` whose records `batch` holds, into
243 /// the points of `samples`, each point running those whose footprint holds it, in order.
244 76913 void ShapePoints(SampleBlock& samples, const std::vector<ReachingCrater>& reaching,
245 const CraterShapeKernel& kernel, const CraterKernelBatch& batch,
246 const std::vector<PreparedCrater>& prepared,
247 const std::vector<std::size_t>& indices, const Surface& surface) {
248 76913 std::vector<std::vector<std::uint32_t>> lists(samples.Size());
249
2/2
✓ Branch 0 taken 1004358 times.
✓ Branch 1 taken 76992 times.
1081350 for (std::size_t i = 0; i < samples.Size(); ++i) {
250
1/1
✓ Branch 1 taken 1004301 times.
1004358 const Vec2d p = samples.Position(i);
251
2/2
✓ Branch 0 taken 12132259 times.
✓ Branch 1 taken 1004398 times.
13136657 for (std::size_t j = 0; j < reaching.size(); ++j) {
252
2/2
✓ Branch 0 taken 1958716 times.
✓ Branch 1 taken 10173543 times.
12132259 if (surface.Contains(reaching[j].footprint, p)) {
253
1/1
✓ Branch 1 taken 1958813 times.
1958716 lists[i].push_back(static_cast<std::uint32_t>(j));
254 }
255 }
256 }
257
1/1
✓ Branch 1 taken 76964 times.
76992 std::vector<std::uint32_t> heads;
258
1/1
✓ Branch 1 taken 76964 times.
76992 const std::vector<std::uint32_t> entries = FlattenKernelLists(lists, heads);
259
1/1
✓ Branch 1 taken 76956 times.
76964 const std::vector<CraterFrame> frames = MakeCraterFrames(prepared, indices, surface);
260
2/2
✓ Branch 0 taken 1007405 times.
✓ Branch 1 taken 76965 times.
1084370 for (std::size_t i = 0; i < samples.Size(); ++i) {
261
4/5
✓ Branch 1 taken 1007149 times.
✓ Branch 3 taken 1007360 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 854127 times.
✓ Branch 6 taken 153233 times.
1007405 if (!samples.IsNoData(i) && heads[2 * i + 1] > 0) {
262 1708519 samples.Height(i) =
263 854356 ShapeAt(kernel, batch, frames, entries.data(), heads[2 * i], heads[2 * i + 1],
264
2/2
✓ Branch 1 taken 853812 times.
✓ Branch 4 taken 854356 times.
2562328 samples.Height(i), samples.Position(i), surface);
265 }
266 }
267 76965 }
268
269 /// The prepared craters point queries reuse: those of `sampling`'s owner kept for its cache, when
270 /// it has both.
271 77055 PreparedReuse PointReuse(const CraterSampling& sampling) {
272
3/4
✓ Branch 0 taken 73348 times.
✓ Branch 1 taken 3707 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 73348 times.
77055 if (sampling.cache == nullptr || sampling.owner == nullptr) {
273 3707 return {};
274 }
275 73348 return {sampling.cache->Id(), sampling.owner};
276 }
277
278 /// Shapes the craters reaching `samples` at their points, with datums fitted, or with `cached`
279 /// taken from `sampling.cache`; false, leaving the samples as they are, when a datum is not held.
280 77036 bool ApplyAtPoints(SampleBlock& samples, const CraterList& craters, const CraterShapeModels& models,
281 const CraterApplyOptions& options, const CraterSampling& sampling, bool cached) {
282 77036 const CraterProfile& profile = *models.profile;
283 77036 std::vector<PreparedCrater> prepared =
284 77036 PrepareCraters(craters, options, sampling.spacing, models, sampling.threads,
285 77036 sampling.reshape, PointReuse(sampling));
286 77027 const std::vector<ReachingCrater> reaching =
287
2/2
✓ Branch 1 taken 76926 times.
✓ Branch 4 taken 77015 times.
77027 CratersReaching(samples, prepared, CraterReachRadii(profile, options), options.surface);
288
1/1
✓ Branch 1 taken 77052 times.
77015 const CraterShapeKernel kernel = MakeCraterShapeKernel(profile, options);
289 77052 std::vector<std::size_t> indices;
290
2/2
✓ Branch 0 taken 145980 times.
✓ Branch 1 taken 77067 times.
223047 for (const ReachingCrater& crater : reaching) {
291
1/1
✓ Branch 1 taken 145995 times.
145980 indices.push_back(static_cast<std::size_t>(crater.prepared - prepared.data()));
292 }
293
1/1
✓ Branch 1 taken 76981 times.
77067 const ReplacingSplit split = SplitReplacing(prepared, indices);
294
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 77002 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
76981 if (!split.replacing.empty() && !sampling.ground) {
295
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Replacing craters at points need the ground");
296 }
297
2/2
✓ Branch 0 taken 1279 times.
✓ Branch 1 taken 75702 times.
76981 if (cached) {
298
3/3
✓ Branch 1 taken 1279 times.
✓ Branch 3 taken 1165 times.
✓ Branch 4 taken 114 times.
1279 if (!UseCachedDatums(prepared, indices, models, options, sampling)) {
299 return false;
300 }
301 } else {
302
4/4
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 73932 times.
✓ Branch 2 taken 464 times.
✓ Branch 3 taken 1306 times.
75702 if (options.datum != CraterDatum::None && !split.others.empty()) {
303
1/1
✓ Branch 1 taken 464 times.
464 FitPointDatums(prepared, split.others, models, kernel, options, sampling);
304 }
305
1/1
✓ Branch 1 taken 75779 times.
75702 FitReplacementDatums(prepared, split.replacing, nullptr, models, kernel, options, sampling);
306 }
307 76944 const CraterKernelBatch batch =
308
1/1
✓ Branch 1 taken 76973 times.
76944 MakeCraterKernelBatch(kernel, profile, prepared, indices, options);
309
1/1
✓ Branch 1 taken 76933 times.
76973 ShapePoints(samples, reaching, kernel, batch, prepared, indices, options.surface);
310 76933 return true;
311 153913 }
312
313 } // namespace
314
315 159739 CraterSampling SamplingFrom(const LayerContext& context, double spacing, const void* owner) {
316
2/2
✓ Branch 0 taken 134530 times.
✓ Branch 1 taken 25209 times.
159739 CraterSampling sampling{spacing, context.threads};
317
2/2
✓ Branch 0 taken 134530 times.
✓ Branch 1 taken 25209 times.
159739 if (context.ground != nullptr) {
318 134530 const Ground* ground = context.ground;
319 273477 sampling.ground = [ground, spacing](double x, double y) {
320 3827 return ground->HeightAt(x, y, spacing);
321 134530 };
322 135116 sampling.groundBatch = [ground, spacing, threads = context.threads](
323 Span<const Vec2d> points, Span<float> heights) {
324 442 ground->HeightsAt(points, heights, spacing, threads);
325
1/1
✓ Branch 1 taken 135116 times.
135120 };
326 }
327 160325 sampling.cache = context.cache;
328 160325 sampling.owner = owner;
329 160325 sampling.backend = context.backend;
330 160325 return sampling;
331 }
332
333 318333 double CraterReachRadii(const CraterProfile& profile, const CraterApplyOptions& options) {
334 318333 const double reach = profile.ReachRadii();
335
2/2
✓ Branch 0 taken 293855 times.
✓ Branch 1 taken 24584 times.
318302 return options.texture.ReachesBeyondRim() ? std::fmax(reach, options.texture.ejectaReachRadii)
336 318439 : reach;
337 }
338
339 501 void ApplyCraters(Raster<float>& raster, const CraterList& craters, const CraterShapeModels& models,
340 const CraterApplyOptions& options, const CraterSampling& sampling) {
341 501 ValidateCraterApplyOptions(options);
342 493 RequireNonNegative(sampling.spacing, "Sample spacing");
343
3/4
✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 488 times.
492 if (raster.Empty() || craters.Empty()) {
344 4 return;
345 }
346 488 const Stopwatch stopwatch;
347 488 const double resolved = ResolvedSpacing(raster.Grid(), sampling.spacing, options.surface);
348 487 const CraterProfile& profile = *models.profile;
349 487 std::vector<PreparedCrater> prepared =
350 487 PrepareCraters(craters, options, resolved, models, sampling.threads, sampling.reshape);
351
1/1
✓ Branch 1 taken 486 times.
486 std::vector<std::vector<PixelWindow>> windows;
352 486 const std::vector<std::size_t> onRaster = CratersOnRaster(
353
2/2
✓ Branch 1 taken 486 times.
✓ Branch 4 taken 486 times.
486 raster, prepared, CraterReachRadii(profile, options), options.surface, windows);
354
1/1
✓ Branch 1 taken 486 times.
486 const CraterShapeKernel kernel = MakeCraterShapeKernel(profile, options);
355
1/1
✓ Branch 1 taken 486 times.
486 const ReplacingSplit split = SplitReplacing(prepared, onRaster);
356
4/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 374 times.
✓ Branch 2 taken 111 times.
✓ Branch 3 taken 1 time.
486 if (options.datum != CraterDatum::None && !split.others.empty()) {
357
1/1
✓ Branch 1 taken 111 times.
111 FitGridDatums(prepared, split.others, raster, models, kernel, options, sampling);
358 }
359
1/1
✓ Branch 1 taken 486 times.
486 FitReplacementDatums(prepared, split.replacing, &raster, models, kernel, options, sampling);
360
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 5 times.
486 if (!onRaster.empty()) {
361 481 const CraterKernelBatch batch =
362
1/1
✓ Branch 1 taken 481 times.
481 MakeCraterKernelBatch(kernel, profile, prepared, onRaster, options);
363 481 const std::vector<CraterFrame> frames =
364
1/1
✓ Branch 1 taken 481 times.
481 MakeCraterFrames(prepared, onRaster, options.surface);
365
1/1
✓ Branch 1 taken 481 times.
481 const CraterTiles tiles = TileLists(raster, windows, options.surface.IsBody());
366
3/3
✓ Branch 1 taken 481 times.
✓ Branch 3 taken 438 times.
✓ Branch 4 taken 43 times.
481 if (!ShapeTilesOnGpu(raster, sampling.backend, sampling.threads, kernel, batch, frames,
367 tiles, options.surface)) {
368
1/1
✓ Branch 1 taken 438 times.
438 ShapeTilesOnCpu(raster, sampling.threads, kernel, batch, frames, tiles,
369 options.surface);
370 }
371 962 }
372 1458 LogInfo("craters", "Applied ", onRaster.size(), " of ", craters.Size(), " craters (",
373
1/1
✓ Branch 1 taken 486 times.
486 craters.Size() - prepared.size(), " unresolved at ", resolved, " m, ",
374
1/1
✓ Branch 1 taken 486 times.
486 prepared.size() - onRaster.size(), " off the raster) in ",
375
1/1
✓ Branch 2 taken 486 times.
972 FormatDuration(stopwatch.Milliseconds()));
376 486 }
377
378 156190 void ApplyCraters(SampleBlock& samples, const CraterList& craters, const CraterShapeModels& models,
379 const CraterApplyOptions& options, const CraterSampling& sampling) {
380
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 155812 times.
156190 if (Raster<float>* grid = samples.Grid()) {
381 378 ApplyCraters(*grid, craters, models, options, sampling);
382 378 return;
383 }
384 155812 ValidateCraterApplyOptions(options);
385 155367 RequirePositive(sampling.spacing, "Sample spacing");
386
3/4
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 154060 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1770 times.
155830 if (options.datum != CraterDatum::None && !sampling.ground) {
387
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Craters with a datum at points need the ground");
388 }
389
4/4
✓ Branch 0 taken 134472 times.
✓ Branch 1 taken 21358 times.
✓ Branch 2 taken 75762 times.
✓ Branch 3 taken 58710 times.
155830 if (samples.Size() == 0 || craters.Empty()) {
390 return;
391 }
392 75762 ApplyAtPoints(samples, craters, models, options, sampling, false);
393 }
394
395 4130 bool ApplyCratersOnCachedDatums(SampleBlock& samples, const CraterList& craters,
396 const CraterShapeModels& models, const CraterApplyOptions& options,
397 const CraterSampling& sampling) {
398
2/4
✓ Branch 0 taken 4130 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4130 times.
✗ Branch 3 not taken.
4130 if (samples.Grid() != nullptr || options.datum == CraterDatum::None ||
399
3/6
✓ Branch 0 taken 4130 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4130 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4130 times.
✗ Branch 5 not taken.
8260 sampling.cache == nullptr || !sampling.ground) {
400 return false;
401 }
402 4130 ValidateCraterApplyOptions(options);
403 4130 RequirePositive(sampling.spacing, "Sample spacing");
404
4/4
✓ Branch 0 taken 4124 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1279 times.
✓ Branch 3 taken 2845 times.
4130 if (samples.Size() == 0 || craters.Empty()) {
405 return true;
406 }
407 1279 return ApplyAtPoints(samples, craters, models, options, sampling, true);
408 }
409
410 } // namespace terraingen
411