./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
102 / 104
98.1%
Branches:
117 / 140
83.6%
Functions:
12 / 12
100.0%

src/layers/boulders/boulder_abundance.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/boulders/boulder_abundance.h"
4
5 #include "layers/boulders/boulder_clusters.h"
6 #include "terraingen/distribution/golombek_rapp.h"
7 #include "terraingen/distribution/power_law.h"
8 #include "terraingen/distribution/summed.h"
9
10 #include <algorithm>
11 #include <cmath>
12 #include <stdexcept>
13 #include <utility>
14
15 namespace terraingen {
16
17 namespace {
18
19 /// Coverages the Golombek-Rapp measures are tabulated at, log-spaced.
20 constexpr int kCoverageSteps = 512;
21 constexpr double kLeastCoverage = 1e-6;
22 constexpr double kMostCoverage = 0.999;
23
24 /// What `map` measures of `distribution`, with the areal fraction of Golombek-Rapp boulders in
25 /// closed form so that it needs no largest diameter.
26 1542 double BoulderMeasure(const DensityMap& map, const SizeFrequencyDistribution& distribution) {
27 1542 const auto* rapp = dynamic_cast<const GolombekRappSfd*>(&distribution);
28
4/4
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1024 times.
✓ Branch 3 taken 512 times.
1542 if (rapp != nullptr && map.measure == DensityMeasure::ArealFraction) {
29 1024 const double beyond =
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1024 times.
1024 std::isinf(map.maxDiameter) ? 0.0 : rapp->FractionalArea(map.maxDiameter);
31 1024 return rapp->FractionalArea(map.minDiameter) - beyond;
32 }
33 518 return MapMeasure(map, distribution);
34 }
35
36 /// The largest background abundance with patches averaging `patchMean` and a density map whose
37 /// largest abundance is `mapMax`, if there is a map.
38 745 double MaxBackground(const BoulderLayerOptions& options, double patchMean,
39 std::optional<double> mapMax) {
40 745 const double a = options.bedrock.abundance;
41 745 double patches = options.patches.spread;
42
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 715 times.
745 double bedrock = options.bedrock.exposure ? std::fmax(1.0, a) : 1.0;
43 745 double map = 1.0;
44
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 733 times.
745 if (mapMax) {
45 12 patches /= patchMean;
46
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 bedrock = options.bedrock.exposure ? std::fmax(1.0, a) / std::fmin(1.0, a) : 1.0;
47 12 map = *mapMax;
48 }
49 745 return patches * bedrock * MaxClusterAbundance(options.clusters) * map;
50 }
51
52 } // namespace
53
54
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 17 times.
18 BoulderMapAbundance::BoulderMapAbundance(const BoulderLayerOptions& options) {
55
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 17 times.
18 if (!options.densityMap) {
56
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder map abundance needs a density map");
57 }
58 17 m_map = *options.densityMap;
59
1/1
✓ Branch 1 taken 17 times.
17 ValidateDensityMap(m_map);
60 17 const BoulderSizes& sizes = options.sizes;
61
5/5
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 16 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 15 times.
17 if (m_map.total && options.ejecta.ThrowingDiameter()) {
62
3/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
3 m_ejectaMeasure = BoulderMeasure(m_map, *EjectaSizes(options));
63 }
64
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
17 if (sizes.distribution || m_map.measure == DensityMeasure::Scale) {
65
3/3
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
✓ Branch 3 taken 5 times.
14 m_measure = sizes.distribution ? BoulderMeasure(m_map, *sizes.distribution) : 1.0;
66
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (!(m_measure > 0.0 && std::isfinite(m_measure))) {
67
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("A boulder density map must measure some of the sizes");
68 }
69 } else {
70
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 m_coverage = sizes.Coverage();
71
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 3 times.
1539 for (int i = 0; i < kCoverageSteps; ++i) {
72 1536 const double k =
73 1536 kLeastCoverage * std::pow(kMostCoverage / kLeastCoverage,
74 1536 static_cast<double>(i) / (kCoverageSteps - 1));
75
1/1
✓ Branch 1 taken 1536 times.
1536 m_coverages.push_back(k);
76
2/2
✓ Branch 1 taken 1536 times.
✓ Branch 4 taken 1536 times.
1536 m_measures.push_back(BoulderMeasure(
77
3/5
✗ Branch 0 not taken.
✓ Branch 1 taken 1536 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1536 times.
✓ Branch 5 taken 1536 times.
3072 m_map, GolombekRappSfd(k, sizes.QConstant(), sizes.QCoverageCoefficient())));
78 }
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!(m_measures.back() > 0.0)) {
80
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("A boulder density map must measure some of the sizes");
81 }
82 }
83
1/1
✓ Branch 1 taken 17 times.
17 const double top = m_map.field->Max();
84
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 14 times.
17 const double largest = m_map.measure == DensityMeasure::Count ? top / m_map.areaUnit : top;
85 17 m_max = std::fmax(1.0, At(largest, 0.0));
86
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
20 }
87
88 14 double BoulderMapAbundance::Coverage(double target) const {
89
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (target <= m_measures.front()) {
90 2 return m_coverages.front() * target / m_measures.front();
91 }
92
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 11 times.
12 if (target >= m_measures.back()) {
93 1 return m_coverages.back();
94 }
95 11 const auto above = std::lower_bound(m_measures.begin(), m_measures.end(), target);
96 11 const auto i = static_cast<std::size_t>(above - m_measures.begin());
97 11 const double t = (target - m_measures[i - 1]) / (m_measures[i] - m_measures[i - 1]);
98 11 return m_coverages[i - 1] + t * (m_coverages[i] - m_coverages[i - 1]);
99 }
100
101 1080973 double BoulderMapAbundance::At(double value, double ejecta) const {
102
2/2
✓ Branch 0 taken 1050972 times.
✓ Branch 1 taken 30001 times.
1080973 if (std::isnan(value)) {
103 return 1.0;
104 }
105
2/2
✓ Branch 0 taken 1050952 times.
✓ Branch 1 taken 20 times.
1050972 if (m_map.measure == DensityMeasure::Scale) {
106 1050952 return std::fmax(0.0, value);
107 }
108 20 const double target = value - ejecta * m_ejectaMeasure;
109
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 time.
20 if (!(target > 0.0)) {
110 return 0.0;
111 }
112
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 return m_coverages.empty() ? target / m_measure : Coverage(target) / m_coverage;
113 }
114
115 15 double MeanPatchAbundance(const BoulderPatches& patches) {
116
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
15 if (!(patches.spread > 1.0)) {
117 return 1.0;
118 }
119 4 return LatticeMean(32.0, [&](double x, double y) {
120
2/3
✓ Branch 1 taken 262144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 262144 times.
262144 return std::pow(patches.spread, PatchNoise({}, x, y, 1.0, kBoulderPatchStream));
121 });
122 }
123
124
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 733 times.
745 BoulderBackground::BoulderBackground(const BoulderLayerOptions& options) {
125
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 733 times.
745 if (options.densityMap) {
126
1/1
✓ Branch 1 taken 12 times.
12 patchMean = MeanPatchAbundance(options.patches);
127
1/1
✓ Branch 1 taken 12 times.
12 map.emplace(options);
128 }
129
3/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 733 times.
✓ Branch 3 taken 745 times.
745 max = MaxBackground(options, patchMean, map ? std::optional<double>(map->Max()) : std::nullopt);
130 745 }
131
132 443 double MaxBackgroundAbundance(const BoulderLayerOptions& options) {
133 443 return BoulderBackground(options).max;
134 }
135
136 104 std::shared_ptr<const SizeFrequencyDistribution> EjectaSizes(const BoulderLayerOptions& options) {
137 104 const BoulderEjecta& ejecta = options.ejecta;
138
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 62 times.
104 if (ejecta.distribution) {
139
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 return ejecta.distribution;
140 }
141
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 38 times.
86 return std::make_shared<GolombekRappSfd>(ejecta.Coverage(), options.sizes.QConstant(),
142
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
124 options.sizes.QCoverageCoefficient());
143 }
144
145 std::shared_ptr<const SizeFrequencyDistribution>
146 133 BoulderCandidates(const BoulderLayerOptions& options) {
147 133 ValidateBoulderLayerOptions(options);
148
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 130 times.
132 if (!options.generateRandom) {
149 2 throw std::invalid_argument(
150
1/1
✓ Branch 2 taken 2 times.
2 "A boulder layer without generateRandom draws no candidate boulders");
151 }
152 130 return BoulderCandidates(options, MaxBackgroundAbundance(options));
153 }
154
155 std::shared_ptr<const SizeFrequencyDistribution>
156 432 BoulderCandidates(const BoulderLayerOptions& options, double richest) {
157 432 const BoulderSizes& sizes = options.sizes;
158
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 297 times.
432 std::vector<WeightedSfd> terms;
159
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 297 times.
432 if (sizes.distribution) {
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 terms.push_back({richest, sizes.distribution});
161
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 127 times.
135 if (options.slopeMap) {
162
1/1
✓ Branch 1 taken 8 times.
8 const SizeSlopeMap& map = *options.slopeMap;
163 8 const double d0 = map.referenceDiameter;
164
1/1
✓ Branch 1 taken 8 times.
8 const double count = richest * sizes.distribution->Cumulative(d0);
165 8 const double low = map.minExponent;
166 8 const double high = map.maxExponent;
167
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 8 times.
8 terms.push_back(
168
1/1
✓ Branch 1 taken 8 times.
8 {1.0, std::make_shared<PowerLawSfd>(count * std::pow(d0, low) * high / low, low)});
169
2/3
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
8 terms.push_back({1.0, std::make_shared<PowerLawSfd>(count * std::pow(d0, high), high)});
170 }
171 } else {
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 terms.push_back(
173
4/4
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 297 times.
✓ Branch 6 taken 297 times.
564 {1.0, std::make_shared<GolombekRappSfd>(sizes.Coverage() * richest, sizes.QConstant(),
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
594 sizes.QCoverageCoefficient())});
175 }
176
3/3
✓ Branch 1 taken 432 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 372 times.
432 if (options.ejecta.ThrowingDiameter()) {
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 terms.push_back({1.0, EjectaSizes(options)});
178 }
179
1/1
✓ Branch 1 taken 432 times.
432 return std::make_shared<SummedSfd>(std::move(terms));
180
9/11
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 135 times.
✓ Branch 6 taken 8 times.
✓ Branch 9 taken 8 times.
✓ Branch 12 taken 8 times.
✓ Branch 15 taken 8 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 297 times.
✓ Branch 20 taken 297 times.
✓ Branch 23 taken 60 times.
1372 }
181
182 } // namespace terraingen
183