./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
73 / 73
100.0%
Branches:
80 / 81
98.8%
Functions:
10 / 10
100.0%

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
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 time.
12 : m_a(std::move(a)), m_b(std::move(b)) {
19
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 10 times.
12 if (!m_a || !m_b) {
20
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A minimum field cannot take a null field");
21 }
22
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
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
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (options.method != ResampleMethod::Nearest && options.method != ResampleMethod::Bilinear) {
34 2 throw std::invalid_argument(
35
1/1
✓ Branch 2 taken 2 times.
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
1/1
✓ Branch 1 taken 15 times.
18 ValidateRasterFieldOptions(m_options);
43
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14 times.
15 if (m_raster.Empty()) {
44
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A raster field needs a raster");
45 }
46
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 time.
14 const GeoTransform& transform = m_raster.Transform();
47
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 time.
14 if (transform.IsRotated()) {
48
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A raster field cannot read a rotated raster");
49 }
50
2/2
✓ Branch 0 taken 20076 times.
✓ Branch 1 taken 13 times.
20089 for (const float value : m_raster.Values()) {
51
6/6
✓ Branch 0 taken 20069 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 20027 times.
✓ Branch 4 taken 36 times.
✓ Branch 5 taken 6 times.
20118 if (std::isfinite(value) && !m_raster.IsNoData(value) &&
52
2/2
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 19830 times.
20063 !(static_cast<double>(value) * m_options.scale <= m_max)) {
53 233 m_max = static_cast<double>(value) * m_options.scale;
54 }
55 }
56
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
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
2/2
✓ Branch 0 taken 41960 times.
✓ Branch 1 taken 63 times.
42023 const auto width = static_cast<double>(m_raster.Width());
64
2/2
✓ Branch 0 taken 41960 times.
✓ Branch 1 taken 63 times.
42023 const auto height = static_cast<double>(m_raster.Height());
65
8/8
✓ Branch 0 taken 41960 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 11864 times.
✓ Branch 3 taken 30096 times.
✓ Branch 4 taken 11784 times.
✓ Branch 5 taken 80 times.
✓ Branch 6 taken 11718 times.
✓ Branch 7 taken 66 times.
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
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11715 times.
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
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
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
2/2
✓ Branch 0 taken 1262 times.
✓ Branch 1 taken 10453 times.
11715 const auto width = static_cast<double>(m_raster.Width());
81 const auto height = static_cast<double>(m_raster.Height());
82
2/2
✓ Branch 0 taken 1262 times.
✓ Branch 1 taken 10453 times.
11715 const double u = std::clamp(pixel.x - 0.5, 0.0, width - 1.0);
83
2/2
✓ Branch 0 taken 5008 times.
✓ Branch 1 taken 6707 times.
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
2/2
✓ Branch 0 taken 1258 times.
✓ Branch 1 taken 10457 times.
11715 const std::size_t right = std::min(col + 1, m_raster.Width() - 1);
89
2/2
✓ Branch 0 taken 10013 times.
✓ Branch 1 taken 1702 times.
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
2/2
✓ Branch 0 taken 24255 times.
✓ Branch 1 taken 22605 times.
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
2/2
✓ Branch 0 taken 46860 times.
✓ Branch 1 taken 11715 times.
58575 std::tuple{col, below, (1.0 - tx) * ty}, std::tuple{right, below, tx * ty}}) {
95
4/4
✓ Branch 0 taken 24255 times.
✓ Branch 1 taken 22605 times.
✓ Branch 3 taken 24250 times.
✓ Branch 4 taken 5 times.
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
2/2
✓ Branch 0 taken 11713 times.
✓ Branch 1 taken 2 times.
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
4/4
✓ Branch 0 taken 30306 times.
✓ Branch 1 taken 11714 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 30304 times.
42020 if (std::isnan(value) && m_options.periodicLongitude) {
106 2 value = Sample(x + 360.0, y);
107
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (std::isnan(value)) {
108 1 value = Sample(x - 360.0, y);
109 }
110 }
111
2/2
✓ Branch 0 taken 30304 times.
✓ Branch 1 taken 11715 times.
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