src/sources/source.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/source.h" | ||
| 4 | |||
| 5 | #include "sources/lattice.h" | ||
| 6 | |||
| 7 | #include <cmath> | ||
| 8 | #include <limits> | ||
| 9 | #include <optional> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | 933 | void MarkMissing(Raster<float>& heights, std::optional<float> noData) { | |
| 16 | 933 | const float fill = noData.value_or(std::numeric_limits<float>::quiet_NaN()); | |
| 17 | 933 | bool missing = false; | |
| 18 | 7971668 | for (float& value : heights.Values()) { | |
| 19 | 7970735 | if (std::isnan(value)) { | |
| 20 | 185888 | value = fill; | |
| 21 | 185888 | missing = true; | |
| 22 | } | ||
| 23 | } | ||
| 24 | 933 | heights.SetNoData(noData || !missing ? noData : std::optional<float>(fill)); | |
| 25 | 933 | } | |
| 26 | |||
| 27 | 180 | std::optional<RasterGrid> detail::ExactSubgrid(const RasterGrid& grid, const PixelWindow& cells) { | |
| 28 | 180 | const GeoTransform& whole = grid.transform; | |
| 29 | 359 | if (whole.IsRotated() || cells.Empty()) { | |
| 30 | 4 | return std::nullopt; | |
| 31 | } | ||
| 32 | 176 | const RasterGrid part = Subgrid(grid, cells); | |
| 33 | 6890 | const auto center = [](const GeoTransform& transform, std::size_t col, std::size_t row) { | |
| 34 | 6890 | return transform.PixelToWorld(static_cast<double>(col) + 0.5, | |
| 35 | 3254 | static_cast<double>(row) + 0.5); | |
| 36 | }; | ||
| 37 | 3780 | for (std::size_t c = 0; c < part.width; ++c) { | |
| 38 | 3636 | if (center(whole, cells.colBegin + c, 0).x != center(part.transform, c, 0).x) { | |
| 39 | 32 | return std::nullopt; | |
| 40 | } | ||
| 41 | } | ||
| 42 | 3396 | for (std::size_t r = 0; r < part.height; ++r) { | |
| 43 | 3254 | if (center(whole, 0, cells.rowBegin + r).y != center(part.transform, 0, r).y) { | |
| 44 | 2 | return std::nullopt; | |
| 45 | } | ||
| 46 | } | ||
| 47 | 142 | return part; | |
| 48 | } | ||
| 49 | |||
| 50 | 2460494 | void Source::Fill(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 51 | 2460494 | FillSamples(samples, spacing, threads); | |
| 52 | 2465416 | if (Raster<float>* grid = samples.Grid()) { | |
| 53 | 930 | MarkMissing(*grid, grid->NoData()); | |
| 54 | } | ||
| 55 | 2465416 | } | |
| 56 | |||
| 57 | 932 | void Source::Fill(Raster<float>& heights, double spacing, unsigned threads) const { | |
| 58 | 932 | SampleBlock samples(heights); | |
| 59 | 932 | Fill(samples, spacing, threads); | |
| 60 | 930 | } | |
| 61 | |||
| 62 | 23 | void Source::Fill(const RasterGrid& grid, Span<float> heights, double spacing, | |
| 63 | unsigned threads) const { | ||
| 64 | 23 | if (heights.size() != grid.width * grid.height) { | |
| 65 | ✗ | throw std::invalid_argument("Filling a source needs " + | |
| 66 | ✗ | std::to_string(grid.width * grid.height) + " heights, not " + | |
| 67 | ✗ | std::to_string(heights.size())); | |
| 68 | } | ||
| 69 | 23 | Raster<float> filled(grid, heights); | |
| 70 | 23 | Fill(filled, spacing, threads); | |
| 71 | 8534 | for (float& value : heights) { | |
| 72 | 8511 | value = filled.IsMissing(value) ? std::numeric_limits<float>::quiet_NaN() : value; | |
| 73 | } | ||
| 74 | 23 | } | |
| 75 | |||
| 76 | 404072 | float Source::HeightAt(double x, double y, double spacing) const { | |
| 77 | 808144 | return detail::PointHeight(x, y, [&](SampleBlock& samples) { Fill(samples, spacing, 1); }); | |
| 78 | } | ||
| 79 | |||
| 80 | 2057486 | void Source::HeightsAt(Span<const Vec2d> points, Span<float> heights, double spacing) const { | |
| 81 | 2057486 | SampleBlock samples(points, heights); | |
| 82 | 2061336 | Fill(samples, spacing, 1); | |
| 83 | 2060761 | } | |
| 84 | |||
| 85 | } // namespace terraingen | ||
| 86 |