src/geometry/cube_faces.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief The two cube layouts directions are gridded on: `CubeSphereCells`' faces and Gaskell's | ||
| 5 | /// ICQ faces. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/geometry/geometry.h" | ||
| 9 | |||
| 10 | #include <array> | ||
| 11 | #include <cmath> | ||
| 12 | #include <cstddef> | ||
| 13 | |||
| 14 | namespace terraingen::detail { | ||
| 15 | |||
| 16 | /// pi / 4 radians, the largest equiangular angle from a face's center to its edge. | ||
| 17 | inline constexpr double kQuarter = kPi / 4.0; | ||
| 18 | |||
| 19 | /// A cube face: its outward normal and the axes its first and second coordinates run along. | ||
| 20 | struct CubeFace { | ||
| 21 | Vec3d normal; | ||
| 22 | Vec3d u; | ||
| 23 | Vec3d v; | ||
| 24 | }; | ||
| 25 | |||
| 26 | /// The faces of `CubeSphereCells`: +x, -x, +y, -y, +z and -z, in order. | ||
| 27 | constexpr std::array<CubeFace, 6> kCubeFaces{{ | ||
| 28 | {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}}, | ||
| 29 | {{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}}, | ||
| 30 | {{0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}}, | ||
| 31 | {{0.0, -1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}}, | ||
| 32 | {{0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}}, | ||
| 33 | {{0.0, 0.0, -1.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}}, | ||
| 34 | }}; | ||
| 35 | |||
| 36 | /// Gaskell's ICQ faces 1 to 6, centered on +z, -y, -x, +y, +x and -z, with `u` the direction I runs | ||
| 37 | /// along and `v` the direction J runs along; measured from his published models. | ||
| 38 | constexpr std::array<CubeFace, 6> kIcqFaces{{ | ||
| 39 | {{0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}}, | ||
| 40 | {{0.0, -1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}}, | ||
| 41 | {{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}}, | ||
| 42 | {{0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}}, | ||
| 43 | {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, -1.0}}, | ||
| 44 | {{0.0, 0.0, -1.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}}, | ||
| 45 | }}; | ||
| 46 | |||
| 47 | /// The point `normal + a u + b v` on the plane of `face`, for gnomonic coordinates `a` and `b`. | ||
| 48 | 190763353 | inline Vec3d CubeFacePoint(const CubeFace& face, double a, double b) { | |
| 49 | 190763353 | return {face.normal.x + a * face.u.x + b * face.v.x, | |
| 50 | 190763353 | face.normal.y + a * face.u.y + b * face.v.y, | |
| 51 | 190763353 | face.normal.z + a * face.u.z + b * face.v.z}; | |
| 52 | } | ||
| 53 | |||
| 54 | /// The index in `faces` of the face a nonzero `direction` points through: the one whose normal it | ||
| 55 | /// is closest to, the first such face on ties. | ||
| 56 | 209544554 | inline std::size_t NearestFace(const std::array<CubeFace, 6>& faces, const Vec3d& direction) { | |
| 57 | 209544554 | std::size_t face = 0; | |
| 58 | 209544554 | double best = -2.0; | |
| 59 | 1423211498 | for (std::size_t f = 0; f < faces.size(); ++f) { | |
| 60 | 1213666944 | const double along = Dot(direction, faces[f].normal); | |
| 61 | 1213666944 | if (along > best) { | |
| 62 | 458753746 | best = along; | |
| 63 | 458753746 | face = f; | |
| 64 | } | ||
| 65 | } | ||
| 66 | 209544554 | return face; | |
| 67 | } | ||
| 68 | |||
| 69 | /// The equiangular coordinate in [0, `cells`] of gnomonic coordinate `g` along a face split into | ||
| 70 | /// `cells` equal angles per edge. | ||
| 71 | 212155516 | inline double EquiangularCoordinate(double g, double cells) { | |
| 72 | 212097914 | return (std::atan(g) + kQuarter) / (2.0 * kQuarter) * cells; | |
| 73 | } | ||
| 74 | |||
| 75 | /// A direction's cube face and its equiangular coordinates along the face's `u` and `v`. | ||
| 76 | struct FacePosition { | ||
| 77 | std::size_t face = 0; ///< Index of the face in its layout. | ||
| 78 | double s = 0.0; ///< Equiangular coordinate along `u`, in [0, cells]. | ||
| 79 | double t = 0.0; ///< Equiangular coordinate along `v`, in [0, cells]. | ||
| 80 | }; | ||
| 81 | |||
| 82 | /// The face of `faces` a nonzero `direction` points through (`NearestFace`) and the direction's | ||
| 83 | /// equiangular coordinates on it, for faces split into `cells` equal angles per edge. | ||
| 84 | 209659589 | inline FacePosition FaceCoordinates(const std::array<CubeFace, 6>& faces, const Vec3d& direction, | |
| 85 | double cells) { | ||
| 86 | 209659589 | const std::size_t face = NearestFace(faces, direction); | |
| 87 | 209659589 | const CubeFace& f = faces[face]; | |
| 88 | 209659589 | const double along = Dot(direction, f.normal); | |
| 89 | 209659589 | return {face, EquiangularCoordinate(Dot(direction, f.u) / along, cells), | |
| 90 | 209659589 | EquiangularCoordinate(Dot(direction, f.v) / along, cells)}; | |
| 91 | } | ||
| 92 | |||
| 93 | } // namespace terraingen::detail | ||
| 94 |