src/raster/resample.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/raster/resample.h" | ||
| 4 | |||
| 5 | #include "raster/kernels.h" | ||
| 6 | #include "terraingen/util/bits.h" | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <array> | ||
| 10 | #include <cmath> | ||
| 11 | #include <limits> | ||
| 12 | #include <optional> | ||
| 13 | #include <stdexcept> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// Keys cubic convolution kernel with a = -0.5, the Catmull-Rom spline. | ||
| 20 | 6214225 | double CubicWeight(double x) { | |
| 21 | 6214225 | constexpr double a = -0.5; | |
| 22 | 6214225 | x = std::fabs(x); | |
| 23 | 6214225 | if (x <= 1.0) { | |
| 24 | 3263721 | return ((a + 2.0) * x - (a + 3.0)) * x * x + 1.0; | |
| 25 | } | ||
| 26 | 2950504 | if (x < 2.0) { | |
| 27 | 2793992 | return ((a * x - 5.0 * a) * x + 8.0 * a) * x - 4.0 * a; | |
| 28 | } | ||
| 29 | return 0.0; | ||
| 30 | } | ||
| 31 | |||
| 32 | /// sin(pi x) / (pi x), 1 at 0. | ||
| 33 | 940602 | double Sinc(double x) { | |
| 34 | 940602 | if (x == 0.0) { | |
| 35 | return 1.0; | ||
| 36 | } | ||
| 37 | 937610 | const double angle = kPi * x; | |
| 38 | 937610 | return std::sin(angle) / angle; | |
| 39 | } | ||
| 40 | |||
| 41 | /// The Lanczos kernel with three lobes, exactly 0 at the whole offsets but 0. | ||
| 42 | 477778 | double Lanczos3Weight(double x) { | |
| 43 | 477778 | x = std::fabs(x); | |
| 44 | 477778 | if (x >= 3.0 || (x > 0.0 && x == std::floor(x))) { | |
| 45 | return 0.0; | ||
| 46 | } | ||
| 47 | 470301 | return Sinc(x) * Sinc(x / 3.0); | |
| 48 | } | ||
| 49 | |||
| 50 | /// The uniform cubic B-spline. | ||
| 51 | 30871 | double BSplineWeight(double x) { | |
| 52 | 30871 | x = std::fabs(x); | |
| 53 | 30871 | if (x < 1.0) { | |
| 54 | 15412 | return 2.0 / 3.0 - x * x + 0.5 * x * x * x; | |
| 55 | } | ||
| 56 | 15459 | if (x < 2.0) { | |
| 57 | 15435 | const double rest = 2.0 - x; | |
| 58 | 15435 | return rest * rest * rest / 6.0; | |
| 59 | } | ||
| 60 | return 0.0; | ||
| 61 | } | ||
| 62 | |||
| 63 | /// `raster` interpolated at pixel coordinates (`u`, `v`) measured from the center of its first | ||
| 64 | /// cell, with cells beyond the edge read from the nearest edge cell; nothing where a contributing | ||
| 65 | /// cell holds no data. | ||
| 66 | 472633 | std::optional<float> Interpolate(const Raster<float>& raster, double u, double v, | |
| 67 | ResampleMethod method) { | ||
| 68 | 4257385 | const auto value = [&](double col, double row) { | |
| 69 | 3784752 | return raster(ClampIndex(col, raster.Width()), ClampIndex(row, raster.Height())); | |
| 70 | 472633 | }; | |
| 71 | 472633 | return InterpolateLattice(u, v, method, value, | |
| 72 | 8205976 | [&](float cell) { return raster.IsNoData(cell); }); | |
| 73 | } | ||
| 74 | |||
| 75 | /// Checks that a raster can be resampled: not empty and not rotated. | ||
| 76 | 517176 | void CheckSource(const Raster<float>& raster) { | |
| 77 | 517176 | if (raster.Empty()) { | |
| 78 | 1 | throw std::invalid_argument("Cannot resample an empty raster"); | |
| 79 | } | ||
| 80 | 517175 | if (raster.Transform().IsRotated()) { | |
| 81 | 1 | throw std::invalid_argument("Cannot resample a rotated raster"); | |
| 82 | } | ||
| 83 | 517174 | } | |
| 84 | |||
| 85 | } // namespace | ||
| 86 | |||
| 87 | 4173097 | int KernelRadius(ResampleMethod method) { | |
| 88 | 4173097 | switch (method) { | |
| 89 | case ResampleMethod::Nearest: | ||
| 90 | case ResampleMethod::Bilinear: | ||
| 91 | return 1; | ||
| 92 | 320576 | case ResampleMethod::Lanczos3: | |
| 93 | 320576 | return 3; | |
| 94 | case ResampleMethod::Cubic: | ||
| 95 | case ResampleMethod::CubicBSpline: | ||
| 96 | break; | ||
| 97 | } | ||
| 98 | 3287304 | return 2; | |
| 99 | } | ||
| 100 | |||
| 101 | 6722255 | double KernelWeight(ResampleMethod method, double x) { | |
| 102 | 6722255 | switch (method) { | |
| 103 | 477778 | case ResampleMethod::Lanczos3: | |
| 104 | 477778 | return Lanczos3Weight(x); | |
| 105 | 30871 | case ResampleMethod::CubicBSpline: | |
| 106 | 30871 | return BSplineWeight(x); | |
| 107 | 6213606 | default: | |
| 108 | 6213606 | return CubicWeight(x); | |
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | 2294516 | std::array<double, 6> KernelWeights(ResampleMethod method, double fraction) { | |
| 113 | 2294516 | std::array<double, 6> weights{}; | |
| 114 | 2294516 | if (method == ResampleMethod::Nearest) { | |
| 115 | return weights; | ||
| 116 | } | ||
| 117 | 2294552 | if (method == ResampleMethod::Bilinear) { | |
| 118 | 653363 | weights[0] = 1.0 - fraction; | |
| 119 | 653363 | weights[1] = fraction; | |
| 120 | 653363 | return weights; | |
| 121 | } | ||
| 122 | 1641189 | const int radius = KernelRadius(method); | |
| 123 | 1641189 | double total = 0.0; | |
| 124 | 8363360 | for (int i = 1 - radius; i <= radius; ++i) { | |
| 125 | 6722171 | const double weight = KernelWeight(method, fraction - i); | |
| 126 | 6722171 | weights[static_cast<std::size_t>(i + radius - 1)] = weight; | |
| 127 | 6722171 | total += weight; | |
| 128 | } | ||
| 129 | 1641189 | if (method == ResampleMethod::Lanczos3) { | |
| 130 | 557403 | for (double& weight : weights) { | |
| 131 | 477774 | weight /= total; | |
| 132 | } | ||
| 133 | } | ||
| 134 | return weights; | ||
| 135 | } | ||
| 136 | |||
| 137 | 517211 | float SampleRaster(const Raster<float>& raster, double x, double y, ResampleMethod method) { | |
| 138 | 517211 | CheckSource(raster); | |
| 139 | 517164 | const Vec2d pixel = raster.Transform().WorldToPixel(x, y); | |
| 140 | 517199 | if (!(pixel.x >= 0.0 && pixel.y >= 0.0 && pixel.x < static_cast<double>(raster.Width()) && | |
| 141 | 483414 | pixel.y < static_cast<double>(raster.Height()))) { | |
| 142 | return std::numeric_limits<float>::quiet_NaN(); | ||
| 143 | } | ||
| 144 | 472636 | const std::optional<float> value = Interpolate(raster, pixel.x - 0.5, pixel.y - 0.5, method); | |
| 145 | 678204 | if (!value || raster.IsNoData(*value)) { | |
| 146 | return std::numeric_limits<float>::quiet_NaN(); | ||
| 147 | } | ||
| 148 | 472435 | return *value; | |
| 149 | } | ||
| 150 | } // namespace terraingen | ||
| 151 |