src/mesh/mesh.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/mesh/mesh.h" | ||
| 4 | |||
| 5 | #include "util/checks.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cmath> | ||
| 9 | #include <cstddef> | ||
| 10 | #include <map> | ||
| 11 | #include <stdexcept> | ||
| 12 | #include <string> | ||
| 13 | #include <tuple> | ||
| 14 | #include <utility> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | 14276 | Box3d Bounds(const TriangleMesh& mesh) { | |
| 19 | 14276 | if (mesh.positions.empty()) { | |
| 20 | 1 | throw std::invalid_argument("An empty mesh has no bounds"); | |
| 21 | } | ||
| 22 | 14275 | Box3d box{mesh.positions.front(), mesh.positions.front()}; | |
| 23 | 6201579 | for (const Vec3d& p : mesh.positions) { | |
| 24 | 6187304 | box.min = {std::fmin(box.min.x, p.x), std::fmin(box.min.y, p.y), std::fmin(box.min.z, p.z)}; | |
| 25 | 6187304 | box.max = {std::fmax(box.max.x, p.x), std::fmax(box.max.y, p.y), std::fmax(box.max.z, p.z)}; | |
| 26 | } | ||
| 27 | 14275 | return box; | |
| 28 | } | ||
| 29 | |||
| 30 | namespace { | ||
| 31 | |||
| 32 | /// Twice the area-weighted normal of triangle `t`. | ||
| 33 | 467026 | Vec3d AreaNormal(const TriangleMesh& mesh, const std::array<std::uint32_t, 3>& t) { | |
| 34 | 467026 | return TriangleAreaNormal(mesh.positions[t[0]], mesh.positions[t[1]], mesh.positions[t[2]]); | |
| 35 | } | ||
| 36 | |||
| 37 | } // namespace | ||
| 38 | |||
| 39 | 1001 | void ValidateMesh(const TriangleMesh& mesh) { | |
| 40 | 1589402 | for (const auto& t : mesh.triangles) { | |
| 41 | 6353619 | for (const std::uint32_t v : t) { | |
| 42 | 4765218 | if (v >= mesh.positions.size()) { | |
| 43 | 5 | throw std::invalid_argument("A triangle indexes past the mesh's vertices"); | |
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | 996 | } | |
| 48 | |||
| 49 | 9 | std::vector<Vec3d> VertexNormals(const TriangleMesh& mesh) { | |
| 50 | 9 | ValidateMesh(mesh); | |
| 51 | 8 | std::vector<Vec3d> normals(mesh.positions.size()); | |
| 52 | 26140 | for (const auto& t : mesh.triangles) { | |
| 53 | 26132 | const Vec3d n = AreaNormal(mesh, t); | |
| 54 | 104528 | for (const std::uint32_t v : t) { | |
| 55 | 78396 | normals[v] = normals[v] + n; | |
| 56 | } | ||
| 57 | } | ||
| 58 | 16491 | for (Vec3d& n : normals) { | |
| 59 | 16483 | n = Normalized(n); | |
| 60 | } | ||
| 61 | 8 | return normals; | |
| 62 | } | ||
| 63 | |||
| 64 | 248 | ShadedMesh WithCreases(const TriangleMesh& mesh, double creaseAngle) { | |
| 65 | 248 | RequireNonNegative(creaseAngle, "Crease angle"); | |
| 66 | 247 | ValidateMesh(mesh); | |
| 67 | 246 | const double cosine = std::cos(creaseAngle); | |
| 68 | 246 | std::vector<Vec3d> areaNormals; | |
| 69 | 246 | std::vector<Vec3d> unitNormals; | |
| 70 | 246 | std::vector<std::vector<std::size_t>> around(mesh.positions.size()); | |
| 71 | 441140 | for (std::size_t f = 0; f < mesh.triangles.size(); ++f) { | |
| 72 | 440894 | areaNormals.push_back(AreaNormal(mesh, mesh.triangles[f])); | |
| 73 | 440894 | unitNormals.push_back(Normalized(areaNormals.back())); | |
| 74 | 1763576 | for (const std::uint32_t v : mesh.triangles[f]) { | |
| 75 | 1322682 | around[v].push_back(f); | |
| 76 | } | ||
| 77 | } | ||
| 78 | 246 | ShadedMesh shaded; | |
| 79 | 246 | std::map<std::tuple<std::uint32_t, double, double, double>, std::uint32_t> made; | |
| 80 | 441140 | for (std::size_t f = 0; f < mesh.triangles.size(); ++f) { | |
| 81 | 440894 | std::array<std::uint32_t, 3> triangle{}; | |
| 82 | 1763576 | for (std::size_t k = 0; k < 3; ++k) { | |
| 83 | 1322682 | const std::uint32_t v = mesh.triangles[f][k]; | |
| 84 | 1322682 | Vec3d sum{}; | |
| 85 | 9246600 | for (const std::size_t g : around[v]) { | |
| 86 | 7923918 | if (Dot(unitNormals[f], unitNormals[g]) >= cosine) { | |
| 87 | 7845609 | sum = sum + areaNormals[g]; | |
| 88 | } | ||
| 89 | } | ||
| 90 | 1322682 | const Vec3d normal = Normalized(sum); | |
| 91 | 1322682 | const auto key = std::make_tuple(v, normal.x, normal.y, normal.z); | |
| 92 | 1322682 | const auto found = made.find(key); | |
| 93 | 1322682 | if (found != made.end()) { | |
| 94 | 1088148 | triangle[k] = found->second; | |
| 95 | 1088148 | continue; | |
| 96 | } | ||
| 97 | 234534 | const auto index = static_cast<std::uint32_t>(shaded.mesh.positions.size()); | |
| 98 | 234534 | shaded.mesh.positions.push_back(mesh.positions[v]); | |
| 99 | 234534 | shaded.normals.push_back(normal); | |
| 100 | 234534 | made.emplace(key, index); | |
| 101 | 234534 | triangle[k] = index; | |
| 102 | } | ||
| 103 | 440894 | shaded.mesh.triangles.push_back(triangle); | |
| 104 | } | ||
| 105 | 492 | return shaded; | |
| 106 | 246 | } | |
| 107 | |||
| 108 | 27010214 | Vec3d RigidTransform::Apply(const Vec3d& p) const { | |
| 109 | 27010214 | return Rotate(p) + translation; | |
| 110 | } | ||
| 111 | |||
| 112 | 27013654 | Vec3d RigidTransform::Rotate(const Vec3d& v) const { | |
| 113 | 27013654 | const auto& r = rotation; | |
| 114 | 27013654 | return {r[0] * v.x + r[1] * v.y + r[2] * v.z, r[3] * v.x + r[4] * v.y + r[5] * v.z, | |
| 115 | 27013654 | r[6] * v.x + r[7] * v.y + r[8] * v.z}; | |
| 116 | } | ||
| 117 | |||
| 118 | 87353 | RigidTransform RigidTransform::After(const RigidTransform& first) const { | |
| 119 | 87353 | RigidTransform combined; | |
| 120 | 349391 | for (std::size_t i = 0; i < 3; ++i) { | |
| 121 | 1048152 | for (std::size_t j = 0; j < 3; ++j) { | |
| 122 | double sum = 0.0; | ||
| 123 | 3144456 | for (std::size_t k = 0; k < 3; ++k) { | |
| 124 | 2358342 | sum += rotation[3 * i + k] * first.rotation[3 * k + j]; | |
| 125 | } | ||
| 126 | 786114 | combined.rotation[3 * i + j] = sum; | |
| 127 | } | ||
| 128 | } | ||
| 129 | 87353 | combined.translation = Apply(first.translation); | |
| 130 | 87353 | return combined; | |
| 131 | } | ||
| 132 | |||
| 133 | 17690 | RigidTransform RigidTransform::RotationX(double angle) { | |
| 134 | 17690 | const double c = std::cos(angle); | |
| 135 | 17690 | const double s = std::sin(angle); | |
| 136 | 17690 | return {{1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c}, {}}; | |
| 137 | } | ||
| 138 | |||
| 139 | 54 | RigidTransform RigidTransform::RotationY(double angle) { | |
| 140 | 54 | const double c = std::cos(angle); | |
| 141 | 54 | const double s = std::sin(angle); | |
| 142 | 54 | return {{c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c}, {}}; | |
| 143 | } | ||
| 144 | |||
| 145 | 17087 | RigidTransform RigidTransform::RotationZ(double angle) { | |
| 146 | 17087 | const double c = std::cos(angle); | |
| 147 | 17087 | const double s = std::sin(angle); | |
| 148 | 17087 | return {{c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0}, {}}; | |
| 149 | } | ||
| 150 | |||
| 151 | 5 | RigidTransform RigidTransform::Translation(const Vec3d& offset) { | |
| 152 | 5 | RigidTransform transform; | |
| 153 | 5 | transform.translation = offset; | |
| 154 | 5 | return transform; | |
| 155 | } | ||
| 156 | |||
| 157 | 300 | void RequireRotation(const std::array<double, 9>& r) { | |
| 158 | 1197 | for (std::size_t i = 0; i < 3; ++i) { | |
| 159 | 3589 | for (std::size_t j = 0; j < 3; ++j) { | |
| 160 | double dot = 0.0; | ||
| 161 | 10768 | for (std::size_t k = 0; k < 3; ++k) { | |
| 162 | 8076 | dot += r[3 * i + k] * r[3 * j + k]; | |
| 163 | } | ||
| 164 | 4486 | if (std::fabs(dot - (i == j ? 1.0 : 0.0)) > 1e-9) { | |
| 165 | 1 | throw std::invalid_argument("A rotation matrix must be orthonormal"); | |
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | 299 | const double det = r[0] * (r[4] * r[8] - r[5] * r[7]) - r[1] * (r[3] * r[8] - r[5] * r[6]) + | |
| 170 | 299 | r[2] * (r[3] * r[7] - r[4] * r[6]); | |
| 171 | 299 | if (det < 0.0) { | |
| 172 | 2 | throw std::invalid_argument("A rotation matrix must not be a reflection"); | |
| 173 | } | ||
| 174 | 297 | } | |
| 175 | |||
| 176 | 3560 | void RequireScaleFactors(const Vec3d& scale, std::string_view what) { | |
| 177 | 14228 | for (const double s : {scale.x, scale.y, scale.z}) { | |
| 178 | 10673 | if (!(std::isfinite(s) && s != 0.0)) { | |
| 179 | 20 | throw std::invalid_argument(std::string(what) + " must be finite and not zero"); | |
| 180 | } | ||
| 181 | } | ||
| 182 | 3555 | } | |
| 183 | |||
| 184 | 3259 | TriangleMesh Scaled(const TriangleMesh& mesh, const Vec3d& scale) { | |
| 185 | 3259 | RequireScaleFactors(scale, "Scale factors"); | |
| 186 | 3258 | TriangleMesh scaled = mesh; | |
| 187 | 679827 | for (Vec3d& p : scaled.positions) { | |
| 188 | 676569 | p = {p.x * scale.x, p.y * scale.y, p.z * scale.z}; | |
| 189 | } | ||
| 190 | 3258 | if (scale.x * scale.y * scale.z < 0.0) { | |
| 191 | 13 | for (auto& t : scaled.triangles) { | |
| 192 | 12 | std::swap(t[1], t[2]); | |
| 193 | } | ||
| 194 | } | ||
| 195 | 3258 | return scaled; | |
| 196 | } | ||
| 197 | |||
| 198 | 28 | void CutAtPlane(TriangleMesh& mesh, const Vec3d& normal, std::vector<double> distances) { | |
| 199 | 28 | std::map<std::pair<std::uint32_t, std::uint32_t>, std::uint32_t> crossings; | |
| 200 | 4694 | const auto crossing = [&](std::uint32_t a, std::uint32_t b) { | |
| 201 | 4666 | const auto key = std::minmax(a, b); | |
| 202 | 4666 | const auto found = crossings.find(key); | |
| 203 | 4666 | if (found != crossings.end()) { | |
| 204 | 2333 | return found->second; | |
| 205 | } | ||
| 206 | 2333 | const double t = distances[a] / (distances[a] - distances[b]); | |
| 207 | 2333 | const Vec3d& pa = mesh.positions[a]; | |
| 208 | 2333 | mesh.positions.push_back(pa + (mesh.positions[b] - pa) * t); | |
| 209 | 2333 | distances.push_back(0.0); | |
| 210 | 2333 | const auto index = static_cast<std::uint32_t>(mesh.positions.size() - 1); | |
| 211 | 2333 | crossings.emplace(key, index); | |
| 212 | 2333 | return index; | |
| 213 | 28 | }; | |
| 214 | 28 | std::vector<std::array<std::uint32_t, 3>> triangles; | |
| 215 | 28 | triangles.reserve(mesh.triangles.size()); | |
| 216 | 31656 | for (const auto& triangle : mesh.triangles) { | |
| 217 | 4712 | const auto above = [&](std::uint32_t v) { return distances[v] > 0.0; }; | |
| 218 | 31628 | const int count = static_cast<int>(above(triangle[0])) + | |
| 219 | 31628 | static_cast<int>(above(triangle[1])) + | |
| 220 | 31628 | static_cast<int>(above(triangle[2])); | |
| 221 | 31628 | if (count == 0 || count == 3) { | |
| 222 | 29295 | triangles.push_back(triangle); | |
| 223 | 29295 | continue; | |
| 224 | } | ||
| 225 | std::size_t lone = 0; | ||
| 226 | 4712 | while (above(triangle[lone]) == (count == 2)) { | |
| 227 | 2379 | ++lone; | |
| 228 | } | ||
| 229 | 2333 | const std::uint32_t a = triangle[lone]; | |
| 230 | 2333 | const std::uint32_t b = triangle[(lone + 1) % 3]; | |
| 231 | 2333 | const std::uint32_t c = triangle[(lone + 2) % 3]; | |
| 232 | 2333 | const std::uint32_t ab = crossing(a, b); | |
| 233 | 2333 | const std::uint32_t ac = crossing(a, c); | |
| 234 | 2333 | triangles.push_back({a, ab, ac}); | |
| 235 | 2333 | triangles.push_back({ab, b, c}); | |
| 236 | 2333 | triangles.push_back({ab, c, ac}); | |
| 237 | } | ||
| 238 | 28 | mesh.triangles = std::move(triangles); | |
| 239 | 18231 | for (std::size_t i = 0; i < distances.size(); ++i) { | |
| 240 | 18203 | if (distances[i] > 0.0) { | |
| 241 | 8339 | mesh.positions[i] = mesh.positions[i] - normal * distances[i]; | |
| 242 | } | ||
| 243 | } | ||
| 244 | 28 | } | |
| 245 | |||
| 246 | } // namespace terraingen | ||
| 247 |