src/geometry/cube_sphere_cells.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/geometry/cells.h" | ||
| 4 | |||
| 5 | #include "geometry/cube_faces.h" | ||
| 6 | #include "terraingen/geometry/surface.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | #include "util/checks.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <array> | ||
| 12 | #include <atomic> | ||
| 13 | #include <cmath> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <limits> | ||
| 16 | #include <stdexcept> | ||
| 17 | |||
| 18 | namespace terraingen { | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | /// A new ID for a set of cube-sphere cells, never 0. | ||
| 23 | 6116 | std::uint64_t NextCellsId() { | |
| 24 | 6116 | static std::atomic<std::uint64_t> next{1}; | |
| 25 | 6116 | return next.fetch_add(1); | |
| 26 | } | ||
| 27 | |||
| 28 | using detail::kQuarter; | ||
| 29 | |||
| 30 | /// Cells per face edge over which an ellipsoid's global bound of the area per steradian is taken. | ||
| 31 | constexpr std::int32_t kCapCells = 64; | ||
| 32 | |||
| 33 | /// The solid angle of the gnomonic rectangle [0, `a`] x [0, `b`] on a face, signed. | ||
| 34 | 104997344 | double Corner(double a, double b) { | |
| 35 | 104997344 | return std::atan(a * b / std::sqrt(1.0 + a * a + b * b)); | |
| 36 | } | ||
| 37 | |||
| 38 | /// The solid angle of the gnomonic rectangle a1, a2, b1, b2 on a face. | ||
| 39 | 90978 | double SolidAngle(const std::array<double, 4>& bounds) { | |
| 40 | 90978 | const auto [a1, a2, b1, b2] = bounds; | |
| 41 | 90978 | return Corner(a2, b2) - Corner(a1, b2) - Corner(a2, b1) + Corner(a1, b1); | |
| 42 | } | ||
| 43 | |||
| 44 | /// The gnomonic coordinate of the edge `k` of `n` cells along a face. | ||
| 45 | 4851136 | double Edge(std::int32_t k, std::int32_t n) { | |
| 46 | 4851136 | return std::tan(-kQuarter + 2.0 * kQuarter * static_cast<double>(k) / static_cast<double>(n)); | |
| 47 | } | ||
| 48 | |||
| 49 | /// The index of the face holding `cell`, of `n` cells per edge. | ||
| 50 | 1562701 | std::size_t FaceIndex(const CellIndex& cell, std::int32_t n) { | |
| 51 | 1562701 | return static_cast<std::size_t>(cell.i / n); | |
| 52 | } | ||
| 53 | |||
| 54 | /// The face holding `cell`, of `n` cells per edge. | ||
| 55 | 1329521 | const detail::CubeFace& FaceAt(const CellIndex& cell, std::int32_t n) { | |
| 56 | 1329521 | return detail::kCubeFaces[FaceIndex(cell, n)]; | |
| 57 | } | ||
| 58 | |||
| 59 | 327863 | void SortUnique(std::vector<CellIndex>& cells) { | |
| 60 | 327863 | std::sort(cells.begin(), cells.end()); | |
| 61 | 327847 | cells.erase(std::unique(cells.begin(), cells.end()), cells.end()); | |
| 62 | 327821 | } | |
| 63 | |||
| 64 | /// Checks a region of longitudes and latitudes: finite and not inverted; a point is allowed. | ||
| 65 | 36321 | void CheckRegion(const Bounds2d& region) { | |
| 66 | 36321 | if (!(region.IsFinite() && region.maxX >= region.minX && region.maxY >= region.minY)) { | |
| 67 | 3 | throw std::invalid_argument("Region must be finite and not inverted"); | |
| 68 | } | ||
| 69 | 36318 | } | |
| 70 | |||
| 71 | /// The angle in radians from the center of a cell to its farthest corner, and that center. | ||
| 72 | 348147 | double CornerAngle(const detail::CubeFace& face, const std::array<double, 4>& bounds, | |
| 73 | Vec3d& center) { | ||
| 74 | 348147 | const auto [a1, a2, b1, b2] = bounds; | |
| 75 | 348147 | center = Normalized(detail::CubeFacePoint(face, 0.5 * (a1 + a2), 0.5 * (b1 + b2))); | |
| 76 | 348152 | double nearest = 1.0; | |
| 77 | 1390593 | for (const double a : {a1, a2}) { | |
| 78 | 2082880 | for (const double b : {b1, b2}) { | |
| 79 | 2247265 | nearest = std::min(nearest, Dot(center, Normalized(detail::CubeFacePoint(face, a, b)))); | |
| 80 | } | ||
| 81 | } | ||
| 82 | 348149 | return std::acos(std::clamp(nearest, -1.0, 1.0)); | |
| 83 | } | ||
| 84 | |||
| 85 | } // namespace | ||
| 86 | |||
| 87 | 15 | CubeSphereCells::CubeSphereCells(double radius) | |
| 88 | 15 | : m_surface(Surface::Sphere(radius)), m_id(NextCellsId()) { | |
| 89 | 14 | m_sphere = m_surface; | |
| 90 | 14 | } | |
| 91 | |||
| 92 | 6102 | CubeSphereCells::CubeSphereCells(const Surface& surface) : m_surface(surface), m_id(NextCellsId()) { | |
| 93 | 6102 | if (!surface.IsBody()) { | |
| 94 | 1 | throw std::invalid_argument("Cube-sphere cells need a body, not a plane"); | |
| 95 | } | ||
| 96 | 6101 | m_sphere = Surface::Sphere(surface.MinRadius()); | |
| 97 | 6101 | if (surface.IsSphere()) { | |
| 98 | return; | ||
| 99 | } | ||
| 100 | 29 | if (surface.Shape() != nullptr) { | |
| 101 | 17 | m_weightCap = surface.Shape()->MaxAreaPerSteradian(); | |
| 102 | 17 | return; | |
| 103 | } | ||
| 104 | 84 | for (std::size_t face = 0; face < detail::kCubeFaces.size(); ++face) { | |
| 105 | 4680 | for (std::int32_t i = 0; i < kCapCells; ++i) { | |
| 106 | 299520 | for (std::int32_t j = 0; j < kCapCells; ++j) { | |
| 107 | 589824 | m_weightCap = std::max( | |
| 108 | 295903 | m_weightCap, WeightBound(face, {Edge(i, kCapCells), Edge(i + 1, kCapCells), | |
| 109 | 294912 | Edge(j, kCapCells), Edge(j + 1, kCapCells)})); | |
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | 1 | } | |
| 114 | |||
| 115 | 917939 | std::array<double, 4> CubeSphereCells::Bounds(const CellIndex& cell, std::int32_t n) const { | |
| 116 | 917939 | const std::int32_t col = cell.i % n; | |
| 117 | 917939 | return {Edge(col, n), Edge(col + 1, n), Edge(cell.j, n), Edge(cell.j + 1, n)}; | |
| 118 | } | ||
| 119 | |||
| 120 | 302048 | double CubeSphereCells::Weight(const Vec2d& point) const { | |
| 121 | 302048 | return m_surface.AreaPerSteradian(point); | |
| 122 | } | ||
| 123 | |||
| 124 | 4267525 | std::int32_t CubeSphereCells::CellsPerEdge(double cellSize) const { | |
| 125 | 4267525 | RequirePositive(cellSize, "Cell size"); | |
| 126 | 4267467 | const double n = std::ceil(Radius() * 2.0 * kQuarter / cellSize); | |
| 127 | 4267467 | if (!(n < static_cast<double>(std::numeric_limits<std::int32_t>::max() / 6))) { | |
| 128 | 1 | throw std::invalid_argument("Cells are too small for the sphere"); | |
| 129 | } | ||
| 130 | 8391830 | return std::max<std::int32_t>(1, static_cast<std::int32_t>(n)); | |
| 131 | } | ||
| 132 | |||
| 133 | 3396612 | CellIndex CubeSphereCells::CellAt(double lonDeg, double latDeg, double cellSize) const { | |
| 134 | 3396612 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 135 | 6791805 | const detail::FacePosition at = detail::FaceCoordinates( | |
| 136 | 3396224 | detail::kCubeFaces, UnitVector(lonDeg, latDeg), static_cast<double>(n)); | |
| 137 | 10186713 | const auto index = [&](double s) { | |
| 138 | 6791132 | return std::clamp<std::int32_t>(static_cast<std::int32_t>(std::floor(s)), 0, n - 1); | |
| 139 | 3395581 | }; | |
| 140 | 3395581 | return {static_cast<std::int32_t>(at.face) * n + index(at.s), index(at.t)}; | |
| 141 | } | ||
| 142 | |||
| 143 | 229928 | std::vector<CellIndex> CubeSphereCells::Neighbors(const CellIndex& cell, double cellSize, | |
| 144 | int rings) const { | ||
| 145 | 229928 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 146 | 229934 | const auto face = static_cast<std::int32_t>(FaceIndex(cell, n)); | |
| 147 | 229934 | const std::int32_t col = cell.i % n; | |
| 148 | 229934 | std::vector<CellIndex> cells; | |
| 149 | 920390 | for (std::int32_t dj = -rings; dj <= rings; ++dj) { | |
| 150 | 2764496 | for (std::int32_t di = -rings; di <= rings; ++di) { | |
| 151 | 2074040 | const std::int32_t c = col + di; | |
| 152 | 2074040 | const std::int32_t r = cell.j + dj; | |
| 153 | 2074040 | if (c >= 0 && c < n && r >= 0 && r < n) { | |
| 154 | 1571314 | cells.push_back({face * n + c, r}); | |
| 155 | } else { | ||
| 156 | 502726 | AddAcrossEdge(FaceAt(cell, n), c, r, n, cellSize, cells); | |
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | 229973 | SortUnique(cells); | |
| 161 | 229949 | return cells; | |
| 162 | } | ||
| 163 | |||
| 164 | 502709 | void CubeSphereCells::AddAcrossEdge(const detail::CubeFace& face, std::int32_t col, | |
| 165 | std::int32_t row, std::int32_t n, double cellSize, | ||
| 166 | std::vector<CellIndex>& cells) const { | ||
| 167 | 502709 | const double step = 2.0 * kQuarter / static_cast<double>(n); | |
| 168 | 2010280 | for (const double su : {0.1, 0.5, 0.9}) { | |
| 169 | 6030462 | for (const double sv : {0.1, 0.5, 0.9}) { | |
| 170 | 4522891 | const double alpha = -kQuarter + (static_cast<double>(col) + su) * step; | |
| 171 | 4522891 | const double beta = -kQuarter + (static_cast<double>(row) + sv) * step; | |
| 172 | 4522891 | if (!(std::fabs(alpha) < 2.0 * kQuarter - step && | |
| 173 | 2038809 | std::fabs(beta) < 2.0 * kQuarter - step)) { | |
| 174 | 2941123 | continue; | |
| 175 | } | ||
| 176 | 1581768 | const Vec3d p = detail::CubeFacePoint(face, std::tan(alpha), std::tan(beta)); | |
| 177 | 1581768 | const Vec2d point = m_sphere.Coordinates(p); | |
| 178 | 3163747 | cells.push_back(CellAt(point.x, point.y, cellSize)); | |
| 179 | } | ||
| 180 | } | ||
| 181 | 502816 | } | |
| 182 | |||
| 183 | 36320 | std::vector<CellIndex> CubeSphereCells::CellsCovering(const Bounds2d& region, | |
| 184 | double cellSize) const { | ||
| 185 | 36320 | RequirePositive(cellSize, "Cell size"); | |
| 186 | 36319 | CheckRegion(region); | |
| 187 | 108951 | const Bounds2d box{region.minX, std::max(-90.0, region.minY), region.maxX, | |
| 188 | 71850 | std::min(90.0, region.maxY)}; | |
| 189 | 36317 | std::vector<CellIndex> near = CellsNear(box, cellSize); | |
| 190 | 36314 | if (!near.empty()) { | |
| 191 | 284 | return near; | |
| 192 | } | ||
| 193 | 36030 | const std::vector<CellIndex> seeds = Seeds(box, cellSize); | |
| 194 | 36032 | std::vector<CellIndex> cells; | |
| 195 | 265353 | for (const CellIndex& seed : seeds) { | |
| 196 | 229323 | const std::vector<CellIndex> around = Neighbors(seed, cellSize, 1); | |
| 197 | 229357 | cells.insert(cells.end(), around.begin(), around.end()); | |
| 198 | 229322 | } | |
| 199 | 36030 | SortUnique(cells); | |
| 200 | 36033 | return KeepReaching(box, seeds, cells, cellSize); | |
| 201 | 36314 | } | |
| 202 | |||
| 203 | 36316 | std::vector<CellIndex> CubeSphereCells::CellsNear(const Bounds2d& box, double cellSize) const { | |
| 204 | // A step of dtheta along the sphere moves a face's equiangular angle by at most dtheta / | ||
| 205 | // sqrt(1 - v^2), under 1.53 dtheta within half a cell of a face of 16 or more cells per edge, | ||
| 206 | // so widening a cell's angles by 2 rho keeps every cell within rho of the center. | ||
| 207 | 36316 | constexpr std::int32_t kMinCells = 16; | |
| 208 | 36316 | constexpr double kAngleRate = 2.0; | |
| 209 | 36316 | constexpr double kMaxLatitude = 80.0; | |
| 210 | 36316 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 211 | 36314 | const double halfLon = 0.5 * (box.maxX - box.minX) * kDegree; | |
| 212 | 36314 | const double halfLat = 0.5 * (box.maxY - box.minY) * kDegree; | |
| 213 | 36314 | const double nearestLat = box.minY > 0.0 ? box.minY : (box.maxY < 0.0 ? -box.maxY : 0.0); | |
| 214 | 36314 | const double rho = halfLat + halfLon * std::cos(nearestLat * kDegree); | |
| 215 | 36314 | const double step = 2.0 * kQuarter / static_cast<double>(n); | |
| 216 | 36314 | if (n < kMinCells || std::fmax(-box.minY, box.maxY) > kMaxLatitude || | |
| 217 | 11020 | !(kAngleRate * rho < step)) { | |
| 218 | 36030 | return {}; | |
| 219 | } | ||
| 220 | 284 | const Vec3d center = UnitVector(0.5 * (box.minX + box.maxX), 0.5 * (box.minY + box.maxY)); | |
| 221 | 284 | const detail::FacePosition at = | |
| 222 | 284 | detail::FaceCoordinates(detail::kCubeFaces, center, static_cast<double>(n)); | |
| 223 | 852 | const auto index = [n](double s) { | |
| 224 | 568 | return std::clamp<std::int32_t>(static_cast<std::int32_t>(std::floor(s)), 0, n - 1); | |
| 225 | 284 | }; | |
| 226 | 284 | const CellIndex home{static_cast<std::int32_t>(at.face) * n + index(at.s), index(at.t)}; | |
| 227 | 284 | const double widen = kAngleRate * rho; | |
| 228 | 284 | std::vector<CellIndex> cells; | |
| 229 | 2855 | for (const CellIndex& cell : Neighbors(home, cellSize, 1)) { | |
| 230 | 2571 | const std::size_t faceIndex = FaceIndex(cell, n); | |
| 231 | 2571 | const detail::CubeFace& face = detail::kCubeFaces[faceIndex]; | |
| 232 | 2571 | const double along = Dot(center, face.normal); | |
| 233 | 2571 | const bool onHome = faceIndex == at.face; | |
| 234 | 2571 | const double alpha = | |
| 235 | 2571 | onHome ? at.s * step - kQuarter : std::atan2(Dot(center, face.u), along); | |
| 236 | 2571 | const double beta = | |
| 237 | 2571 | onHome ? at.t * step - kQuarter : std::atan2(Dot(center, face.v), along); | |
| 238 | 2571 | const double alpha0 = -kQuarter + static_cast<double>(cell.i % n) * step; | |
| 239 | 2571 | const double beta0 = -kQuarter + static_cast<double>(cell.j) * step; | |
| 240 | 2571 | if (along <= 0.0 || (alpha >= alpha0 - widen && alpha <= alpha0 + step + widen && | |
| 241 | 1720 | beta >= beta0 - widen && beta <= beta0 + step + widen)) { | |
| 242 | 1179 | cells.push_back(cell); | |
| 243 | } | ||
| 244 | 284 | } | |
| 245 | 284 | return cells; | |
| 246 | 284 | } | |
| 247 | |||
| 248 | 36030 | std::vector<CellIndex> CubeSphereCells::Seeds(const Bounds2d& box, double cellSize) const { | |
| 249 | 36030 | const double step = cellSize / 4.0 / m_sphere.UnitLength(); | |
| 250 | 36030 | const auto rows = static_cast<std::int64_t>(std::ceil((box.maxY - box.minY) / step)); | |
| 251 | 36030 | std::vector<CellIndex> seeds; | |
| 252 | 221238 | for (std::int64_t r = 0; r <= rows; ++r) { | |
| 253 | 185205 | const double lat = std::min(box.maxY, box.minY + static_cast<double>(r) * step); | |
| 254 | 185205 | const double width = std::min(box.maxX - box.minX, 360.0); | |
| 255 | 185205 | const double cosLat = std::cos(lat * kDegree); | |
| 256 | 185205 | const auto columns = static_cast<std::int64_t>(std::ceil(width * cosLat / step)); | |
| 257 | 1752009 | for (std::int64_t c = 0; c <= columns; ++c) { | |
| 258 | 1566801 | const double lon = columns == 0 ? box.minX | |
| 259 | 1566801 | : box.minX + width * static_cast<double>(c) / | |
| 260 | 1566801 | static_cast<double>(columns); | |
| 261 | 3133605 | seeds.push_back(CellAt(lon, lat, cellSize)); | |
| 262 | } | ||
| 263 | } | ||
| 264 | 36033 | SortUnique(seeds); | |
| 265 | 36032 | return seeds; | |
| 266 | } | ||
| 267 | |||
| 268 | 36033 | std::vector<CellIndex> CubeSphereCells::KeepReaching(const Bounds2d& box, | |
| 269 | const std::vector<CellIndex>& seeds, | ||
| 270 | const std::vector<CellIndex>& cells, | ||
| 271 | double cellSize) const { | ||
| 272 | // A ring cell reaches the region only if its center lies within its farthest corner's angle of | ||
| 273 | // it, since cell edges are great circles; one expansion by the largest such angle tests them. | ||
| 274 | 36033 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 275 | 36033 | std::vector<CellIndex> ring; | |
| 276 | 36033 | std::vector<Vec2d> centers; | |
| 277 | 36033 | double widest = 0.0; | |
| 278 | 613573 | for (const CellIndex& cell : cells) { | |
| 279 | 577541 | if (std::binary_search(seeds.begin(), seeds.end(), cell)) { | |
| 280 | 229375 | continue; | |
| 281 | } | ||
| 282 | 348141 | Vec3d center; | |
| 283 | 348141 | widest = std::max(widest, CornerAngle(FaceAt(cell, n), Bounds(cell, n), center)); | |
| 284 | 348154 | ring.push_back(cell); | |
| 285 | 696325 | centers.push_back(m_sphere.Coordinates(center)); | |
| 286 | } | ||
| 287 | 36032 | if (ring.empty()) { | |
| 288 | 10179 | return cells; | |
| 289 | } | ||
| 290 | 25853 | const Bounds2d reach = m_sphere.Expand(box, Radius() * (widest * (1.0 + 1e-9) + 1e-12)); | |
| 291 | 25853 | std::vector<CellIndex> kept = seeds; | |
| 292 | 374022 | for (std::size_t k = 0; k < ring.size(); ++k) { | |
| 293 | 348169 | if (m_sphere.Contains(reach, centers[k])) { | |
| 294 | 86448 | kept.push_back(ring[k]); | |
| 295 | } | ||
| 296 | } | ||
| 297 | 25853 | SortUnique(kept); | |
| 298 | 25853 | return kept; | |
| 299 | 36032 | } | |
| 300 | |||
| 301 | 319681 | double CubeSphereCells::CellArea(const CellIndex& cell, double cellSize) const { | |
| 302 | 319681 | struct Recalled { | |
| 303 | std::uint64_t cells = 0; | ||
| 304 | std::uint64_t size = 0; | ||
| 305 | CellIndex cell; | ||
| 306 | double area = 0.0; | ||
| 307 | }; | ||
| 308 | 319681 | thread_local std::array<Recalled, 256> recalled; | |
| 309 | 319681 | const std::uint64_t size = BitCast<std::uint64_t>(cellSize); | |
| 310 | 319681 | const std::uint32_t mixed = (static_cast<std::uint32_t>(cell.i) * 0x9E3779B1u) ^ | |
| 311 | 319681 | (static_cast<std::uint32_t>(cell.j) * 0x85EBCA77u) ^ | |
| 312 | 319681 | static_cast<std::uint32_t>(size >> 29U); | |
| 313 | 319681 | Recalled& slot = recalled[(mixed >> 16U) % recalled.size()]; | |
| 314 | 319681 | if (slot.cells != m_id || slot.size != size || slot.cell != cell) { | |
| 315 | 161714 | slot = {m_id, size, cell, ComputeCellArea(cell, cellSize)}; | |
| 316 | } | ||
| 317 | 319689 | return slot.area; | |
| 318 | } | ||
| 319 | |||
| 320 | 161723 | double CubeSphereCells::ComputeCellArea(const CellIndex& cell, double cellSize) const { | |
| 321 | 161723 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 322 | 161735 | const std::array<double, 4> bounds = Bounds(cell, n); | |
| 323 | 161735 | if (m_surface.IsSphere()) { | |
| 324 | 87609 | return Radius() * Radius() * SolidAngle(bounds); | |
| 325 | } | ||
| 326 | 74126 | constexpr std::array<double, 5> kNodes{-0.906179845938664, -0.538469310105683, 0.0, | |
| 327 | 0.538469310105683, 0.906179845938664}; | ||
| 328 | 74126 | constexpr std::array<double, 5> kWeights{0.236926885056189, 0.478628670499366, | |
| 329 | 0.568888888888889, 0.478628670499366, | ||
| 330 | 0.236926885056189}; | ||
| 331 | 74126 | const auto [a1, a2, b1, b2] = bounds; | |
| 332 | 74126 | const detail::CubeFace& face = FaceAt(cell, n); | |
| 333 | 74126 | double area = 0.0; | |
| 334 | 444491 | for (std::size_t i = 0; i < kNodes.size(); ++i) { | |
| 335 | 370378 | const double a = 0.5 * (a1 + a2) + 0.5 * (a2 - a1) * kNodes[i]; | |
| 336 | 2215273 | for (std::size_t j = 0; j < kNodes.size(); ++j) { | |
| 337 | 1844908 | const double b = 0.5 * (b1 + b2) + 0.5 * (b2 - b1) * kNodes[j]; | |
| 338 | 1844908 | const double jacobian = std::pow(1.0 + a * a + b * b, -1.5); | |
| 339 | 1846252 | const double weight = m_surface.AreaPerSteradian( | |
| 340 | 1844908 | m_surface.Coordinates(detail::CubeFacePoint(face, a, b))); | |
| 341 | 1844895 | area += kWeights[i] * kWeights[j] * weight * jacobian; | |
| 342 | } | ||
| 343 | } | ||
| 344 | 74113 | return area * 0.25 * (a2 - a1) * (b2 - b1); | |
| 345 | } | ||
| 346 | |||
| 347 | 295729 | double CubeSphereCells::WeightBound(std::size_t face, const std::array<double, 4>& bounds) const { | |
| 348 | 887187 | const auto absolute = [](double lo, double hi) { | |
| 349 | 591458 | if (lo <= 0.0 && hi >= 0.0) { | |
| 350 | 28656 | return std::array<double, 2>{0.0, std::max(-lo, hi)}; | |
| 351 | } | ||
| 352 | 572306 | return std::array<double, 2>{std::min(std::fabs(lo), std::fabs(hi)), | |
| 353 | 858458 | std::max(std::fabs(lo), std::fabs(hi))}; | |
| 354 | }; | ||
| 355 | 295729 | const auto [a1, a2, b1, b2] = bounds; | |
| 356 | 295729 | const auto [aLo, aHi] = absolute(a1, a2); | |
| 357 | 295729 | const auto [bLo, bHi] = absolute(b1, b2); | |
| 358 | 295729 | const std::array<double, 3> largest{1.0 / (1.0 + aLo * aLo + bLo * bLo), | |
| 359 | 295729 | aHi * aHi / (1.0 + aHi * aHi + bLo * bLo), | |
| 360 | 295729 | bHi * bHi / (1.0 + aLo * aLo + bHi * bHi)}; | |
| 361 | 295729 | const std::array<double, 3> smallest{1.0 / (1.0 + aHi * aHi + bHi * bHi), | |
| 362 | 295729 | aLo * aLo / (1.0 + aLo * aLo + bHi * bHi), | |
| 363 | 295729 | bLo * bLo / (1.0 + aHi * aHi + bLo * bLo)}; | |
| 364 | 295729 | const detail::CubeFace& f = detail::kCubeFaces[face]; | |
| 365 | 295729 | const std::array<Vec3d, 3> axes{f.normal, f.u, f.v}; | |
| 366 | 295729 | const Vec3d radii = m_surface.Radii(); | |
| 367 | 295729 | const std::array<double, 3> r{radii.x, radii.y, radii.z}; | |
| 368 | 295729 | double numerator = 0.0; | |
| 369 | 295729 | double denominator = 0.0; | |
| 370 | 1182916 | for (std::size_t k = 0; k < 3; ++k) { | |
| 371 | 887187 | const std::size_t axis = axes[k].x != 0.0 ? 0 : (axes[k].y != 0.0 ? 1 : 2); | |
| 372 | 887187 | const double inverse = 1.0 / (r[axis] * r[axis]); | |
| 373 | 887187 | numerator += inverse * inverse * largest[k]; | |
| 374 | 887187 | denominator += inverse * smallest[k]; | |
| 375 | } | ||
| 376 | 295729 | return std::sqrt(numerator) / (denominator * denominator); | |
| 377 | } | ||
| 378 | |||
| 379 | 5427 | SurfaceCells::Sampling CubeSphereCells::CellSampling(const CellIndex& cell, double cellSize) const { | |
| 380 | 5427 | if (m_weightCap == 0.0) { | |
| 381 | 2059 | return {CellArea(cell, cellSize), 0.0}; | |
| 382 | } | ||
| 383 | 3368 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 384 | 3368 | const std::array<double, 4> bounds = Bounds(cell, n); | |
| 385 | 3368 | const double bound = m_surface.Shape() != nullptr | |
| 386 | 3368 | ? m_weightCap | |
| 387 | 1150 | : std::min(WeightBound(FaceIndex(cell, n), bounds), m_weightCap); | |
| 388 | 3368 | return {SolidAngle(bounds) * bound, bound}; | |
| 389 | } | ||
| 390 | |||
| 391 | 404732 | Vec2d CubeSphereCells::SamplePoint(double u, double v, const CellIndex& cell, | |
| 392 | double cellSize) const { | ||
| 393 | 404732 | const std::int32_t n = CellsPerEdge(cellSize); | |
| 394 | 404739 | const std::array<double, 4> box = Bounds(cell, n); | |
| 395 | 404739 | const double a1 = box[0]; | |
| 396 | 404739 | const double a2 = box[1]; | |
| 397 | 404739 | const double b1 = box[2]; | |
| 398 | 404739 | const double b2 = box[3]; | |
| 399 | 26598329 | const auto strip = [&](double a) { | |
| 400 | 26193590 | return Corner(a, b2) - Corner(a, b1) - Corner(a1, b2) + Corner(a1, b1); | |
| 401 | 404739 | }; | |
| 402 | 404739 | const double total = strip(a2); | |
| 403 | 404739 | double lo = a1; | |
| 404 | 404739 | double hi = a2; | |
| 405 | 26204851 | for (int iteration = 0; iteration < 64; ++iteration) { | |
| 406 | 25800179 | const double mid = 0.5 * (lo + hi); | |
| 407 | 25800179 | (strip(mid) < u * total ? lo : hi) = mid; | |
| 408 | } | ||
| 409 | 404710 | const double a = 0.5 * (lo + hi); | |
| 410 | 1618849 | const auto ratio = [&](double b) { return b / std::sqrt(1.0 + a * a + b * b); }; | |
| 411 | 404710 | const double k = ratio(b1) + v * (ratio(b2) - ratio(b1)); | |
| 412 | 404706 | const double b = k * std::sqrt(1.0 + a * a) / std::sqrt(1.0 - k * k); | |
| 413 | 404706 | return m_sphere.Coordinates(detail::CubeFacePoint(FaceAt(cell, n), a, b)); | |
| 414 | } | ||
| 415 | |||
| 416 | 3 | Vec3d CubeSphereCells::Direction(int face, double alpha, double beta) { | |
| 417 | 3 | const detail::CubeFace& f = | |
| 418 | 6 | detail::kCubeFaces[static_cast<std::size_t>(std::clamp(face, 0, 5))]; | |
| 419 | 3 | const Vec3d p = detail::CubeFacePoint(f, std::tan(alpha), std::tan(beta)); | |
| 420 | 3 | return p / Length(p); | |
| 421 | } | ||
| 422 | |||
| 423 | } // namespace terraingen | ||
| 424 |