./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
93 / 94
98.9%
Branches:
36 / 38
94.7%
Functions:
12 / 12, 1 excl
100.0%

src/albedo/slope_albedo.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/slope_albedo.h"
4
5 #include "albedo/bands.h"
6 #include "terraingen/albedo/albedo_fields.h"
7 #include "terraingen/geometry/geometry.h"
8
9 #include <cmath>
10 #include <limits>
11 #include <stdexcept>
12 #include <utility>
13
14 namespace terraingen {
15
16 ✗ struct SlopeAlbedo::Lattice {
17 double step = 1.0; ///< Step between nodes in surface coordinates.
18 double i0 = 0.0; ///< Column of the first node, in steps from the origin.
19 double j0 = 0.0; ///< Row of the first node.
20 std::size_t columns = 0;
21 std::size_t rows = 0;
22 std::vector<double> slopes; ///< Degrees, row-major; 0 on the outer ring.
23
24 81847 double At(std::size_t i, std::size_t j) const {
25 81847 return slopes[j * columns + i];
26 }
27 };
28
29 18 void ValidateSlopeAlbedoOptions(const SlopeAlbedoOptions& options) {
30 18 CheckRanges(options);
31
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 11 times.
12 if (!(options.maxMaturity >= options.backgroundMaturity)) {
32
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Slope maturity cap must not be below the background");
33 }
34 11 }
35
36 9 SlopeAlbedo::SlopeAlbedo(SlopeAlbedoOptions options) : m_options(options) {
37 9 ValidateSlopeAlbedoOptions(m_options);
38 9 }
39
40 81990 double SlopeAlbedo::MaturityAt(double slopeDegrees) const {
41 81990 const double excess =
42 81990 m_options.maturityPerDegree * std::fmax(0.0, slopeDegrees - m_options.onsetDegrees);
43 81990 return std::fmin(m_options.backgroundMaturity + excess, m_options.maxMaturity);
44 }
45
46 73 SlopeAlbedo::Lattice SlopeAlbedo::ReadLattice(const Bounds2d& region, const Ground& ground,
47 const Surface& surface) const {
48
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 72 times.
73 Lattice lattice;
49
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 72 times.
73 const double step = m_options.baseline / surface.UnitLength();
50 73 lattice.step = step;
51 73 lattice.i0 = std::floor(region.minX / step) - 1.0;
52 73 lattice.j0 = std::floor(region.minY / step) - 1.0;
53 73 lattice.columns =
54 73 static_cast<std::size_t>(std::floor(region.maxX / step) + 2.0 - lattice.i0) + 1;
55 73 lattice.rows = static_cast<std::size_t>(std::floor(region.maxY / step) + 2.0 - lattice.j0) + 1;
56 21064 const auto node = [&](std::size_t i, std::size_t j) {
57 20991 return Vec2d{(lattice.i0 + static_cast<double>(i)) * step,
58 20991 (lattice.j0 + static_cast<double>(j)) * step};
59 73 };
60
1/1
✓ Branch 1 taken 73 times.
73 std::vector<Vec2d> points;
61
1/1
✓ Branch 1 taken 73 times.
73 points.reserve(lattice.columns * lattice.rows);
62
2/2
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 73 times.
454 for (std::size_t j = 0; j < lattice.rows; ++j) {
63
2/2
✓ Branch 0 taken 5187 times.
✓ Branch 1 taken 381 times.
5568 for (std::size_t i = 0; i < lattice.columns; ++i) {
64
1/1
✓ Branch 1 taken 5187 times.
5187 points.push_back(node(i, j));
65 }
66 }
67
1/1
✓ Branch 1 taken 73 times.
73 std::vector<float> heights(points.size());
68
1/1
✓ Branch 3 taken 73 times.
73 ground.HeightsAt(points, heights, m_options.baseline);
69
1/1
✓ Branch 1 taken 73 times.
73 lattice.slopes.assign(points.size(), 0.0);
70
2/2
✓ Branch 0 taken 235 times.
✓ Branch 1 taken 73 times.
308 for (std::size_t j = 1; j + 1 < lattice.rows; ++j) {
71
2/2
✓ Branch 0 taken 3951 times.
✓ Branch 1 taken 235 times.
4186 for (std::size_t i = 1; i + 1 < lattice.columns; ++i) {
72 7902 const auto z = [&](std::size_t a, std::size_t b) {
73 7902 return static_cast<double>(heights[b * lattice.columns + a]);
74 3951 };
75 3951 const double dx =
76
1/1
✓ Branch 1 taken 3951 times.
3951 (z(i + 1, j) - z(i - 1, j)) / surface.Distance(node(i - 1, j), node(i + 1, j));
77 3951 const double dy =
78
1/1
✓ Branch 1 taken 3951 times.
3951 (z(i, j + 1) - z(i, j - 1)) / surface.Distance(node(i, j - 1), node(i, j + 1));
79 3951 const double slope = GradientSlopeDegrees(dx, dy);
80
1/2
✓ Branch 0 taken 3951 times.
✗ Branch 1 not taken.
7902 lattice.slopes[j * lattice.columns + i] = std::isnan(slope) ? 0.0 : slope;
81 }
82 }
83 73 return lattice;
84 73 }
85
86 81847 double SlopeAlbedo::Interpolate(const Lattice& lattice, double x, double y) {
87 81847 const double u = x / lattice.step;
88 81847 const double v = y / lattice.step;
89 81847 const double fu = std::floor(u);
90 81847 const double fv = std::floor(v);
91 81847 const auto i = static_cast<std::size_t>(fu - lattice.i0);
92 81847 const auto j = static_cast<std::size_t>(fv - lattice.j0);
93 81847 const double tu = u - fu;
94 81847 const double tv = v - fv;
95 81847 return (1.0 - tv) * ((1.0 - tu) * lattice.At(i, j) + tu * lattice.At(i + 1, j)) +
96 81847 tv * ((1.0 - tu) * lattice.At(i, j + 1) + tu * lattice.At(i + 1, j + 1));
97 }
98
99 70 double SlopeAlbedo::SlopeAt(double x, double y, const Ground& ground,
100 const Surface& surface) const {
101 70 return Interpolate(ReadLattice({x, y, x, y}, ground, surface), x, y);
102 }
103
104 81957 void SlopeAlbedo::Mature(AlbedoSample& value, std::size_t bands, double slopeDegrees) const {
105 81957 const double maturity = MaturityAt(slopeDegrees);
106
2/2
✓ Branch 0 taken 35525 times.
✓ Branch 1 taken 46432 times.
81957 if (maturity != m_options.backgroundMaturity) {
107 35525 MatureBands(value, bands, m_options.backgroundMaturity, maturity,
108 35525 m_options.originReflectance);
109 }
110 81896 }
111
112 64 void SlopeAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y,
113 const LayerContext& context) const {
114
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 1 time.
64 if (context.ground != nullptr) {
115 63 Mature(value, bands, SlopeAt(x, y, *context.ground, context.SurfaceOrPlane()));
116 }
117 64 }
118
119 namespace {
120
121 /// The box of the cell centers of `raster`.
122 3 Bounds2d CellCenterBounds(const Raster<float>& raster) {
123 3 const std::size_t lastCol = raster.Width() - 1;
124 3 const std::size_t lastRow = raster.Height() - 1;
125 3 Bounds2d centers{
126 std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(),
127 -std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity()};
128 12 for (const auto& [col, row] : {std::pair<std::size_t, std::size_t>{0, 0},
129 3 {lastCol, 0},
130 3 {0, lastRow},
131
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 {lastCol, lastRow}}) {
132 12 const Vec2d c = raster.CellCenter(col, row);
133 12 centers = {std::fmin(centers.minX, c.x), std::fmin(centers.minY, c.y),
134 12 std::fmax(centers.maxX, c.x), std::fmax(centers.maxY, c.y)};
135 }
136 3 return centers;
137 }
138
139 } // namespace
140
141 4 void SlopeAlbedo::Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const {
142
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
4 if (context.ground == nullptr || bands.empty()) {
143 1 return;
144 }
145 3 const Lattice lattice =
146 3 ReadLattice(CellCenterBounds(bands[0]), *context.ground, context.SurfaceOrPlane());
147
1/1
✓ Branch 1 taken 3 times.
3 const std::size_t count = bands.size();
148
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 ForEachAlbedoCell(bands, context.threads, [&](AlbedoSample& value, double x, double y) {
149 81731 Mature(value, count, Interpolate(lattice, x, y));
150 81783 });
151 3 }
152
153 } // namespace terraingen
154