./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
85 / 86
98.8%
Branches:
66 / 73
90.4%
Functions:
11 / 11
100.0%

src/geometry/radial_shape.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/geometry/radial_shape.h"
4
5 #include "geometry/cube_faces.h"
6 #include "terraingen/util/bits.h"
7 #include "util/checks.h"
8
9 #include <algorithm>
10 #include <array>
11 #include <cmath>
12 #include <map>
13 #include <stdexcept>
14 #include <utility>
15
16 namespace terraingen {
17
18 namespace {
19
20 /// Factor on the largest area per steradian found at quadrilateral corners, for its variation
21 /// within the curved bilinear quadrilaterals.
22 constexpr double kAreaMargin = 1.25;
23
24 /// The gnomonic coordinate of label `k` of `q`, exactly -1, 0 or 1 at multiples of 45 degrees and
25 /// odd in `k - q/2`, so labels shared between faces get bitwise the same direction.
26 18163268 double Gnomonic(std::size_t k, std::size_t q) {
27
2/2
✓ Branch 0 taken 5970498 times.
✓ Branch 1 taken 12192770 times.
18163268 if (2 * k > q) {
28 5970498 return -Gnomonic(q - k, q);
29 }
30
2/2
✓ Branch 0 taken 11784564 times.
✓ Branch 1 taken 408206 times.
12192770 if (k == 0) {
31 return -1.0;
32 }
33
2/2
✓ Branch 0 taken 11532868 times.
✓ Branch 1 taken 251696 times.
11784564 if (2 * k == q) {
34 return 0.0;
35 }
36 11532868 return std::tan(detail::kQuarter *
37 11532868 (2.0 * static_cast<double>(k) / static_cast<double>(q) - 1.0));
38 }
39
40 } // namespace
41
42 6096391 Vec3d RadialShape::LabelDirection(std::size_t q, std::size_t i, std::size_t j, std::size_t face) {
43
6/6
✓ Branch 0 taken 6096388 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6096386 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 6096385 times.
6096391 if (q == 0 || i > q || j > q || face < 1 || face > 6) {
44
1/1
✓ Branch 2 taken 6 times.
6 throw std::invalid_argument("An ICQ label needs Q > 0, I and J <= Q and F from 1 to 6");
45 }
46 6096385 const Vec3d p =
47 6096385 detail::CubeFacePoint(detail::kIcqFaces[face - 1], Gnomonic(i, q), Gnomonic(j, q));
48 6096385 return p / Length(p);
49 }
50
51 101 RadialShape RadialShape::Sample(std::size_t q, const std::function<double(const Vec3d&)>& radius) {
52
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 100 times.
101 if (q == 0) {
53
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A radial shape needs Q > 0");
54 }
55 100 std::vector<double> radii(6 * (q + 1) * (q + 1));
56
1/1
✓ Branch 1 taken 98 times.
100 ForEachIcqLabel(q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) {
57 1056494 radii[k] = radius(LabelDirection(q, i, j, face));
58 1056492 });
59
1/1
✓ Branch 1 taken 98 times.
196 return RadialShape(q, std::move(radii));
60 100 }
61
62 117 RadialShape::RadialShape(std::size_t q, std::vector<double> radii)
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 : m_q(q), m_radii(std::move(radii)) {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (m_q == 0) {
65
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("A radial shape needs Q > 0");
66 }
67
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 115 times.
117 if (m_radii.size() != 6 * (m_q + 1) * (m_q + 1)) {
68
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A radial shape needs 6 (Q + 1)^2 label radii");
69 }
70
3/3
✓ Branch 1 taken 115 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 114 times.
115 if (!std::all_of(m_radii.begin(), m_radii.end(), IsPositive)) {
71
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A radial shape's radii must be positive and finite");
72 }
73
1/1
✓ Branch 2 taken 113 times.
114 const auto [lo, hi] = std::minmax_element(m_radii.begin(), m_radii.end());
74
1/1
✓ Branch 1 taken 113 times.
114 m_min = *lo;
75 114 m_max = *hi;
76
1/1
✓ Branch 1 taken 113 times.
114 CheckSharedLabels();
77
1/1
✓ Branch 1 taken 113 times.
113 m_maxArea = CornerAreaBound() * kAreaMargin;
78 117 }
79
80 114 void RadialShape::CheckSharedLabels() const {
81
1/1
✓ Branch 1 taken 113 times.
114 std::map<std::array<double, 3>, double> shared;
82
1/1
✓ Branch 1 taken 113 times.
114 ForEachIcqLabel(m_q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) {
83
8/8
✓ Branch 0 taken 1052100 times.
✓ Branch 1 taken 23893 times.
✓ Branch 2 taken 1028208 times.
✓ Branch 3 taken 23892 times.
✓ Branch 4 taken 1005676 times.
✓ Branch 5 taken 22532 times.
✓ Branch 6 taken 983144 times.
✓ Branch 7 taken 22532 times.
1075993 if (i != 0 && i != m_q && j != 0 && j != m_q) {
84 983144 return;
85 }
86 92849 const Vec3d d = LabelDirection(m_q, i, j, face);
87 185698 const auto [it, inserted] =
88
2/2
✓ Branch 1 taken 46872 times.
✓ Branch 2 taken 45977 times.
92849 shared.emplace(std::array<double, 3>{d.x, d.y, d.z}, m_radii[k]);
89
4/4
✓ Branch 0 taken 46872 times.
✓ Branch 1 taken 45977 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 46871 times.
92849 if (!inserted && std::fabs(it->second - m_radii[k]) > 1e-9 * m_max) {
90 1 throw std::invalid_argument(
91
1/1
✓ Branch 2 taken 1 time.
1 "A radial shape's labels shared between faces must have equal radii");
92 }
93 });
94 113 }
95
96 113 double RadialShape::CornerAreaBound() const {
97 113 double largest = 0.0;
98
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 113 times.
791 for (std::size_t face = 1; face <= 6; ++face) {
99
2/2
✓ Branch 0 taken 23208 times.
✓ Branch 1 taken 678 times.
23886 for (std::size_t j = 0; j < m_q; ++j) {
100
2/2
✓ Branch 0 taken 1028880 times.
✓ Branch 1 taken 23208 times.
1052088 for (std::size_t i = 0; i < m_q; ++i) {
101
2/2
✓ Branch 1 taken 11397 times.
✓ Branch 2 taken 1017483 times.
1040277 largest = std::max(largest, QuadAreaBound(i, j, face));
102 }
103 }
104 }
105 113 return largest;
106 }
107
108 1028880 double RadialShape::QuadAreaBound(std::size_t i, std::size_t j, std::size_t face) const {
109 1028880 const std::array<std::pair<std::size_t, std::size_t>, 4> corners{
110 1028880 {{i, j}, {i + 1, j}, {i + 1, j + 1}, {i, j + 1}}};
111 1028880 std::array<Vec3d, 4> p{};
112
2/2
✓ Branch 0 taken 4115520 times.
✓ Branch 1 taken 1028880 times.
5144400 for (std::size_t k = 0; k < 4; ++k) {
113 4115520 const auto [ci, cj] = corners[k];
114 4115520 p[k] = LabelDirection(m_q, ci, cj, face) * m_radii[LabelIndex(ci, cj, face)];
115 }
116 1028880 Vec3d normal = Normalized(Cross(p[2] - p[0], p[3] - p[1]));
117
1/2
✓ Branch 0 taken 1028880 times.
✗ Branch 1 not taken.
1028880 if (Dot(normal, p[0]) < 0.0) {
118 1028880 normal = -normal;
119 }
120 1028880 double largest = 0.0;
121
2/2
✓ Branch 0 taken 4115520 times.
✓ Branch 1 taken 1028880 times.
5144400 for (const Vec3d& corner : p) {
122 4115520 const double r = Length(corner);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4115520 times.
4115520 const double cosine = Dot(corner, normal) / r;
124
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4115520 times.
✓ Branch 2 taken 2208104 times.
✓ Branch 3 taken 1907416 times.
6323624 largest = std::max(largest, r * r / std::max(cosine, 1e-3));
125 }
126 1028880 return largest;
127 }
128
129 206758751 double RadialShape::At(const Vec3d& direction) const {
130 206758751 const detail::FacePosition at =
131 206758751 detail::FaceCoordinates(detail::kIcqFaces, direction, static_cast<double>(m_q));
132 590366505 const auto position = [&](double s) {
133 383607754 const std::size_t k = ClampIndex(std::floor(s), m_q);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383607754 times.
383607754 return std::pair<std::size_t, double>{k, std::clamp(s - static_cast<double>(k), 0.0, 1.0)};
135 206758751 };
136 206758751 const auto [i, fi] = position(at.s);
137 206758751 const auto [j, fj] = position(at.t);
138 206758751 const std::size_t face = at.face + 1;
139 206758751 const double r00 = m_radii[LabelIndex(i, j, face)];
140 206758751 const double r10 = m_radii[LabelIndex(i + 1, j, face)];
141 206758751 const double r01 = m_radii[LabelIndex(i, j + 1, face)];
142 206758751 const double r11 = m_radii[LabelIndex(i + 1, j + 1, face)];
143 206758751 return (1.0 - fj) * ((1.0 - fi) * r00 + fi * r10) + fj * ((1.0 - fi) * r01 + fi * r11);
144 }
145
146 } // namespace terraingen
147