include/terraingen/layers/craters/crater.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `Crater` and `CraterList`, and `CraterSelection`, which of them a model applies to. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/noise/hash.h" | ||
| 8 | |||
| 9 | #include <cmath> | ||
| 10 | #include <cstddef> | ||
| 11 | #include <cstdint> | ||
| 12 | #include <functional> | ||
| 13 | #include <initializer_list> | ||
| 14 | #include <limits> | ||
| 15 | #include <optional> | ||
| 16 | #include <string_view> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | /// `value`, unset when NaN, as a column of optional values holds it. | ||
| 22 | 19706276 | inline std::optional<double> UnlessNan(double value) { | |
| 23 | 9853140 | return std::isnan(value) ? std::nullopt : std::optional<double>(value); | |
| 24 | } | ||
| 25 | |||
| 26 | /// One crater: center in surface coordinates, rim-to-rim diameter, age and a stable ID, and for a | ||
| 27 | /// manual crater optionally its depth and rim height. | ||
| 28 | 77458 | struct Crater { | |
| 29 | double x = 0.0; ///< Surface x of the center. | ||
| 30 | double y = 0.0; ///< Surface y of the center. | ||
| 31 | double diameter = 0.0; ///< Rim-to-rim diameter (m). | ||
| 32 | double ageGyr = 0.0; ///< Age (Gyr). | ||
| 33 | std::uint64_t id = 0; ///< Stable ID. | ||
| 34 | /// Placed by hand, identified in imagery or read from a catalog rather than generated; takes | ||
| 35 | /// precedence over generated craters and is never removed by spacing. | ||
| 36 | bool manual = false; | ||
| 37 | /// Rim-to-floor depth (m) of a manual crater; unset takes it from the degradation model. | ||
| 38 | std::optional<double> depth{}; | ||
| 39 | /// Rim crest above the pre-impact surface (m) of a manual crater with a `depth`; unset derives | ||
| 40 | /// it from the depth (`ResolvedCraterState`). | ||
| 41 | std::optional<double> rimHeight{}; | ||
| 42 | /// Replaces whatever the terrain holds where it lies with a sharp crater cut into a datum | ||
| 43 | /// fitted around it (`CraterReplacement`), such as a crater a DEM holds blurred. It stands in | ||
| 44 | /// for a crater the terrain held, so selections take it as generated (`CraterSelection`): it | ||
| 45 | /// gets the irregular outline, secondaries and albedo generated craters get. | ||
| 46 | bool replace = false; | ||
| 47 | |||
| 48 | /// Sets the age in Gyr, for chaining after `ManualCrater`. | ||
| 49 | Crater& AgeGyr(double gyr) { | ||
| 50 | ageGyr = gyr; | ||
| 51 | return *this; | ||
| 52 | } | ||
| 53 | /// Sets the rim-to-floor depth in meters. | ||
| 54 | 11 | Crater& Depth(double meters) { | |
| 55 | 9 | depth = meters; | |
| 56 | 10 | return *this; | |
| 57 | } | ||
| 58 | /// Sets the rim crest above the pre-impact surface in meters; needs a `depth`. | ||
| 59 | 1 | Crater& RimHeight(double meters) { | |
| 60 | 1 | rimHeight = meters; | |
| 61 | 1 | return *this; | |
| 62 | } | ||
| 63 | /// Sets whether the crater replaces the terrain beneath it. | ||
| 64 | 52 | Crater& Replace(bool replaces = true) { | |
| 65 | 58 | replace = replaces; | |
| 66 | 52 | return *this; | |
| 67 | } | ||
| 68 | |||
| 69 | 463991 | bool operator==(const Crater& other) const { | |
| 70 | 201069 | return x == other.x && y == other.y && diameter == other.diameter && | |
| 71 | 201084 | ageGyr == other.ageGyr && id == other.id && manual == other.manual && | |
| 72 | 1067179 | depth == other.depth && rimHeight == other.rimHeight && replace == other.replace; | |
| 73 | } | ||
| 74 | 3 | bool operator!=(const Crater& other) const { | |
| 75 | 3 | return !(*this == other); | |
| 76 | } | ||
| 77 | }; | ||
| 78 | |||
| 79 | /// A stable ID for a manual crater that has none, hashed with `Philox4x32`: the same for the same | ||
| 80 | /// crater in every run. | ||
| 81 | /// @param diameter In meters. | ||
| 82 | std::uint64_t ManualCraterId(double x, double y, double diameter); | ||
| 83 | |||
| 84 | /// A fresh crater placed by hand at (`x`, `y`), `diameter` meters across, flagged `manual`, with | ||
| 85 | /// its ID left 0 for the layer to hash; set the rest by name: | ||
| 86 | /// | ||
| 87 | /// ManualCrater(-4.0, 3.0, 9.0).AgeGyr(0.5).Depth(1.8) | ||
| 88 | 51 | inline Crater ManualCrater(double x, double y, double diameter) { | |
| 89 | 51 | Crater crater; | |
| 90 | 51 | crater.x = x; | |
| 91 | 51 | crater.y = y; | |
| 92 | 51 | crater.diameter = diameter; | |
| 93 | 51 | crater.manual = true; | |
| 94 | 51 | return crater; | |
| 95 | } | ||
| 96 | |||
| 97 | /// The 32-bit key that seeds crater `id`'s noise or texture under `seed`, the same on the CPU and | ||
| 98 | /// the GPU, which is handed it. | ||
| 99 | 692457 | constexpr std::uint32_t CraterKey(std::uint64_t id, std::uint32_t seed) { | |
| 100 | 348854 | return HashWord64(id, seed); | |
| 101 | } | ||
| 102 | |||
| 103 | /// Craters stored as one array per field, so each column can be shared with NumPy or a GPU | ||
| 104 | /// without copying. | ||
| 105 | class CraterList { | ||
| 106 | public: | ||
| 107 | /// An empty list. | ||
| 108 | 3441841 | CraterList() = default; | |
| 109 | |||
| 110 | /// A list of `craters`, in order. | ||
| 111 | CraterList(std::initializer_list<Crater> craters); | ||
| 112 | |||
| 113 | /// The number of craters. | ||
| 114 | 17018271 | std::size_t Size() const { | |
| 115 | 16460768 | return m_id.size(); | |
| 116 | } | ||
| 117 | |||
| 118 | /// Whether the list holds no crater. | ||
| 119 | 3907558 | bool Empty() const { | |
| 120 | 3907558 | return m_id.empty(); | |
| 121 | } | ||
| 122 | |||
| 123 | /// Reserves room for `count` craters in every column. | ||
| 124 | void Reserve(std::size_t count); | ||
| 125 | |||
| 126 | /// Appends a crater. | ||
| 127 | void Append(const Crater& crater); | ||
| 128 | |||
| 129 | /// Appends every crater of `other`, in its order. | ||
| 130 | void Append(const CraterList& other); | ||
| 131 | |||
| 132 | /// The crater at `index`. | ||
| 133 | /// @throws std::out_of_range if `index` >= `Size()`. | ||
| 134 | Crater At(std::size_t index) const; | ||
| 135 | |||
| 136 | /// Surface x of every center. | ||
| 137 | 52 | const std::vector<double>& X() const { | |
| 138 | 6457018 | return m_x; | |
| 139 | } | ||
| 140 | |||
| 141 | /// Surface y of every center. | ||
| 142 | 1832508 | const std::vector<double>& Y() const { | |
| 143 | 8289474 | return m_y; | |
| 144 | } | ||
| 145 | |||
| 146 | /// Every diameter, in meters. | ||
| 147 | 128745 | const std::vector<double>& Diameters() const { | |
| 148 | 6663182 | return m_diameter; | |
| 149 | } | ||
| 150 | |||
| 151 | /// Every age, in Gyr. | ||
| 152 | 49 | const std::vector<double>& AgesGyr() const { | |
| 153 | 77507 | return m_ageGyr; | |
| 154 | } | ||
| 155 | |||
| 156 | /// Every ID. | ||
| 157 | 83 | const std::vector<std::uint64_t>& Ids() const { | |
| 158 | 77543 | return m_id; | |
| 159 | } | ||
| 160 | |||
| 161 | /// 1 for manual craters, 0 for generated ones; bytes so it can be shared without copying. | ||
| 162 | 46 | const std::vector<std::uint8_t>& Manual() const { | |
| 163 | 4757 | return m_manual; | |
| 164 | } | ||
| 165 | |||
| 166 | /// Every explicit depth in meters (`Crater::depth`), NaN where it comes from the model. | ||
| 167 | 2 | const std::vector<double>& Depths() const { | |
| 168 | 2 | return m_depth; | |
| 169 | } | ||
| 170 | |||
| 171 | /// Every explicit rim height in meters (`Crater::rimHeight`), NaN where it is derived. | ||
| 172 | 1 | const std::vector<double>& RimHeights() const { | |
| 173 | 1 | return m_rimHeight; | |
| 174 | } | ||
| 175 | |||
| 176 | /// 1 for craters that replace the terrain beneath them (`Crater::replace`), 0 for others. | ||
| 177 | 814 | const std::vector<std::uint8_t>& Replace() const { | |
| 178 | 814 | return m_replace; | |
| 179 | } | ||
| 180 | |||
| 181 | /// Whether any crater replaces the terrain beneath it. | ||
| 182 | bool AnyReplace() const; | ||
| 183 | |||
| 184 | private: | ||
| 185 | std::vector<double> m_x; | ||
| 186 | std::vector<double> m_y; | ||
| 187 | std::vector<double> m_diameter; | ||
| 188 | std::vector<double> m_ageGyr; | ||
| 189 | std::vector<std::uint64_t> m_id; | ||
| 190 | std::vector<std::uint8_t> m_manual; | ||
| 191 | std::vector<double> m_depth; | ||
| 192 | std::vector<double> m_rimHeight; | ||
| 193 | std::vector<std::uint8_t> m_replace; | ||
| 194 | }; | ||
| 195 | |||
| 196 | /// The craters `keep(index, crater)` keeps, in their order. | ||
| 197 | 1172 | template <typename Keep> CraterList FilterCraters(const CraterList& craters, const Keep& keep) { | |
| 198 | 1172 | CraterList kept; | |
| 199 | 1172 | kept.Reserve(craters.Size()); | |
| 200 | 556287 | for (std::size_t n = 0; n < craters.Size(); ++n) { | |
| 201 | 555115 | const Crater crater = craters.At(n); | |
| 202 | 555115 | if (keep(n, crater)) { | |
| 203 | 537068 | kept.Append(crater); | |
| 204 | } | ||
| 205 | } | ||
| 206 | 1172 | return kept; | |
| 207 | } | ||
| 208 | |||
| 209 | /// Appends `manual` to `craters`, flagged manual and given a `ManualCraterId` where their ID is 0. | ||
| 210 | /// @param keep Which craters to append; empty for all. | ||
| 211 | /// @throws std::invalid_argument for any crater, kept or not, with a field outside the range its | ||
| 212 | /// comment gives, or a rim height without a depth. | ||
| 213 | void AppendManualCraters(const CraterList& manual, CraterList& craters, | ||
| 214 | const std::function<bool(const Crater&)>& keep = {}); | ||
| 215 | |||
| 216 | /// Which craters a model applies to, by diameter, age and whether manual. | ||
| 217 | 8 | struct CraterSelection { | |
| 218 | double minDiameter = 0.0; ///< Smallest diameter (m). | ||
| 219 | double maxDiameter = std::numeric_limits<double>::infinity(); ///< Largest diameter (m). | ||
| 220 | double minAgeGyr = 0.0; ///< Youngest age (Gyr). | ||
| 221 | double maxAgeGyr = std::numeric_limits<double>::infinity(); ///< Oldest age (Gyr). | ||
| 222 | /// Whether manual craters are included; replacing craters (`Crater::replace`), which stand in | ||
| 223 | /// for craters the terrain held, are selected as generated craters are either way. | ||
| 224 | bool manual = true; | ||
| 225 | |||
| 226 | /// Whether a crater is selected: not for a manual crater that does not replace without | ||
| 227 | /// `manual`, or a diameter or age outside its range. | ||
| 228 | bool Includes(const Crater& crater) const; | ||
| 229 | |||
| 230 | /// Memberwise equality. | ||
| 231 | 5 | bool operator==(const CraterSelection& other) const { | |
| 232 | 5 | return minDiameter == other.minDiameter && maxDiameter == other.maxDiameter && | |
| 233 | 10 | minAgeGyr == other.minAgeGyr && maxAgeGyr == other.maxAgeGyr && | |
| 234 | 5 | manual == other.manual; | |
| 235 | } | ||
| 236 | bool operator!=(const CraterSelection& other) const { | ||
| 237 | return !(*this == other); | ||
| 238 | } | ||
| 239 | }; | ||
| 240 | |||
| 241 | /// Checks a crater selection. | ||
| 242 | /// @param what What the selection is for, which starts the error message. | ||
| 243 | /// @throws std::invalid_argument "<what> size and age ranges must satisfy 0 <= min <= max" | ||
| 244 | /// otherwise. | ||
| 245 | void ValidateCraterSelection(const CraterSelection& selection, std::string_view what); | ||
| 246 | |||
| 247 | } // namespace terraingen | ||
| 248 |