./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
1 / 1
100.0%
Branches:
1 / 1
100.0%
Functions:
-
-%

include/terraingen/layers/craters/generation/population.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Generating crater populations cell by cell, in size bands.
5 #pragma once
6
7 #include "terraingen/distribution/size_frequency.h"
8 #include "terraingen/geometry/cells.h"
9 #include "terraingen/geometry/surface.h"
10 #include "terraingen/layers/craters/ages/age_model.h"
11 #include "terraingen/layers/craters/crater.h"
12 #include "terraingen/layers/craters/generation/spacing.h"
13 #include "terraingen/layers/field.h"
14
15 #include <cstddef>
16 #include <cstdint>
17 #include <functional>
18 #include <limits>
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <vector>
23
24 namespace terraingen {
25
26 /// A size band of crater generation: diameters [`low`, `high`) meters, from 2^`exponent` to
27 /// 2^(`exponent` + 1) clipped to the requested range.
28 struct CraterBand {
29 int exponent = 0; ///< The band's power of two.
30 double low = 0.0; ///< Smallest diameter (m).
31 double high = 0.0; ///< Largest diameter (m), exclusive.
32 double cellSize = 0.0; ///< Side of the cells the band is drawn in (m).
33
34 /// Memberwise equality.
35 bool operator==(const CraterBand& other) const {
36 return exponent == other.exponent && low == other.low && high == other.high &&
37 cellSize == other.cellSize;
38 }
39 bool operator!=(const CraterBand& other) const {
40 return !(*this == other);
41 }
42 };
43
44 /// The bands `GenerateCraters` uses to cover [`minDiameter`, `maxDiameter`) meters, smallest first.
45 /// @param cellSizeFactor Cell side over each band's largest whole diameter.
46 std::vector<CraterBand> CraterBands(double minDiameter, double maxDiameter, double cellSizeFactor);
47
48 /// The craters `GenerateCraters` draws in one cell of one band, in the order drawn, before
49 /// regions, manual craters, spacing and caps apply.
50 CraterList GenerateCellCraters(const SurfaceCells& cells, const CraterBand& band,
51 const CellIndex& cell, const SizeFrequencyDistribution& density,
52 const CraterAgeModel& ages, std::uint32_t seed);
53
54 /// The craters of one cell in the order drawn, with their indices ordered by y so the craters of a
55 /// row range are found by binary search.
56
1/1
✓ Branch 5 taken 109668 times.
236050 struct CellCraters {
57 CraterList craters; ///< The craters in the order drawn.
58 std::vector<std::uint32_t> byY; ///< Indices into `craters` by increasing y, then index.
59 double maxDiameter = 0.0; ///< The largest diameter, in meters; 0 without craters.
60
61 /// Indexes `craters` by y.
62 explicit CellCraters(CraterList craters);
63
64 /// The indices, in drawn order, of the craters with y in [`minY`, `maxY`].
65 std::vector<std::uint32_t> Rows(double minY, double maxY) const;
66 };
67
68 /// Supplies the craters of one cell of one band as `GenerateCellCraters` would, for example from
69 /// a cache; must be thread-safe.
70 using CellCraterSource = std::function<std::shared_ptr<const CellCraters>(const CraterBand& band,
71 const CellIndex& cell)>;
72
73 /// The share of generated craters like `crater` that `GenerateCraters` keeps, in [0, 1].
74 using CraterKeepShare = std::function<double(const Crater& crater)>;
75
76 /// Which craters `GenerateCraters` draws, and how.
77 struct CraterGenerationOptions {
78 /// Smallest diameter generated, in meters; must be set to a positive value.
79 double minDiameter = 0.0;
80 /// Largest diameter (m), exclusive; infinity means the top of the density's valid range, which
81 /// must then be finite.
82 double maxDiameter = std::numeric_limits<double>::infinity();
83 /// Also keep craters centered outside the region within this many radii of it.
84 double marginRadii = 0.0;
85 /// Cell side as a multiple of each band's largest diameter.
86 double cellSizeFactor = 16.0;
87 /// Upper limit on the expected number of craters, to catch requests that would exhaust memory.
88 double maxExpectedCraters = 5e7;
89 /// Removes conflicting craters (`SpacingConflicts`); disabled by default, since it lowers the
90 /// density below `density`.
91 CraterSpacing spacing{};
92 /// Manual craters to merge in: those within the region and margin follow the generated craters,
93 /// flagged and given IDs as `AppendManualCraters` does; with spacing, they remove conflicting
94 /// generated craters.
95 CraterList manual{};
96 /// Keep at most this many generated craters, the largest (ties to the smaller ID); unset keeps
97 /// all. Manual craters are always kept and do not count. A capped region no longer equals its
98 /// tiles joined; `CountLimitDiameter` bounds counts tile-safely.
99 std::optional<std::size_t> maxCraters{};
100 /// Skip bands whose craters are all smaller than this (m) and, with spacing, too small to
101 /// remove a crater this large; craters at or above it are unchanged. 0 generates every band.
102 double minResolvedDiameter = 0.0;
103 /// Skip bands whose craters are all larger than this (m), unless spacing or `maxCraters` is on,
104 /// where larger craters change which smaller ones are kept; craters at or below it are
105 /// unchanged. Infinity generates every band.
106 double maxBandDiameter = std::numeric_limits<double>::infinity();
107 /// Where each cell's craters come from; empty draws them with `GenerateCellCraters`.
108 CellCraterSource cellCraters{};
109 /// The craters per unit area expected in a band, the density's `ExpectedCount(1, band.low,
110 /// band.high)` to the bit, for example from a table; empty asks the density. Must be
111 /// thread-safe.
112 std::function<double(const CraterBand& band)> bandCount{};
113 /// Threads the cells are drawn on, merged in their order so the craters do not depend on it;
114 /// 0 for every hardware thread.
115 unsigned threads = 1;
116 /// The share of generated craters like each one kept, in [0, 1], from its position and size:
117 /// drawing at the density's highest local level and keeping each crater with its share, by a
118 /// uniform keyed to its ID, makes the density vary over the surface. Must be thread-safe and
119 /// depend only on the crater; unset keeps every crater.
120 CraterKeepShare keepShare{};
121 /// The diameter (m) from which `manual` craters are a complete catalog at each point: generated
122 /// craters at least this large centered there are dropped, so the catalog stands in for them
123 /// rather than adding to them. Infinite or NaN where there is no catalog; unset keeps every
124 /// generated crater. A DEM's own craters are such a catalog (`SourceResolvedLimit`), with
125 /// or without `manual` craters.
126 std::shared_ptr<const SurfaceField> completeness{};
127 /// What the region and margins are measured on: on a sphere, regions are longitude and
128 /// latitude boxes and margins are meters, and `cells` must be `CubeSphereCells` of its radius.
129 Surface surface{};
130 /// What the drawn features are called in the log: its category and the noun of its counts.
131 std::string logName = "craters";
132 };
133
134 /// Checks crater generation options.
135 /// @throws std::invalid_argument for invalid spacing, a non-positive `minDiameter`, a `maxDiameter`
136 /// not above it, a negative margin, expected limit or resolved diameter, or a non-positive cell
137 /// size factor or largest band.
138 void ValidateCraterGenerationOptions(const CraterGenerationOptions& options);
139
140 /// The largest diameter generated in meters: `maxDiameter`, or for an infinite `maxDiameter` the
141 /// top of the density's valid range; infinite when both are unbounded.
142 double ResolveMaxDiameter(double maxDiameter, const SizeFrequencyDistribution& density);
143
144 /// The smallest diameter in [`minDiameter`, `maxDiameter`) meters from which `density` expects
145 /// about `count` craters over `area` m^2; `minDiameter` if even that expects fewer.
146 /// @throws std::invalid_argument unless `count` and `area` are positive and 0 < `minDiameter` <
147 /// `maxDiameter`.
148 /// @throws std::domain_error if the interval leaves the density's valid range.
149 double CountLimitDiameter(const SizeFrequencyDistribution& density, double area, double count,
150 double minDiameter, double maxDiameter);
151
152 /// Generates the craters of a density centered in `region` (surface coordinates) or within
153 /// `marginRadii` radii of it, in band, cell and index order; tiles join exactly.
154 /// @param density A `Density` distribution valid over [`minDiameter`, `maxDiameter`), or
155 /// extrapolating.
156 /// @throws std::invalid_argument for invalid options, a bad region or density kind, or more
157 /// expected craters than `maxExpectedCraters`.
158 /// @throws std::domain_error if the diameter interval leaves the density's valid range.
159 CraterList GenerateCraters(const SurfaceCells& cells, const Bounds2d& region,
160 const SizeFrequencyDistribution& density, const CraterAgeModel& ages,
161 std::uint32_t seed, const CraterGenerationOptions& options);
162
163 } // namespace terraingen
164