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

include/terraingen/albedo/splotches.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Splotches: the dark and bright marks secondary impacts of loose regolith leave.
5 #pragma once
6
7 #include "terraingen/albedo/albedo.h"
8 #include "terraingen/geometry/geometry.h"
9 #include "terraingen/geometry/surface.h"
10 #include "terraingen/layers/craters/crater.h"
11 #include "terraingen/layers/fade.h"
12 #include "terraingen/random/stream.h"
13 #include "terraingen/random/value_distribution.h"
14
15 #include <cstdint>
16 #include <optional>
17 #include <vector>
18
19 namespace terraingen {
20
21 /// Swarms of splotches thrown by new craters, the low-velocity secondaries of their ejecta
22 /// (Robinson et al. 2015; Speyerer et al. 2016; Housen and Holsapple 2011), off by default.
23 struct SplotchSwarms {
24 bool enabled = false; ///< Whether new craters throw swarms.
25 double craterRate = 4.84e-6; ///< New craters of at least 10 m, per km^2 per year.
26 double craterSlope = 3.06; ///< Cumulative slope of the new craters; positive.
27 double minCraterDiameter = 10.0; ///< Smallest crater with a swarm (m); positive.
28 double maxCraterDiameter = 50.0; ///< Largest (m); above the smallest.
29 double referenceDiameter = 18.8; ///< Diameter of the reference crater (m).
30 double referenceCount = 248.0; ///< Splotches of the reference crater; positive.
31 double mu = 0.41; ///< Velocity exponent of the target, in (0, 1).
32 double minRange = 4300.0; ///< Nearest landing distance (m); positive.
33 double maxRange = 30000.0; ///< Farthest landing distance (m); above the nearest.
34 /// Diameter of a swarm splotch (m); positive and bounded.
35 ValueDistribution splotchDiameter{ValueDistribution::LogNormal(6.0, 0.6).Clamped(2.0, 22.0)};
36 double darkShare = 239.0 / 248.0; ///< Share darker than the ground, in [0, 1].
37 double brightShare = 3.0 / 248.0; ///< Share brighter; the rest are mixed.
38 /// Reflectance change of a dark swarm splotch; above -1 and bounded.
39 ValueDistribution darkChange{ValueDistribution::Constant(-0.04)};
40 /// Reflectance change of a bright swarm splotch; above -1 and bounded.
41 ValueDistribution brightChange{ValueDistribution::Uniform(0.04, 0.12)};
42 double wedgeShare = 0.2; ///< Share of wedges pointing back to the crater, [0, 1].
43 double wedgeElongation = 2.0; ///< Long over short axis of a wedge; at least 1.
44 /// New craters given by hand, such as an observed impact, each throwing a swarm like the
45 /// generated ones; IDs of 0 come from `ManualCraterId`.
46 std::vector<Crater> craters;
47
48 /// New craters of at least 1 m per m^2 within `lifetimeMyr`.
49 double Coefficient(double lifetimeMyr) const;
50
51 /// The splotches a crater `diameter` meters across throws, on average.
52 double Count(double diameter) const;
53
54 bool operator==(const SplotchSwarms& other) const {
55 return enabled == other.enabled && craterRate == other.craterRate &&
56 craterSlope == other.craterSlope && minCraterDiameter == other.minCraterDiameter &&
57 maxCraterDiameter == other.maxCraterDiameter &&
58 referenceDiameter == other.referenceDiameter &&
59 referenceCount == other.referenceCount && mu == other.mu &&
60 minRange == other.minRange && maxRange == other.maxRange &&
61 splotchDiameter == other.splotchDiameter && darkShare == other.darkShare &&
62 brightShare == other.brightShare && darkChange == other.darkChange &&
63 brightChange == other.brightChange && wedgeShare == other.wedgeShare &&
64 wedgeElongation == other.wedgeElongation && craters == other.craters;
65 }
66 bool operator!=(const SplotchSwarms& other) const {
67 return !(*this == other);
68 }
69 };
70
71 /// Splotches after Speyerer et al. (2016): formed at `rateCoefficient` d^-`rateExponent` per km^2
72 /// per year (d in km), each lasting `lifetimeMyr`; dark, bright or mixed.
73 struct SplotchAlbedoOptions {
74 std::uint32_t seed = 0; ///< Seed of where splotches fall and what they are.
75 double rateCoefficient = 1.483e-11; ///< Formation rate coefficient, per km^2 per year.
76 double rateExponent = 4.144; ///< Cumulative slope of the formation rate; positive.
77 double lifetimeMyr = 0.081; ///< How long a splotch lasts, fading linearly (Myr).
78 double minDiameter = 1.0; ///< Smallest splotch (m); the fit is extrapolated below 10.
79 double maxDiameter = 40.0; ///< Largest splotch (m), above `minDiameter`.
80 double darkShare = 0.907; ///< Share of dark splotches, in [0, 1].
81 double brightShare = 0.074; ///< Share of bright ones; the rest are mixed.
82 /// Reflectance change of a dark splotch, relative to the ground before it; above -1.
83 ValueDistribution darkChange{ValueDistribution::Constant(-0.04)};
84 /// Reflectance change of a bright splotch; above -1.
85 ValueDistribution brightChange{ValueDistribution::Constant(0.10)};
86 /// Share of splotches on slopes steeper than `steepSlopeDegrees` that are bright, in [0, 1];
87 /// unset keeps `brightShare`. Bright splotches are found mostly on such slopes, where mature
88 /// regolith is thin (Speyerer et al. 2016), but the sources give no share; needs the ground.
89 std::optional<double> steepBrightShare;
90 double steepSlopeDegrees = 15.0; ///< Slope from which ground is steep, in (0, 90) degrees.
91 double softness = 0.3; ///< Share of the radius over which the edge fades, in (0, 1].
92 double raggedness = 0.15; ///< Share of the radius the edge's lobes pull in, in [0, 1).
93 ResolutionFade fade{}; ///< How splotches too small to resolve fade out.
94 SplotchSwarms swarms{}; ///< Swarms around new craters; off by default.
95
96 /// The cumulative number of splotches of diameter at least 1 m per m^2 at any time.
97 double Coefficient() const;
98
99 bool operator==(const SplotchAlbedoOptions& other) const {
100 return seed == other.seed && rateCoefficient == other.rateCoefficient &&
101 rateExponent == other.rateExponent && lifetimeMyr == other.lifetimeMyr &&
102 minDiameter == other.minDiameter && maxDiameter == other.maxDiameter &&
103 darkShare == other.darkShare && brightShare == other.brightShare &&
104 darkChange == other.darkChange && brightChange == other.brightChange &&
105 steepBrightShare == other.steepBrightShare &&
106 steepSlopeDegrees == other.steepSlopeDegrees && softness == other.softness &&
107 raggedness == other.raggedness && fade == other.fade && swarms == other.swarms;
108 }
109 bool operator!=(const SplotchAlbedoOptions& other) const {
110 return !(*this == other);
111 }
112 };
113
114 /// Checks splotch options.
115 /// @throws std::invalid_argument naming the first field out of its range, here or in `swarms`, or
116 /// for diameters, crater diameters or ranges that do not increase, dark and bright shares above 1
117 /// together, or invalid distributions.
118 void ValidateSplotchAlbedoOptions(const SplotchAlbedoOptions& options);
119
120 /// One splotch.
121 struct Splotch {
122 Crater mark; ///< Center, diameter, age and ID, as a crater's.
123 double change = 0.0; ///< Reflectance change of its dark or only side.
124 double otherChange = 0.0; ///< Reflectance change of its other side; `change` unless mixed.
125 double split = 0.0; ///< Direction across which a mixed splotch changes side, radians.
126 std::uint32_t key = 0; ///< Key of its edge's lobes.
127 double elongation = 1.0; ///< Long over short axis; 1 for round.
128 double heading = 0.0; ///< Toward the crater that threw it, radians from east.
129 bool wedge = false; ///< Narrowing to a point toward the crater.
130 };
131
132 /// The splotches of `SplotchAlbedoOptions` on the context's surface, darkening and brightening the
133 /// albedo; the same in any grid, tile or point.
134 23 class SplotchAlbedo final : public AlbedoLayer {
135 public:
136 /// @throws std::invalid_argument for options invalid as `ValidateSplotchAlbedoOptions`.
137 explicit SplotchAlbedo(SplotchAlbedoOptions options = {});
138
139 void ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y,
140 const LayerContext& context) const override;
141
142 void Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const override;
143
144 /// The splotches on `surface` resolved at `spacing` meters reaching `region`, youngest first;
145 /// with `ground`, those on steep slopes follow `steepBrightShare`.
146 std::vector<Splotch> Splotches(const Bounds2d& region, double spacing,
147 const Ground* ground = nullptr,
148 const Surface& surface = {}) const;
149
150 /// The splotches of the swarm `crater` throws on `surface`, resolved at `spacing` meters (0
151 /// for all).
152 std::vector<Splotch> Swarm(const Crater& crater, double spacing,
153 const Surface& surface = {}) const;
154
155 /// The relative reflectance change `splotch` alone gives (`x`, `y`) on `surface` at `spacing`
156 /// meters.
157 double Change(const Splotch& splotch, double x, double y, double spacing,
158 const Surface& surface = {}) const;
159
160 /// What the splotches are.
161 1 const SplotchAlbedoOptions& Options() const {
162 1 return m_options;
163 }
164
165 private:
166 /// The splotches of `region` on `surface` resolved at `spacing` meters, before their sides
167 /// are drawn.
168 std::vector<Splotch> Marks(const Bounds2d& region, double spacing,
169 const Surface& surface) const;
170 /// The slope in degrees of `ground` under each of `splotches`, over its width, read at
171 /// `spacing` meters (half the smallest splotch for 0); all 0 without `ground` or a steep
172 /// bright share.
173 std::vector<double> SteepSlopes(const std::vector<Splotch>& splotches, double spacing,
174 const Ground* ground, const Surface& surface) const;
175 /// Draws whether `splotch` on ground of `slopeDegrees` is dark, bright or mixed, and its
176 /// changes, split and lobes.
177 void DrawSides(Splotch& splotch, double slopeDegrees) const;
178 /// Adds the splotches of the swarms reaching `region` to `splotches`.
179 void AddSwarms(const Bounds2d& region, double spacing, const Surface& surface,
180 std::vector<Splotch>& splotches) const;
181 /// The `index`th splotch of the swarm `crater` throws, its draws the next of `random`.
182 Splotch DrawSwarmSplotch(RandomStream& random, const Crater& crater, std::uint64_t index,
183 const Surface& surface) const;
184 /// The change of `splotches`, youngest first, at (`x`, `y`).
185 double ChangeAt(const std::vector<const Splotch*>& splotches, double x, double y,
186 double spacing, const Surface& surface) const;
187
188 SplotchAlbedoOptions m_options;
189 };
190
191 } // namespace terraingen
192