./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
137 / 138
99.3%
Branches:
67 / 74
90.5%
Functions:
22 / 22
100.0%

src/layers/craters/secondary_model.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/secondary_model.h"
4
5 #include "terraingen/geometry/geometry.h"
6 #include "terraingen/layers/craters/crater_fields.h"
7 #include "terraingen/layers/fade.h"
8 #include "terraingen/noise/value.h"
9 #include "terraingen/random/streams.h"
10 #include "util/checks.h"
11
12 #include <algorithm>
13 #include <cmath>
14 #include <limits>
15 #include <stdexcept>
16
17 namespace terraingen {
18
19 namespace {
20
21 /// Singer et al. (2020, Table 3): d = a R^-b, a = kSingerScale D^kSingerSizeExponent, b =
22 /// kSingerRangeExponent ln D, in km.
23 constexpr double kSingerScale = 0.022;
24 constexpr double kSingerSizeExponent = 1.96;
25 constexpr double kSingerRangeExponent = 0.14;
26
27 /// The outer range of rays as a multiple of their median length (Elliott et al. 2018).
28 constexpr double kLongestRayOverMedian = 1.6;
29
30 /// How strongly secondaries clump at `u` clump wavelengths along a ray keyed `key`: 0 between
31 /// clumps, 1 in them.
32 29867923 double ClumpBump(double u, std::uint32_t key) {
33 29867923 const double n = static_cast<double>(ValueNoise2(static_cast<float>(u), 0.5f, key));
34
2/2
✓ Branch 0 taken 6270302 times.
✓ Branch 1 taken 23597621 times.
59735846 return Smoothstep(std::clamp(0.5 + 1.5 * n, 0.0, 1.0));
35 }
36
37 /// The mean of `ClumpBump` along a ray, found once.
38 1032 double MeanClumpBump() {
39 6 static const double mean = [] {
40 6 constexpr int kSamples = 1 << 16;
41 6 double sum = 0.0;
42
2/2
✓ Branch 0 taken 393216 times.
✓ Branch 1 taken 6 times.
393222 for (int i = 0; i < kSamples; ++i) {
43 393216 sum += ClumpBump(0.0371 * i, kSecondaryClumpStream);
44 }
45 6 return sum / kSamples;
46
4/5
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1026 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6 times.
1032 }();
47 1032 return mean;
48 }
49
50 } // namespace
51
52 3080533 double SingerMaxSecondaryDiameter(double diameter, double range) {
53 3080533 const double d = diameter / 1000.0;
54 3080533 const double r = range / 1000.0;
55 3080533 return 1000.0 * kSingerScale * std::pow(d, kSingerSizeExponent) *
56 3080533 std::pow(r, -kSingerRangeExponent * std::log(d));
57 }
58
59 3241 double SingerMaxRange(double diameter, double size) {
60 3241 const double d = diameter / 1000.0;
61 3241 const double logD = std::log(d);
62
2/2
✓ Branch 0 taken 3239 times.
✓ Branch 1 taken 2 times.
3241 if (!(logD > 0.0)) {
63 return std::numeric_limits<double>::infinity();
64 }
65 3239 const double a = kSingerScale * std::pow(d, kSingerSizeExponent);
66 3239 return 1000.0 * std::pow(a / (size / 1000.0), 1.0 / (kSingerRangeExponent * logD));
67 }
68
69 483632 double SecondaryOnsetRange(double diameter) {
70 483632 const double small = 3000.0;
71 483632 const double large = 31000.0;
72 483632 double rim = 1.8;
73
2/2
✓ Branch 0 taken 483627 times.
✓ Branch 1 taken 5 times.
483632 if (diameter >= large) {
74 rim = 1.3;
75
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 483624 times.
483627 } else if (diameter > small) {
76 3 const double t = std::log(diameter / small) / std::log(large / small);
77 3 rim = 1.8 + t * (1.3 - 1.8);
78 }
79 483632 return (0.5 + rim) * diameter;
80 }
81
82 961837 double ElliottMedianRayLength(double diameter, double coefficient, double exponent) {
83 961837 return 1000.0 * coefficient * std::pow(0.0005 * diameter, exponent);
84 }
85
86 78 void ValidateSecondaryCraterOptions(const SecondaryCraterOptions& options) {
87
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 77 times.
78 if (!options.onsetCount) {
88 1 throw std::invalid_argument("Secondary craters need an onset count: the sources give their "
89
1/1
✓ Branch 2 taken 1 time.
1 "sizes, not their number");
90 }
91 77 CheckRanges(options);
92
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 63 times.
64 if (!(options.maxDiameter > options.minDiameter)) {
93
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Secondary max_diameter_m must be above min_diameter_m");
94 }
95 63 ValidateCraterSelection(options.selection, "Primary");
96
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (!IsPositive(options.maxExpected)) {
97
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("The expected secondary limit must be positive");
98 }
99 62 ValidateRayLayout(options.rays.layout);
100 61 const ValueDistribution& ellipticity = options.shapes.ellipticity;
101 61 ValidateValueDistribution(ellipticity);
102
3/4
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 59 times.
✗ Branch 5 not taken.
60 if (!(ellipticity.Lowest() >= 1.0 && std::isfinite(ellipticity.Highest()))) {
103
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Secondary ellipticity must be bounded and at least 1");
104 }
105 59 }
106
107 482599 double SecondaryOuterRange(double diameter, const SecondaryCraterOptions& options) {
108
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482597 times.
482599 if (options.outerRange) {
109 2 return *options.outerRange * diameter;
110 }
111 482597 return kLongestRayOverMedian * ElliottMedianRayLength(diameter);
112 }
113
114 1090 std::optional<SecondaryRayField> SecondaryRayField::Make(const Crater& primary,
115 const SecondaryRays& rays) {
116
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 1033 times.
1090 if (!rays.enabled) {
117 57 return std::nullopt;
118 }
119
1/1
✓ Branch 1 taken 1033 times.
1033 SecondaryRayField field;
120
1/1
✓ Branch 1 taken 1033 times.
2066 field.rays = RayDirections(primary, rays.layout);
121
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1032 times.
1033 if (field.rays.empty()) {
122 1 return std::nullopt;
123 }
124 1032 field.diameter = primary.diameter;
125 1032 field.onset = SecondaryOnsetRange(primary.diameter);
126 1032 field.share = rays.share;
127 1032 field.sinFloor = std::sin(rays.minHalfAngleDegrees * kDegree);
128 1032 field.clustering = rays.clustering;
129 1032 field.spacing = rays.clusterSpacing;
130
1/1
✓ Branch 1 taken 1032 times.
1032 field.meanClump = MeanClumpBump();
131 1032 return field;
132 1033 }
133
134 44566676 double SecondaryRayField::Width(const RayDirection& ray, double range) const {
135 44566676 return std::fmax(diameter * ray.sinHalfWidth, range * sinFloor);
136 }
137
138 31740244 double SecondaryRayField::Profile(const RayDirection& ray, double range, double azimuth) const {
139 31740244 const double delta = std::remainder(azimuth - ray.azimuth, 2.0 * kPi);
140
2/2
✓ Branch 0 taken 12826426 times.
✓ Branch 1 taken 18913818 times.
31740244 if (std::cos(delta) <= 0.0) {
141 return 0.0;
142 }
143 12826426 const double across = range * std::sin(std::fabs(delta)) / Width(ray, range);
144 12826426 return std::exp(-across * across);
145 }
146
147 29474707 double SecondaryRayField::Clump(const RayDirection& ray, double range) const {
148 29474707 const double u = std::log(range / onset) / spacing;
149 29474707 return (1.0 - clustering) + clustering * ClumpBump(u, ray.key) / meanClump;
150 }
151
152 2897238 double SecondaryRayField::MeanProfile(double range) const {
153 2897238 double sum = 0.0;
154
2/2
✓ Branch 0 taken 31740250 times.
✓ Branch 1 taken 2897238 times.
34637488 for (const RayDirection& ray : rays) {
155 31740250 sum += std::sqrt(kPi) * Width(ray, range);
156 }
157 2897238 return sum / (2.0 * kPi * range);
158 }
159
160 2691140 double SecondaryRayField::Density(double range, double azimuth) const {
161 2691140 double sum = 0.0;
162
2/2
✓ Branch 0 taken 29474707 times.
✓ Branch 1 taken 2691140 times.
32165847 for (const RayDirection& ray : rays) {
163 29474707 sum += Profile(ray, range, azimuth) * Clump(ray, range);
164 }
165 2691140 return (1.0 - share) + share * sum / MeanProfile(range);
166 }
167
168 6250 double SecondaryRayField::Peak(double r0, double r1) const {
169 6250 double peak = 0.0;
170
2/2
✓ Branch 0 taken 18750 times.
✓ Branch 1 taken 6250 times.
25000 for (const double range : {r0, 0.5 * (r0 + r1), r1}) {
171
2/2
✓ Branch 0 taken 206097 times.
✓ Branch 1 taken 18750 times.
224847 for (const RayDirection& at : rays) {
172 double sum = 0.0;
173
2/2
✓ Branch 0 taken 2265537 times.
✓ Branch 1 taken 206097 times.
2471634 for (const RayDirection& ray : rays) {
174 2265537 sum += Profile(ray, range, at.azimuth);
175 }
176 206097 peak = std::fmax(peak, sum / MeanProfile(range));
177 }
178 }
179 6250 const double clump = (1.0 - clustering) + clustering / meanClump;
180 6250 return (1.0 - share) + share * 1.1 * peak * clump;
181 }
182
183 480223 SecondaryDensity::SecondaryDensity(const Crater& primary, const SecondaryCraterOptions& options)
184 480223 : m_sizeSlope(options.sizeSlope), m_minSize(options.minDiameter),
185
1/1
✓ Branch 1 taken 480223 times.
480223 m_maxSize(options.maxDiameter), m_diameter(primary.diameter) {
186
4/5
✓ Branch 1 taken 480223 times.
✓ Branch 3 taken 480219 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 480219 times.
✗ Branch 6 not taken.
480223 if (!options.selection.Includes(primary) || !options.onsetCount) {
187 return;
188 }
189 480219 m_onset = SecondaryOnsetRange(m_diameter);
190 480219 const double outer = SecondaryOuterRange(m_diameter, options);
191
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 479131 times.
480219 if (!(outer > m_onset)) {
192 return;
193 }
194 1088 m_outer = outer;
195
1/1
✓ Branch 1 taken 1088 times.
1088 m_density0 = *options.onsetCount / (0.25 * kPi * m_diameter * m_diameter);
196 1088 m_reference = options.countFraction * m_diameter;
197
1/1
✓ Branch 1 taken 1088 times.
1088 m_rays = SecondaryRayField::Make(primary, options.rays);
198 }
199
200 503668 double SecondaryDensity::Measure(const Vec2d& offset, double minDiameter, double maxDiameter,
201 bool cover) const {
202 503668 const double range = std::hypot(offset.x, offset.y);
203
3/4
✓ Branch 0 taken 441730 times.
✓ Branch 1 taken 61938 times.
✓ Branch 2 taken 441730 times.
✗ Branch 3 not taken.
503668 if (!(range >= m_onset && range < m_outer)) {
204 return 0.0;
205 }
206 441730 const double low = std::fmax(minDiameter, m_minSize);
207 441730 const double high =
208 441730 std::fmin(std::fmin(maxDiameter, m_maxSize), SingerMaxSecondaryDiameter(m_diameter, range));
209
1/2
✓ Branch 0 taken 441730 times.
✗ Branch 1 not taken.
441730 if (!(high > low)) {
210 return 0.0;
211 }
212 441730 const double r = ScaledRange(range);
213 441730 const double s = m_sizeSlope;
214 441730 double sizes = SizeShare(low, high);
215
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 441727 times.
441730 if (cover) {
216 3 const double scale = 0.25 * kPi * s * std::pow(m_reference, s);
217
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 sizes = s == 2.0 ? scale * std::log(high / low)
218 2 : scale * (std::pow(high, 2.0 - s) - std::pow(low, 2.0 - s)) / (2.0 - s);
219 }
220 441730 const double density = m_density0 / (r * r) * sizes;
221
2/2
✓ Branch 0 taken 90000 times.
✓ Branch 1 taken 351730 times.
441730 return m_rays ? density * m_rays->Density(range, std::atan2(offset.y, offset.x)) : density;
222 }
223
224 503665 double SecondaryDensity::At(const Vec2d& offset, double minDiameter, double maxDiameter) const {
225 503665 return Measure(offset, minDiameter, maxDiameter, false);
226 }
227
228 3 double SecondaryDensity::CoverAt(const Vec2d& offset, double minDiameter,
229 double maxDiameter) const {
230 3 return Measure(offset, minDiameter, maxDiameter, true);
231 }
232
233 444961 double SecondaryDensity::SizeShare(double low, double high) const {
234 444961 return std::pow(low / m_reference, -m_sizeSlope) - std::pow(high / m_reference, -m_sizeSlope);
235 }
236
237 1284 double LargestSecondary(double diameter, const SecondaryCraterOptions& options) {
238 1284 const double onset = SecondaryOnsetRange(diameter);
239 1284 const double outer = SecondaryOuterRange(diameter, options);
240
2/2
✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 2 times.
1284 if (!(outer > onset)) {
241 return 0.0;
242 }
243 1282 return std::fmin(options.maxDiameter, std::fmax(SingerMaxSecondaryDiameter(diameter, onset),
244 1282 SingerMaxSecondaryDiameter(diameter, outer)));
245 }
246
247 } // namespace terraingen
248