src/sources/shape_model_source.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/shape_model_source.h" | ||
| 4 | |||
| 5 | #include "geometry/cube_faces.h" | ||
| 6 | #include "sources/lattice.h" | ||
| 7 | #include "terraingen/geometry/radial_shape.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <optional> | ||
| 12 | #include <stdexcept> | ||
| 13 | #include <utility> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// Equiangular angles beyond a face's center are kept this far short of a right angle, where the | ||
| 20 | /// face's plane no longer reaches. | ||
| 21 | constexpr double kMaxFaceAngle = 0.5 * kPi - 0.01; | ||
| 22 | |||
| 23 | /// The gnomonic coordinate of equiangular coordinate `s` of a face of `q` intervals per edge, | ||
| 24 | /// beyond the face too, up to `kMaxFaceAngle` from its center. | ||
| 25 | 351041660 | double ExtendedGnomonic(double s, double q) { | |
| 26 | 351041660 | const double angle = (s / q - 0.5) * 0.5 * kPi; | |
| 27 | 351041660 | return std::tan(std::clamp(angle, -kMaxFaceAngle, kMaxFaceAngle)); | |
| 28 | } | ||
| 29 | |||
| 30 | /// The weight of a face at equiangular coordinate `s` of `q` intervals per edge: 1 inside, 0 | ||
| 31 | /// half a post beyond either edge and a half on it. | ||
| 32 | 3107877 | double EdgeWeight(double s, double q) { | |
| 33 | 4587953 | return std::clamp(std::min(s, q - s) + 0.5, 0.0, 1.0); | |
| 34 | } | ||
| 35 | |||
| 36 | /// Where a direction lies on a face: its equiangular coordinates `s` and `t` and the face's weight | ||
| 37 | /// there. | ||
| 38 | struct FacePoint { | ||
| 39 | double s = 0.0; | ||
| 40 | double t = 0.0; | ||
| 41 | double weight = 0.0; | ||
| 42 | }; | ||
| 43 | |||
| 44 | /// Where unit `direction` lies on ICQ face `face` of `q` intervals per edge, if the face weighs it. | ||
| 45 | 3146264 | std::optional<FacePoint> OnFace(const Vec3d& direction, std::size_t face, double q) { | |
| 46 | 3146264 | const detail::CubeFace& f = detail::kIcqFaces[face]; | |
| 47 | 3146264 | const double along = Dot(direction, f.normal); | |
| 48 | 3146264 | if (along <= 0.0) { | |
| 49 | 1572157 | return std::nullopt; | |
| 50 | } | ||
| 51 | 1574107 | FacePoint at; | |
| 52 | 1574107 | at.s = detail::EquiangularCoordinate(Dot(direction, f.u) / along, q); | |
| 53 | 1574107 | at.t = detail::EquiangularCoordinate(Dot(direction, f.v) / along, q); | |
| 54 | 1574107 | at.weight = EdgeWeight(at.s, q) * EdgeWeight(at.t, q); | |
| 55 | 1574107 | return at.weight > 0.0 ? std::optional<FacePoint>(at) : std::nullopt; | |
| 56 | } | ||
| 57 | |||
| 58 | } // namespace | ||
| 59 | |||
| 60 | 168 | ShapeModelSource::FacePosts::FacePosts(const ShapeModelSource& source, std::size_t face) | |
| 61 | 168 | : m_source(source), m_face(face) {} | |
| 62 | |||
| 63 | 21314 | GeoTransform ShapeModelSource::FacePosts::Transform() const { | |
| 64 | 21314 | const double size = m_source.m_labelDegrees; | |
| 65 | 21314 | return GeoTransform::NorthUp(-0.5 * size, 0.5 * size, size); | |
| 66 | } | ||
| 67 | |||
| 68 | 30 | std::optional<PixelWindow> ShapeModelSource::FacePosts::Extent() const { | |
| 69 | 30 | const std::size_t first = m_face * static_cast<std::size_t>(kFaceColumns); | |
| 70 | 30 | const std::size_t labels = m_source.m_body.Shape()->Q() + 1; | |
| 71 | 30 | return PixelWindow{first, first + labels, 0, labels}; | |
| 72 | } | ||
| 73 | |||
| 74 | 92930 | void ShapeModelSource::FacePosts::Read(std::int64_t col0, std::int64_t row0, std::size_t width, | |
| 75 | std::size_t height, Span<float> heights) const { | ||
| 76 | 92930 | const RadialShape& shape = *m_source.m_body.Shape(); | |
| 77 | 92930 | const auto q = static_cast<std::int64_t>(shape.Q()); | |
| 78 | 92930 | const std::int64_t first = static_cast<std::int64_t>(m_face) * kFaceColumns; | |
| 79 | 92930 | const detail::CubeFace& face = detail::kIcqFaces[m_face]; | |
| 80 | 3434074 | for (std::size_t r = 0; r < height; ++r) { | |
| 81 | 3341132 | const std::int64_t j = row0 + static_cast<std::int64_t>(r); | |
| 82 | 200338306 | for (std::size_t c = 0; c < width; ++c) { | |
| 83 | 196997162 | const std::int64_t i = col0 + static_cast<std::int64_t>(c) - first; | |
| 84 | 196997162 | double radius = 0.0; | |
| 85 | 196997162 | if (i >= 0 && i <= q && j >= 0 && j <= q) { | |
| 86 | 14438488 | radius = shape.Radii()[shape.LabelIndex(static_cast<std::size_t>(i), | |
| 87 | 14438488 | static_cast<std::size_t>(j), m_face + 1)]; | |
| 88 | } else { | ||
| 89 | 182558674 | const Vec3d p = | |
| 90 | 182558674 | detail::CubeFacePoint(face, ExtendedGnomonic(static_cast<double>(i), q), | |
| 91 | ExtendedGnomonic(static_cast<double>(j), q)); | ||
| 92 | 182558674 | radius = shape.At(p / Length(p)); | |
| 93 | } | ||
| 94 | 192660880 | heights[r * width + c] = static_cast<float>(radius - m_source.m_reference); | |
| 95 | } | ||
| 96 | } | ||
| 97 | 92942 | } | |
| 98 | |||
| 99 | 33 | ShapeModelSource::ShapeModelSource(Surface body, std::shared_ptr<const Upsampler> upsampler) | |
| 100 | 33 | : m_body(std::move(body)) { | |
| 101 | 33 | const RadialShape* shape = m_body.Shape(); | |
| 102 | 33 | if (!shape) { | |
| 103 | 5 | throw std::invalid_argument("A shape-model source needs a shape-model body"); | |
| 104 | } | ||
| 105 | 28 | m_reference = 0.5 * (shape->MinRadius() + shape->MaxRadius()); | |
| 106 | 28 | m_labelDegrees = 90.0 / static_cast<double>(shape->Q()); | |
| 107 | 196 | for (std::size_t face = 0; face < m_posts.size(); ++face) { | |
| 108 | 168 | m_posts[face] = std::make_unique<FacePosts>(*this, face); | |
| 109 | 444 | m_upsamplers[face] = FittedUpsampler(upsampler, *m_posts[face]); | |
| 110 | } | ||
| 111 | 43 | } | |
| 112 | |||
| 113 | 11 | RasterGrid ShapeModelSource::Grid() const { | |
| 114 | 11 | return {0, 0, GeoTransform::NorthUp(-180.0, 90.0, m_labelDegrees)}; | |
| 115 | } | ||
| 116 | |||
| 117 | 5384 | void ShapeModelSource::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 118 | 5384 | detail::FillByTiles(samples, threads, [&](Span<const Vec2d> points, Span<float> heights) { | |
| 119 | 5390 | FillPoints(points, heights, spacing); | |
| 120 | 18 | }); | |
| 121 | 5384 | } | |
| 122 | |||
| 123 | 5389 | void ShapeModelSource::FillPoints(Span<const Vec2d> points, Span<float> heights, | |
| 124 | double spacing) const { | ||
| 125 | 5389 | const RadialShape& shape = *m_body.Shape(); | |
| 126 | 5389 | const auto q = static_cast<double>(shape.Q()); | |
| 127 | 5389 | std::vector<Vec3d> directions(points.size()); | |
| 128 | 534825 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 129 | 529435 | directions[i] = UnitVector(points[i].x, points[i].y); | |
| 130 | } | ||
| 131 | 5390 | std::vector<double> weighted(points.size(), 0.0); | |
| 132 | 5390 | std::vector<double> total(points.size(), 0.0); | |
| 133 | 5390 | std::vector<double> weights(points.size(), 0.0); | |
| 134 | 37730 | for (std::size_t face = 0; face < detail::kIcqFaces.size(); ++face) { | |
| 135 | 32340 | detail::EvaluateChosen( | |
| 136 | points.size(), | ||
| 137 | 3166485 | [&](std::size_t i) -> std::optional<Vec2d> { | |
| 138 | 3101805 | const std::optional<FacePoint> at = OnFace(directions[i], face, q); | |
| 139 | 3101805 | if (!at) { | |
| 140 | 2512727 | return std::nullopt; | |
| 141 | } | ||
| 142 | 589078 | weights[i] = at->weight; | |
| 143 | 589078 | const double column = | |
| 144 | 589078 | at->s + static_cast<double>(face) * static_cast<double>(kFaceColumns); | |
| 145 | 589078 | return Vec2d{column * m_labelDegrees, -at->t * m_labelDegrees}; | |
| 146 | }, | ||
| 147 | 72651 | [&](Span<const Vec2d> onFace, Span<float> values) { | |
| 148 | 7971 | m_upsamplers[face]->HeightsAt(*m_posts[face], onFace, values, spacing); | |
| 149 | 7971 | }, | |
| 150 | 32340 | [&](std::size_t i, float value) { | |
| 151 | 589181 | weighted[i] += weights[i] * static_cast<double>(value); | |
| 152 | 589181 | total[i] += weights[i]; | |
| 153 | 589181 | }); | |
| 154 | } | ||
| 155 | 536490 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 156 | 1062200 | heights[i] = | |
| 157 | 531100 | static_cast<float>(weighted[i] / total[i] + m_reference - shape.At(directions[i])); | |
| 158 | } | ||
| 159 | 5390 | } | |
| 160 | |||
| 161 | 393145 | double ShapeModelSource::ResolutionAt(double /*x*/, double /*y*/) const { | |
| 162 | 393145 | return m_labelDegrees; | |
| 163 | } | ||
| 164 | |||
| 165 | 1 | double ShapeModelSource::PostSpacingAt(double /*x*/, double /*y*/) const { | |
| 166 | 1 | return m_labelDegrees; | |
| 167 | } | ||
| 168 | |||
| 169 | } // namespace terraingen | ||
| 170 |