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 | 246148 | for (const std::size_t k : indices) { | |
| 45 | 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 | 557091 | for (PreparedCrater& crater : prepared) { | |
| 62 | 480046 | const Crater& c = crater.crater; | |
| 63 | 480046 | const ReachingCrater candidate{&crater, Footprint(crater, reachRadii, surface)}; | |
| 64 | 480385 | const KernelOutline outline(crater.outline, c.diameter); | |
| 65 | 480398 | const double reach = reachRadii * 0.5 * c.diameter; | |
| 66 | 43332688 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 67 | 42998439 | const Vec2d p = samples.Position(i); | |
| 68 | 43533365 | if (surface.Contains(candidate.footprint, p) && | |
| 69 | 43387376 | outline.ProfileDistance(surface.Offset({c.x, c.y}, p)) < reach) { | |
| 70 | 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 | 77430 | if (!surface.IsBody() || indices.empty()) { | |
| 84 | 72511 | return {CraterFrame{}}; | |
| 85 | } | ||
| 86 | 4919 | std::vector<CraterFrame> frames(indices.size()); | |
| 87 | 83838 | for (std::size_t n = 0; n < indices.size(); ++n) { | |
| 88 | 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 | 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 | 23072 | for (std::size_t n = 0; n < windows.size(); ++n) { | |
| 125 | 45182 | for (const PixelWindow& window : windows[n]) { | |
| 126 | 22591 | ForEachTileIn(window, kTileCells, kTileCells, | |
| 127 | 22591 | [&](std::size_t tx, std::size_t ty, const PixelWindow&) { | |
| 128 | 301949 | std::vector<std::uint32_t>& list = lists[ty * tilesX + tx]; | |
| 129 | 301949 | if (list.empty() || list.back() != n) { | |
| 130 | 301949 | list.push_back(static_cast<std::uint32_t>(n)); | |
| 131 | } | ||
| 132 | 301949 | }); | |
| 133 | } | ||
| 134 | } | ||
| 135 | 481 | CraterTiles tiles; | |
| 136 | 1443 | tiles.heads = {static_cast<std::uint32_t>(tilesX), static_cast<std::uint32_t>(kTileCells), | |
| 137 | 871 | onBody ? 1u : 0u}; | |
| 138 | 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 | 43 | if (surface.IsBody()) { | |
| 153 | 21 | cells.points.resize(raster.Size()); | |
| 154 | 21 | ParallelFor(raster.Height(), threads, [&](std::size_t row) { | |
| 155 | 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 | 3661 | for (std::size_t col = 0; col < raster.Width(); ++col) { | |
| 164 | 7278 | cells.lows.push_back(MakeSplitPoint(surface, raster.CellCenter(col, 0)).xLow); | |
| 165 | } | ||
| 166 | 3485 | for (std::size_t row = 0; row < raster.Height(); ++row) { | |
| 167 | 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 | 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 | 43 | MakeKernelBuffer(frames), | |
| 188 | 65 | MakeKernelBuffer(cells.points, surface.IsBody() ? std::size_t{1} : std::size_t{0}), | |
| 189 | 43 | MakeKernelBuffer(cells.lows)}; | |
| 190 | 43 | if (!RunKernel(backend, CompiledKernel<CraterShapeKernel>(), AsBytes(Span(&kernel, 1)), raster, | |
| 191 | 86 | Vec2d{}, extra)) { | |
| 192 | return false; | ||
| 193 | } | ||
| 194 | 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 | 31274 | static_cast<std::uint32_t>(cells.rowBegin)); | |
| 207 | 31274 | const std::uint32_t first = tiles.heads[head]; | |
| 208 | 31274 | const std::uint32_t count = tiles.heads[head + 1]; | |
| 209 | 31274 | if (count == 0) { | |
| 210 | return; | ||
| 211 | } | ||
| 212 | 379715 | for (std::size_t row = cells.rowBegin; row < cells.rowEnd; ++row) { | |
| 213 | 5717919 | for (std::size_t col = cells.colBegin; col < cells.colEnd; ++col) { | |
| 214 | 5361726 | float& cell = raster(col, row); | |
| 215 | 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 | 29372 | for (std::size_t k = 0; k < prepared.size(); ++k) { | |
| 232 | 28886 | const Bounds2d box = Footprint(prepared[k], reachRadii, surface); | |
| 233 | 28886 | std::vector<PixelWindow> around = PixelWindowsIn(grid, box, surface); | |
| 234 | 28886 | if (!around.empty()) { | |
| 235 | 22591 | onRaster.push_back(k); | |
| 236 | 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 | 1081350 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 250 | 1004358 | const Vec2d p = samples.Position(i); | |
| 251 | 13136657 | for (std::size_t j = 0; j < reaching.size(); ++j) { | |
| 252 | 12132259 | if (surface.Contains(reaching[j].footprint, p)) { | |
| 253 | 1958716 | lists[i].push_back(static_cast<std::uint32_t>(j)); | |
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | 76992 | std::vector<std::uint32_t> heads; | |
| 258 | 76992 | const std::vector<std::uint32_t> entries = FlattenKernelLists(lists, heads); | |
| 259 | 76964 | const std::vector<CraterFrame> frames = MakeCraterFrames(prepared, indices, surface); | |
| 260 | 1084370 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 261 | 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 | 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 | 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 | 77027 | CratersReaching(samples, prepared, CraterReachRadii(profile, options), options.surface); | |
| 288 | 77015 | const CraterShapeKernel kernel = MakeCraterShapeKernel(profile, options); | |
| 289 | 77052 | std::vector<std::size_t> indices; | |
| 290 | 223047 | for (const ReachingCrater& crater : reaching) { | |
| 291 | 145980 | indices.push_back(static_cast<std::size_t>(crater.prepared - prepared.data())); | |
| 292 | } | ||
| 293 | 77067 | const ReplacingSplit split = SplitReplacing(prepared, indices); | |
| 294 | 76981 | if (!split.replacing.empty() && !sampling.ground) { | |
| 295 | ✗ | throw std::invalid_argument("Replacing craters at points need the ground"); | |
| 296 | } | ||
| 297 | 76981 | if (cached) { | |
| 298 | 1279 | if (!UseCachedDatums(prepared, indices, models, options, sampling)) { | |
| 299 | return false; | ||
| 300 | } | ||
| 301 | } else { | ||
| 302 | 75702 | if (options.datum != CraterDatum::None && !split.others.empty()) { | |
| 303 | 464 | FitPointDatums(prepared, split.others, models, kernel, options, sampling); | |
| 304 | } | ||
| 305 | 75702 | FitReplacementDatums(prepared, split.replacing, nullptr, models, kernel, options, sampling); | |
| 306 | } | ||
| 307 | 76944 | const CraterKernelBatch batch = | |
| 308 | 76944 | MakeCraterKernelBatch(kernel, profile, prepared, indices, options); | |
| 309 | 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 | 159739 | CraterSampling sampling{spacing, context.threads}; | |
| 317 | 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 | 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 | 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 | 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 | 486 | std::vector<std::vector<PixelWindow>> windows; | |
| 352 | 486 | const std::vector<std::size_t> onRaster = CratersOnRaster( | |
| 353 | 486 | raster, prepared, CraterReachRadii(profile, options), options.surface, windows); | |
| 354 | 486 | const CraterShapeKernel kernel = MakeCraterShapeKernel(profile, options); | |
| 355 | 486 | const ReplacingSplit split = SplitReplacing(prepared, onRaster); | |
| 356 | 486 | if (options.datum != CraterDatum::None && !split.others.empty()) { | |
| 357 | 111 | FitGridDatums(prepared, split.others, raster, models, kernel, options, sampling); | |
| 358 | } | ||
| 359 | 486 | FitReplacementDatums(prepared, split.replacing, &raster, models, kernel, options, sampling); | |
| 360 | 486 | if (!onRaster.empty()) { | |
| 361 | 481 | const CraterKernelBatch batch = | |
| 362 | 481 | MakeCraterKernelBatch(kernel, profile, prepared, onRaster, options); | |
| 363 | 481 | const std::vector<CraterFrame> frames = | |
| 364 | 481 | MakeCraterFrames(prepared, onRaster, options.surface); | |
| 365 | 481 | const CraterTiles tiles = TileLists(raster, windows, options.surface.IsBody()); | |
| 366 | 481 | if (!ShapeTilesOnGpu(raster, sampling.backend, sampling.threads, kernel, batch, frames, | |
| 367 | tiles, options.surface)) { | ||
| 368 | 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 | 486 | craters.Size() - prepared.size(), " unresolved at ", resolved, " m, ", | |
| 374 | 486 | prepared.size() - onRaster.size(), " off the raster) in ", | |
| 375 | 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 | 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 | 155830 | if (options.datum != CraterDatum::None && !sampling.ground) { | |
| 387 | ✗ | throw std::invalid_argument("Craters with a datum at points need the ground"); | |
| 388 | } | ||
| 389 | 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 | 4130 | if (samples.Grid() != nullptr || options.datum == CraterDatum::None || | |
| 399 | 8260 | sampling.cache == nullptr || !sampling.ground) { | |
| 400 | return false; | ||
| 401 | } | ||
| 402 | 4130 | ValidateCraterApplyOptions(options); | |
| 403 | 4130 | RequirePositive(sampling.spacing, "Sample spacing"); | |
| 404 | 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 |