include/terraingen/layers/craters/shaping/irregularity.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Irregular, non-circular crater outlines: elongation and rough rims. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/surface.h" | ||
| 8 | #include "terraingen/layers/craters/crater.h" | ||
| 9 | #include "terraingen/layers/kernels/craters.h" | ||
| 10 | #include "terraingen/random/value_distribution.h" | ||
| 11 | |||
| 12 | #include <cstdint> | ||
| 13 | #include <optional> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | /// How rough crater rims are drawn. | ||
| 19 | enum class RimRoughnessModel { | ||
| 20 | /// Harmonics `minHarmonic` to `maxHarmonic` with amplitudes falling as k^-`spectralSlope`, of | ||
| 21 | /// RMS `roughness`: a simple, configurable spectrum. | ||
| 22 | PowerLaw, | ||
| 23 | /// The fitted rim crest spectra of fresh lunar craters of Du et al. (2024), scattered as | ||
| 24 | /// measured when `scatter`, tabulated at 1024 angles; outlines deviating by more than | ||
| 25 | /// `maxDeviation` radii are scaled down to it. | ||
| 26 | Du2024, | ||
| 27 | }; | ||
| 28 | |||
| 29 | /// Which craters have irregular outlines and the distributions their shapes are drawn from, | ||
| 30 | /// per crater from its ID. | ||
| 31 | /// | ||
| 32 | /// The rim lies at R s(theta) and the whole profile is stretched by s; s is an area-preserving | ||
| 33 | /// ellipse for `elongatedShare` of selected craters, times 1 plus rim harmonics for `roughShare`. | ||
| 34 | struct CraterIrregularity { | ||
| 35 | bool enabled = false; ///< Off: every crater is round. | ||
| 36 | /// Varies the outlines; unset follows the crater layer's `seed`, and is 0 outside a layer. | ||
| 37 | std::optional<std::uint32_t> seed{}; | ||
| 38 | /// The craters that can be irregular; manual craters are left round by default. | ||
| 39 | CraterSelection selection = [] { | ||
| 40 | CraterSelection generated; | ||
| 41 | generated.manual = false; | ||
| 42 | return generated; | ||
| 43 | }(); | ||
| 44 | |||
| 45 | double roughShare = 1.0; ///< Share of selected craters with rough outlines. | ||
| 46 | RimRoughnessModel model = RimRoughnessModel::PowerLaw; ///< How rough rims are drawn. | ||
| 47 | bool scatter = true; ///< With `Du2024`, scatter each crater's spectrum as measured. | ||
| 48 | double maxDeviation = 0.3; ///< With `Du2024`, the largest deviation kept, in radii. | ||
| 49 | /// With `PowerLaw`, the outline's RMS deviation from a circle, in radii. | ||
| 50 | ValueDistribution roughness{ValueDistribution::Kind::Uniform, 0.01, 0.03}; | ||
| 51 | /// With `PowerLaw`, the exponent amplitudes fall with: A_k ~ k^-slope. | ||
| 52 | ValueDistribution spectralSlope{ValueDistribution::Kind::Constant, 1.5}; | ||
| 53 | int minHarmonic = 2; ///< With `PowerLaw`, the lowest harmonic. | ||
| 54 | int maxHarmonic = 12; ///< With `PowerLaw`, the highest harmonic. | ||
| 55 | |||
| 56 | double elongatedShare = 0.054; ///< Share of selected craters that are elliptical. | ||
| 57 | /// Long over short rim-to-rim diameter of elliptical craters; at least 1. | ||
| 58 | ValueDistribution ellipticity{ValueDistribution::Kind::Uniform, 1.1, 1.5}; | ||
| 59 | /// Heading of the long axis, degrees counter-clockwise from +x, spread by up to | ||
| 60 | /// `headingSpreadDegrees` either way; unset for random headings. | ||
| 61 | std::optional<double> headingDegrees; | ||
| 62 | double headingSpreadDegrees = 0.0; ///< Spread of `headingDegrees` either way. | ||
| 63 | |||
| 64 | /// Share of selected craters with radial marks: the wall's profile is read nearer to or | ||
| 65 | /// farther from the center by a shift that varies smoothly around the crater, leaving ridges | ||
| 66 | /// and grooves down the wall that are strongest where it is steepest. No published | ||
| 67 | /// measurements of such marks are known, so the defaults are set by eye. | ||
| 68 | double marksShare = 0.0; | ||
| 69 | /// Largest shift of the marks, in radii, drawn per crater; each knot is uniform within it. | ||
| 70 | ValueDistribution marksAmplitude{ValueDistribution::Kind::Uniform, 0.0, 0.005}; | ||
| 71 | int marksKnots = 45; ///< Random shifts around the crater the marks pass through smoothly. | ||
| 72 | double marksInnerRadii = 0.3; ///< Where the marks reach full strength going out, in radii. | ||
| 73 | double marksOuterRadii = 0.9; ///< Where the marks start to fade going out, in radii. | ||
| 74 | double marksFadeRadii = 0.1; ///< Width of the fades either side, in radii. | ||
| 75 | |||
| 76 | /// Memberwise equality. | ||
| 77 | 4 | bool operator==(const CraterIrregularity& other) const { | |
| 78 | 8 | return enabled == other.enabled && seed == other.seed && selection == other.selection && | |
| 79 | 4 | roughShare == other.roughShare && model == other.model && scatter == other.scatter && | |
| 80 | 8 | maxDeviation == other.maxDeviation && roughness == other.roughness && | |
| 81 | 8 | spectralSlope == other.spectralSlope && minHarmonic == other.minHarmonic && | |
| 82 | 8 | maxHarmonic == other.maxHarmonic && elongatedShare == other.elongatedShare && | |
| 83 | 8 | ellipticity == other.ellipticity && headingDegrees == other.headingDegrees && | |
| 84 | 4 | headingSpreadDegrees == other.headingSpreadDegrees && | |
| 85 | 4 | marksShare == other.marksShare && marksAmplitude == other.marksAmplitude && | |
| 86 | 4 | marksKnots == other.marksKnots && marksInnerRadii == other.marksInnerRadii && | |
| 87 | 8 | marksOuterRadii == other.marksOuterRadii && marksFadeRadii == other.marksFadeRadii; | |
| 88 | } | ||
| 89 | bool operator!=(const CraterIrregularity& other) const { | ||
| 90 | return !(*this == other); | ||
| 91 | } | ||
| 92 | }; | ||
| 93 | |||
| 94 | /// Checks irregularity settings. | ||
| 95 | /// @throws std::invalid_argument for a field outside its range (`CheckRanges`), harmonics or marks | ||
| 96 | /// out of order, or a roughness or mark amplitude large enough to fold the rim or wall. | ||
| 97 | void ValidateCraterIrregularity(const CraterIrregularity& irregularity); | ||
| 98 | |||
| 99 | /// One crater's outline: the factor its rim radius and profile are stretched by in each | ||
| 100 | /// direction. | ||
| 101 | struct CraterOutline { | ||
| 102 | double ellipticity = 1.0; ///< Long over short axis; 1 for none. | ||
| 103 | double heading = 0.0; ///< Long axis, radians counter-clockwise from +x. | ||
| 104 | std::vector<double> amplitudes; ///< A_k from k = `firstHarmonic`. | ||
| 105 | std::vector<double> phases; ///< phi_k, in step with `amplitudes`. | ||
| 106 | int firstHarmonic = 2; ///< The harmonic of `amplitudes[0]`. | ||
| 107 | /// The rim's deviation from a circle at angles 2 pi n / size, interpolated linearly, in place | ||
| 108 | /// of `amplitudes` when not empty. | ||
| 109 | std::vector<double> table; | ||
| 110 | /// Radial shifts of the wall, in radii, at angles 2 pi n / size, passed through by a periodic | ||
| 111 | /// Catmull-Rom spline; empty for no marks. | ||
| 112 | std::vector<double> marks; | ||
| 113 | double marksInnerRadii = 0.3; ///< Where the marks reach full strength, in radii. | ||
| 114 | double marksOuterRadii = 0.9; ///< Where the marks start to fade, in radii. | ||
| 115 | double marksFadeRadii = 0.1; ///< Width of the fades, in radii. | ||
| 116 | |||
| 117 | /// The factor the rim radius is stretched by toward (`dx`, `dy`) meters east and north of the | ||
| 118 | /// center, as `KernelOutlineScale` in single precision; 1 for a round crater. | ||
| 119 | double Scale(double dx, double dy) const; | ||
| 120 | |||
| 121 | /// The shift in radii the marks add at `radii` from the center toward (`dx`, `dy`), as | ||
| 122 | /// `KernelMarkShift` in single precision; 0 outside the marks' window, which ends inside the | ||
| 123 | /// rim. | ||
| 124 | double MarkShift(double dx, double dy, double radii) const; | ||
| 125 | |||
| 126 | /// An upper bound of `Scale` in any direction. | ||
| 127 | double MaxScale() const; | ||
| 128 | |||
| 129 | /// Whether the outline is a circle: no ellipticity, harmonics, table or marks. | ||
| 130 | 1838228 | bool Round() const { | |
| 131 | 1838228 | return ellipticity == 1.0 && amplitudes.empty() && table.empty() && marks.empty(); | |
| 132 | } | ||
| 133 | }; | ||
| 134 | |||
| 135 | /// The outline `irregularity` gives a crater, seeded by its ID; round unless irregularity is | ||
| 136 | /// enabled and the crater selected. | ||
| 137 | CraterOutline IrregularOutline(const Crater& crater, const CraterIrregularity& irregularity); | ||
| 138 | |||
| 139 | /// The largest stretch any crater's outline can have, to widen footprints by; 1 when disabled. | ||
| 140 | double MaxOutlineScale(const CraterIrregularity& irregularity); | ||
| 141 | |||
| 142 | /// Appends `outline`'s harmonics, deviations and marks to `data` as the crater kernel reads them, | ||
| 143 | /// noting where, and its ellipse, on `record`; with a table, its broad harmonics stand in for | ||
| 144 | /// `amplitudes`, so the kernel can fade the table's fine roughness faster toward the center. | ||
| 145 | void AppendKernelOutline(const CraterOutline& outline, CraterRecord& record, | ||
| 146 | std::vector<float>& data); | ||
| 147 | |||
| 148 | /// A crater's outline in the crater kernel's form, to read many points with. | ||
| 149 | 1225482 | class KernelOutline { | |
| 150 | public: | ||
| 151 | /// A round outline. | ||
| 152 | 519500 | KernelOutline() : KernelOutline(CraterOutline{}, 1.0) {} | |
| 153 | |||
| 154 | /// `outline` of a crater of `diameter` meters. | ||
| 155 | KernelOutline(const CraterOutline& outline, double diameter); | ||
| 156 | |||
| 157 | /// `KernelOutlineScale` toward `offset` (m). | ||
| 158 | double Scale(const Vec2d& offset) const; | ||
| 159 | |||
| 160 | /// `KernelMarkShift` at `radii` toward `offset` (m); 0 without marks. | ||
| 161 | double MarkShift(const Vec2d& offset, double radii) const; | ||
| 162 | |||
| 163 | /// `KernelProfileDistance` (m) at `offset` meters east and north of the center; the exact | ||
| 164 | /// distance for a round outline. | ||
| 165 | double ProfileDistance(const Vec2d& offset) const; | ||
| 166 | |||
| 167 | private: | ||
| 168 | 736521 | CraterData Data() const { | |
| 169 | 736521 | CraterData view; | |
| 170 | 736521 | view.values = m_data.data(); | |
| 171 | 736521 | return view; | |
| 172 | } | ||
| 173 | |||
| 174 | bool m_round = true; | ||
| 175 | CraterRecord m_record; | ||
| 176 | std::vector<float> m_data{0.0f}; | ||
| 177 | }; | ||
| 178 | |||
| 179 | /// The distance in meters at which a crater's round profile is read at surface (x, y): the true | ||
| 180 | /// distance over the stretch of its `outline` (`IrregularOutline`) in that direction, shifted by | ||
| 181 | /// its marks; `KernelOutline::ProfileDistance`. | ||
| 182 | double CraterProfileDistance(const Crater& crater, const CraterOutline& outline, double x, double y, | ||
| 183 | const Surface& surface = {}); | ||
| 184 | |||
| 185 | /// `CraterProfileDistance` for a point `offset` meters east and north of the crater's center in | ||
| 186 | /// its local frame. | ||
| 187 | double CraterProfileDistance(const Crater& crater, const CraterOutline& outline, | ||
| 188 | const Vec2d& offset); | ||
| 189 | |||
| 190 | } // namespace terraingen | ||
| 191 |