./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
90 / 90
100.0%
Branches:
44 / 53
83.0%
Functions:
8 / 8
100.0%

src/albedo/zone_shadows.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "albedo/zone_shadows.h"
4
5 #include "terraingen/layers/fade.h"
6
7 #include <algorithm>
8 #include <array>
9 #include <cmath>
10 #include <cstdint>
11 #include <limits>
12 #include <optional>
13 #include <utility>
14
15 namespace terraingen::detail {
16
17 1737 double ShadowProfile::Shade(double distance, double angle) const {
18
2/2
✓ Branch 0 taken 1736 times.
✓ Branch 1 taken 1 time.
1737 if (distance <= start) {
19 return 0.0;
20 }
21 1736 const auto rings = static_cast<double>(azimuths);
22 1736 const double r = std::fmin((distance - start) / step, static_cast<double>(samples - 1));
23 1736 double a = angle / (2.0 * kPi) * rings;
24 1736 a -= std::floor(a / rings) * rings;
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1736 times.
1736 const std::size_t a0 = std::min(static_cast<std::size_t>(a), azimuths - 1);
26 1736 const std::size_t a1 = (a0 + 1) % azimuths;
27
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1736 times.
1736 const std::size_t r0 = std::min(static_cast<std::size_t>(r), samples - 1);
28
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1736 times.
1736 const std::size_t r1 = std::min(r0 + 1, samples - 1);
29 1736 const double fa = a - static_cast<double>(a0);
30 1736 const double fr = r - static_cast<double>(r0);
31 8680 const auto at = [&](std::size_t i, std::size_t j) {
32 6944 const std::size_t k = (i + azimuths - first) % azimuths;
33
2/4
✓ Branch 0 taken 6944 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6944 times.
6944 return k < rays ? static_cast<double>(gap[k * used + std::min(j, used - 1)]) : 0.0;
34 1736 };
35 1736 const double g = (1.0 - fa) * ((1.0 - fr) * at(a0, r0) + fr * at(a0, r1)) +
36 1736 fa * ((1.0 - fr) * at(a1, r0) + fr * at(a1, r1));
37 1736 return Smoothstep(g / depth);
38 }
39
40 75 std::vector<Vec2d> ShadowProfile::Points(const Vec2d& center, const Surface& surface) const {
41
1/1
✓ Branch 1 taken 75 times.
75 std::vector<Vec2d> points;
42
1/1
✓ Branch 1 taken 75 times.
75 points.reserve(rays * used);
43
2/2
✓ Branch 0 taken 1256 times.
✓ Branch 1 taken 75 times.
1331 for (std::size_t k = 0; k < rays; ++k) {
44 1256 const std::size_t i = (first + k) % azimuths;
45 1256 const double angle = 2.0 * kPi * static_cast<double>(i) / static_cast<double>(azimuths);
46
2/2
✓ Branch 0 taken 23995 times.
✓ Branch 1 taken 1256 times.
25251 for (std::size_t j = 0; j < used; ++j) {
47 23995 const double distance = start + static_cast<double>(j) * step;
48 47990 points.push_back(
49
1/1
✓ Branch 1 taken 23995 times.
47990 surface.Locate(center, {distance * std::cos(angle), distance * std::sin(angle)}));
50 }
51 }
52 75 return points;
53 }
54
55 75 void ShadowProfile::TraceJets(const std::vector<float>& heights, double drop) {
56 75 gap.assign(heights.size(), 0.0f);
57
2/2
✓ Branch 0 taken 1256 times.
✓ Branch 1 taken 75 times.
1331 for (std::size_t k = 0; k < rays; ++k) {
58 1256 const std::size_t row = k * used;
59 1256 double jet = static_cast<double>(heights[row]);
60
2/2
✓ Branch 0 taken 22739 times.
✓ Branch 1 taken 1256 times.
23995 for (std::size_t j = 1; j < used; ++j) {
61
2/2
✓ Branch 0 taken 22733 times.
✓ Branch 1 taken 6 times.
22739 const double h = static_cast<double>(heights[row + j]);
62
4/4
✓ Branch 0 taken 22733 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 22730 times.
22739 if (std::isnan(h) || std::isnan(jet)) {
63 9 jet = h;
64 9 continue;
65 }
66 22730 jet = std::fmax(h, jet - drop);
67 22730 gap[row + j] = static_cast<float>(jet - h);
68 }
69 }
70 75 }
71
72 namespace {
73
74 /// The offsets from `center` of the corners of `region`.
75 75 std::array<Vec2d, 4> CornerOffsets(const Vec2d& center, const Bounds2d& region,
76 const Surface& surface) {
77 75 return {surface.Offset(center, {region.minX, region.minY}),
78 75 surface.Offset(center, {region.minX, region.maxY}),
79 75 surface.Offset(center, {region.maxX, region.minY}),
80 75 surface.Offset(center, {region.maxX, region.maxY})};
81 }
82
83 /// The middle direction of `corners` and the largest angle of any of them from it, in radians;
84 /// nothing when the corners surround the center.
85 75 std::optional<std::pair<double, double>> AngleSpan(const std::array<Vec2d, 4>& corners) {
86 75 constexpr double kInf = std::numeric_limits<double>::infinity();
87 75 Bounds2d box{kInf, kInf, -kInf, -kInf};
88
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 75 times.
375 for (const Vec2d& c : corners) {
89 300 box = {std::fmin(box.minX, c.x), std::fmin(box.minY, c.y), std::fmax(box.maxX, c.x),
90 300 std::fmax(box.maxY, c.y)};
91 }
92
6/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
75 if (box.minX <= 0.0 && box.maxX >= 0.0 && box.minY <= 0.0 && box.maxY >= 0.0) {
93 1 return std::nullopt;
94 }
95 74 const double mid = std::atan2(0.5 * (box.minY + box.maxY), 0.5 * (box.minX + box.maxX));
96 74 double half = 0.0;
97
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 74 times.
370 for (const Vec2d& c : corners) {
98 296 half = std::fmax(half, std::fabs(std::remainder(std::atan2(c.y, c.x) - mid, 2.0 * kPi)));
99 }
100 74 return std::pair{mid, half};
101 }
102
103 } // namespace
104
105 75 RayWindow RaysOver(const Vec2d& center, const Bounds2d& region, const Surface& surface,
106 std::size_t azimuths) {
107 75 const std::array<Vec2d, 4> corners = CornerOffsets(center, region, surface);
108 75 RayWindow window;
109 75 window.rays = azimuths;
110
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 75 times.
375 for (const Vec2d& c : corners) {
111 300 window.far = std::fmax(window.far, std::hypot(c.x, c.y));
112 }
113 75 const auto span = AngleSpan(corners);
114
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 1 time.
75 if (!span) {
115 return window;
116 }
117 74 const double ray = 2.0 * kPi / static_cast<double>(azimuths);
118
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 const double from = std::floor((span->first - span->second) / ray) - 1.0;
119 74 const double to = std::ceil((span->first + span->second) / ray) + 1.0;
120
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 if (to - from + 1.0 >= static_cast<double>(azimuths)) {
121 return window;
122 }
123 74 const auto n = static_cast<std::int64_t>(azimuths);
124 74 window.first = static_cast<std::size_t>(((static_cast<std::int64_t>(from) % n) + n) % n);
125 74 window.rays = static_cast<std::size_t>(to - from) + 1;
126 74 return window;
127 }
128
129 75 ShadowProfile MakeShadowProfile(const ZoneShadows& shadows, double radius, double reach,
130 const RayWindow& window) {
131
132
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 74 times.
75 ShadowProfile profile;
133 75 profile.azimuths = static_cast<std::size_t>(shadows.azimuths);
134 75 profile.samples = static_cast<std::size_t>(shadows.radialSamples);
135 75 profile.start = radius;
136 75 profile.step = (reach - radius) / static_cast<double>(profile.samples - 1);
137 75 profile.depth = shadows.depth;
138 75 const double used = std::ceil((window.far - radius) / profile.step) + 2.0;
139 75 profile.first = window.first;
140 75 profile.rays = window.rays;
141 150 profile.used =
142
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 74 times.
75 static_cast<std::size_t>(std::clamp(used, 2.0, static_cast<double>(profile.samples)));
143 75 return profile;
144 }
145
146 } // namespace terraingen::detail
147