src/layers/field.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/field.h" | ||
| 4 | |||
| 5 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 6 | #include "util/checks.h" | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <cmath> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <tuple> | ||
| 12 | #include <utility> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | 12 | MinimumField::MinimumField(std::shared_ptr<const SurfaceField> a, | |
| 17 | 12 | std::shared_ptr<const SurfaceField> b) | |
| 18 | 12 | : m_a(std::move(a)), m_b(std::move(b)) { | |
| 19 | 12 | if (!m_a || !m_b) { | |
| 20 | 2 | throw std::invalid_argument("A minimum field cannot take a null field"); | |
| 21 | } | ||
| 22 | 13 | } | |
| 23 | |||
| 24 | 205 | double MinimumField::ValueAt(double x, double y) const { | |
| 25 | 205 | return std::fmin(m_a->ValueAt(x, y), m_b->ValueAt(x, y)); | |
| 26 | } | ||
| 27 | |||
| 28 | 3 | double MinimumField::Max() const { | |
| 29 | 3 | return std::fmax(m_a->Max(), m_b->Max()); | |
| 30 | } | ||
| 31 | |||
| 32 | 18 | void ValidateRasterFieldOptions(const RasterFieldOptions& options) { | |
| 33 | 18 | if (options.method != ResampleMethod::Nearest && options.method != ResampleMethod::Bilinear) { | |
| 34 | 2 | throw std::invalid_argument( | |
| 35 | 2 | "A raster field reads nearest or bilinear, which stay within the cells read"); | |
| 36 | } | ||
| 37 | 16 | RequirePositive(options.scale, "Raster field scale"); | |
| 38 | 15 | } | |
| 39 | |||
| 40 | 18 | RasterField::RasterField(Raster<float> raster, RasterFieldOptions options) | |
| 41 | 18 | : m_raster(std::move(raster)), m_options(options) { | |
| 42 | 18 | ValidateRasterFieldOptions(m_options); | |
| 43 | 15 | if (m_raster.Empty()) { | |
| 44 | 1 | throw std::invalid_argument("A raster field needs a raster"); | |
| 45 | } | ||
| 46 | 14 | const GeoTransform& transform = m_raster.Transform(); | |
| 47 | 14 | if (transform.IsRotated()) { | |
| 48 | 1 | throw std::invalid_argument("A raster field cannot read a rotated raster"); | |
| 49 | } | ||
| 50 | 20089 | for (const float value : m_raster.Values()) { | |
| 51 | 20118 | if (std::isfinite(value) && !m_raster.IsNoData(value) && | |
| 52 | 20063 | !(static_cast<double>(value) * m_options.scale <= m_max)) { | |
| 53 | 233 | m_max = static_cast<double>(value) * m_options.scale; | |
| 54 | } | ||
| 55 | } | ||
| 56 | 13 | if (std::isfinite(m_options.fallback) && !(m_options.fallback <= m_max)) { | |
| 57 | 1 | m_max = m_options.fallback; | |
| 58 | } | ||
| 59 | 18 | } | |
| 60 | |||
| 61 | 42023 | double RasterField::Sample(double x, double y) const { | |
| 62 | 42023 | const Vec2d pixel = m_raster.Transform().WorldToPixel(x, y); | |
| 63 | 42023 | const auto width = static_cast<double>(m_raster.Width()); | |
| 64 | 42023 | const auto height = static_cast<double>(m_raster.Height()); | |
| 65 | 42023 | if (!(pixel.x >= 0.0 && pixel.y >= 0.0 && pixel.x < width && pixel.y < height)) { | |
| 66 | return std::numeric_limits<double>::quiet_NaN(); | ||
| 67 | } | ||
| 68 | 11718 | return m_options.method == ResampleMethod::Nearest ? SampleNearest(pixel) | |
| 69 | 11715 | : SampleBilinear(pixel); | |
| 70 | } | ||
| 71 | |||
| 72 | 3 | double RasterField::SampleNearest(const Vec2d& pixel) const { | |
| 73 | 3 | const auto col = static_cast<std::size_t>(pixel.x); | |
| 74 | 3 | const auto row = static_cast<std::size_t>(pixel.y); | |
| 75 | 3 | return m_raster.IsMissing(m_raster(col, row)) ? std::numeric_limits<double>::quiet_NaN() | |
| 76 | 3 | : static_cast<double>(m_raster(col, row)); | |
| 77 | } | ||
| 78 | |||
| 79 | 11715 | double RasterField::SampleBilinear(const Vec2d& pixel) const { | |
| 80 | 11715 | const auto width = static_cast<double>(m_raster.Width()); | |
| 81 | const auto height = static_cast<double>(m_raster.Height()); | ||
| 82 | 11715 | const double u = std::clamp(pixel.x - 0.5, 0.0, width - 1.0); | |
| 83 | 11715 | const double v = std::clamp(pixel.y - 0.5, 0.0, height - 1.0); | |
| 84 | 11715 | const auto col = static_cast<std::size_t>(u); | |
| 85 | 11715 | const auto row = static_cast<std::size_t>(v); | |
| 86 | 11715 | const double tx = u - static_cast<double>(col); | |
| 87 | 11715 | const double ty = v - static_cast<double>(row); | |
| 88 | 11715 | const std::size_t right = std::min(col + 1, m_raster.Width() - 1); | |
| 89 | 11715 | const std::size_t below = std::min(row + 1, m_raster.Height() - 1); | |
| 90 | 11715 | double sum = 0.0; | |
| 91 | 11715 | double weights = 0.0; | |
| 92 | 71115 | for (const auto& [c, r, w] : | |
| 93 | 11715 | {std::tuple{col, row, (1.0 - tx) * (1.0 - ty)}, std::tuple{right, row, tx * (1.0 - ty)}, | |
| 94 | 58575 | std::tuple{col, below, (1.0 - tx) * ty}, std::tuple{right, below, tx * ty}}) { | |
| 95 | 46860 | if (w > 0.0 && !m_raster.IsMissing(m_raster(c, r))) { | |
| 96 | 24250 | sum += w * static_cast<double>(m_raster(c, r)); | |
| 97 | 24250 | weights += w; | |
| 98 | } | ||
| 99 | } | ||
| 100 | 11715 | return weights > 0.0 ? sum / weights : std::numeric_limits<double>::quiet_NaN(); | |
| 101 | } | ||
| 102 | |||
| 103 | 42020 | double RasterField::ValueAt(double x, double y) const { | |
| 104 | 42020 | double value = Sample(x, y); | |
| 105 | 42020 | if (std::isnan(value) && m_options.periodicLongitude) { | |
| 106 | 2 | value = Sample(x + 360.0, y); | |
| 107 | 2 | if (std::isnan(value)) { | |
| 108 | 1 | value = Sample(x - 360.0, y); | |
| 109 | } | ||
| 110 | } | ||
| 111 | 42019 | if (std::isnan(value)) { | |
| 112 | 30304 | return m_options.fallback; | |
| 113 | } | ||
| 114 | 11716 | return value * m_options.scale; | |
| 115 | } | ||
| 116 | |||
| 117 | 5 | std::shared_ptr<const RasterField> ReadRasterField(const std::filesystem::path& path, | |
| 118 | RasterFieldOptions options, std::size_t band) { | ||
| 119 | 7 | return std::make_shared<RasterField>(ReadGeoTiff(path, band).raster, options); | |
| 120 | } | ||
| 121 | |||
| 122 | } // namespace terraingen | ||
| 123 |