src/geometry/radial_shape.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/geometry/radial_shape.h" | ||
| 4 | |||
| 5 | #include "geometry/cube_faces.h" | ||
| 6 | #include "terraingen/util/bits.h" | ||
| 7 | #include "util/checks.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <array> | ||
| 11 | #include <cmath> | ||
| 12 | #include <map> | ||
| 13 | #include <stdexcept> | ||
| 14 | #include <utility> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | /// Factor on the largest area per steradian found at quadrilateral corners, for its variation | ||
| 21 | /// within the curved bilinear quadrilaterals. | ||
| 22 | constexpr double kAreaMargin = 1.25; | ||
| 23 | |||
| 24 | /// The gnomonic coordinate of label `k` of `q`, exactly -1, 0 or 1 at multiples of 45 degrees and | ||
| 25 | /// odd in `k - q/2`, so labels shared between faces get bitwise the same direction. | ||
| 26 | 18163268 | double Gnomonic(std::size_t k, std::size_t q) { | |
| 27 | 18163268 | if (2 * k > q) { | |
| 28 | 5970498 | return -Gnomonic(q - k, q); | |
| 29 | } | ||
| 30 | 12192770 | if (k == 0) { | |
| 31 | return -1.0; | ||
| 32 | } | ||
| 33 | 11784564 | if (2 * k == q) { | |
| 34 | return 0.0; | ||
| 35 | } | ||
| 36 | 11532868 | return std::tan(detail::kQuarter * | |
| 37 | 11532868 | (2.0 * static_cast<double>(k) / static_cast<double>(q) - 1.0)); | |
| 38 | } | ||
| 39 | |||
| 40 | } // namespace | ||
| 41 | |||
| 42 | 6096391 | Vec3d RadialShape::LabelDirection(std::size_t q, std::size_t i, std::size_t j, std::size_t face) { | |
| 43 | 6096391 | if (q == 0 || i > q || j > q || face < 1 || face > 6) { | |
| 44 | 6 | throw std::invalid_argument("An ICQ label needs Q > 0, I and J <= Q and F from 1 to 6"); | |
| 45 | } | ||
| 46 | 6096385 | const Vec3d p = | |
| 47 | 6096385 | detail::CubeFacePoint(detail::kIcqFaces[face - 1], Gnomonic(i, q), Gnomonic(j, q)); | |
| 48 | 6096385 | return p / Length(p); | |
| 49 | } | ||
| 50 | |||
| 51 | 101 | RadialShape RadialShape::Sample(std::size_t q, const std::function<double(const Vec3d&)>& radius) { | |
| 52 | 101 | if (q == 0) { | |
| 53 | 1 | throw std::invalid_argument("A radial shape needs Q > 0"); | |
| 54 | } | ||
| 55 | 100 | std::vector<double> radii(6 * (q + 1) * (q + 1)); | |
| 56 | 100 | ForEachIcqLabel(q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) { | |
| 57 | 1056494 | radii[k] = radius(LabelDirection(q, i, j, face)); | |
| 58 | 1056492 | }); | |
| 59 | 196 | return RadialShape(q, std::move(radii)); | |
| 60 | 100 | } | |
| 61 | |||
| 62 | 117 | RadialShape::RadialShape(std::size_t q, std::vector<double> radii) | |
| 63 | 117 | : m_q(q), m_radii(std::move(radii)) { | |
| 64 | 117 | if (m_q == 0) { | |
| 65 | ✗ | throw std::invalid_argument("A radial shape needs Q > 0"); | |
| 66 | } | ||
| 67 | 117 | if (m_radii.size() != 6 * (m_q + 1) * (m_q + 1)) { | |
| 68 | 2 | throw std::invalid_argument("A radial shape needs 6 (Q + 1)^2 label radii"); | |
| 69 | } | ||
| 70 | 115 | if (!std::all_of(m_radii.begin(), m_radii.end(), IsPositive)) { | |
| 71 | 1 | throw std::invalid_argument("A radial shape's radii must be positive and finite"); | |
| 72 | } | ||
| 73 | 114 | const auto [lo, hi] = std::minmax_element(m_radii.begin(), m_radii.end()); | |
| 74 | 114 | m_min = *lo; | |
| 75 | 114 | m_max = *hi; | |
| 76 | 114 | CheckSharedLabels(); | |
| 77 | 113 | m_maxArea = CornerAreaBound() * kAreaMargin; | |
| 78 | 117 | } | |
| 79 | |||
| 80 | 114 | void RadialShape::CheckSharedLabels() const { | |
| 81 | 114 | std::map<std::array<double, 3>, double> shared; | |
| 82 | 114 | ForEachIcqLabel(m_q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) { | |
| 83 | 1075993 | if (i != 0 && i != m_q && j != 0 && j != m_q) { | |
| 84 | 983144 | return; | |
| 85 | } | ||
| 86 | 92849 | const Vec3d d = LabelDirection(m_q, i, j, face); | |
| 87 | 185698 | const auto [it, inserted] = | |
| 88 | 92849 | shared.emplace(std::array<double, 3>{d.x, d.y, d.z}, m_radii[k]); | |
| 89 | 92849 | if (!inserted && std::fabs(it->second - m_radii[k]) > 1e-9 * m_max) { | |
| 90 | 1 | throw std::invalid_argument( | |
| 91 | 1 | "A radial shape's labels shared between faces must have equal radii"); | |
| 92 | } | ||
| 93 | }); | ||
| 94 | 113 | } | |
| 95 | |||
| 96 | 113 | double RadialShape::CornerAreaBound() const { | |
| 97 | 113 | double largest = 0.0; | |
| 98 | 791 | for (std::size_t face = 1; face <= 6; ++face) { | |
| 99 | 23886 | for (std::size_t j = 0; j < m_q; ++j) { | |
| 100 | 1052088 | for (std::size_t i = 0; i < m_q; ++i) { | |
| 101 | 1040277 | largest = std::max(largest, QuadAreaBound(i, j, face)); | |
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | 113 | return largest; | |
| 106 | } | ||
| 107 | |||
| 108 | 1028880 | double RadialShape::QuadAreaBound(std::size_t i, std::size_t j, std::size_t face) const { | |
| 109 | 1028880 | const std::array<std::pair<std::size_t, std::size_t>, 4> corners{ | |
| 110 | 1028880 | {{i, j}, {i + 1, j}, {i + 1, j + 1}, {i, j + 1}}}; | |
| 111 | 1028880 | std::array<Vec3d, 4> p{}; | |
| 112 | 5144400 | for (std::size_t k = 0; k < 4; ++k) { | |
| 113 | 4115520 | const auto [ci, cj] = corners[k]; | |
| 114 | 4115520 | p[k] = LabelDirection(m_q, ci, cj, face) * m_radii[LabelIndex(ci, cj, face)]; | |
| 115 | } | ||
| 116 | 1028880 | Vec3d normal = Normalized(Cross(p[2] - p[0], p[3] - p[1])); | |
| 117 | 1028880 | if (Dot(normal, p[0]) < 0.0) { | |
| 118 | 1028880 | normal = -normal; | |
| 119 | } | ||
| 120 | 1028880 | double largest = 0.0; | |
| 121 | 5144400 | for (const Vec3d& corner : p) { | |
| 122 | 4115520 | const double r = Length(corner); | |
| 123 | 4115520 | const double cosine = Dot(corner, normal) / r; | |
| 124 | 6323624 | largest = std::max(largest, r * r / std::max(cosine, 1e-3)); | |
| 125 | } | ||
| 126 | 1028880 | return largest; | |
| 127 | } | ||
| 128 | |||
| 129 | 206758751 | double RadialShape::At(const Vec3d& direction) const { | |
| 130 | 206758751 | const detail::FacePosition at = | |
| 131 | 206758751 | detail::FaceCoordinates(detail::kIcqFaces, direction, static_cast<double>(m_q)); | |
| 132 | 590366505 | const auto position = [&](double s) { | |
| 133 | 383607754 | const std::size_t k = ClampIndex(std::floor(s), m_q); | |
| 134 | 383607754 | return std::pair<std::size_t, double>{k, std::clamp(s - static_cast<double>(k), 0.0, 1.0)}; | |
| 135 | 206758751 | }; | |
| 136 | 206758751 | const auto [i, fi] = position(at.s); | |
| 137 | 206758751 | const auto [j, fj] = position(at.t); | |
| 138 | 206758751 | const std::size_t face = at.face + 1; | |
| 139 | 206758751 | const double r00 = m_radii[LabelIndex(i, j, face)]; | |
| 140 | 206758751 | const double r10 = m_radii[LabelIndex(i + 1, j, face)]; | |
| 141 | 206758751 | const double r01 = m_radii[LabelIndex(i, j + 1, face)]; | |
| 142 | 206758751 | const double r11 = m_radii[LabelIndex(i + 1, j + 1, face)]; | |
| 143 | 206758751 | return (1.0 - fj) * ((1.0 - fi) * r00 + fi * r10) + fj * ((1.0 - fi) * r01 + fi * r11); | |
| 144 | } | ||
| 145 | |||
| 146 | } // namespace terraingen | ||
| 147 |