include/terraingen/layers/craters/shaping/apply.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Applying a list of craters to heights, over a raster or at scattered points. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/gpu/backend.h" | ||
| 8 | #include "terraingen/layers/craters/degradation/degradation_model.h" | ||
| 9 | #include "terraingen/layers/craters/generation/population.h" | ||
| 10 | #include "terraingen/layers/craters/profiles/profile.h" | ||
| 11 | #include "terraingen/layers/craters/shaping/apply_options.h" | ||
| 12 | #include "terraingen/layers/craters/shaping/attributes.h" | ||
| 13 | #include "terraingen/layers/craters/shaping/datum.h" | ||
| 14 | #include "terraingen/layers/craters/shaping/prepared.h" | ||
| 15 | #include "terraingen/layers/evaluation_cache.h" | ||
| 16 | #include "terraingen/layers/ground.h" | ||
| 17 | #include "terraingen/layers/layer.h" | ||
| 18 | #include "terraingen/raster/raster.h" | ||
| 19 | #include "terraingen/sources/sample_block.h" | ||
| 20 | |||
| 21 | #include <functional> | ||
| 22 | #include <memory> | ||
| 23 | |||
| 24 | namespace terraingen { | ||
| 25 | |||
| 26 | /// The models craters are shaped with: their profile and the degradation state it is drawn in. | ||
| 27 | 56 | struct CraterShapeModels { | |
| 28 | std::shared_ptr<const CraterProfile> profile; ///< Height by distance from the center. | ||
| 29 | std::shared_ptr<const DegradationModel> degradation; ///< Each crater's state. | ||
| 30 | |||
| 31 | /// A crater's depth, rim height and freshness in its `ResolvedCraterState`. | ||
| 32 | /// @throws std::domain_error as `DegradationModel::State`. | ||
| 33 | /// @throws std::invalid_argument as `ResolvedCraterState`. | ||
| 34 | CraterAttributes Attributes(const Crater& crater) const; | ||
| 35 | }; | ||
| 36 | |||
| 37 | /// How `ApplyCraters` samples a raster or points. | ||
| 38 | struct CraterSampling { | ||
| 39 | /// Sample spacing in world units; 0 or anything finer means the raster's cell size. | ||
| 40 | double spacing = 0.0; | ||
| 41 | unsigned threads = 1; ///< Worker threads, 0 for all; the result does not depend on it. | ||
| 42 | /// The ground datums are fitted to; empty for the raster as passed in, interpolated | ||
| 43 | /// bilinearly. | ||
| 44 | HeightSampler ground{}; | ||
| 45 | /// `ground` at many points in one pass, to the same values; empty reads them one at a time. | ||
| 46 | HeightsSampler groundBatch{}; | ||
| 47 | /// Changes each crater's outline after its irregularity, such as secondaries stretched toward | ||
| 48 | /// their primary; empty leaves outlines as they are. Must be deterministic and thread-safe. | ||
| 49 | std::function<void(const Crater&, CraterOutline&)> reshape{}; | ||
| 50 | /// Where point queries keep the datums they fit, shared across batches; null to fit each time. | ||
| 51 | EvaluationCache* cache = nullptr; | ||
| 52 | const void* owner = nullptr; ///< The layer the cached datums belong to. | ||
| 53 | /// Where grids are shaped: the CPU, or a GPU backend when it can take them, to within float | ||
| 54 | /// rounding of the CPU; points always run on the CPU. | ||
| 55 | Backend backend = Backend::Cpu; | ||
| 56 | }; | ||
| 57 | |||
| 58 | /// The sampling a crater layer evaluated in `context` applies its craters with: `spacing`, and the | ||
| 59 | /// context's threads, ground, cache and backend, with cached datums kept under `owner`. | ||
| 60 | CraterSampling SamplingFrom(const LayerContext& context, double spacing, const void* owner); | ||
| 61 | |||
| 62 | /// How far, in crater radii, a crater changes the terrain: the profile's reach, or the ejecta's | ||
| 63 | /// when `options.texture` draws beyond the rim and they reach further. | ||
| 64 | double CraterReachRadii(const CraterProfile& profile, const CraterApplyOptions& options); | ||
| 65 | |||
| 66 | /// Applies craters to a raster in the order `options` gives, each weighted by `ResolutionWeight`; | ||
| 67 | /// no-data cells are left unchanged. | ||
| 68 | /// @param craters With a datum, every crater within `DatumMargin` (or `ReplacementMargin`). | ||
| 69 | /// @param models Neither may be null. | ||
| 70 | /// @throws std::invalid_argument for invalid `options` or a negative or non-finite spacing. | ||
| 71 | /// @throws std::domain_error if the raster's transform is not invertible. | ||
| 72 | void ApplyCraters(Raster<float>& raster, const CraterList& craters, const CraterShapeModels& models, | ||
| 73 | const CraterApplyOptions& options = {}, const CraterSampling& sampling = {}); | ||
| 74 | |||
| 75 | /// `ApplyCraters` on a sample block: a grid as above, or scattered points, each of which ends at | ||
| 76 | /// the same height as a grid cell centered there. | ||
| 77 | /// @throws std::invalid_argument as above; for points also without a positive spacing, or with a | ||
| 78 | /// datum or a replacing crater reaching them and no `sampling.ground`. | ||
| 79 | void ApplyCraters(SampleBlock& samples, const CraterList& craters, const CraterShapeModels& models, | ||
| 80 | const CraterApplyOptions& options = {}, const CraterSampling& sampling = {}); | ||
| 81 | |||
| 82 | /// `ApplyCraters` at points with a datum, taking every datum from `sampling.cache` so `craters` | ||
| 83 | /// need only reach the points, not the craters beneath their datums; false, leaving the samples as | ||
| 84 | /// they are, for a grid, without a datum, cache or ground, or when a datum is not held yet. | ||
| 85 | /// @throws std::invalid_argument as `ApplyCraters`. | ||
| 86 | bool ApplyCratersOnCachedDatums(SampleBlock& samples, const CraterList& craters, | ||
| 87 | const CraterShapeModels& models, const CraterApplyOptions& options, | ||
| 88 | const CraterSampling& sampling); | ||
| 89 | |||
| 90 | } // namespace terraingen | ||
| 91 |