./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
102 / 104
98.1%
Branches:
77 / 80
96.2%
Functions:
10 / 10
100.0%

src/mesh/polyhedra.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/mesh/polyhedra.h"
4
5 #include <algorithm>
6 #include <cmath>
7 #include <initializer_list>
8 #include <limits>
9 #include <map>
10 #include <stdexcept>
11 #include <utility>
12
13 namespace terraingen {
14
15 namespace {
16
17 /// Each triangle turned to face away from the origin, which lies inside every base solid.
18 3297 TriangleMesh Outward(TriangleMesh mesh) {
19
2/2
✓ Branch 0 taken 47994 times.
✓ Branch 1 taken 3297 times.
51291 for (auto& t : mesh.triangles) {
20
2/2
✓ Branch 0 taken 10725 times.
✓ Branch 1 taken 37269 times.
47994 const Vec3d& a = mesh.positions[t[0]];
21 47994 const Vec3d normal = TriangleAreaNormal(a, mesh.positions[t[1]], mesh.positions[t[2]]);
22
2/2
✓ Branch 0 taken 10725 times.
✓ Branch 1 taken 37269 times.
47994 if (Dot(normal, a + mesh.positions[t[1]] + mesh.positions[t[2]]) < 0.0) {
23 10725 std::swap(t[1], t[2]);
24 }
25 }
26 3297 return mesh;
27 }
28
29 /// Two triangles for the quadrilateral a, b, c, d, in order around it.
30 10710 void AddQuad(TriangleMesh& mesh, std::uint32_t a, std::uint32_t b, std::uint32_t c,
31 std::uint32_t d) {
32 10710 mesh.triangles.push_back({a, b, c});
33 10710 mesh.triangles.push_back({a, c, d});
34 10710 }
35
36 1279 TriangleMesh IcosahedronMesh() {
37 1279 const double t = (1.0 + std::sqrt(5.0)) / 2.0;
38 1279 TriangleMesh mesh;
39 31971 for (const Vec3d& v : std::initializer_list<Vec3d>{{-1, t, 0},
40 {1, t, 0},
41 {-1, -t, 0},
42 {1, -t, 0},
43 {0, -1, t},
44 {0, 1, t},
45 {0, -1, -t},
46 {0, 1, -t},
47 {t, 0, -1},
48 {t, 0, 1},
49 {-t, 0, -1},
50
2/2
✓ Branch 0 taken 15346 times.
✓ Branch 1 taken 1279 times.
16625 {-t, 0, 1}}) {
51
1/1
✓ Branch 2 taken 15346 times.
15346 mesh.positions.push_back(Normalized(v));
52 }
53 ✗ mesh.triangles = {{0, 11, 5}, {0, 5, 1}, {0, 1, 7}, {0, 7, 10}, {0, 10, 11},
54 {1, 5, 9}, {5, 11, 4}, {11, 10, 2}, {10, 7, 6}, {7, 1, 8},
55 {3, 9, 4}, {3, 4, 2}, {3, 2, 6}, {3, 6, 8}, {3, 8, 9},
56
1/1
✓ Branch 1 taken 1279 times.
1279 {4, 9, 5}, {2, 4, 11}, {6, 2, 10}, {8, 6, 7}, {9, 8, 1}};
57 1279 return mesh;
58 }
59
60 1703 TriangleMesh CubeMesh() {
61 1703 TriangleMesh mesh;
62
2/2
✓ Branch 0 taken 13624 times.
✓ Branch 1 taken 1703 times.
15327 for (int i = 0; i < 8; ++i) {
63
1/1
✓ Branch 1 taken 13624 times.
13624 mesh.positions.push_back(
64
6/6
✓ Branch 0 taken 6812 times.
✓ Branch 1 taken 6812 times.
✓ Branch 2 taken 6812 times.
✓ Branch 3 taken 6812 times.
✓ Branch 4 taken 6812 times.
✓ Branch 5 taken 6812 times.
27248 {(i & 1) != 0 ? 1.0 : -1.0, (i & 2) != 0 ? 1.0 : -1.0, (i & 4) != 0 ? 1.0 : -1.0});
65 }
66
1/1
✓ Branch 1 taken 1703 times.
1703 AddQuad(mesh, 0, 1, 3, 2);
67
1/1
✓ Branch 1 taken 1703 times.
1703 AddQuad(mesh, 4, 5, 7, 6);
68
1/1
✓ Branch 1 taken 1703 times.
1703 AddQuad(mesh, 0, 1, 5, 4);
69
1/1
✓ Branch 1 taken 1703 times.
1703 AddQuad(mesh, 2, 3, 7, 6);
70
1/1
✓ Branch 1 taken 1703 times.
1703 AddQuad(mesh, 0, 2, 6, 4);
71
1/1
✓ Branch 1 taken 1703 times.
1703 AddQuad(mesh, 1, 3, 7, 5);
72 1703 return mesh;
73 }
74
75 123 TriangleMesh PyramidMesh() {
76
1/1
✓ Branch 1 taken 123 times.
123 TriangleMesh mesh;
77
1/1
✓ Branch 1 taken 123 times.
123 mesh.positions = {{-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {0, 0, 1}};
78
1/1
✓ Branch 1 taken 123 times.
123 AddQuad(mesh, 0, 1, 2, 3);
79
2/2
✓ Branch 0 taken 492 times.
✓ Branch 1 taken 123 times.
615 for (std::uint32_t i = 0; i < 4; ++i) {
80
1/1
✓ Branch 1 taken 492 times.
492 mesh.triangles.push_back({i, (i + 1) % 4, 4});
81 }
82 123 return mesh;
83 }
84
85 123 TriangleMesh PrismMesh() {
86 123 TriangleMesh mesh;
87
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 123 times.
369 for (const double x : {-1.0, 1.0}) {
88
1/1
✓ Branch 1 taken 246 times.
246 mesh.positions.push_back({x, -1, -1});
89
1/1
✓ Branch 1 taken 246 times.
246 mesh.positions.push_back({x, 1, -1});
90
1/1
✓ Branch 1 taken 246 times.
246 mesh.positions.push_back({x, 0, 1});
91 }
92
1/1
✓ Branch 1 taken 123 times.
123 mesh.triangles = {{0, 1, 2}, {3, 4, 5}};
93
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 123 times.
492 for (std::uint32_t i = 0; i < 3; ++i) {
94 369 const std::uint32_t j = (i + 1) % 3;
95
1/1
✓ Branch 1 taken 369 times.
369 AddQuad(mesh, i, j, j + 3, i + 3);
96 }
97 123 return mesh;
98 }
99
100 69 TriangleMesh TetrahedronMesh() {
101
1/1
✓ Branch 1 taken 69 times.
69 TriangleMesh mesh;
102
1/1
✓ Branch 1 taken 69 times.
69 mesh.positions = {{1, 1, 1}, {1, -1, -1}, {-1, 1, -1}, {-1, -1, 1}};
103
1/1
✓ Branch 1 taken 69 times.
69 mesh.triangles = {{0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3}};
104 69 return mesh;
105 }
106
107 } // namespace
108
109 3298 TriangleMesh BaseSolidMesh(BaseSolid solid) {
110
6/6
✓ Branch 0 taken 1279 times.
✓ Branch 1 taken 1703 times.
✓ Branch 2 taken 123 times.
✓ Branch 3 taken 123 times.
✓ Branch 4 taken 69 times.
✓ Branch 5 taken 1 time.
3298 switch (solid) {
111 1279 case BaseSolid::Icosahedron:
112 2558 return Outward(IcosahedronMesh());
113 1703 case BaseSolid::Cube:
114 3406 return Outward(CubeMesh());
115 123 case BaseSolid::Pyramid:
116 246 return Outward(PyramidMesh());
117 123 case BaseSolid::Prism:
118 246 return Outward(PrismMesh());
119 69 case BaseSolid::Tetrahedron:
120 138 return Outward(TetrahedronMesh());
121 }
122
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Unknown base solid");
123 }
124
125 3273 TriangleMesh Subdivide(const TriangleMesh& mesh, int levels, bool onUnitSphere) {
126
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3272 times.
3273 if (levels < 0) {
127
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Subdivision levels must not be negative");
128 }
129 3272 TriangleMesh result = mesh;
130
2/2
✓ Branch 1 taken 5401 times.
✓ Branch 2 taken 3272 times.
8673 for (int level = 0; level < levels; ++level) {
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5401 times.
5401 const double vertices = static_cast<double>(result.positions.size()) +
132 5401 1.5 * static_cast<double>(result.triangles.size());
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5401 times.
5401 if (vertices > static_cast<double>(std::numeric_limits<std::uint32_t>::max())) {
134
0/1
✗ Branch 2 not taken.
✗ throw std::invalid_argument("Subdivision would exceed 2^32 vertices");
135 }
136
1/1
✓ Branch 1 taken 5401 times.
5401 std::map<std::pair<std::uint32_t, std::uint32_t>, std::uint32_t> midpoints;
137 1345992 const auto midpoint = [&](std::uint32_t a, std::uint32_t b) {
138
2/2
✓ Branch 0 taken 670423 times.
✓ Branch 1 taken 670168 times.
1340591 const auto key = std::minmax(a, b);
139 1340591 const auto found = midpoints.find(key);
140
2/2
✓ Branch 0 taken 670407 times.
✓ Branch 1 taken 670249 times.
1340656 if (found != midpoints.end()) {
141 670407 return found->second;
142 }
143
2/2
✓ Branch 0 taken 519336 times.
✓ Branch 1 taken 150913 times.
670249 Vec3d middle = (result.positions[a] + result.positions[b]) * 0.5;
144
2/2
✓ Branch 0 taken 519336 times.
✓ Branch 1 taken 150913 times.
670249 if (onUnitSphere) {
145 519336 middle = Normalized(middle);
146 }
147 670245 const auto index = static_cast<std::uint32_t>(result.positions.size());
148 670245 result.positions.push_back(middle);
149 670408 midpoints.emplace(key, index);
150 670272 return index;
151 5401 };
152
1/1
✓ Branch 1 taken 5401 times.
5401 std::vector<std::array<std::uint32_t, 3>> triangles;
153
1/1
✓ Branch 1 taken 5401 times.
5401 triangles.reserve(4 * result.triangles.size());
154
2/2
✓ Branch 0 taken 447014 times.
✓ Branch 1 taken 5401 times.
452415 for (const auto& t : result.triangles) {
155
1/1
✓ Branch 1 taken 446959 times.
447014 const std::uint32_t ab = midpoint(t[0], t[1]);
156
1/1
✓ Branch 1 taken 446947 times.
446959 const std::uint32_t bc = midpoint(t[1], t[2]);
157
1/1
✓ Branch 1 taken 446906 times.
446947 const std::uint32_t ca = midpoint(t[2], t[0]);
158
1/1
✓ Branch 1 taken 447002 times.
446906 triangles.push_back({t[0], ab, ca});
159
1/1
✓ Branch 1 taken 447019 times.
447002 triangles.push_back({ab, t[1], bc});
160
1/1
✓ Branch 1 taken 447010 times.
447019 triangles.push_back({ca, bc, t[2]});
161
1/1
✓ Branch 1 taken 447014 times.
447010 triangles.push_back({ab, bc, ca});
162 }
163 5401 result.triangles = std::move(triangles);
164 5401 }
165 3272 return result;
166 }
167
168 } // namespace terraingen
169