./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
50 / 54
92.6%
Branches:
48 / 74
64.9%
Functions:
6 / 8, 3 excl
75.0%

include/terraingen/layers/craters/secondary_model.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief The laws secondary craters follow, their options, and their density around a primary.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/layers/craters/crater.h"
9 #include "terraingen/layers/craters/rays.h"
10 #include "terraingen/random/value_distribution.h"
11
12 #include <cstdint>
13 #include <limits>
14 #include <memory>
15 #include <optional>
16 #include <vector>
17
18 namespace terraingen {
19
20 /// The largest secondary crater's diameter at `range` meters from a primary of `diameter` meters,
21 /// in meters, after the generalized fit of Singer et al. (2020): d = a R^-b with a = 0.022 D^1.96
22 /// and b = 0.14 ln D, D and R in km.
23 double SingerMaxSecondaryDiameter(double diameter, double range);
24
25 /// The range in meters beyond which `SingerMaxSecondaryDiameter` falls below `size` meters: its
26 /// inverse in range. Infinite for primaries of 1 km or less, whose largest secondary does not
27 /// shrink with range.
28 double SingerMaxRange(double diameter, double size);
29
30 /// How far from its center a primary of `diameter` meters throws its first secondaries (Singer et
31 /// al. 2020): the radius plus about 1.8 diameters up to 3 km and 1.3 from 31 km, in meters.
32 double SecondaryOnsetRange(double diameter);
33
34 /// The median ray length in meters of Elliott et al. (2018, eq. 2): `coefficient` R^`exponent` km
35 /// for a crater of radius R km.
36 /// @param diameter In meters.
37 /// @param coefficient In km.
38 double ElliottMedianRayLength(double diameter, double coefficient = 6.59, double exponent = 1.27);
39
40 /// Secondaries gathered into their primary's rays and clumped along them (Singer et al. 2020,
41 /// https://doi.org/10.1029/2019JE006313); off by default. `share` of them follow the rays of
42 /// `layout` with a Gaussian across-profile, clumped by noise in log range; none are added. Give a
43 /// crater albedo layer the same layout for bright rays over the secondaries.
44 67 struct SecondaryRays {
45 bool enabled = false; ///< Whether secondaries follow rays.
46 RayLayout layout{}; ///< The rays.
47 double share = 0.7; ///< Share of secondaries in rays, in [0, 1].
48 double minHalfAngleDegrees = 2.0; ///< Narrowest half width of a ray, in (0, 90) degrees.
49 double clustering = 0.8; ///< How strongly they clump along a ray, in [0, 1].
50 double clusterSpacing = 0.15; ///< Wavelength of the clumps in ln range; positive.
51
52 /// Memberwise equality.
53 1 bool operator==(const SecondaryRays& other) const {
54
2/4
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 return enabled == other.enabled && layout == other.layout && share == other.share &&
55
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 minHalfAngleDegrees == other.minHalfAngleDegrees && clustering == other.clustering &&
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 clusterSpacing == other.clusterSpacing;
57 }
58 bool operator!=(const SecondaryRays& other) const {
59 return !(*this == other);
60 }
61 };
62
63 /// Secondaries stretched toward their primary, elliptical along the line back to it and rounder
64 /// with range, as distant secondaries are (Singer et al. 2020); round by default. The ellipticity
65 /// falls linearly to 1 at the outer range, which is illustrative.
66
2/3
<unknown function 44>:
✓ Branch 4 taken 47 times.
<unknown function 66>:
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
178 struct SecondaryShapes {
67 double elongatedShare = 0.0; ///< Share of secondaries that are elliptical, in [0, 1].
68 /// Long over short axis near the primary; at least 1 and bounded.
69 ValueDistribution ellipticity{ValueDistribution::Uniform(1.2, 1.8)};
70
71 /// Memberwise equality.
72 1 bool operator==(const SecondaryShapes& other) const {
73 1 return elongatedShare == other.elongatedShare && ellipticity == other.ellipticity;
74 }
75 bool operator!=(const SecondaryShapes& other) const {
76 return !(*this == other);
77 }
78 };
79
80 /// Which primaries throw secondaries, how many, and over what sizes.
81 ///
82 /// At range R from a primary of diameter D, secondaries at least d across number
83 /// n(R) (d / (`countFraction` D))^-`sizeSlope` per m^2, below `SingerMaxSecondaryDiameter` and
84 /// between `SecondaryOnsetRange` and the outer range.
85 struct SecondaryCraterOptions {
86 std::uint32_t seed = 0; ///< Varies the secondaries.
87 /// Secondaries at least `countFraction` D across in an area the size of the primary at the
88 /// onset; required, as the sources fix sizes but not numbers.
89 std::optional<double> onsetCount;
90 double countFraction = 0.01; ///< Reference size of `onsetCount`, over D.
91 double sizeSlope = 4.0; ///< Cumulative size slope, positive.
92 double minDiameter = 1.0; ///< Smallest secondary generated (m).
93 double maxDiameter = std::numeric_limits<double>::infinity(); ///< Largest (m), exclusive.
94 /// Outer range in primary diameters from the center; unset uses 1.6 times Elliott et al.'s
95 /// median ray length (`ElliottMedianRayLength`).
96 std::optional<double> outerRange;
97 /// The primaries that throw secondaries.
98 CraterSelection selection{};
99 /// Polar cell side as a multiple of each size band's largest diameter.
100 double cellSizeFactor = 16.0;
101 /// Most secondaries expected over one region, to catch requests that would exhaust memory.
102 double maxExpected = 5e7;
103 SecondaryRays rays{}; ///< Rays and clusters; off by default.
104 SecondaryShapes shapes{}; ///< Elongation toward the primary; round by default.
105 /// Source cells across from which the source's measured data already holds a secondary: with
106 /// a source given to the layer, secondaries at least that many of its cells across where they
107 /// land are left out (`SourceResolvedLimit`). Unset, or without a source, keeps every one.
108 std::optional<double> sourceResolvesPixels = 4.0;
109
110 /// Memberwise equality.
111 2 bool operator==(const SecondaryCraterOptions& other) const {
112
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return seed == other.seed && onsetCount == other.onsetCount &&
113
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 countFraction == other.countFraction && sizeSlope == other.sizeSlope &&
114
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 minDiameter == other.minDiameter && maxDiameter == other.maxDiameter &&
115
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 outerRange == other.outerRange && selection == other.selection &&
116
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 cellSizeFactor == other.cellSizeFactor && maxExpected == other.maxExpected &&
117
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
5 rays == other.rays && shapes == other.shapes &&
118
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 sourceResolvesPixels == other.sourceResolvesPixels;
119 }
120 bool operator!=(const SecondaryCraterOptions& other) const {
121 return !(*this == other);
122 }
123 };
124
125 /// Checks secondary crater options.
126 /// @throws std::invalid_argument without an `onsetCount`, for a field outside its range
127 /// (`CheckRanges`), a `maxDiameter` not above `minDiameter`, or an invalid selection, ray layout
128 /// or ellipticity.
129 void ValidateSecondaryCraterOptions(const SecondaryCraterOptions& options);
130
131 /// The outer range from its center, in meters, of the secondaries of a primary of `diameter`
132 /// meters.
133 double SecondaryOuterRange(double diameter, const SecondaryCraterOptions& options);
134
135 /// The largest secondary a primary of `diameter` meters throws under `options`, in meters: the
136 /// larger of `SingerMaxSecondaryDiameter` at the onset and the outer range, at most `maxDiameter`;
137 /// 0 when its outer range does not pass its onset.
138 double LargestSecondary(double diameter, const SecondaryCraterOptions& options);
139
140 /// Where a primary's secondaries gather: its rays and the clumps along them, as a density over
141 /// the even spread's.
142
1/1
✓ Branch 2 taken 1033 times.
4130 struct SecondaryRayField {
143 std::vector<RayDirection> rays; ///< The primary's rays.
144 double diameter = 0.0; ///< The primary's, in meters.
145 double onset = 0.0; ///< `SecondaryOnsetRange`, in meters.
146 double share = 0.0; ///< `SecondaryRays::share`.
147 double sinFloor = 0.0; ///< Sine of `SecondaryRays::minHalfAngleDegrees`.
148 double clustering = 0.0; ///< `SecondaryRays::clustering`.
149 double spacing = 1.0; ///< `SecondaryRays::clusterSpacing`.
150 double meanClump = 1.0; ///< The mean clump bump, which the clumping is divided by.
151
152 /// The rays of `primary`'s secondaries under `rays`; none when they are off or the primary
153 /// has no rays.
154 static std::optional<SecondaryRayField> Make(const Crater& primary, const SecondaryRays& rays);
155
156 /// The across-ray half width of `ray` at `range` meters.
157 double Width(const RayDirection& ray, double range) const;
158
159 /// The across-ray profile of `ray` at `range` meters and `azimuth`, 1 on its center line.
160 double Profile(const RayDirection& ray, double range, double azimuth) const;
161
162 /// The clumping along `ray` at `range` meters, 1 on average.
163 double Clump(const RayDirection& ray, double range) const;
164
165 /// The mean over azimuth of the summed profiles at `range` meters.
166 double MeanProfile(double range) const;
167
168 /// The density of secondaries at `range` meters and `azimuth` over the even spread's.
169 double Density(double range, double azimuth) const;
170
171 /// An upper bound of `Density` between `r0` and `r1` meters, with a margin.
172 double Peak(double r0, double r1) const;
173 };
174
175 /// The density of the secondaries one primary throws, on average, as `SecondaryCraterLayer` draws
176 /// them: rings of n(R) (d / (`countFraction` D))^-`sizeSlope` below the largest secondary at each
177 /// range, gathered into rays when they are on.
178
1/2
✗ Branch 13 not taken.
✓ Branch 14 taken 1 time.
480225 class SecondaryDensity {
179 public:
180 /// The secondaries `primary` throws under `options`; none when its selection leaves it out.
181 SecondaryDensity(const Crater& primary, const SecondaryCraterOptions& options);
182
183 /// Secondaries per m^2 with diameters in [`minDiameter`, `maxDiameter`) meters at `offset`
184 /// meters east and north of the primary's center, in its local frame.
185 double At(const Vec2d& offset, double minDiameter, double maxDiameter) const;
186
187 /// The share of ground covered by those secondaries, their areas pi d^2 / 4 summed per m^2.
188 double CoverAt(const Vec2d& offset, double minDiameter, double maxDiameter) const;
189
190 /// The farthest a secondary lands from the primary's center, in meters; 0 when it throws none.
191 994547 double Reach() const {
192
10/12
<unknown function 142>:
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 279660 times.
✓ Branch 3 taken 230302 times.
<unknown function 66>:
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 time.
terraingen::SecondaryCraterOptions::operator==(terraingen::SecondaryCraterOptions const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 1130 times.
✓ Branch 2 taken 3229 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 479134 times.
✓ Branch 5 taken 1080 times.
994547 return m_outer;
193 }
194
195 /// Where the secondaries start (`SecondaryOnsetRange`), in meters from the primary's center.
196 11169 double Onset() const {
197
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1130 times.
✓ Branch 2 taken 3229 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6803 times.
11169 return m_onset;
198 }
199
200 /// Secondaries per m^2 at least `Reference` across at the onset.
201 6803 double OnsetDensity() const {
202
2/2
✓ Branch 0 taken 6250 times.
✓ Branch 1 taken 553 times.
6803 return m_density0;
203 }
204
205 /// The size `OnsetDensity` counts from, `countFraction` times the diameter, in meters.
206 3138440 double Reference() const {
207 3138440 return m_reference;
208 }
209
210 /// Where the secondaries gather; none spreads them evenly.
211 1 const std::optional<SecondaryRayField>& Rays() const {
212
4/4
✓ Branch 0 taken 2601140 times.
✓ Branch 1 taken 17658 times.
✓ Branch 3 taken 3117717 times.
✓ Branch 4 taken 20723 times.
5763489 return m_rays;
213 }
214
215 /// Chapman's scaled range 1 + (range - onset) / D at `range` meters, whose inverse square the
216 /// density falls with.
217 3586974 double ScaledRange(double range) const {
218
4/4
✓ Branch 0 taken 2634356 times.
✓ Branch 1 taken 504084 times.
✓ Branch 2 taken 6250 times.
✓ Branch 3 taken 553 times.
3586974 return 1.0 + (range - m_onset) / m_diameter;
219 }
220
221 /// The share of the cumulative count at `Reference` with diameters in [`low`, `high`) meters.
222 double SizeShare(double low, double high) const;
223
224 private:
225 /// The count, or with `cover` the covered share, per m^2 in [`minDiameter`, `maxDiameter`) at
226 /// `offset`.
227 double Measure(const Vec2d& offset, double minDiameter, double maxDiameter, bool cover) const;
228
229 double m_sizeSlope = 4.0;
230 double m_minSize = 0.0;
231 double m_maxSize = 0.0;
232 double m_diameter = 0.0;
233 double m_onset = 0.0;
234 double m_outer = 0.0;
235 double m_density0 = 0.0;
236 double m_reference = 0.0;
237 std::optional<SecondaryRayField> m_rays;
238 };
239
240 } // namespace terraingen
241