./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
342 / 342
100.0%
Branches:
257 / 288
89.2%
Functions:
30 / 30
100.0%

src/albedo/crater_albedo.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/crater_albedo.h"
4
5 #include "albedo/bands.h"
6 #include "albedo/zone_shadows.h"
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/layers/craters/secondaries.h"
9 #include "terraingen/layers/craters/shaping/irregularity.h"
10 #include "terraingen/noise/hash.h"
11 #include "terraingen/noise/value.h"
12 #include "terraingen/random/stream.h"
13 #include "terraingen/raster/grid.h"
14 #include "terraingen/util/log.h"
15 #include "util/checks.h"
16
17 #include <algorithm>
18 #include <cmath>
19 #include <limits>
20 #include <optional>
21 #include <stdexcept>
22 #include <utility>
23
24 namespace terraingen {
25
26 namespace {
27
28 constexpr std::size_t kBlockRows = 32;
29
30 /// How far `crater`'s ejecta edge reaches at `angle` radians around it, over its mean reach.
31 2043214 double LobeScale(double angle, std::uint32_t key, const CraterAlbedoTexture& texture) {
32
2/2
✓ Branch 0 taken 2712 times.
✓ Branch 1 taken 2040502 times.
2043214 if (texture.lobes == 0.0) {
33 return 1.0;
34 }
35 2712 const double ring = static_cast<double>(texture.lobeCount) / (2.0 * kPi);
36 2712 const double n = RingNoise(ring, angle, 0.0, key ^ kEjectaLobeStream, 2);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2712 times.
5424 return 1.0 + texture.lobes * std::clamp(n, -1.0, 1.0);
38 }
39
40 /// The share of the excess the filaments leave at `radii` radii and `angle` radians around a
41 /// crater: 1 along a filament, down to 1 - `streaks` between them.
42 140487 double StreakShare(double radii, double angle, std::uint32_t key,
43 const CraterAlbedoTexture& texture) {
44
2/2
✓ Branch 0 taken 3956 times.
✓ Branch 1 taken 136531 times.
140487 if (texture.streaks == 0.0) {
45 return 1.0;
46 }
47 3956 const double ring = kRadian / texture.streakWidthDegrees;
48 3956 const double n = RingNoise(ring, angle, radii / texture.streakLengthRadii, key, 3);
49 3956 const double filament = Smoothstep(0.5 + 1.5 * n);
50 3956 return 1.0 - texture.streaks * (1.0 - filament);
51 }
52
53 /// One reflectance zone of a crater, drawn.
54 struct DrawnZone {
55 double change = 0.0;
56 double strength = 0.0;
57 double resolution = 0.0;
58 double reach = 0.0;
59 double raggedness = 0.0;
60 double ring = 0.0;
61 double feather = 1.0;
62 bool streaked = false;
63 bool distal = false;
64 std::uint32_t key = 0;
65
66 /// How much of the change the zone gives at `distance` meters and `angle` radians from the
67 /// crater's center, from 1 inside to 0 beyond its ragged edge, whose fingers bend and break
68 /// with distance.
69 3869281 double Cover(double distance, double angle) const {
70
2/2
✓ Branch 0 taken 1033229 times.
✓ Branch 1 taken 2836052 times.
3869281 if (distance >= reach) {
71 return 0.0;
72 }
73 1033229 const double finger = EdgeLobe(raggedness, ring, angle, 3.0 * distance / reach, key, 4);
74 1033164 return SoftEdge(distance / (reach * finger), feather);
75 }
76 };
77
78 /// `zone` drawn from the next draws of `random` around a crater `diameter` meters across with
79 /// `life` of its zone life left; nothing when the crater lacks it or it has faded.
80 24220 std::optional<DrawnZone> DrawZone(const ReflectanceZone& zone, RandomStream& random,
81 double diameter, double life, double fadeExponent,
82 double resolution) {
83 24220 const double present = random.NextDouble();
84 24220 const double c1 = random.NextDouble();
85 24220 const double c2 = random.NextDouble();
86 24220 const double s1 = random.NextDouble();
87 24220 const double s2 = random.NextDouble();
88 24220 DrawnZone drawn;
89 24220 drawn.key = random.NextU32();
90
4/4
✓ Branch 0 taken 20371 times.
✓ Branch 1 taken 3849 times.
✓ Branch 2 taken 3069 times.
✓ Branch 3 taken 17302 times.
24220 if (present >= zone.share || life <= 0.0) {
91 6918 return std::nullopt;
92 }
93 17302 drawn.change = zone.change.Sample(c1, c2);
94 17302 drawn.strength = std::pow(life, fadeExponent);
95 17302 drawn.resolution = resolution;
96 17302 drawn.reach = zone.coefficient * std::pow(diameter, zone.exponent) * zone.scale.Sample(s1, s2);
97 17302 drawn.raggedness = zone.raggedness;
98 17302 drawn.ring = kRadian / zone.fingerWidthDegrees;
99 17302 drawn.feather = zone.feather;
100 17302 return drawn;
101 }
102
103 } // namespace
104
105 /// One ray, in the frame of its crater.
106 struct CraterAlbedo::Ray {
107 double cos = 1.0;
108 double sin = 0.0;
109 double length = 0.0;
110 double sinHalfWidth = 0.0;
111 std::uint32_t seed = 0;
112
113 /// The ray's excess at `along` meters from the crater's center along it and `lateral` across,
114 /// for a crater of `radius` meters.
115 45804853 double ExcessAt(double along, double lateral, double radius, const CraterRays& rays) const {
116
4/4
✓ Branch 0 taken 21161034 times.
✓ Branch 1 taken 24643819 times.
✓ Branch 2 taken 8991944 times.
✓ Branch 3 taken 12169090 times.
45804853 if (along <= radius || along >= length) {
117 return 0.0;
118 }
119 8991944 const double half = std::fmin(along, 2.0 * radius) * sinHalfWidth;
120
2/2
✓ Branch 0 taken 69222 times.
✓ Branch 1 taken 8922722 times.
8991944 if (lateral >= half) {
121 return 0.0;
122 }
123 69222 const double q = 1.0 - (lateral / half) * (lateral / half);
124 69222 double strength = rays.excess * q * q * (length - along) / (length - radius);
125
2/2
✓ Branch 0 taken 66992 times.
✓ Branch 1 taken 2230 times.
69222 if (rays.patchiness > 0.0) {
126 66992 const double wavelength = rays.patchRadii * radius;
127 66992 const double u = along / wavelength;
128 66992 const double iu = std::floor(u);
129 66992 const float noise = ValueNoise2(static_cast<std::int32_t>(iu), 0,
130 66992 static_cast<float>(u - iu), 0.5f, seed);
131 66992 strength *= 1.0 - rays.patchiness * (0.5 + 0.5 * static_cast<double>(noise));
132 }
133 return strength;
134 }
135 };
136
137 /// A crater's bright material, ready to evaluate at points.
138 struct CraterAlbedo::Prepared {
139 Crater crater;
140 CraterOutline outline;
141 KernelOutline profile; ///< `outline` as the crater kernel reads it.
142 double weight = 0.0;
143 double reach = 0.0;
144 std::vector<Ray> rays;
145 std::vector<DrawnZone> zones;
146 std::shared_ptr<const detail::ShadowProfile> shadow;
147
148 /// The relative reflectance change of the zones at (`x`, `y`), inner zones covering outer as
149 /// long as they last, the bright proximal zone broken by `texture`'s filaments.
150 1592143 double Change(const CraterAlbedoTexture& texture, const Surface& surface, double x,
151 double y) const {
152
2/2
✓ Branch 0 taken 1289226 times.
✓ Branch 1 taken 302917 times.
1592143 if (zones.empty()) {
153 return 0.0;
154 }
155 1289226 const Vec2d offset = surface.Offset({crater.x, crater.y}, {x, y});
156 1289050 const double distance = std::hypot(offset.x, offset.y);
157 1289050 const double angle = std::atan2(offset.y, offset.x);
158
2/2
✓ Branch 0 taken 1737 times.
✓ Branch 1 taken 1287313 times.
1289050 const double shade = shadow ? shadow->Shade(distance, angle) : 0.0;
159 1289060 ChangeOverlay overlay;
160
2/2
✓ Branch 0 taken 3868643 times.
✓ Branch 1 taken 1289068 times.
5157711 for (const DrawnZone& zone : zones) {
161
2/2
✓ Branch 1 taken 1294013 times.
✓ Branch 2 taken 2574638 times.
3868643 const double cover = zone.Cover(distance, angle) * (zone.distal ? 1.0 - shade : 1.0);
162
2/2
✓ Branch 0 taken 3334024 times.
✓ Branch 1 taken 534627 times.
3868651 if (cover == 0.0) {
163 3334024 continue;
164 }
165 534627 const double streak = zone.streaked
166
2/2
✓ Branch 0 taken 1934 times.
✓ Branch 1 taken 532693 times.
534627 ? StreakShare(2.0 * distance / crater.diameter, angle,
167 1934 CraterKey(crater.id, texture.seed), texture)
168 534627 : 1.0;
169 534627 overlay.Add(cover * zone.strength, zone.resolution * streak * zone.change);
170 }
171 return overlay.Change();
172 }
173
174 /// The excess optical maturity at (`x`, `y`).
175 2057352 double Excess(const CraterAlbedoOptions& o, const Surface& surface, double x, double y) const {
176
1/2
✓ Branch 0 taken 2057476 times.
✗ Branch 1 not taken.
2057352 if (weight == 0.0) {
177 return 0.0;
178 }
179 2057476 const double radii = profile.ProfileDistance(surface.Offset({crater.x, crater.y}, {x, y})) /
180 2057268 (0.5 * crater.diameter);
181 2057268 const CraterAlbedoTexture& texture = o.texture;
182
3/4
✓ Branch 0 taken 2054015 times.
✓ Branch 1 taken 3253 times.
✓ Branch 2 taken 2054034 times.
✗ Branch 3 not taken.
2057268 const bool textured = texture.streaks > 0.0 || texture.lobes > 0.0;
183 2057268 const Vec2d offset = surface.Offset({crater.x, crater.y}, {x, y});
184
2/2
✓ Branch 0 taken 3317 times.
✓ Branch 1 taken 2054192 times.
2057509 const double angle = textured ? std::atan2(offset.y, offset.x) : 0.0;
185 3317 const std::uint32_t key = textured ? CraterKey(crater.id, texture.seed) : 0u;
186
2/2
✓ Branch 0 taken 14443 times.
✓ Branch 1 taken 2043066 times.
2057509 if (radii < 1.0) {
187 14443 return InteriorExcess(o, radii, angle, key);
188 }
189 2043066 const double excess = OutsideExcess(o, offset, radii, angle, key);
190
2/2
✓ Branch 0 taken 124255 times.
✓ Branch 1 taken 1920643 times.
2044898 if (excess == 0.0) {
191 return 0.0;
192 }
193 124255 const double streak = StreakShare(radii, angle, key, texture);
194
2/2
✓ Branch 0 taken 2667 times.
✓ Branch 1 taken 121622 times.
124289 if (texture.continuousRadii > 0.0) {
195 2667 const double ramp = SmoothRamp(radii, 1.0, texture.continuousRadii);
196 2667 return weight * excess * (1.0 - (1.0 - streak) * ramp);
197 }
198 121622 return weight * excess * streak;
199 }
200
201 /// The weighted excess inside the rim, `radii` radii and `angle` radians from the center,
202 /// its filaments keyed by `key`.
203 14442 double InteriorExcess(const CraterAlbedoOptions& o, double radii, double angle,
204 std::uint32_t key) const {
205 14442 const CraterAlbedoTexture& texture = o.texture;
206 14442 double interior = o.interiorExcess;
207 14442 const double wall = SmoothRamp(radii, 0.3, 0.6);
208
2/2
✓ Branch 0 taken 569 times.
✓ Branch 1 taken 13873 times.
14442 if (texture.floorShare < 1.0) {
209 569 interior *= texture.floorShare + (1.0 - texture.floorShare) * wall;
210 }
211 14442 const double streak = StreakShare(radii, angle, key, texture);
212 14443 return weight * interior * (1.0 - (1.0 - streak) * wall);
213 }
214
215 /// The unweighted excess of the ejecta and rays beyond the rim at `offset` meters from the
216 /// center, `radii` radii and `angle` radians, before the filaments.
217 2043214 double OutsideExcess(const CraterAlbedoOptions& o, const Vec2d& offset, double radii,
218 double angle, std::uint32_t key) const {
219 2043214 double excess = 0.0;
220 2043214 const double lobed = radii / LobeScale(angle, key, o.texture);
221
2/2
✓ Branch 0 taken 100739 times.
✓ Branch 1 taken 1942526 times.
2043265 if (lobed < o.ejectaReachRadii) {
222 100739 const double edge = std::pow(o.ejectaReachRadii, -o.ejectaExponent);
223 100739 excess = o.ejectaExcess * (std::pow(std::fmax(lobed, 1.0), -o.ejectaExponent) - edge) /
224 100739 (1.0 - edge);
225 }
226 2043265 const double radius = 0.5 * crater.diameter;
227
2/2
✓ Branch 0 taken 45893822 times.
✓ Branch 1 taken 2043265 times.
47937087 for (const Ray& ray : rays) {
228 45893822 const double along = offset.x * ray.cos + offset.y * ray.sin;
229 45893822 const double lateral = std::fabs(offset.y * ray.cos - offset.x * ray.sin);
230 45893822 excess += ray.ExcessAt(along, lateral, radius, o.rays);
231 }
232 2043265 return excess;
233 }
234 };
235
236 402 CraterAlbedo::CraterAlbedo(std::shared_ptr<const CraterLayer> craters, CraterAlbedoOptions options)
237
1/1
✓ Branch 1 taken 402 times.
402 : m_craters(std::move(craters)), m_options(options) {
238
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 401 times.
402 if (!m_craters) {
239
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Crater albedo needs a crater layer");
240 }
241
1/1
✓ Branch 1 taken 382 times.
401 ValidateCraterAlbedoOptions(m_options);
242
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1 time.
422 }
243
244 519498 bool CraterAlbedo::Eligible(const Crater& crater, RandomStream& random) const {
245
4/4
✓ Branch 1 taken 519493 times.
✓ Branch 2 taken 5 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 519492 times.
519498 return m_options.selection.Includes(crater) && random.NextDouble() < m_options.share;
246 }
247
248 30012962 CraterAlbedo::Ray CraterAlbedo::DrawRay(RandomStream& random, double median,
249 const CraterImpact& impact,
250 const RayDirection* direction) const {
251 30012962 const CraterRays& rays = m_options.rays;
252 30012962 const ObliqueImpacts& oblique = rays.oblique;
253 30012962 const double asymmetry = (impact.speedRatio - 1.0) / (impact.speedRatio + 1.0);
254 30012962 const double zone = oblique.zoneHalfWidthDegrees * kDegree;
255 30012962 const double drawn = 2.0 * kPi * random.NextDouble();
256
2/2
✓ Branch 0 taken 5824 times.
✓ Branch 1 taken 30007138 times.
30012962 const double azimuth = direction != nullptr ? direction->azimuth : drawn;
257 30012962 const double l1 = random.NextDouble();
258 30012962 const double l2 = random.NextDouble();
259 30012962 const double w1 = random.NextDouble();
260 30012962 const double w2 = random.NextDouble();
261 30012962 Ray ray;
262 30012962 ray.cos = std::cos(azimuth);
263 30012962 ray.sin = std::sin(azimuth);
264 30012962 ray.length = median * std::exp(rays.lengthSpread.Sample(l1, l2));
265 30012962 const double fromDownrange = std::remainder(azimuth - impact.heading, 2.0 * kPi);
266
2/2
✓ Branch 0 taken 15439666 times.
✓ Branch 1 taken 14573296 times.
30012962 if (asymmetry > 0.0) {
267 15439666 const double speed = 1.0 + asymmetry * std::cos(fromDownrange);
268 15439666 ray.length *= speed * speed / (1.0 + 0.5 * asymmetry * asymmetry);
269 }
270 30012962 const bool uprange = std::fabs(fromDownrange) > kPi - zone;
271 30012962 const bool downrange = std::fabs(fromDownrange) < zone;
272
8/8
✓ Branch 0 taken 29981194 times.
✓ Branch 1 taken 31768 times.
✓ Branch 2 taken 6614604 times.
✓ Branch 3 taken 23366590 times.
✓ Branch 4 taken 4877039 times.
✓ Branch 5 taken 1737565 times.
✓ Branch 6 taken 28243629 times.
✓ Branch 7 taken 31768 times.
30012962 if ((oblique.enabled && impact.angleDegrees < oblique.forbiddenDegrees && uprange) ||
273
4/4
✓ Branch 0 taken 570032 times.
✓ Branch 1 taken 27673597 times.
✓ Branch 2 taken 212532 times.
✓ Branch 3 taken 357500 times.
28243629 (oblique.enabled && impact.angleDegrees < oblique.butterflyDegrees && downrange)) {
274 1950097 ray.length = 0.0;
275 }
276 60025924 ray.sinHalfWidth = direction != nullptr
277
2/2
✓ Branch 0 taken 5824 times.
✓ Branch 1 taken 30007138 times.
30012962 ? direction->sinHalfWidth
278 30007138 : std::sin(0.5 * rays.widthDegrees.Sample(w1, w2) * kDegree);
279 30012962 ray.seed = random.NextU32();
280 30012962 return ray;
281 }
282
283 479202 void CraterAlbedo::AddRays(Prepared& prepared, RandomStream& random) const {
284 479202 const Crater& crater = prepared.crater;
285 479202 const double c1 = random.NextDouble();
286 479202 const double c2 = random.NextDouble();
287
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 478474 times.
479202 const std::vector<RayDirection> directions = m_options.rays.layout
288
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 478474 times.
479202 ? RayDirections(crater, *m_options.rays.layout)
289 479202 : std::vector<RayDirection>{};
290
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 478474 times.
479202 const auto count = m_options.rays.layout
291
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 478474 times.
479202 ? static_cast<int>(directions.size())
292
1/1
✓ Branch 1 taken 478474 times.
478474 : static_cast<int>(std::lround(m_options.rays.count.Sample(c1, c2)));
293
1/1
✓ Branch 1 taken 479202 times.
479202 const double median = m_options.rays.MedianLength(crater.diameter);
294
1/1
✓ Branch 1 taken 479202 times.
479202 const CraterImpact impact = Impact(crater);
295
2/2
✓ Branch 0 taken 30012962 times.
✓ Branch 1 taken 479202 times.
30492164 for (int i = 0; i < count; ++i) {
296 30012962 const Ray ray =
297
3/3
✓ Branch 0 taken 5824 times.
✓ Branch 1 taken 30007138 times.
✓ Branch 3 taken 30012962 times.
30018786 DrawRay(random, median, impact,
298
2/2
✓ Branch 0 taken 5824 times.
✓ Branch 1 taken 30007138 times.
30012962 m_options.rays.layout ? &directions[static_cast<std::size_t>(i)] : nullptr);
299
2/2
✓ Branch 0 taken 28041087 times.
✓ Branch 1 taken 1971875 times.
30012962 if (ray.length > 0.5 * crater.diameter) {
300 28041087 prepared.reach = std::fmax(prepared.reach, ray.length);
301
1/1
✓ Branch 1 taken 28041087 times.
28041087 prepared.rays.push_back(ray);
302 }
303 }
304 479202 }
305
306 6055 void CraterAlbedo::AddZones(Prepared& prepared, double left, double resolution) const {
307 6055 const CraterReflectanceZones& z = m_options.zones;
308 6055 const Crater& crater = prepared.crater;
309 6055 const IdWords id = SplitId(crater.id);
310 6055 RandomStream random(z.seed, kAlbedoZoneStream, id.low, id.high);
311 6055 const bool immature = z.excavationRatio * crater.diameter > z.matureDepth;
312 6055 std::vector<DrawnZone> distal;
313
2/2
✓ Branch 0 taken 24220 times.
✓ Branch 1 taken 6055 times.
30275 for (const ReflectanceZone* zone : z.All()) {
314
4/4
✓ Branch 0 taken 6055 times.
✓ Branch 1 taken 18165 times.
✓ Branch 2 taken 3057 times.
✓ Branch 3 taken 2998 times.
24220 const double life = zone == &z.proximalHigh && !immature ? 0.0
315
2/2
✓ Branch 0 taken 18164 times.
✓ Branch 1 taken 3058 times.
21222 : zone->retentionMyr
316
3/3
✓ Branch 0 taken 18164 times.
✓ Branch 1 taken 3058 times.
✓ Branch 3 taken 18164 times.
21222 ? std::fmin(left, LifeLeft(crater.ageGyr, *zone->retentionMyr))
317 24220 : left;
318 24220 std::optional<DrawnZone> drawn =
319
1/1
✓ Branch 1 taken 24220 times.
24220 DrawZone(*zone, random, crater.diameter, life, m_options.fadeExponent, resolution);
320
2/2
✓ Branch 0 taken 6918 times.
✓ Branch 1 taken 17302 times.
24220 if (!drawn) {
321 6918 continue;
322 }
323
2/2
✓ Branch 0 taken 14493 times.
✓ Branch 1 taken 2809 times.
17302 drawn->streaked = zone == &z.proximalHigh;
324
6/6
✓ Branch 0 taken 14493 times.
✓ Branch 1 taken 2809 times.
✓ Branch 2 taken 9079 times.
✓ Branch 3 taken 5414 times.
✓ Branch 4 taken 9079 times.
✓ Branch 5 taken 8223 times.
26381 drawn->distal = zone == &z.distalHigh || zone == &z.distalLow;
325 17302 prepared.reach = std::fmax(prepared.reach, drawn->reach);
326
3/3
✓ Branch 0 taken 9079 times.
✓ Branch 1 taken 8223 times.
✓ Branch 3 taken 17302 times.
17302 (drawn->distal ? distal : prepared.zones).push_back(*drawn);
327 }
328 6055 std::sort(distal.begin(), distal.end(),
329
2/20
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2678 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 2678 times.
5356 [](const DrawnZone& a, const DrawnZone& b) { return a.reach < b.reach; });
330
1/1
✓ Branch 1 taken 6055 times.
6055 prepared.zones.insert(prepared.zones.end(), distal.begin(), distal.end());
331 6055 }
332
333 519498 CraterAlbedo::Prepared CraterAlbedo::Prepare(const Crater& crater, double spacing) const {
334 519498 Prepared prepared;
335 519498 prepared.crater = crater;
336 519498 const CraterAlbedoOptions& o = m_options;
337
1/1
✓ Branch 1 taken 519498 times.
519498 const IdWords id = SplitId(crater.id);
338
1/1
✓ Branch 1 taken 519498 times.
519498 RandomStream random(o.seed, kCraterAlbedoStream, id.low, id.high);
339
3/3
✓ Branch 1 taken 519498 times.
✓ Branch 3 taken 519492 times.
✓ Branch 4 taken 6 times.
519498 if (!Eligible(crater, random)) {
340 return prepared;
341 }
342
2/2
✓ Branch 1 taken 519492 times.
✓ Branch 4 taken 519492 times.
519492 const double left = LifeLeft(crater.ageGyr, o.retention.Myr(crater.diameter));
343
1/1
✓ Branch 1 taken 519492 times.
519492 const double resolution = ResolutionWeight(crater.diameter, spacing, o.fade);
344
4/4
✓ Branch 0 taken 486923 times.
✓ Branch 1 taken 32569 times.
✓ Branch 2 taken 486922 times.
✓ Branch 3 taken 1 time.
519492 if (left <= 0.0 || resolution == 0.0) {
345 return prepared;
346 }
347 486922 prepared.weight = std::pow(left, o.fadeExponent) * resolution;
348
1/1
✓ Branch 1 taken 486922 times.
486922 prepared.outline = IrregularOutline(crater, m_craters->Options().apply.irregularity);
349
1/1
✓ Branch 1 taken 486922 times.
486922 prepared.profile = KernelOutline(prepared.outline, crater.diameter);
350
1/1
✓ Branch 1 taken 486922 times.
486922 prepared.reach = o.ejectaReachRadii * (0.5 * crater.diameter) * prepared.outline.MaxScale() *
351 486922 (1.0 + o.texture.lobes);
352
2/2
✓ Branch 0 taken 479202 times.
✓ Branch 1 taken 7720 times.
486922 if (o.rays.enabled) {
353
1/1
✓ Branch 1 taken 479202 times.
479202 AddRays(prepared, random);
354 }
355
4/4
✓ Branch 0 taken 6223 times.
✓ Branch 1 taken 480699 times.
✓ Branch 2 taken 6055 times.
✓ Branch 3 taken 168 times.
486922 if (o.zones.enabled && crater.diameter <= o.zones.maxDiameter) {
356
1/1
✓ Branch 1 taken 6055 times.
6055 AddZones(prepared, left, resolution);
357 }
358 return prepared;
359 }
360
361 471314 double CraterAlbedo::Excess(const Crater& crater, double x, double y, double spacing) const {
362
1/1
✓ Branch 2 taken 471314 times.
471314 return Prepare(crater, spacing).Excess(m_options, m_craters->Options().apply.surface, x, y);
363 }
364
365 4324 double CraterAlbedo::ReflectanceChange(const Crater& crater, double x, double y, double spacing,
366 const Ground* ground) const {
367 4324 Prepared prepared = Prepare(crater, spacing);
368
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4313 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
4324 if (ground != nullptr && m_options.zones.shadows.enabled) {
369
1/1
✓ Branch 2 taken 11 times.
11 AddShadows(prepared, *ground, PointBounds(x, y));
370 }
371
1/1
✓ Branch 1 taken 4324 times.
8648 return prepared.Change(m_options.texture, m_craters->Options().apply.surface, x, y);
372 4324 }
373
374 79 void CraterAlbedo::AddShadows(Prepared& prepared, const Ground& ground,
375 const Bounds2d& region) const {
376 79 const ZoneShadows& s = m_options.zones.shadows;
377 79 double reach = 0.0;
378
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 79 times.
384 for (const DrawnZone& zone : prepared.zones) {
379
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 155 times.
305 reach = zone.distal ? std::fmax(reach, zone.reach) : reach;
380 }
381 79 const Crater& crater = prepared.crater;
382 79 const double radius = 0.5 * crater.diameter;
383
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 75 times.
79 if (reach <= radius) {
384 4 return;
385 }
386 75 const Vec2d center{crater.x, crater.y};
387 75 const Surface& surface = m_craters->Options().apply.surface;
388 75 const detail::RayWindow window =
389 75 detail::RaysOver(center, region, surface, static_cast<std::size_t>(s.azimuths));
390 75 auto profile = std::make_shared<detail::ShadowProfile>(
391 150 detail::MakeShadowProfile(s, radius, reach, window));
392
1/1
✓ Branch 1 taken 75 times.
75 const std::vector<Vec2d> points = profile->Points(center, surface);
393
1/1
✓ Branch 1 taken 75 times.
75 std::vector<float> heights(points.size());
394
1/1
✓ Branch 3 taken 75 times.
75 ground.HeightsAt(points, heights, profile->step);
395
1/1
✓ Branch 1 taken 75 times.
75 profile->TraceJets(heights, std::tan(s.jetAngleDegrees * kDegree) * profile->step);
396 75 prepared.shadow = std::move(profile);
397
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 75 times.
75 }
398
399 499504 CraterImpact CraterAlbedo::Impact(const Crater& crater) const {
400 499504 const ObliqueImpacts& oblique = m_options.rays.oblique;
401 499504 CraterImpact impact;
402
2/2
✓ Branch 0 taken 496613 times.
✓ Branch 1 taken 2891 times.
499504 if (!oblique.enabled) {
403 return impact;
404 }
405 496613 const IdWords id = SplitId(crater.id);
406 496613 RandomStream random(m_options.seed, kObliqueRayStream, id.low, id.high);
407 496613 impact.angleDegrees = std::asin(std::sqrt(random.NextDouble())) * kRadian;
408 496613 const double h = random.NextDouble();
409
2/2
✓ Branch 0 taken 467168 times.
✓ Branch 1 taken 29445 times.
496613 impact.heading = oblique.headingDegrees ? *oblique.headingDegrees * kDegree : 2.0 * kPi * h;
410 496613 const double r1 = random.NextDouble();
411 496613 const double r2 = random.NextDouble();
412
2/2
✓ Branch 0 taken 255390 times.
✓ Branch 1 taken 241223 times.
496613 if (impact.angleDegrees < oblique.asymmetricDegrees) {
413 255390 impact.speedRatio = oblique.downrangeSpeedRatio.Sample(r1, r2);
414 }
415 return impact;
416 }
417
418 10 double CraterAlbedo::Reach(const Crater& crater) const {
419 10 return Prepare(crater, 0.0).reach;
420 }
421
422 379 double CraterAlbedo::GatherMargin() const {
423 379 const double largest = m_craters->MaxFeatureDiameter();
424 379 const double rayReach = m_options.rays.enabled
425
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 347 times.
411 ? m_options.rays.MedianLength(largest) *
426 32 std::exp(m_options.rays.lengthSpread.Highest()) *
427 32 m_options.rays.oblique.MaxStretch()
428 379 : 0.0;
429 758 return std::fmax(rayReach, 0.5 * largest * m_options.ejectaReachRadii *
430 379 MaxOutlineScale(m_craters->Options().apply.irregularity) *
431 379 (1.0 + m_options.texture.lobes));
432 }
433
434 1387 void CraterAlbedo::TakePrepared(const CraterList& list, const Bounds2d& region, double spacing,
435 std::vector<Prepared>& prepared) const {
436 1387 const Surface& surface = m_craters->Options().apply.surface;
437
2/2
✓ Branch 0 taken 43850 times.
✓ Branch 1 taken 1387 times.
45237 for (std::size_t i = 0; i < list.Size(); ++i) {
438 43850 Prepared one = Prepare(list.At(i), spacing);
439
2/2
✓ Branch 0 taken 32573 times.
✓ Branch 1 taken 11277 times.
43850 if (one.weight == 0.0) {
440 32573 continue;
441 }
442 11277 const Crater& c = one.crater;
443
3/3
✓ Branch 1 taken 11277 times.
✓ Branch 3 taken 1435 times.
✓ Branch 4 taken 9842 times.
11277 if (surface.Contains(surface.Expand(region, one.reach), {c.x, c.y})) {
444
1/1
✓ Branch 1 taken 1435 times.
1435 prepared.push_back(std::move(one));
445 }
446 43850 }
447 1387 }
448
449 namespace {
450
451 /// Sorts `prepared` by crater ID and drops repeats.
452 379 template <typename Prepared> void SortUniqueById(std::vector<Prepared>& prepared) {
453 379 std::sort(prepared.begin(), prepared.end(),
454
14/20
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 405 times.
✓ Branch 5 taken 1051 times.
✓ Branch 6 taken 41 times.
✓ Branch 7 taken 49 times.
✓ Branch 8 taken 132 times.
✓ Branch 9 taken 49 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 5 times.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 1 time.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 15 times.
✓ Branch 19 taken 990 times.
2740 [](const Prepared& a, const Prepared& b) { return a.crater.id < b.crater.id; });
455 379 prepared.erase(std::unique(prepared.begin(), prepared.end(),
456 1066 [](const Prepared& a, const Prepared& b) {
457
4/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 334 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 606 times.
1066 return a.crater.id == b.crater.id;
458 }),
459 379 prepared.end());
460 379 }
461
462 /// The craters of `prepared` whose reach meets the rows of `raster` from `row0` to `row1`.
463 template <typename Prepared>
464 143 std::vector<const Prepared*> NearRows(const std::vector<Prepared>& prepared,
465 const Raster<float>& raster, std::size_t row0,
466 std::size_t row1, const Surface& surface) {
467 143 const double ya = raster.CellCenter(0, row0).y;
468 143 const double yb = raster.CellCenter(0, row1 - 1).y;
469 143 std::vector<const Prepared*> near;
470
2/2
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 143 times.
652 for (const Prepared& p : prepared) {
471
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 502 times.
509 const double reachY = p.reach / surface.UnitLength();
472
4/4
✓ Branch 0 taken 333 times.
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 209 times.
✓ Branch 3 taken 124 times.
509 if (p.crater.y + reachY >= std::fmin(ya, yb) && p.crater.y - reachY <= std::fmax(ya, yb)) {
473
1/1
✓ Branch 1 taken 209 times.
209 near.push_back(&p);
474 }
475 }
476 143 return near;
477 }
478
479 /// Moves `value` to the reflectance at the background maturity plus `excess`, capped, then
480 /// changes it by the relative reflectance `change`, never below 0.
481 1744804 void Brighten(AlbedoSample& value, std::size_t bands, double excess, double change,
482 const CraterAlbedoOptions& o) {
483
2/2
✓ Branch 0 taken 647737 times.
✓ Branch 1 taken 1097067 times.
1744804 if (excess == 0.0 && change == 0.0) {
484 return;
485 }
486 647737 const double factor = std::fmax(0.0, 1.0 + change);
487
2/2
✓ Branch 0 taken 523827 times.
✓ Branch 1 taken 123910 times.
647737 if (excess == 0.0) {
488 523827 ScaleBands(value, bands, factor);
489 523827 return;
490 }
491 123910 const double maturity = std::fmin(o.backgroundMaturity + excess, o.maxMaturity);
492 123910 MatureBands(value, bands, o.backgroundMaturity, maturity, o.originReflectance, factor);
493 }
494
495 } // namespace
496
497 379 std::vector<CraterAlbedo::Prepared> CraterAlbedo::Gather(const Bounds2d& region, double spacing,
498 const Ground* ground) const {
499 379 const double margin = GatherMargin();
500
1/1
✓ Branch 1 taken 379 times.
379 const Surface& surface = m_craters->Options().apply.surface;
501
1/1
✓ Branch 1 taken 379 times.
379 std::vector<Prepared> prepared;
502
3/3
✓ Branch 1 taken 379 times.
✓ Branch 4 taken 379 times.
✓ Branch 7 taken 379 times.
379 TakePrepared(m_craters->Craters(surface.Expand(region, margin), spacing), region, spacing,
503 prepared);
504 379 const CraterReflectanceZones& zones = m_options.zones;
505
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 45 times.
379 if (zones.enabled) {
506
1/1
✓ Branch 1 taken 334 times.
334 for (double high = std::fmin(m_craters->MaxFeatureDiameter(), zones.maxDiameter);
507
3/3
✓ Branch 1 taken 1342 times.
✓ Branch 3 taken 1008 times.
✓ Branch 4 taken 334 times.
1342 zones.MaxReach(high) > margin; high *= 0.5) {
508
4/4
✓ Branch 1 taken 1008 times.
✓ Branch 4 taken 1008 times.
✓ Branch 7 taken 1008 times.
✓ Branch 10 taken 1008 times.
1008 TakePrepared(m_craters->Craters(surface.Expand(region, zones.MaxReach(high)), spacing,
509 0.5 * high, high),
510 region, spacing, prepared);
511 }
512 }
513 379 SortUniqueById(prepared);
514
5/6
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 307 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
379 if (ground != nullptr && zones.enabled && zones.shadows.enabled) {
515 68 std::size_t shadowed = 0;
516
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 68 times.
136 for (Prepared& p : prepared) {
517
1/1
✓ Branch 1 taken 68 times.
68 AddShadows(p, *ground, region);
518
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 66 times.
70 shadowed += p.shadow ? 1u : 0u;
519 }
520
1/1
✓ Branch 1 taken 68 times.
68 LogDebug("albedo", "Sampled the ground for the zone shadows of ", shadowed, " craters");
521 }
522 379 return prepared;
523 }
524
525 350 void CraterAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y,
526 const LayerContext& context) const {
527 350 const Surface& surface = m_craters->Options().apply.surface;
528 350 double excess = 0.0;
529 350 double change = 0.0;
530
2/2
✓ Branch 2 taken 411 times.
✓ Branch 3 taken 350 times.
761 for (const Prepared& p : Gather(PointBounds(x, y), context.spacing, context.ground)) {
531
1/1
✓ Branch 1 taken 411 times.
411 excess += p.Excess(m_options, surface, x, y);
532
1/1
✓ Branch 1 taken 411 times.
411 change += p.Change(m_options.texture, surface, x, y);
533 350 }
534 350 Brighten(value, bands, excess, change, m_options);
535 350 }
536
537 30 void CraterAlbedo::Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const {
538
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29 times.
30 if (bands.empty()) {
539 1 return;
540 }
541 29 const Surface& surface = m_craters->Options().apply.surface;
542 29 const Raster<float>& first = bands.front();
543 29 const std::vector<Prepared> prepared =
544 29 Gather(first.Grid().Bounds(), context.spacing, context.ground);
545
1/1
✓ Branch 1 taken 29 times.
29 const std::size_t count = bands.size();
546
2/2
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
29 ForEachAlbedoBlock(bands, context.threads, kBlockRows, [&](std::size_t row0, std::size_t row1) {
547 143 std::vector<const Prepared*> near = NearRows(prepared, first, row0, row1, surface);
548
1/1
✓ Branch 1 taken 143 times.
143 return AlbedoCellVisitor([this, near = std::move(near), &surface,
549
0/1
✗ Branch 1 not taken.
286 count](AlbedoSample& value, double x, double y) {
550 1739851 double excess = 0.0;
551 1739851 double change = 0.0;
552
2/2
✓ Branch 0 taken 2155643 times.
✓ Branch 1 taken 1740212 times.
3895855 for (const Prepared* p : near) {
553
4/4
✓ Branch 0 taken 2143833 times.
✓ Branch 1 taken 11810 times.
✓ Branch 2 taken 573898 times.
✓ Branch 3 taken 1569935 times.
2155643 if (!surface.IsBody() && std::fabs(x - p->crater.x) > p->reach) {
554 573898 continue;
555 }
556 1581745 excess += p->Excess(m_options, surface, x, y);
557 1587548 change += p->Change(m_options.texture, surface, x, y);
558 }
559 1740212 Brighten(value, count, excess, change, m_options);
560
1/1
✓ Branch 1 taken 143 times.
286 });
561 143 });
562 29 }
563
564 } // namespace terraingen
565