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 | 51291 | for (auto& t : mesh.triangles) { | |
| 20 | 47994 | const Vec3d& a = mesh.positions[t[0]]; | |
| 21 | 47994 | const Vec3d normal = TriangleAreaNormal(a, mesh.positions[t[1]], mesh.positions[t[2]]); | |
| 22 | 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 | 16625 | {-t, 0, 1}}) { | |
| 51 | 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 | 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 | 15327 | for (int i = 0; i < 8; ++i) { | |
| 63 | 13624 | mesh.positions.push_back( | |
| 64 | 27248 | {(i & 1) != 0 ? 1.0 : -1.0, (i & 2) != 0 ? 1.0 : -1.0, (i & 4) != 0 ? 1.0 : -1.0}); | |
| 65 | } | ||
| 66 | 1703 | AddQuad(mesh, 0, 1, 3, 2); | |
| 67 | 1703 | AddQuad(mesh, 4, 5, 7, 6); | |
| 68 | 1703 | AddQuad(mesh, 0, 1, 5, 4); | |
| 69 | 1703 | AddQuad(mesh, 2, 3, 7, 6); | |
| 70 | 1703 | AddQuad(mesh, 0, 2, 6, 4); | |
| 71 | 1703 | AddQuad(mesh, 1, 3, 7, 5); | |
| 72 | 1703 | return mesh; | |
| 73 | } | ||
| 74 | |||
| 75 | 123 | TriangleMesh PyramidMesh() { | |
| 76 | 123 | TriangleMesh mesh; | |
| 77 | 123 | mesh.positions = {{-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {0, 0, 1}}; | |
| 78 | 123 | AddQuad(mesh, 0, 1, 2, 3); | |
| 79 | 615 | for (std::uint32_t i = 0; i < 4; ++i) { | |
| 80 | 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 | 369 | for (const double x : {-1.0, 1.0}) { | |
| 88 | 246 | mesh.positions.push_back({x, -1, -1}); | |
| 89 | 246 | mesh.positions.push_back({x, 1, -1}); | |
| 90 | 246 | mesh.positions.push_back({x, 0, 1}); | |
| 91 | } | ||
| 92 | 123 | mesh.triangles = {{0, 1, 2}, {3, 4, 5}}; | |
| 93 | 492 | for (std::uint32_t i = 0; i < 3; ++i) { | |
| 94 | 369 | const std::uint32_t j = (i + 1) % 3; | |
| 95 | 369 | AddQuad(mesh, i, j, j + 3, i + 3); | |
| 96 | } | ||
| 97 | 123 | return mesh; | |
| 98 | } | ||
| 99 | |||
| 100 | 69 | TriangleMesh TetrahedronMesh() { | |
| 101 | 69 | TriangleMesh mesh; | |
| 102 | 69 | mesh.positions = {{1, 1, 1}, {1, -1, -1}, {-1, 1, -1}, {-1, -1, 1}}; | |
| 103 | 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 | 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 | throw std::invalid_argument("Unknown base solid"); | |
| 123 | } | ||
| 124 | |||
| 125 | 3273 | TriangleMesh Subdivide(const TriangleMesh& mesh, int levels, bool onUnitSphere) { | |
| 126 | 3273 | if (levels < 0) { | |
| 127 | 1 | throw std::invalid_argument("Subdivision levels must not be negative"); | |
| 128 | } | ||
| 129 | 3272 | TriangleMesh result = mesh; | |
| 130 | 8673 | for (int level = 0; level < levels; ++level) { | |
| 131 | 5401 | const double vertices = static_cast<double>(result.positions.size()) + | |
| 132 | 5401 | 1.5 * static_cast<double>(result.triangles.size()); | |
| 133 | 5401 | if (vertices > static_cast<double>(std::numeric_limits<std::uint32_t>::max())) { | |
| 134 | ✗ | throw std::invalid_argument("Subdivision would exceed 2^32 vertices"); | |
| 135 | } | ||
| 136 | 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 | 1340591 | const auto key = std::minmax(a, b); | |
| 139 | 1340591 | const auto found = midpoints.find(key); | |
| 140 | 1340656 | if (found != midpoints.end()) { | |
| 141 | 670407 | return found->second; | |
| 142 | } | ||
| 143 | 670249 | Vec3d middle = (result.positions[a] + result.positions[b]) * 0.5; | |
| 144 | 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 | 5401 | std::vector<std::array<std::uint32_t, 3>> triangles; | |
| 153 | 5401 | triangles.reserve(4 * result.triangles.size()); | |
| 154 | 452415 | for (const auto& t : result.triangles) { | |
| 155 | 447014 | const std::uint32_t ab = midpoint(t[0], t[1]); | |
| 156 | 446959 | const std::uint32_t bc = midpoint(t[1], t[2]); | |
| 157 | 446947 | const std::uint32_t ca = midpoint(t[2], t[0]); | |
| 158 | 446906 | triangles.push_back({t[0], ab, ca}); | |
| 159 | 447002 | triangles.push_back({ab, t[1], bc}); | |
| 160 | 447019 | triangles.push_back({ca, bc, t[2]}); | |
| 161 | 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 |