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 | 103770 | for (std::size_t t = 0; t < mesh.triangles.size(); ++t) { | |
| 28 | 103760 | std::array<Vec3d, 3> corner{}; | |
| 29 | 415040 | for (std::size_t k = 0; k < 3; ++k) { | |
| 30 | 311280 | corner[k] = mesh.positions[mesh.triangles[t][k]]; | |
| 31 | } | ||
| 32 | 726320 | for (std::size_t face = 0; face < 6; ++face) { | |
| 33 | 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 | 169568 | for (std::size_t j = j0; j <= j1; ++j) { | |
| 48 | 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 | 2490240 | for (const Vec3d& p : corner) { | |
| 67 | 1867680 | const double along = Dot(p, f.normal); | |
| 68 | 1867680 | if (along <= 0.0) { | |
| 69 | 945860 | behind = true; | |
| 70 | 945860 | continue; | |
| 71 | } | ||
| 72 | 921820 | front = true; | |
| 73 | 921820 | const double s = | |
| 74 | 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 | 921820 | sLo = std::min(sLo, s); | |
| 78 | 921820 | sHi = std::max(sHi, s); | |
| 79 | 921820 | tLo = std::min(tLo, t); | |
| 80 | 1353484 | tHi = std::max(tHi, t); | |
| 81 | } | ||
| 82 | 622560 | if (!front) { | |
| 83 | 309888 | return; | |
| 84 | } | ||
| 85 | 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 | 1356838 | for (std::size_t j = j0; j <= j1; ++j) { | |
| 96 | 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 | 13529710 | const Vec3d e1 = b - a; | |
| 112 | 13529710 | const Vec3d e2 = c - a; | |
| 113 | 13529710 | const Vec3d p = Cross(direction, e2); | |
| 114 | 13529710 | const double det = Dot(e1, p); | |
| 115 | 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 | 13527022 | const double u = Dot(s, p) * inverse; | |
| 121 | 13527022 | if (u < -kEdge || u > 1.0 + kEdge) { | |
| 122 | 13039092 | return std::nullopt; | |
| 123 | } | ||
| 124 | 487930 | const Vec3d q = Cross(s, e1); | |
| 125 | 487930 | const double v = Dot(direction, q) * inverse; | |
| 126 | 487930 | if (v < -kEdge || u + v > 1.0 + kEdge) { | |
| 127 | 422078 | return std::nullopt; | |
| 128 | } | ||
| 129 | 65852 | const double t = Dot(e2, q) * inverse; | |
| 130 | 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 | 13 | if (q == 0) { | |
| 140 | 2 | throw std::invalid_argument("A radial shape needs Q > 0"); | |
| 141 | } | ||
| 142 | 11 | if (mesh.triangles.empty()) { | |
| 143 | 1 | throw std::invalid_argument("A shape model mesh needs triangles"); | |
| 144 | } | ||
| 145 | 10 | const TriangleBins bins(mesh, q); | |
| 146 | 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 | 13587312 | for (const std::uint32_t t : near) { | |
| 155 | 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 | 13529710 | if (hit) { | |
| 159 | 65852 | outer = std::max(outer, *hit); | |
| 160 | 125888 | inner = std::min(inner, *hit); | |
| 161 | } | ||
| 162 | } | ||
| 163 | 57602 | if (outer == 0.0) { | |
| 164 | 2 | throw std::runtime_error( | |
| 165 | 2 | "A ray from the shape model's origin misses it: the mesh must enclose its origin"); | |
| 166 | } | ||
| 167 | 57600 | if (outer - inner > 1e-6 * outer) { | |
| 168 | 150 | ++folded; | |
| 169 | } | ||
| 170 | 57600 | return outer; | |
| 171 | 12 | }); | |
| 172 | 8 | if (folded > 0) { | |
| 173 | 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 |