./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
32 / 32
100.0%
Branches:
34 / 39
87.2%
Functions:
4 / 4, 3 excl
100.0%

include/terraingen/layers/craters/crater_layer.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `CraterLayer`, craters generated over the grid being evaluated and applied to it.
5 #pragma once
6
7 #include "terraingen/distribution/size_frequency.h"
8 #include "terraingen/layers/craters/ages/age_model.h"
9 #include "terraingen/layers/craters/crater_placer.h"
10 #include "terraingen/layers/craters/degradation/degradation_model.h"
11 #include "terraingen/layers/craters/generation/population.h"
12 #include "terraingen/layers/craters/profiles/profile.h"
13 #include "terraingen/layers/craters/secondary_model.h"
14 #include "terraingen/layers/craters/shaping/apply.h"
15 #include "terraingen/layers/craters/shaping/attributes.h"
16 #include "terraingen/layers/density_map.h"
17 #include "terraingen/layers/layer.h"
18 #include "terraingen/layers/terrain_base.h"
19
20 #include <cstdint>
21 #include <limits>
22 #include <memory>
23 #include <optional>
24 #include <vector>
25
26 namespace terraingen {
27
28 class CraterCellCache;
29
30 /// The models a crater layer generates and shapes craters with.
31 struct CraterModels {
32 /// Craters per m^2 by diameter; may be null for a layer that generates none.
33 std::shared_ptr<const SizeFrequencyDistribution> density;
34 /// Each crater's age; null takes the degradation's `AgeModel`, so ages are given once, and may
35 /// stay null for a layer that generates no craters.
36 std::shared_ptr<const CraterAgeModel> ages;
37 std::shared_ptr<const DegradationModel> degradation; ///< Each crater's state.
38 /// Height by distance; null takes the one the degradation requires, else Martin, Parkes and
39 /// Dunstan's (`MatchedProfile`).
40 std::shared_ptr<const CraterProfile> profile;
41
42 /// The profile and degradation, the models that shape the craters.
43 171759 CraterShapeModels Shape() const {
44 515985 return {profile, degradation};
45
2/4
✓ Branch 0 taken 171759 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 171976 times.
✗ Branch 3 not taken.
343735 }
46 };
47
48 /// The crater models a configuration's crater layer has by default: a 3.5 Gyr extended Neukum
49 /// production surface capped by Hartmann equilibrium (Fassett 2022,
50 /// https://doi.org/10.1029/2022JE007510), steady-state ages on it, Martin-Parkes-Dunstan
51 /// profiles, and diffusion matched to the ages from the lunar fresh morphometry.
52 CraterModels DefaultCraterModels();
53
54 /// Craters a coarser level or a DEM catalog holds that the layer's source softens, stamped again
55 /// sharp in place of what the source holds; generated craters they stand in for are dropped.
56
6/7
<unknown function 56>:
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 15 taken 1 time.
✗ Branch 16 not taken.
✓ Branch 22 taken 2 times.
terraingen::CraterModels::Shape() const:
✓ Branch 5 taken 2 times.
2143 struct InheritedCraters {
57 CraterList craters; ///< The coarser level's craters; empty for none.
58 /// Source cells across from which an inherited crater is stamped again and generated craters
59 /// are dropped; unset follows `CraterLayerOptions::sourceResolvesPixels`, and with both unset
60 /// every inherited crater is stamped. Lower than that to clean up the smaller craters a
61 /// coarser run of the same layer drew blurred into the source.
62 std::optional<double> minSourcePixels{};
63 /// Source cells across from which inherited craters are left as the source holds them, since
64 /// interpolation barely softens them; unset stamps every one from `minSourcePixels`.
65 std::optional<double> maxSourcePixels{};
66 };
67
68 /// Checks inherited crater settings.
69 /// @throws std::invalid_argument for a field outside its range (`CheckRanges`) or a
70 /// `maxSourcePixels` not above `minSourcePixels`.
71 void ValidateInheritedCraters(const InheritedCraters& inherited);
72
73 /// How a `CraterLayer` generates and applies its craters.
74 struct CraterLayerOptions {
75 /// Draws random craters from the density; off places only the manual craters in
76 /// `generation`, which are placed either way.
77 bool generateRandom = true;
78 std::uint32_t seed = 1; ///< Varies the generated craters.
79 /// Diameter range, spacing, manual craters and cap; `marginRadii` is set from the profile's
80 /// reach. By default 2.5 m, where the extended Neukum production function begins, to 250 m.
81 270 CraterGenerationOptions generation = [] {
82
10/10
<unknown function 56>:
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 16 times.
✓ Branch 10 taken 8 times.
<unknown function 81>:
✓ Branch 1 taken 14 times.
terraingen::CraterModels::Shape() const:
✓ Branch 1 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
270 CraterGenerationOptions options;
83 270 options.minDiameter = 2.5;
84 270 options.maxDiameter = 250.0;
85
9/9
<unknown function 56>:
✓ Branch 1 taken 17 times.
✓ Branch 4 taken 191 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 16 times.
✓ Branch 10 taken 8 times.
<unknown function 81>:
✓ Branch 1 taken 14 times.
terraingen::CraterModels::Shape() const:
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 1 time.
270 return options;
86 }();
87 /// How the craters are shaped into the terrain; by default `DefaultCraterApply`.
88 CraterApplyOptions apply = DefaultCraterApply();
89 /// Most craters to keep generated cells of (`CraterCellCache`); 0 regenerates cells every time.
90 std::size_t cacheCraters = 4000000;
91 /// A map that sets the density point by point (`DensityMap`); unset keeps the density the
92 /// same everywhere. Manual craters are not thinned by it; with a catalog among them, set
93 /// `generation.completeness` so generated craters do not repeat it.
94 std::optional<DensityMap> densityMap{};
95 /// The secondaries `SecondaryCraterLayer`s throw from these craters. A `total` count or areal
96 /// fraction map counts them too, so craters too small to throw secondaries the map measures
97 /// are drawn at the map less those secondaries' density there; larger craters, which throw
98 /// them, follow the map alone; with spacing they also take precedence over smaller craters, so
99 /// the throwers never depend on what they throw at. Not with `generation.maxCraters`.
100 std::vector<SecondaryCraterOptions> secondaries{};
101 /// Source cells across from which the source's measured data already holds a crater: with a
102 /// source given to the layer, generated craters at least that many of its cells across are
103 /// dropped (`SourceResolvedLimit`), the minimum with `generation.completeness` when both are
104 /// set. Unset, or without a source, keeps every generated crater.
105 std::optional<double> sourceResolvesPixels = 4.0;
106 /// Craters of a coarser level stamped again sharp where the source softened them; none by
107 /// default.
108 InheritedCraters inherited{};
109 };
110
111 /// Craters generated over the grid being evaluated (`GenerateCraters`) and applied to it
112 /// (`ApplyCraters`) at the evaluation's spacing.
113 ///
114 /// Generated craters are the same in a tile as in any larger grid, unless `maxCraters` caps them
115 /// over the whole grid; bands too small to resolve are not generated.
116 class CraterLayer final : public Layer, public CraterPlacer {
117 public:
118 /// Builds the layer.
119 /// @param models The density and ages may be null without `generateRandom` or a density map.
120 /// @param base The surface the craters lie on, and the source.
121 /// @throws std::invalid_argument for a missing model, a rejected profile or invalid options.
122 CraterLayer(CraterModels models = DefaultCraterModels(), CraterLayerOptions options = {},
123 TerrainBase base = {});
124
125 /// A layer of only `craters`, shaped by `models` and `apply`: `generateRandom` off, so no
126 /// random craters, and the density and ages are not used.
127 /// @param craters Flagged manual and given IDs as `AppendManualCraters` does.
128 /// @throws std::invalid_argument as the constructor.
129 static std::shared_ptr<CraterLayer> Manual(CraterList craters,
130 CraterModels models = DefaultCraterModels(),
131 CraterApplyOptions apply = DefaultCraterApply(),
132 TerrainBase base = {});
133
134 /// `maxDiameter` (the density's largest valid diameter when unbounded) or the largest manual
135 /// crater, in meters.
136 double MaxFeatureDiameter() const;
137
138 /// The craters this layer applies to a grid covering `region` (surface coordinates).
139 /// @param spacing Sample spacing in meters; 0 keeps every band.
140 /// @param minDiameter,maxDiameter Diameters (m) wanted; craters outside them may still come.
141 /// @param threads Threads the cells are drawn on; the craters do not depend on it.
142 CraterList Craters(const Bounds2d& region, double spacing = 0.0, double minDiameter = 0.0,
143 double maxDiameter = std::numeric_limits<double>::infinity(),
144 unsigned threads = 1) const;
145
146 CraterFeatures CratersOver(const RasterGrid& grid, double spacing) const override;
147
148 /// The models the layer was made with.
149 62211 const CraterModels& Models() const {
150
1/1
✓ Branch 1 taken 49172 times.
62207 return m_models;
151 }
152
153 /// The options, with surfaces and `marginRadii` filled in.
154 48 const CraterLayerOptions& Options() const {
155
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 40 times.
48 return m_options;
156 }
157
158 663 std::optional<TerrainBase> LayerBase() const override {
159
4/6
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 504 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 663 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 663 times.
822 return TerrainBase{m_surface, m_source};
160 }
161
162 /// How far the largest crater's datum fit (`DatumMargin`) or replacement ring
163 /// (`ReplacementMargin`) reaches beyond the samples it changes.
164 double GroundReach(double spacing) const override;
165
166 /// The layer with its seed `ReseedWord(seed, 0)` and the apply seeds of the irregularity,
167 /// noise, texture, slope, terraces and peaks `ReseedWord(seed, 1)` to `ReseedWord(seed, 6)`,
168 /// sharing its models and craters but not its cached cells.
169 LayerPtr Reseeded(std::uint64_t seed) const override;
170
171 /// The layer built again from its models and options on `base`.
172 /// @throws std::invalid_argument as the constructor.
173 LayerPtr Rebased(const TerrainBase& base) const override;
174
175 protected:
176 /// Generates and applies the craters reaching the samples. Datums are fitted to
177 /// `context.ground` where given, otherwise to a grid's heights as passed in.
178 /// @throws std::invalid_argument for points when the layer has a datum and no
179 /// `context.ground`, or has `maxCraters` (which ranks craters over a whole grid).
180 /// @throws std::domain_error as `DegradationModel::State`.
181 void ApplySamples(SampleBlock& samples, const LayerContext& context) const override;
182
183 private:
184 /// Drops the generated craters the source resolves, adds the inherited craters it resolves to
185 /// the manual ones, replacing, and drops the generated craters they stand in for.
186 void FollowSource();
187 /// Counts the bands generation draws into `m_bandCounts`.
188 void SetUpBandCounts();
189 /// Draws craters at the density map's largest scale and thins them by the map, taking the
190 /// secondaries out of a `total` map.
191 void FollowDensityMap();
192 /// The smallest primary (m) throwing secondaries the map measures; infinite for none.
193 static double SmallestThrower(const DensityMap& map, const SecondaryCraterOptions& secondaries);
194 /// The craters large enough to throw secondaries that can reach `region`.
195 CraterList Throwers(const Bounds2d& region) const;
196 /// The manual craters centered in `region` or within `marginRadii` of their radii of it.
197 CraterList ManualCratersNear(const Bounds2d& region, double marginRadii) const;
198 /// The keep share for craters over `region`: the map's, less the secondaries thrown there
199 /// by the craters around it that are large enough, when they are taken out of the map.
200 CraterKeepShare KeepShare(const Bounds2d& region) const;
201 /// Sets `generation.bandCount` to read the bands' counts from `m_bandCounts`.
202 void CountBands(CraterGenerationOptions& generation) const;
203
204 /// A band's expected craters per square meter at the density of `m_candidates`.
205 struct BandCount {
206 double low = 0.0;
207 double high = 0.0;
208 double count = 0.0;
209 };
210
211 CraterModels m_models;
212 CraterLayerOptions m_options;
213 /// The options as given, before the source's completeness and inherited craters.
214 std::shared_ptr<const CraterLayerOptions> m_given;
215 Surface m_surface;
216 std::shared_ptr<const Source> m_source;
217 std::shared_ptr<const SurfaceCells> m_cells;
218 std::shared_ptr<CraterCellCache> m_cache;
219 /// The density craters are drawn at: `m_models.density`, times the map's largest scale.
220 std::shared_ptr<const SizeFrequencyDistribution> m_candidates;
221 std::shared_ptr<const DensityScale> m_scale;
222 /// The counts of the bands generation draws, so point queries do not compute them each time.
223 std::vector<BandCount> m_bandCounts;
224 /// Craters at least this large (m) throw secondaries the map measures and follow it alone.
225 double m_throwFrom = std::numeric_limits<double>::infinity();
226 /// How far (m) beyond a grid the replacing manual craters can change it (`ReplacementMargin`).
227 double m_replacementMargin = 0.0;
228 };
229
230 } // namespace terraingen
231