./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
51 / 51
100.0%
Branches:
21 / 22
95.5%
Functions:
9 / 9
100.0%

src/albedo/blast_zones.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/blast_zones.h"
4
5 #include "albedo/bands.h"
6 #include "terraingen/albedo/albedo_fields.h"
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/random/streams.h"
9
10 #include <cmath>
11 #include <stdexcept>
12 #include <utility>
13
14 namespace terraingen {
15
16 namespace {
17
18 constexpr double kFullRadius = 0.3;
19
20 /// The zone's radial profile at `rho` of its radius: 1 to `kFullRadius`, fading to 0 at 1.
21 176663 double Profile(double rho) {
22 176663 return FadeOut(rho, kFullRadius, 1.0 - kFullRadius);
23 }
24
25 /// The semi-axes (long, short) in meters of `zone`.
26 623544 std::pair<double, double> SemiAxes(const BlastZone& zone) {
27 623544 const double minor = std::sqrt(zone.area / (kPi * zone.elongation));
28 623544 return {zone.elongation * minor, minor};
29 }
30
31 } // namespace
32
33 27 void ValidateBlastZone(const BlastZone& zone) {
34 27 CheckRanges(zone);
35 10 }
36
37 270022 double BlastZoneMeanShare() {
38 6 static const double share = [] {
39 6 constexpr int kSteps = 1 << 14;
40 6 double sum = 0.0;
41
2/2
✓ Branch 0 taken 98304 times.
✓ Branch 1 taken 6 times.
98310 for (int i = 0; i < kSteps; ++i) {
42 98304 const double rho = (i + 0.5) / kSteps;
43 98304 sum += Profile(rho) * 2.0 * rho / kSteps;
44 }
45 6 return sum;
46
4/5
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 270016 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6 times.
270022 }();
47 270022 return share;
48 }
49
50 16 BlastZoneAlbedo::BlastZoneAlbedo(std::vector<BlastZone> zones) : m_zones(std::move(zones)) {
51
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (const BlastZone& zone : m_zones) {
52
1/1
✓ Branch 1 taken 8 times.
16 ValidateBlastZone(zone);
53 }
54 16 }
55
56 353534 double BlastZoneAlbedo::Reach(const BlastZone& zone) {
57 353534 return std::fmax(SemiAxes(zone).first * zone.haloReach, zone.coreRadius);
58 }
59
60 354062 double BlastZoneAlbedo::Change(const BlastZone& zone, double x, double y, const Surface& surface) {
61 354062 const Vec2d offset = surface.Offset({zone.x, zone.y}, {x, y});
62 354064 const double distance = std::hypot(offset.x, offset.y);
63
2/2
✓ Branch 1 taken 270018 times.
✓ Branch 2 taken 83997 times.
354064 if (distance >= Reach(zone)) {
64 return 0.0;
65 }
66 270018 const auto [major, minor] = SemiAxes(zone);
67 270018 const double heading = zone.headingDegrees * kDegree;
68 270018 const double u = (offset.x * std::cos(heading) + offset.y * std::sin(heading)) / major;
69 270018 const double v = (offset.y * std::cos(heading) - offset.x * std::sin(heading)) / minor;
70 540037 const double lobe = EdgeLobe(zone.raggedness, kLobeRing, std::atan2(v, u), 0.0,
71 270018 zone.seed ^ kBlastZoneStream, 3);
72 270019 const double rho = std::hypot(u, v) / lobe;
73 270019 const double share = BlastZoneMeanShare();
74 270019 const double peak = (zone.change - zone.haloChange * (1.0 - share)) / share;
75
2/2
✓ Branch 0 taken 78359 times.
✓ Branch 1 taken 191660 times.
270019 const double inner = rho < 1.0 ? Profile(rho) : 0.0;
76
2/2
✓ Branch 0 taken 191660 times.
✓ Branch 1 taken 78359 times.
270019 const double halo = rho <= 1.0 ? 1.0
77
2/2
✓ Branch 0 taken 191505 times.
✓ Branch 1 taken 155 times.
191660 : zone.haloReach > 1.0 ? FadeOut(rho, 1.0, zone.haloReach - 1.0)
78 270019 : 0.0;
79 270019 double change = peak * inner + (1.0 - inner) * zone.haloChange * halo;
80
2/2
✓ Branch 0 taken 241236 times.
✓ Branch 1 taken 28783 times.
270019 if (zone.coreRadius > 0.0) {
81 241236 const double core = FadeOut(distance / zone.coreRadius, 0.7, 0.3);
82 241236 change = zone.coreChange * core + (1.0 - core) * change;
83 }
84 return change;
85 }
86
87 324897 void BlastZoneAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y,
88 const LayerContext& context) const {
89 324897 double change = 0.0;
90
2/2
✓ Branch 0 taken 324905 times.
✓ Branch 1 taken 324755 times.
649660 for (const BlastZone& zone : m_zones) {
91 324905 change += Change(zone, x, y, context.SurfaceOrPlane());
92 }
93 324755 ChangeBands(value, bands, change);
94 324877 }
95
96 } // namespace terraingen
97