./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
71 / 71
100.0%
Branches:
59 / 62
95.2%
Functions:
11 / 11
100.0%

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
2/2
✓ Branch 0 taken 3263721 times.
✓ Branch 1 taken 2950504 times.
6214225 if (x <= 1.0) {
24 3263721 return ((a + 2.0) * x - (a + 3.0)) * x * x + 1.0;
25 }
26
2/2
✓ Branch 0 taken 2793992 times.
✓ Branch 1 taken 156512 times.
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
2/2
✓ Branch 0 taken 937610 times.
✓ Branch 1 taken 2992 times.
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
6/6
✓ Branch 0 taken 476282 times.
✓ Branch 1 taken 1496 times.
✓ Branch 2 taken 474786 times.
✓ Branch 3 taken 1496 times.
✓ Branch 4 taken 468805 times.
✓ Branch 5 taken 5981 times.
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
2/2
✓ Branch 0 taken 15412 times.
✓ Branch 1 taken 15459 times.
30871 if (x < 1.0) {
54 15412 return 2.0 / 3.0 - x * x + 0.5 * x * x * x;
55 }
56
2/2
✓ Branch 0 taken 15435 times.
✓ Branch 1 taken 24 times.
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
7/8
✓ Branch 0 taken 3251266 times.
✓ Branch 1 taken 1230277 times.
✓ Branch 2 taken 258 times.
✓ Branch 3 taken 3251008 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 532 times.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
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
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 517175 times.
517176 if (raster.Empty()) {
78
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Cannot resample an empty raster");
79 }
80
2/2
✓ Branch 0 taken 517174 times.
✓ Branch 1 taken 1 time.
517175 if (raster.Transform().IsRotated()) {
81
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Cannot resample a rotated raster");
82 }
83 517174 }
84
85 } // namespace
86
87 4173097 int KernelRadius(ResampleMethod method) {
88
3/3
✓ Branch 0 taken 320576 times.
✓ Branch 1 taken 3287304 times.
✓ Branch 2 taken 565217 times.
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
3/3
✓ Branch 0 taken 477778 times.
✓ Branch 1 taken 30871 times.
✓ Branch 2 taken 6213606 times.
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
1/2
✓ Branch 0 taken 2294552 times.
✗ Branch 1 not taken.
2294516 if (method == ResampleMethod::Nearest) {
115 return weights;
116 }
117
2/2
✓ Branch 0 taken 653363 times.
✓ Branch 1 taken 1641189 times.
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
2/2
✓ Branch 0 taken 6722171 times.
✓ Branch 1 taken 1641189 times.
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
2/2
✓ Branch 0 taken 79629 times.
✓ Branch 1 taken 1561560 times.
1641189 if (method == ResampleMethod::Lanczos3) {
130
2/2
✓ Branch 0 taken 477774 times.
✓ Branch 1 taken 79629 times.
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
8/8
✓ Branch 0 taken 505611 times.
✓ Branch 1 taken 11588 times.
✓ Branch 2 taken 494462 times.
✓ Branch 3 taken 11149 times.
✓ Branch 4 taken 483414 times.
✓ Branch 5 taken 11048 times.
✓ Branch 6 taken 472636 times.
✓ Branch 7 taken 10778 times.
517199 if (!(pixel.x >= 0.0 && pixel.y >= 0.0 && pixel.x < static_cast<double>(raster.Width()) &&
141
2/2
✓ Branch 0 taken 472636 times.
✓ Branch 1 taken 10778 times.
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
5/6
✓ Branch 0 taken 472435 times.
✓ Branch 1 taken 207 times.
✓ Branch 2 taken 205562 times.
✓ Branch 3 taken 266873 times.
✓ Branch 4 taken 205562 times.
✗ Branch 5 not taken.
678204 if (!value || raster.IsNoData(*value)) {
146 return std::numeric_limits<float>::quiet_NaN();
147 }
148 472435 return *value;
149 }
150 } // namespace terraingen
151