./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
112 / 113
99.1%
Branches:
82 / 84
97.6%
Functions:
7 / 7, 1 excl
100.0%

src/mesh/shape_model.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/mesh/shape_model.h"
4
5 #include "geometry/cube_faces.h"
6 #include "terraingen/util/bits.h"
7 #include "terraingen/util/log.h"
8
9 #include <algorithm>
10 #include <array>
11 #include <cmath>
12 #include <cstdint>
13 #include <limits>
14 #include <optional>
15 #include <stdexcept>
16 #include <vector>
17
18 namespace terraingen {
19
20 namespace {
21
22 /// The triangles near each patch of a cube grid of directions, by the gnomonic projections of
23 /// their corners, so each ray tests only the triangles around it.
24 2 class TriangleBins {
25 public:
26 10 TriangleBins(const TriangleMesh& mesh, std::size_t n) : m_n(n), m_bins(6 * n * n) {
27
2/2
✓ Branch 0 taken 103760 times.
✓ Branch 1 taken 10 times.
103770 for (std::size_t t = 0; t < mesh.triangles.size(); ++t) {
28 103760 std::array<Vec3d, 3> corner{};
29
2/2
✓ Branch 0 taken 311280 times.
✓ Branch 1 taken 103760 times.
415040 for (std::size_t k = 0; k < 3; ++k) {
30 311280 corner[k] = mesh.positions[mesh.triangles[t][k]];
31 }
32
2/2
✓ Branch 0 taken 622560 times.
✓ Branch 1 taken 103760 times.
726320 for (std::size_t face = 0; face < 6; ++face) {
33
1/1
✓ Branch 1 taken 622560 times.
622560 Insert(static_cast<std::uint32_t>(t), face, corner);
34 }
35 }
36 10 }
37
38 /// The triangles in the patches around patch coordinates (`s`, `t`) of `face`.
39 57602 void Near(std::size_t face, double s, double t, std::vector<std::uint32_t>& out) const {
40 57602 out.clear();
41 172806 const auto range = [&](double x) {
42 115204 const double k = std::floor(x);
43 115204 return std::array<std::size_t, 2>{ClampIndex(k - 1.0, m_n), ClampIndex(k, m_n)};
44 57602 };
45 57602 const auto [i0, i1] = range(s);
46 57602 const auto [j0, j1] = range(t);
47
2/2
✓ Branch 0 taken 111966 times.
✓ Branch 1 taken 57602 times.
169568 for (std::size_t j = j0; j <= j1; ++j) {
48
2/2
✓ Branch 0 taken 220632 times.
✓ Branch 1 taken 111966 times.
332598 for (std::size_t i = i0; i <= i1; ++i) {
49 220632 const auto& bin = m_bins[(face * m_n + j) * m_n + i];
50 220632 out.insert(out.end(), bin.begin(), bin.end());
51 }
52 }
53 57602 std::sort(out.begin(), out.end());
54 57602 out.erase(std::unique(out.begin(), out.end()), out.end());
55 57602 }
56
57 private:
58 622560 void Insert(std::uint32_t triangle, std::size_t face, const std::array<Vec3d, 3>& corner) {
59 622560 const detail::CubeFace& f = detail::kCubeFaces[face];
60 622560 double sLo = static_cast<double>(m_n);
61 622560 double sHi = 0.0;
62 622560 double tLo = static_cast<double>(m_n);
63 622560 double tHi = 0.0;
64 622560 bool behind = false;
65 622560 bool front = false;
66
2/2
✓ Branch 0 taken 1867680 times.
✓ Branch 1 taken 622560 times.
2490240 for (const Vec3d& p : corner) {
67
2/2
✓ Branch 0 taken 945860 times.
✓ Branch 1 taken 921820 times.
1867680 const double along = Dot(p, f.normal);
68
2/2
✓ Branch 0 taken 945860 times.
✓ Branch 1 taken 921820 times.
1867680 if (along <= 0.0) {
69 945860 behind = true;
70 945860 continue;
71 }
72 921820 front = true;
73 921820 const double s =
74
2/2
✓ Branch 0 taken 408014 times.
✓ Branch 1 taken 513806 times.
921820 detail::EquiangularCoordinate(Dot(p, f.u) / along, static_cast<double>(m_n));
75 921820 const double t =
76 921820 detail::EquiangularCoordinate(Dot(p, f.v) / along, static_cast<double>(m_n));
77
2/2
✓ Branch 0 taken 408014 times.
✓ Branch 1 taken 513806 times.
921820 sLo = std::min(sLo, s);
78
2/2
✓ Branch 0 taken 417870 times.
✓ Branch 1 taken 503950 times.
921820 sHi = std::max(sHi, s);
79
2/2
✓ Branch 0 taken 437270 times.
✓ Branch 1 taken 484550 times.
921820 tLo = std::min(tLo, t);
80
2/2
✓ Branch 0 taken 431664 times.
✓ Branch 1 taken 490156 times.
1353484 tHi = std::max(tHi, t);
81 }
82
2/2
✓ Branch 0 taken 309888 times.
✓ Branch 1 taken 312672 times.
622560 if (!front) {
83 309888 return;
84 }
85
2/2
✓ Branch 0 taken 10772 times.
✓ Branch 1 taken 301900 times.
312672 if (behind) {
86 10772 sLo = 0.0;
87 10772 tLo = 0.0;
88 10772 sHi = static_cast<double>(m_n);
89 10772 tHi = static_cast<double>(m_n);
90 }
91 312672 const std::size_t i0 = ClampIndex(std::floor(sLo) - 1.0, m_n);
92 312672 const std::size_t i1 = ClampIndex(std::floor(sHi) + 1.0, m_n);
93 312672 const std::size_t j0 = ClampIndex(std::floor(tLo) - 1.0, m_n);
94 312672 const std::size_t j1 = ClampIndex(std::floor(tHi) + 1.0, m_n);
95
2/2
✓ Branch 0 taken 1044166 times.
✓ Branch 1 taken 312672 times.
1356838 for (std::size_t j = j0; j <= j1; ++j) {
96
2/2
✓ Branch 0 taken 11513638 times.
✓ Branch 1 taken 1044166 times.
12557804 for (std::size_t i = i0; i <= i1; ++i) {
97 11513638 m_bins[(face * m_n + j) * m_n + i].push_back(triangle);
98 }
99 }
100 }
101
102 std::size_t m_n;
103 std::vector<std::vector<std::uint32_t>> m_bins;
104 };
105
106 /// The distance along unit `direction` from the origin at which the ray crosses triangle
107 /// (`a`, `b`, `c`), with a small tolerance at its edges; empty if it misses.
108 13529710 std::optional<double> Crossing(const Vec3d& direction, const Vec3d& a, const Vec3d& b,
109 const Vec3d& c) {
110 13529710 constexpr double kEdge = 1e-9;
111
2/2
✓ Branch 0 taken 2688 times.
✓ Branch 1 taken 13527022 times.
13529710 const Vec3d e1 = b - a;
112 13529710 const Vec3d e2 = c - a;
113 13529710 const Vec3d p = Cross(direction, e2);
114
2/2
✓ Branch 0 taken 2688 times.
✓ Branch 1 taken 13527022 times.
13529710 const double det = Dot(e1, p);
115
2/2
✓ Branch 0 taken 2688 times.
✓ Branch 1 taken 13527022 times.
13529710 if (det == 0.0) {
116 2688 return std::nullopt;
117 }
118 13527022 const double inverse = 1.0 / det;
119 13527022 const Vec3d s{-a.x, -a.y, -a.z};
120
2/2
✓ Branch 0 taken 7084260 times.
✓ Branch 1 taken 6442762 times.
13527022 const double u = Dot(s, p) * inverse;
121
4/4
✓ Branch 0 taken 7084260 times.
✓ Branch 1 taken 6442762 times.
✓ Branch 2 taken 6596330 times.
✓ Branch 3 taken 487930 times.
13527022 if (u < -kEdge || u > 1.0 + kEdge) {
122 13039092 return std::nullopt;
123 }
124 487930 const Vec3d q = Cross(s, e1);
125
2/2
✓ Branch 0 taken 276741 times.
✓ Branch 1 taken 211189 times.
487930 const double v = Dot(direction, q) * inverse;
126
4/4
✓ Branch 0 taken 276741 times.
✓ Branch 1 taken 211189 times.
✓ Branch 2 taken 210889 times.
✓ Branch 3 taken 65852 times.
487930 if (v < -kEdge || u + v > 1.0 + kEdge) {
127 422078 return std::nullopt;
128 }
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65852 times.
65852 const double t = Dot(e2, q) * inverse;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65852 times.
65852 if (!(t > 0.0)) {
131 ✗ return std::nullopt;
132 }
133 65852 return t;
134 }
135
136 } // namespace
137
138 13 RadialShape ShapeModelFromMesh(const TriangleMesh& mesh, std::size_t q) {
139
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
13 if (q == 0) {
140
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A radial shape needs Q > 0");
141 }
142
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
11 if (mesh.triangles.empty()) {
143
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A shape model mesh needs triangles");
144 }
145 10 const TriangleBins bins(mesh, q);
146
1/1
✓ Branch 1 taken 10 times.
10 std::vector<std::uint32_t> near;
147 10 std::size_t folded = 0;
148 57622 const RadialShape shape = RadialShape::Sample(q, [&](const Vec3d& direction) {
149 57602 const detail::FacePosition at =
150 57602 detail::FaceCoordinates(detail::kCubeFaces, direction, static_cast<double>(q));
151 57602 bins.Near(at.face, at.s, at.t, near);
152 57602 double outer = 0.0;
153 57602 double inner = std::numeric_limits<double>::infinity();
154
2/2
✓ Branch 0 taken 13529710 times.
✓ Branch 1 taken 57602 times.
13587312 for (const std::uint32_t t : near) {
155
2/2
✓ Branch 0 taken 65852 times.
✓ Branch 1 taken 13463858 times.
13529710 const auto& tri = mesh.triangles[t];
156 13529710 const std::optional<double> hit = Crossing(
157 13529710 direction, mesh.positions[tri[0]], mesh.positions[tri[1]], mesh.positions[tri[2]]);
158
2/2
✓ Branch 0 taken 65852 times.
✓ Branch 1 taken 13463858 times.
13529710 if (hit) {
159
2/2
✓ Branch 0 taken 59639 times.
✓ Branch 1 taken 6213 times.
65852 outer = std::max(outer, *hit);
160
2/2
✓ Branch 0 taken 60036 times.
✓ Branch 1 taken 5816 times.
125888 inner = std::min(inner, *hit);
161 }
162 }
163
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 57600 times.
57602 if (outer == 0.0) {
164 2 throw std::runtime_error(
165
1/1
✓ Branch 2 taken 2 times.
2 "A ray from the shape model's origin misses it: the mesh must enclose its origin");
166 }
167
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 57450 times.
57600 if (outer - inner > 1e-6 * outer) {
168 150 ++folded;
169 }
170 57600 return outer;
171
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 8 times.
12 });
172
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 7 times.
8 if (folded > 0) {
173
1/1
✓ Branch 1 taken 1 time.
1 LogWarning("mesh", folded, " of ", shape.Radii().size(),
174 " rays from the origin cross the shape model more than once; it is not "
175 "star-shaped from its origin, so its outermost surface is kept");
176 }
177 16 return shape;
178 10 }
179
180 } // namespace terraingen
181