./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
270 / 276
97.8%
Branches:
152 / 186
81.7%
Functions:
29 / 29
100.0%

src/albedo/splotches.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/splotches.h"
4
5 #include "albedo/bands.h"
6 #include "terraingen/albedo/albedo_fields.h"
7 #include "terraingen/distribution/power_law.h"
8 #include "terraingen/geometry/cells.h"
9 #include "terraingen/geometry/geometry.h"
10 #include "terraingen/layers/craters/ages/age_model.h"
11 #include "terraingen/layers/craters/generation/population.h"
12 #include "terraingen/layers/ground.h"
13 #include "terraingen/random/samplers.h"
14 #include "terraingen/random/stream.h"
15 #include "terraingen/raster/grid.h"
16 #include "terraingen/util/bits.h"
17 #include "terraingen/util/span.h"
18
19 #include <algorithm>
20 #include <array>
21 #include <cmath>
22 #include <stdexcept>
23 #include <string>
24 #include <utility>
25
26 namespace terraingen {
27
28 namespace {
29
30 constexpr double kBinCells = 16.0;
31
32 /// Checks that dark and bright shares sum to at most 1.
33 66 void CheckShares(double dark, double bright, const char* what) {
34
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 63 times.
66 if (!(dark + bright <= 1.0)) {
35
1/1
✓ Branch 1 taken 3 times.
6 throw std::invalid_argument(std::string(what) +
36
2/2
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
9 " dark and bright shares must sum to at most 1");
37 }
38 63 }
39
40 /// Checks the rules of splotch swarms that tie their fields together.
41 31 void CheckSwarms(const SplotchSwarms& s) {
42
4/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 29 times.
31 if (!(s.maxCraterDiameter > s.minCraterDiameter && s.maxRange > s.minRange)) {
43
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Splotch swarms need increasing crater diameters and ranges");
44 }
45 29 ValidateValueDistribution(s.splotchDiameter);
46
2/4
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
28 if (!(s.splotchDiameter.Lowest() > 0.0 && std::isfinite(s.splotchDiameter.Highest()))) {
47
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Swarm splotch diameters must be positive and bounded");
48 }
49
1/1
✓ Branch 2 taken 27 times.
56 ValidateRelativeChange(s.darkChange, "A dark swarm splotch's change");
50
1/1
✓ Branch 2 taken 27 times.
54 ValidateRelativeChange(s.brightChange, "A bright swarm splotch's change");
51 27 CheckShares(s.darkShare, s.brightShare, "Swarm");
52 26 }
53
54 /// How much of `splotch` covers the point `offset` meters from its center, from 1 inside to 0
55 /// beyond its lobed, soft edge.
56 3333534 double Cover(const Splotch& splotch, const Vec2d& offset, const SplotchAlbedoOptions& options) {
57 3333534 const double radius = 0.5 * splotch.mark.diameter;
58 3333534 double distance = std::hypot(offset.x, offset.y) / radius;
59 3333534 double angle = 0.0;
60
3/4
✓ Branch 0 taken 3333010 times.
✓ Branch 1 taken 524 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3333010 times.
3333534 if (splotch.elongation != 1.0 || splotch.wedge) {
61 524 const double stretch = std::sqrt(splotch.elongation);
62 524 const double c = std::cos(splotch.heading);
63 524 const double s = std::sin(splotch.heading);
64 524 const double along = (offset.x * c + offset.y * s) / (radius * stretch);
65 524 double across = (offset.y * c - offset.x * s) * stretch / radius;
66
1/2
✓ Branch 0 taken 524 times.
✗ Branch 1 not taken.
524 if (splotch.wedge) {
67 524 across /= std::fmax(0.15, 0.5 * (1.0 - along));
68 }
69 524 distance = std::hypot(along, across);
70 524 angle = std::atan2(across, along);
71
1/2
✓ Branch 0 taken 3333276 times.
✗ Branch 1 not taken.
3333534 } else if (options.raggedness > 0.0) {
72 3333276 angle = std::atan2(offset.y, offset.x);
73 }
74
2/2
✓ Branch 0 taken 196144 times.
✓ Branch 1 taken 3137390 times.
3333534 if (distance >= 1.0) {
75 return 0.0;
76 }
77 196144 const double lobe = EdgeLobe(options.raggedness, kLobeRing, angle, 0.0, splotch.key, 3);
78 196200 return SoftEdge(distance / lobe, options.softness);
79 }
80
81 /// The change of `splotch` at `offset` meters from its center, crossing from one side to the
82 /// other of a mixed splotch.
83 168503 double SideChange(const Splotch& splotch, const Vec2d& offset) {
84
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 167927 times.
168503 if (splotch.otherChange == splotch.change) {
85 return splotch.change;
86 }
87 576 const double across =
88 576 (offset.x * std::cos(splotch.split) + offset.y * std::sin(splotch.split)) /
89 576 (0.5 * splotch.mark.diameter);
90 576 const double w = Smoothstep(0.5 + across / 0.6);
91 576 return splotch.change + w * (splotch.otherChange - splotch.change);
92 }
93
94 /// Gives `splotch` its sides from a uniform `kind`: dark below `darkShare`, bright up to
95 /// `darkShare` + `brightShare`, and mixed beyond, dark on one side and bright on the other.
96 157478 void SetSides(Splotch& splotch, double kind, double darkShare, double brightShare, double dark,
97 double bright) {
98 157478 const bool mixed = kind >= darkShare + brightShare;
99
4/4
✓ Branch 0 taken 16172 times.
✓ Branch 1 taken 141306 times.
✓ Branch 2 taken 2688 times.
✓ Branch 3 taken 13484 times.
157478 splotch.change = kind >= darkShare && !mixed ? bright : dark;
100
2/2
✓ Branch 0 taken 154790 times.
✓ Branch 1 taken 2688 times.
157478 splotch.otherChange = mixed ? bright : splotch.change;
101 157478 }
102
103 /// The spacing slopes under splotches are read at: `spacing`, or half the smallest splotch when 0.
104 380 double SlopeSpacing(double spacing, const SplotchAlbedoOptions& options) {
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380 times.
380 return spacing > 0.0 ? spacing : 0.5 * options.minDiameter;
106 }
107
108 /// The slope in degrees from the heights at `GradientPoints` half a splotch `diameter` out; 0
109 /// where any is missing.
110 13690 double SlopeDegrees(Span<const float> heights, double diameter) {
111 13690 const std::optional<Vec2d> gradient = GradientFromHeights(heights, 0.5 * diameter);
112
1/2
✓ Branch 0 taken 13690 times.
✗ Branch 1 not taken.
13690 return gradient ? GradientSlopeDegrees(gradient->x, gradient->y) : 0.0;
113 }
114
115 /// The splotches of a grid's bounds in square bins of `kBinCells` cells, each bin listing those
116 /// that may reach it.
117 24 class SplotchBins {
118 public:
119 12 SplotchBins(const RasterGrid& grid, const std::vector<Splotch>& splotches,
120 const Surface& surface)
121
1/1
✓ Branch 2 taken 12 times.
12 : m_bounds(grid.Bounds()) {
122 12 const double width = m_bounds.maxX - m_bounds.minX;
123 12 const double height = m_bounds.maxY - m_bounds.minY;
124 12 m_bin = kBinCells * std::fmax(width / static_cast<double>(grid.width),
125 12 height / static_cast<double>(grid.height));
126 12 m_columns = static_cast<std::size_t>(std::ceil(width / m_bin));
127 12 m_rows = static_cast<std::size_t>(std::ceil(height / m_bin));
128
1/1
✓ Branch 1 taken 12 times.
12 m_bins.resize(m_columns * m_rows);
129
2/2
✓ Branch 0 taken 9038 times.
✓ Branch 1 taken 12 times.
9050 for (const Splotch& splotch : splotches) {
130
1/1
✓ Branch 1 taken 9038 times.
9038 Add(splotch, surface);
131 }
132 12 }
133
134 /// The splotches that may reach (`x`, `y`).
135 364420 const std::vector<const Splotch*>& Near(double x, double y) const {
136 364420 return m_bins[Index(y, m_bounds.minY, m_rows) * m_columns +
137 364420 Index(x, m_bounds.minX, m_columns)];
138 }
139
140 private:
141 762938 std::size_t Index(double value, double origin, std::size_t count) const {
142 762938 return ClampIndex(std::floor((value - origin) / m_bin), count);
143 }
144
145 9038 void Add(const Splotch& splotch, const Surface& surface) {
146 9038 const Bounds2d box =
147 9038 surface.Expand(PointBounds(splotch.mark.x, splotch.mark.y),
148 9038 0.5 * splotch.mark.diameter * std::sqrt(splotch.elongation));
149
5/6
✓ Branch 0 taken 9035 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 9034 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 9034 times.
✗ Branch 5 not taken.
9038 if (box.maxX < m_bounds.minX || box.minX > m_bounds.maxX || box.maxY < m_bounds.minY ||
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9034 times.
9034 box.minY > m_bounds.maxY) {
151 4 return;
152 }
153 9034 const std::size_t c0 = Index(box.minX, m_bounds.minX, m_columns);
154 9034 const std::size_t c1 = Index(box.maxX, m_bounds.minX, m_columns);
155 9034 const std::size_t r0 = Index(box.minY, m_bounds.minY, m_rows);
156 9034 const std::size_t r1 = Index(box.maxY, m_bounds.minY, m_rows);
157
2/2
✓ Branch 0 taken 10879 times.
✓ Branch 1 taken 9034 times.
19913 for (std::size_t r = r0; r <= r1; ++r) {
158
2/2
✓ Branch 0 taken 13506 times.
✓ Branch 1 taken 10879 times.
24385 for (std::size_t c = c0; c <= c1; ++c) {
159 13506 m_bins[r * m_columns + c].push_back(&splotch);
160 }
161 }
162 }
163
164 Bounds2d m_bounds;
165 double m_bin = 0.0;
166 std::size_t m_columns = 0;
167 std::size_t m_rows = 0;
168 std::vector<std::vector<const Splotch*>> m_bins;
169 };
170
171 } // namespace
172
173 259 double SplotchSwarms::Coefficient(double lifetimeMyr) const {
174 259 return craterRate * std::pow(10.0, craterSlope) * lifetimeMyr;
175 }
176
177 9722 double SplotchSwarms::Count(double diameter) const {
178 9722 return referenceCount * std::pow(diameter / referenceDiameter, 3.0 + 1.5 * mu);
179 }
180
181 848 double SplotchAlbedoOptions::Coefficient() const {
182 848 return rateCoefficient * std::pow(1000.0, rateExponent) * lifetimeMyr;
183 }
184
185 63 void ValidateSplotchAlbedoOptions(const SplotchAlbedoOptions& options) {
186 63 CheckRanges(options);
187 41 const double coefficient = options.Coefficient();
188
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 if (!(std::isfinite(coefficient) && coefficient > 0.0)) {
189
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Splotches need a positive, finite formation rate");
190 }
191
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39 times.
41 if (!(options.minDiameter < options.maxDiameter)) {
192
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Splotch diameters must be increasing");
193 }
194 39 CheckShares(options.darkShare, options.brightShare, "Splotch");
195
1/1
✓ Branch 2 taken 35 times.
74 ValidateRelativeChange(options.darkChange, "A dark splotch's change");
196
1/1
✓ Branch 2 taken 33 times.
68 ValidateRelativeChange(options.brightChange, "A bright splotch's change");
197 33 ValidateResolutionFade(options.fade);
198 31 CheckSwarms(options.swarms);
199 26 }
200
201 35 SplotchAlbedo::SplotchAlbedo(SplotchAlbedoOptions options) : m_options(std::move(options)) {
202
1/1
✓ Branch 1 taken 23 times.
35 ValidateSplotchAlbedoOptions(m_options);
203 35 }
204
205 804 std::vector<Splotch> SplotchAlbedo::Marks(const Bounds2d& region, double spacing,
206 const Surface& surface) const {
207 804 const SplotchAlbedoOptions& o = m_options;
208 804 CraterGenerationOptions generation;
209 804 generation.minDiameter = o.minDiameter;
210 804 generation.maxDiameter = o.maxDiameter;
211 804 generation.marginRadii = 1.0;
212 804 generation.surface = surface;
213
1/2
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
804 if (spacing > 0.0) {
214 804 generation.minResolvedDiameter = o.fade.startSamples * spacing;
215 }
216
1/1
✓ Branch 2 taken 804 times.
804 const PowerLawSfd density(o.Coefficient(), o.rateExponent);
217
1/1
✓ Branch 1 taken 804 times.
804 const UniformAges ages(1e-3 * o.lifetimeMyr);
218 804 const CraterList marks =
219
2/2
✓ Branch 1 taken 804 times.
✓ Branch 4 taken 804 times.
804 GenerateCraters(*MakeCells(surface), region, density, ages, o.seed, generation);
220
1/1
✓ Branch 1 taken 804 times.
804 std::vector<Splotch> splotches;
221
1/1
✓ Branch 1 taken 804 times.
804 splotches.reserve(marks.Size());
222
2/2
✓ Branch 0 taken 60275 times.
✓ Branch 1 taken 804 times.
61079 for (std::size_t n = 0; n < marks.Size(); ++n) {
223
4/4
✓ Branch 1 taken 60275 times.
✓ Branch 4 taken 60275 times.
✓ Branch 6 taken 52471 times.
✓ Branch 7 taken 7804 times.
60275 if (ResolutionWeight(marks.At(n).diameter, spacing, o.fade) > 0.0) {
224
2/2
✓ Branch 1 taken 52471 times.
✓ Branch 4 taken 52471 times.
52471 splotches.push_back({marks.At(n)});
225 }
226 }
227 804 return splotches;
228 804 }
229
230 804 std::vector<double> SplotchAlbedo::SteepSlopes(const std::vector<Splotch>& splotches,
231 double spacing, const Ground* ground,
232 const Surface& surface) const {
233
2/2
✓ Branch 1 taken 381 times.
✓ Branch 2 taken 423 times.
804 std::vector<double> slopes(splotches.size(), 0.0);
234
4/4
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 423 times.
✓ Branch 2 taken 380 times.
✓ Branch 3 taken 1 time.
804 if (ground == nullptr || !m_options.steepBrightShare) {
235 return slopes;
236 }
237
1/1
✓ Branch 1 taken 380 times.
380 std::vector<Vec2d> points;
238
1/1
✓ Branch 1 taken 380 times.
380 points.reserve(4 * splotches.size());
239
2/2
✓ Branch 0 taken 13690 times.
✓ Branch 1 taken 380 times.
14070 for (const Splotch& splotch : splotches) {
240 13690 const auto four =
241
1/1
✓ Branch 1 taken 13690 times.
13690 GradientPoints(surface, {splotch.mark.x, splotch.mark.y}, 0.5 * splotch.mark.diameter);
242
1/1
✓ Branch 1 taken 13690 times.
13690 points.insert(points.end(), four.begin(), four.end());
243 }
244
1/1
✓ Branch 1 taken 380 times.
380 std::vector<float> heights(points.size());
245
1/1
✓ Branch 3 taken 380 times.
380 ground->HeightsAt(points, heights, SlopeSpacing(spacing, m_options));
246
2/2
✓ Branch 0 taken 13690 times.
✓ Branch 1 taken 380 times.
14070 for (std::size_t n = 0; n < splotches.size(); ++n) {
247
1/1
✓ Branch 5 taken 13690 times.
13690 slopes[n] = SlopeDegrees(Span(heights).subspan(4 * n).first(4), splotches[n].mark.diameter);
248 }
249 380 return slopes;
250 380 }
251
252 52471 void SplotchAlbedo::DrawSides(Splotch& splotch, double slopeDegrees) const {
253 52471 const SplotchAlbedoOptions& o = m_options;
254 52471 double darkShare = o.darkShare;
255 52471 double brightShare = o.brightShare;
256
4/4
✓ Branch 0 taken 19904 times.
✓ Branch 1 taken 32567 times.
✓ Branch 2 taken 7476 times.
✓ Branch 3 taken 12428 times.
52471 if (o.steepBrightShare && slopeDegrees > o.steepSlopeDegrees) {
257 7476 const double mixed = 1.0 - darkShare - brightShare;
258 7476 brightShare = *o.steepBrightShare;
259 7476 darkShare = std::fmax(0.0, 1.0 - mixed - brightShare);
260 }
261 52471 const IdWords id = SplitId(splotch.mark.id);
262 52471 RandomStream random(o.seed, kSplotchStream, id.low, id.high);
263 52471 const double kind = random.NextDouble();
264 52471 const double d1 = random.NextDouble();
265 52471 const double d2 = random.NextDouble();
266 52471 const double b1 = random.NextDouble();
267 52471 const double b2 = random.NextDouble();
268 52471 splotch.split = 2.0 * kPi * random.NextDouble();
269 52471 splotch.key = random.NextU32();
270 52471 SetSides(splotch, kind, darkShare, brightShare, o.darkChange.Sample(d1, d2),
271 o.brightChange.Sample(b1, b2));
272 52471 }
273
274 804 std::vector<Splotch> SplotchAlbedo::Splotches(const Bounds2d& region, double spacing,
275 const Ground* ground, const Surface& surface) const {
276 804 std::vector<Splotch> splotches = Marks(region, spacing, surface);
277
1/1
✓ Branch 1 taken 804 times.
804 const std::vector<double> slopes = SteepSlopes(splotches, spacing, ground, surface);
278
2/2
✓ Branch 0 taken 52471 times.
✓ Branch 1 taken 804 times.
53275 for (std::size_t n = 0; n < splotches.size(); ++n) {
279
1/1
✓ Branch 1 taken 52471 times.
52471 DrawSides(splotches[n], slopes[n]);
280 }
281
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 546 times.
804 if (m_options.swarms.enabled) {
282
1/1
✓ Branch 1 taken 258 times.
258 AddSwarms(region, spacing, surface, splotches);
283 }
284 804 std::sort(splotches.begin(), splotches.end(), [](const Splotch& a, const Splotch& b) {
285
11/24
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 191007 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 293585 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 259243 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5403 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2695 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1392 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 2708 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1380 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 683 times.
✓ Branch 23 taken 2 times.
758110 return a.mark.ageGyr != b.mark.ageGyr ? a.mark.ageGyr < b.mark.ageGyr
286 14 : a.mark.id < b.mark.id;
287 });
288 804 return splotches;
289 804 }
290
291 258 void SplotchAlbedo::AddSwarms(const Bounds2d& region, double spacing, const Surface& surface,
292 std::vector<Splotch>& splotches) const {
293 258 const SplotchAlbedoOptions& o = m_options;
294 258 const SplotchSwarms& s = o.swarms;
295 258 const double margin = 0.5 * s.splotchDiameter.Highest() * std::sqrt(s.wedgeElongation);
296 258 CraterGenerationOptions generation;
297 258 generation.minDiameter = s.minCraterDiameter;
298 258 generation.maxDiameter = s.maxCraterDiameter;
299 258 generation.surface = surface;
300
1/1
✓ Branch 2 taken 258 times.
258 const PowerLawSfd density(s.Coefficient(o.lifetimeMyr), s.craterSlope);
301
1/1
✓ Branch 1 taken 258 times.
258 const UniformAges ages(1e-3 * o.lifetimeMyr);
302 258 const CraterList craters =
303
1/1
✓ Branch 1 taken 258 times.
258 GenerateCraters(*MakeCells(surface), surface.Expand(region, s.maxRange + margin), density,
304
2/2
✓ Branch 1 taken 258 times.
✓ Branch 4 taken 258 times.
516 ages, SubstreamSeed(o.seed, kSwarmCraterStream), generation);
305
1/1
✓ Branch 1 taken 258 times.
258 const Bounds2d wanted = surface.Expand(region, margin);
306
1/1
✓ Branch 1 taken 258 times.
258 const Bounds2d reach = surface.Expand(region, s.maxRange + margin);
307 9956 const auto addSwarm = [&](const Crater& crater) {
308
2/2
✓ Branch 1 taken 33023 times.
✓ Branch 2 taken 9698 times.
42721 for (const Splotch& splotch : Swarm(crater, spacing, surface)) {
309
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 32946 times.
33023 if (surface.Contains(wanted, {splotch.mark.x, splotch.mark.y})) {
310
1/1
✓ Branch 1 taken 77 times.
77 splotches.push_back(splotch);
311 }
312 9698 }
313 9698 };
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258 times.
258 for (Crater crater : s.craters) {
315
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (crater.id == 0) {
316
0/1
✗ Branch 1 not taken.
✗ crater.id = ManualCraterId(crater.x, crater.y, crater.diameter);
317 }
318
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (surface.Contains(reach, {crater.x, crater.y})) {
319
0/1
✗ Branch 1 not taken.
✗ addSwarm(crater);
320 }
321 }
322
2/2
✓ Branch 0 taken 9698 times.
✓ Branch 1 taken 258 times.
9956 for (std::size_t n = 0; n < craters.Size(); ++n) {
323
2/2
✓ Branch 1 taken 9698 times.
✓ Branch 4 taken 9698 times.
9698 addSwarm(craters.At(n));
324 }
325 258 }
326
327 105007 Splotch SplotchAlbedo::DrawSwarmSplotch(RandomStream& random, const Crater& crater,
328 std::uint64_t index, const Surface& surface) const {
329 105007 const SplotchSwarms& s = m_options.swarms;
330 105007 const double a = 1.5 * s.mu;
331 105007 const double near = std::pow(s.minRange, -a);
332 105007 const double far = std::pow(s.maxRange, -a);
333 105007 const double range = std::pow(near - random.NextDouble() * (near - far), -1.0 / a);
334 105007 const double azimuth = 2.0 * kPi * random.NextDouble();
335 105007 const double s1 = random.NextDouble();
336 105007 const double s2 = random.NextDouble();
337 105007 const double kind = random.NextDouble();
338 105007 const double d1 = random.NextDouble();
339 105007 const double d2 = random.NextDouble();
340 105007 const double b1 = random.NextDouble();
341 105007 const double b2 = random.NextDouble();
342 105007 Splotch splotch;
343 105007 splotch.split = 2.0 * kPi * random.NextDouble();
344 105007 splotch.key = random.NextU32();
345 105007 splotch.wedge = random.NextDouble() < s.wedgeShare;
346 210014 const Vec2d at = surface.Locate({crater.x, crater.y},
347 105007 {range * std::cos(azimuth), range * std::sin(azimuth)});
348 105007 splotch.mark = {at.x, at.y, s.splotchDiameter.Sample(s1, s2), crater.ageGyr,
349 105007 crater.id ^ ((index + 1) * 0x9E3779B97F4A7C15ull)};
350 105007 SetSides(splotch, kind, s.darkShare, s.brightShare, s.darkChange.Sample(d1, d2),
351 s.brightChange.Sample(b1, b2));
352
2/2
✓ Branch 0 taken 20838 times.
✓ Branch 1 taken 84169 times.
105007 if (splotch.wedge) {
353 20838 const Vec2d back = surface.Offset(at, {crater.x, crater.y});
354 20838 splotch.elongation = s.wedgeElongation;
355 20838 splotch.heading = std::atan2(back.y, back.x);
356 }
357 105007 return splotch;
358 }
359
360 9719 std::vector<Splotch> SplotchAlbedo::Swarm(const Crater& crater, double spacing,
361 const Surface& surface) const {
362 9719 const IdWords id = SplitId(crater.id);
363 9719 RandomStream random(m_options.seed, kSplotchSwarmStream, id.low, id.high);
364 9719 const std::uint64_t count = SamplePoisson(random, m_options.swarms.Count(crater.diameter));
365 9719 std::vector<Splotch> splotches;
366
2/2
✓ Branch 0 taken 105007 times.
✓ Branch 1 taken 9719 times.
114726 for (std::uint64_t i = 0; i < count; ++i) {
367
1/1
✓ Branch 1 taken 105007 times.
105007 const Splotch splotch = DrawSwarmSplotch(random, crater, i, surface);
368
3/3
✓ Branch 1 taken 105007 times.
✓ Branch 3 taken 38190 times.
✓ Branch 4 taken 66817 times.
105007 if (ResolutionWeight(splotch.mark.diameter, spacing, m_options.fade) > 0.0) {
369
1/1
✓ Branch 1 taken 38190 times.
38190 splotches.push_back(splotch);
370 }
371 }
372 9719 return splotches;
373 }
374
375 11 double SplotchAlbedo::Change(const Splotch& splotch, double x, double y, double spacing,
376 const Surface& surface) const {
377
1/1
✓ Branch 2 taken 11 times.
11 return ChangeAt({&splotch}, x, y, spacing, surface);
378 }
379
380 365234 double SplotchAlbedo::ChangeAt(const std::vector<const Splotch*>& splotches, double x, double y,
381 double spacing, const Surface& surface) const {
382 365234 ChangeOverlay overlay;
383
2/2
✓ Branch 0 taken 3335094 times.
✓ Branch 1 taken 364997 times.
3700091 for (const Splotch* splotch : splotches) {
384 3335094 const Vec2d offset = surface.Offset({splotch->mark.x, splotch->mark.y}, {x, y});
385 3334179 const double cover = Cover(*splotch, offset, m_options);
386
2/2
✓ Branch 0 taken 3166357 times.
✓ Branch 1 taken 168496 times.
3334853 if (cover == 0.0) {
387 3166357 continue;
388 }
389 168496 const double left = LifeLeft(splotch->mark.ageGyr, m_options.lifetimeMyr);
390 168500 overlay.Add(std::fmax(0.0, left) * cover,
391 168496 ResolutionWeight(splotch->mark.diameter, spacing, m_options.fade) *
392 168504 SideChange(*splotch, offset));
393 }
394 364997 return overlay.Change();
395 }
396
397 779 void SplotchAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y,
398 const LayerContext& context) const {
399 779 const Surface& surface = context.SurfaceOrPlane();
400 779 const std::vector<Splotch> splotches =
401 779 Splotches(PointBounds(x, y), context.spacing, context.ground, surface);
402
1/1
✓ Branch 1 taken 779 times.
779 std::vector<const Splotch*> pointers;
403
1/1
✓ Branch 1 taken 779 times.
779 pointers.reserve(splotches.size());
404
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 779 times.
1525 for (const Splotch& splotch : splotches) {
405
1/1
✓ Branch 1 taken 746 times.
746 pointers.push_back(&splotch);
406 }
407
2/2
✓ Branch 1 taken 779 times.
✓ Branch 4 taken 779 times.
779 ChangeBands(value, bands, ChangeAt(pointers, x, y, context.spacing, surface));
408 779 }
409
410 13 void SplotchAlbedo::Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const {
411
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 12 times.
13 if (bands.empty()) {
412 1 return;
413 }
414 12 const RasterGrid grid = bands.front().Grid();
415 12 const Surface& surface = context.SurfaceOrPlane();
416 12 const std::vector<Splotch> splotches =
417 12 Splotches(grid.Bounds(), context.spacing, context.ground, surface);
418
1/1
✓ Branch 1 taken 12 times.
12 const SplotchBins bins(grid, splotches, surface);
419
1/1
✓ Branch 1 taken 12 times.
12 const std::size_t count = bands.size();
420 12 const double spacing = context.spacing;
421
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
24 ForEachAlbedoCell(bands, context.threads, [&](AlbedoSample& value, double x, double y) {
422 364407 ChangeBands(value, count, ChangeAt(bins.Near(x, y), x, y, spacing, surface));
423 364383 });
424 12 }
425
426 } // namespace terraingen
427