src/sources/raster_source.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/raster_source.h" | ||
| 4 | |||
| 5 | #include "sources/lattice.h" | ||
| 6 | #include "sources/raster_heights.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | #include "terraingen/util/parallel.h" | ||
| 9 | #include "util/checks.h" | ||
| 10 | |||
| 11 | #include <algorithm> | ||
| 12 | #include <cmath> | ||
| 13 | #include <limits> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <utility> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | namespace terraingen { | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | /// A turn of longitude, in degrees. | ||
| 23 | constexpr double kTurnDegrees = 360.0; | ||
| 24 | |||
| 25 | /// The heights over the whole of a raster. | ||
| 26 | std::shared_ptr<const detail::RasterHeights> | ||
| 27 | 122 | WholeHeights(Raster<float> heights, const RasterSourceOptions& options, const GeoKeys& keys) { | |
| 28 | 122 | if (heights.Empty()) { | |
| 29 | 1 | throw std::invalid_argument("A raster source needs a raster with cells"); | |
| 30 | } | ||
| 31 | 121 | detail::ValidateRasterSourceOptions(options); | |
| 32 | 116 | const RasterPlacement placement = RasterPlacement::Whole(heights); | |
| 33 | 116 | const bool wraps = detail::WrapsLongitude(keys, placement.whole); | |
| 34 | 116 | const std::optional<float> noData = heights.NoData(); | |
| 35 | 116 | return std::make_shared<const detail::RasterHeights>( | |
| 36 | 232 | std::make_shared<RasterPosts>(std::make_shared<const Raster<float>>(std::move(heights)), | |
| 37 | placement, wraps), | ||
| 38 | 348 | placement.whole, noData, options, WholeWindow(placement.whole), "the raster", wraps); | |
| 39 | } | ||
| 40 | |||
| 41 | } // namespace | ||
| 42 | |||
| 43 | namespace detail { | ||
| 44 | |||
| 45 | 222 | bool WrapsLongitude(const GeoKeys& keys, const RasterGrid& lattice) { | |
| 46 | 222 | const GeoTransform& transform = lattice.transform; | |
| 47 | 222 | const double span = std::fabs(static_cast<double>(lattice.width) * transform.pixelWidth); | |
| 48 | 222 | return keys.IsGeographic() && !transform.IsRotated() && | |
| 49 | 13 | std::fabs(span - kTurnDegrees) <= 1e-9 * kTurnDegrees; | |
| 50 | } | ||
| 51 | |||
| 52 | 281 | void ValidateRasterSourceOptions(const RasterSourceOptions& options) { | |
| 53 | 281 | if (options.effectivePixelSize) { | |
| 54 | 16 | RequirePositive(*options.effectivePixelSize, "Effective pixel size"); | |
| 55 | } | ||
| 56 | 275 | } | |
| 57 | |||
| 58 | 104 | PixelWindow NativeWindow(const RasterGrid& native, int reach, const PixelWindow& cells) { | |
| 59 | 104 | const double margin = static_cast<double>(std::max(reach, 0)) + 1.0; | |
| 60 | 104 | const std::size_t columns = native.width + 1; | |
| 61 | 104 | const std::size_t rows = native.height + 1; | |
| 62 | 104 | return {ClampIndex(static_cast<double>(cells.colBegin) - margin, columns), | |
| 63 | 104 | ClampIndex(static_cast<double>(cells.colEnd) + margin, columns), | |
| 64 | 104 | ClampIndex(static_cast<double>(cells.rowBegin) - margin, rows), | |
| 65 | 104 | ClampIndex(static_cast<double>(cells.rowEnd) + margin, rows)}; | |
| 66 | } | ||
| 67 | |||
| 68 | 220 | RasterHeights::RasterHeights(std::shared_ptr<const PostLattice> posts, const RasterGrid& lattice, | |
| 69 | std::optional<float> noData, const RasterSourceOptions& options, | ||
| 70 | 220 | const PixelWindow& cells, std::string name, bool wrapLongitude) | |
| 71 | 220 | : m_posts(std::move(posts)), m_lattice(lattice), m_noData(noData), m_cells(cells), | |
| 72 | 304 | m_upsampler(FittedUpsampler(options.upsampler, *m_posts)), m_name(std::move(name)), | |
| 73 | 220 | m_wrapLongitude(wrapLongitude) { | |
| 74 | 220 | m_cellSpacing = PostSpacing(m_lattice.transform); | |
| 75 | 220 | m_resolution = options.effectivePixelSize.value_or(m_cellSpacing); | |
| 76 | 220 | } | |
| 77 | |||
| 78 | 1332353 | bool RasterHeights::Covers(double col, double row) const { | |
| 79 | 2622807 | return col >= static_cast<double>(m_cells.colBegin) && | |
| 80 | 1290454 | row >= static_cast<double>(m_cells.rowBegin) && | |
| 81 | 2596246 | col < static_cast<double>(m_cells.colEnd) && row < static_cast<double>(m_cells.rowEnd); | |
| 82 | } | ||
| 83 | |||
| 84 | 425 | PixelWindow RasterHeights::CoveredWindow(const RasterGrid& grid) const { | |
| 85 | 425 | const PixelWindow meets = PixelWindowIn(grid, Subgrid(m_lattice, m_cells).Bounds()); | |
| 86 | 425 | if (meets.Empty()) { | |
| 87 | 2 | return {}; | |
| 88 | } | ||
| 89 | 423 | return {meets.colBegin > 0 ? meets.colBegin - 1 : 0, std::min(meets.colEnd + 1, grid.width), | |
| 90 | 1173 | meets.rowBegin > 0 ? meets.rowBegin - 1 : 0, std::min(meets.rowEnd + 1, grid.height)}; | |
| 91 | } | ||
| 92 | |||
| 93 | 427 | void RasterHeights::FillUnwrapped(Raster<float>& heights, double spacing, unsigned threads) const { | |
| 94 | 427 | const GeoTransform& target = heights.Transform(); | |
| 95 | 427 | if (target.IsRotated() && !(AtPostSpacing(m_lattice.transform, spacing) && | |
| 96 | 2 | detail::OffsetOnLattice(m_lattice.transform, target))) { | |
| 97 | 4 | throw std::invalid_argument("A rotated grid off the lattice of " + m_name + | |
| 98 | 6 | " cannot be filled"); | |
| 99 | } | ||
| 100 | 425 | const RasterGrid grid = heights.Grid(); | |
| 101 | 425 | const PixelWindow window = CoveredWindow(grid); | |
| 102 | 425 | const std::optional<RasterGrid> part = | |
| 103 | 425 | window == WholeWindow(grid) ? std::nullopt : detail::ExactSubgrid(grid, window); | |
| 104 | 425 | Raster<float> covered; | |
| 105 | 425 | if (part) { | |
| 106 | 106 | covered = Raster<float>(*part); | |
| 107 | 106 | m_upsampler->Fill(*m_posts, covered, spacing, threads); | |
| 108 | 319 | } else if (!window.Empty()) { | |
| 109 | 317 | m_upsampler->Fill(*m_posts, heights, spacing, threads); | |
| 110 | } | ||
| 111 | 425 | ParallelFor(heights.Height(), threads, [&](std::size_t row) { | |
| 112 | 891233 | for (std::size_t col = 0; col < heights.Width(); ++col) { | |
| 113 | 880514 | heights(col, row) = HeightIn(heights, col, row, window, part ? &covered : nullptr); | |
| 114 | } | ||
| 115 | 10719 | }); | |
| 116 | 425 | heights.SetNoData(m_noData); | |
| 117 | 425 | } | |
| 118 | |||
| 119 | 882664 | float RasterHeights::HeightIn(const Raster<float>& heights, std::size_t col, std::size_t row, | |
| 120 | const PixelWindow& window, const Raster<float>* covered) const { | ||
| 121 | 882664 | constexpr float kMissing = std::numeric_limits<float>::quiet_NaN(); | |
| 122 | 882664 | if (col < window.colBegin || col >= window.colEnd || row < window.rowBegin || | |
| 123 | 771807 | row >= window.rowEnd) { | |
| 124 | return kMissing; | ||
| 125 | } | ||
| 126 | 760236 | const Vec2d center = heights.CellCenter(col, row); | |
| 127 | 760236 | const Vec2d pixel = m_lattice.transform.WorldToPixel(center.x, center.y); | |
| 128 | 760490 | if (!Covers(pixel.x, pixel.y)) { | |
| 129 | return kMissing; | ||
| 130 | } | ||
| 131 | 1430273 | return covered != nullptr ? (*covered)(col - window.colBegin, row - window.rowBegin) | |
| 132 | 677268 | : heights(col, row); | |
| 133 | } | ||
| 134 | |||
| 135 | 571652 | double RasterHeights::Turns(double x) const { | |
| 136 | 571652 | if (!m_wrapLongitude) { | |
| 137 | return 0.0; | ||
| 138 | } | ||
| 139 | 342 | const GeoTransform& transform = m_lattice.transform; | |
| 140 | 342 | const double left = | |
| 141 | 684 | std::min(transform.originX, | |
| 142 | 342 | transform.originX + static_cast<double>(m_lattice.width) * transform.pixelWidth); | |
| 143 | 342 | return std::floor((x - left) / kTurnDegrees); | |
| 144 | } | ||
| 145 | |||
| 146 | 308181 | void RasterHeights::Fill(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 147 | 308181 | if (Raster<float>* grid = samples.Grid()) { | |
| 148 | 422 | FillGrid(*grid, spacing, threads); | |
| 149 | } else { | ||
| 150 | 307759 | FillPoints(samples.Points(), samples.Heights(), spacing); | |
| 151 | } | ||
| 152 | 308170 | } | |
| 153 | |||
| 154 | 422 | void RasterHeights::FillGrid(Raster<float>& heights, double spacing, unsigned threads) const { | |
| 155 | 422 | const std::size_t width = heights.Width(); | |
| 156 | 520 | const auto turnsAt = [&](std::size_t col) { return Turns(heights.CellCenter(col, 0).x); }; | |
| 157 | 422 | if (!m_wrapLongitude || heights.Empty() || heights.Transform().IsRotated() || | |
| 158 | 9 | (turnsAt(0) == 0.0 && turnsAt(width - 1) == 0.0)) { | |
| 159 | 417 | FillUnwrapped(heights, spacing, threads); | |
| 160 | 415 | return; | |
| 161 | } | ||
| 162 | 15 | for (std::size_t begin = 0; begin < width;) { | |
| 163 | 10 | const double turns = turnsAt(begin); | |
| 164 | 10 | std::size_t end = begin + 1; | |
| 165 | 80 | while (end < width && turnsAt(end) == turns) { | |
| 166 | 70 | ++end; | |
| 167 | } | ||
| 168 | 10 | GeoTransform transform = heights.Transform(); | |
| 169 | 10 | transform.originX += | |
| 170 | 10 | static_cast<double>(begin) * transform.pixelWidth - turns * kTurnDegrees; | |
| 171 | 10 | Raster<float> part(RasterGrid{end - begin, heights.Height(), transform}); | |
| 172 | 10 | FillUnwrapped(part, spacing, threads); | |
| 173 | 58 | for (std::size_t row = 0; row < part.Height(); ++row) { | |
| 174 | 48 | std::copy_n(part.Row(row).begin(), part.Width(), heights.Row(row).begin() + begin); | |
| 175 | } | ||
| 176 | 10 | begin = end; | |
| 177 | 10 | } | |
| 178 | 5 | heights.SetNoData(m_noData); | |
| 179 | } | ||
| 180 | |||
| 181 | 571556 | Vec2d RasterHeights::Unwrapped(const Vec2d& point) const { | |
| 182 | 571556 | return {point.x - Turns(point.x) * kTurnDegrees, point.y}; | |
| 183 | } | ||
| 184 | |||
| 185 | 307759 | void RasterHeights::FillPoints(Span<const Vec2d> points, Span<float> heights, | |
| 186 | double spacing) const { | ||
| 187 | 307759 | std::fill(heights.begin(), heights.end(), std::numeric_limits<float>::quiet_NaN()); | |
| 188 | 307759 | detail::EvaluateChosen( | |
| 189 | points.size(), | ||
| 190 | 931158 | [&](std::size_t i) -> std::optional<Vec2d> { | |
| 191 | 315646 | const Vec2d at = Unwrapped(points[i]); | |
| 192 | 315647 | const Vec2d pixel = m_lattice.transform.WorldToPixel(at.x, at.y); | |
| 193 | 315647 | return Covers(pixel.x, pixel.y) ? std::optional<Vec2d>(at) : std::nullopt; | |
| 194 | }, | ||
| 195 | 862607 | [&](Span<const Vec2d> covered, Span<float> values) { | |
| 196 | 247095 | m_upsampler->HeightsAt(*m_posts, covered, values, spacing); | |
| 197 | 247091 | }, | |
| 198 | 562291 | [&](std::size_t i, float value) { heights[i] = value; }); | |
| 199 | 307753 | } | |
| 200 | |||
| 201 | 255908 | bool RasterHeights::HasHeight(double x, double y) const { | |
| 202 | 255908 | const Vec2d at = Unwrapped({x, y}); | |
| 203 | 255908 | const Vec2d pixel = m_lattice.transform.WorldToPixel(at.x, at.y); | |
| 204 | 255918 | if (!Covers(pixel.x, pixel.y)) { | |
| 205 | return false; | ||
| 206 | } | ||
| 207 | 228465 | float post = 0.0f; | |
| 208 | 228465 | m_posts->Read(static_cast<std::int64_t>(std::floor(pixel.x)), | |
| 209 | static_cast<std::int64_t>(std::floor(pixel.y)), 1, 1, Span<float>(&post, 1)); | ||
| 210 | 228465 | return !std::isnan(post); | |
| 211 | } | ||
| 212 | |||
| 213 | 255900 | double RasterHeights::ResolutionAt(double x, double y) const { | |
| 214 | 255900 | return HasHeight(x, y) ? m_resolution : std::numeric_limits<double>::infinity(); | |
| 215 | } | ||
| 216 | |||
| 217 | 6 | double RasterHeights::PostSpacingAt(double x, double y) const { | |
| 218 | 6 | return HasHeight(x, y) ? m_cellSpacing : std::numeric_limits<double>::infinity(); | |
| 219 | } | ||
| 220 | |||
| 221 | 255899 | double RasterHeightsSource::ResolutionAt(double x, double y) const { | |
| 222 | 255899 | return Heights().ResolutionAt(x, y); | |
| 223 | } | ||
| 224 | |||
| 225 | 6 | double RasterHeightsSource::PostSpacingAt(double x, double y) const { | |
| 226 | 6 | return Heights().PostSpacingAt(x, y); | |
| 227 | } | ||
| 228 | |||
| 229 | 308180 | void RasterHeightsSource::FillSamples(SampleBlock& samples, double spacing, | |
| 230 | unsigned threads) const { | ||
| 231 | 308180 | Heights().Fill(samples, spacing, threads); | |
| 232 | 308171 | } | |
| 233 | |||
| 234 | } // namespace detail | ||
| 235 | |||
| 236 | 122 | RasterSource::RasterSource(Raster<float> heights, const RasterSourceOptions& options, GeoKeys keys) | |
| 237 | 122 | : RasterHeightsSource(heights.Grid(), WholeWindow(heights.Grid()), std::move(keys)), | |
| 238 | 250 | m_heights(WholeHeights(std::move(heights), options, Keys())) {} | |
| 239 | |||
| 240 | 1 | const std::shared_ptr<const Upsampler>& RasterSource::UpsamplerInUse() const { | |
| 241 | 1 | return m_heights->UpsamplerInUse(); | |
| 242 | } | ||
| 243 | |||
| 244 | 178288 | const detail::RasterHeights& RasterSource::Heights() const { | |
| 245 | 178288 | return *m_heights; | |
| 246 | } | ||
| 247 | |||
| 248 | } // namespace terraingen | ||
| 249 |