./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
102 / 102
100.0%
Branches:
62 / 63
98.4%
Functions:
16 / 16
100.0%

src/albedo/albedo.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/albedo.h"
4
5 #include "terraingen/util/bits.h"
6 #include "terraingen/util/log.h"
7 #include "terraingen/util/parallel.h"
8 #include "util/checks.h"
9
10 #include <algorithm>
11 #include <cmath>
12 #include <limits>
13 #include <stdexcept>
14
15 namespace terraingen {
16
17 1866 void ValidateAlbedoContext(const LayerContext& context) {
18 1866 RequirePositive(context.spacing, "Albedo sample spacing");
19 1864 }
20
21 namespace {
22
23 /// Calls `visit(value, col)` for the albedo of each cell of one row of `bands`, writing back what
24 /// it leaves in `value`.
25 template <typename Visit>
26 19575 void VisitRowCells(std::vector<Raster<float>>& bands, std::size_t row, const Visit& visit) {
27
4/4
void terraingen::(anonymous namespace)::VisitRowCells<terraingen::(anonymous namespace)::VisitRow(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, std::function<void (std::array<float, 3ul>&, double, double)> const&)::{lambda(std::array<float, 3ul>&, unsigned long)#1}>(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, terraingen::(anonymous namespace)::VisitRow(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, std::function<void (std::array<float, 3ul>&, double, double)> const&)::{lambda(std::array<float, 3ul>&, unsigned long)#1} const&):
✓ Branch 0 taken 6841770 times.
✓ Branch 1 taken 18631 times.
void terraingen::(anonymous namespace)::VisitRowCells<terraingen::ForEachAlbedoPixel(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned int, std::function<void (std::array<float, 3ul>&, unsigned long, unsigned long)> const&)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(std::array<float, 3ul>&, unsigned long)#1}>(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, terraingen::ForEachAlbedoPixel(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned int, std::function<void (std::array<float, 3ul>&, unsigned long, unsigned long)> const&)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(std::array<float, 3ul>&, unsigned long)#1} const&):
✓ Branch 0 taken 327268 times.
✓ Branch 1 taken 943 times.
7188612 for (std::size_t col = 0; col < bands.front().Width(); ++col) {
28 7169038 AlbedoSample value{};
29
4/4
void terraingen::(anonymous namespace)::VisitRowCells<terraingen::(anonymous namespace)::VisitRow(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, std::function<void (std::array<float, 3ul>&, double, double)> const&)::{lambda(std::array<float, 3ul>&, unsigned long)#1}>(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, terraingen::(anonymous namespace)::VisitRow(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, std::function<void (std::array<float, 3ul>&, double, double)> const&)::{lambda(std::array<float, 3ul>&, unsigned long)#1} const&):
✓ Branch 0 taken 7303059 times.
✓ Branch 1 taken 6831162 times.
void terraingen::(anonymous namespace)::VisitRowCells<terraingen::ForEachAlbedoPixel(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned int, std::function<void (std::array<float, 3ul>&, unsigned long, unsigned long)> const&)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(std::array<float, 3ul>&, unsigned long)#1}>(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, terraingen::ForEachAlbedoPixel(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned int, std::function<void (std::array<float, 3ul>&, unsigned long, unsigned long)> const&)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(std::array<float, 3ul>&, unsigned long)#1} const&):
✓ Branch 0 taken 321581 times.
✓ Branch 1 taken 328070 times.
14783872 for (std::size_t band = 0; band < bands.size(); ++band) {
30 7624640 value[band] = bands[band](col, row);
31 }
32 7479838 visit(value, col);
33
4/4
void terraingen::(anonymous namespace)::VisitRowCells<terraingen::(anonymous namespace)::VisitRow(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, std::function<void (std::array<float, 3ul>&, double, double)> const&)::{lambda(std::array<float, 3ul>&, unsigned long)#1}>(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, terraingen::(anonymous namespace)::VisitRow(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, std::function<void (std::array<float, 3ul>&, double, double)> const&)::{lambda(std::array<float, 3ul>&, unsigned long)#1} const&):
✓ Branch 0 taken 7299608 times.
✓ Branch 1 taken 6841769 times.
void terraingen::(anonymous namespace)::VisitRowCells<terraingen::ForEachAlbedoPixel(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned int, std::function<void (std::array<float, 3ul>&, unsigned long, unsigned long)> const&)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(std::array<float, 3ul>&, unsigned long)#1}>(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned long, terraingen::ForEachAlbedoPixel(std::vector<terraingen::Raster<float>, std::allocator<terraingen::Raster<float> > >&, unsigned int, std::function<void (std::array<float, 3ul>&, unsigned long, unsigned long)> const&)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(std::array<float, 3ul>&, unsigned long)#1} const&):
✓ Branch 0 taken 321408 times.
✓ Branch 1 taken 327268 times.
14790053 for (std::size_t band = 0; band < bands.size(); ++band) {
34 7621016 bands[band](col, row) = value[band];
35 }
36 }
37 19574 }
38
39 /// Visits the cells of one row of `bands` by their centers.
40 18631 void VisitRow(std::vector<Raster<float>>& bands, std::size_t row, const AlbedoCellVisitor& visit) {
41 18631 const Raster<float>& first = bands.front();
42 18631 VisitRowCells(bands, row, [&](AlbedoSample& value, std::size_t col) {
43 6833220 const Vec2d center = first.CellCenter(col, row);
44 6833220 visit(value, center.x, center.y);
45 6833732 });
46 18631 }
47
48 } // namespace
49
50 7 void ForEachAlbedoPixel(std::vector<Raster<float>>& bands, unsigned threads,
51 const AlbedoPixelVisitor& visit) {
52
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 time.
7 if (bands.empty()) {
53 return;
54 }
55
1/1
✓ Branch 1 taken 6 times.
12 ParallelFor(bands.front().Height(), threads, [&](std::size_t row) {
56 943 VisitRowCells(bands, row,
57 329013 [&](AlbedoSample& value, std::size_t col) { visit(value, col, row); });
58 943 });
59 }
60
61 109 void ForEachAlbedoCell(std::vector<Raster<float>>& bands, unsigned threads,
62 const AlbedoCellVisitor& visit) {
63
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1 time.
109 if (bands.empty()) {
64 return;
65 }
66 108 ParallelFor(bands.front().Height(), threads,
67
1/1
✓ Branch 2 taken 108 times.
14582 [&](std::size_t row) { VisitRow(bands, row, visit); });
68 }
69
70 31 void ForEachAlbedoBlock(
71 std::vector<Raster<float>>& bands, unsigned threads, std::size_t blockRows,
72 const std::function<AlbedoCellVisitor(std::size_t rowBegin, std::size_t rowEnd)>& makeVisitor) {
73
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
31 if (bands.empty()) {
74 1 return;
75 }
76 30 const std::size_t height = bands.front().Height();
77 30 const std::size_t blocks = CeilDiv(height, blockRows);
78
1/1
✓ Branch 2 taken 30 times.
60 ParallelFor(blocks, threads, [&](std::size_t block) {
79 146 const std::size_t rowBegin = block * blockRows;
80
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 30 times.
146 const std::size_t rowEnd = std::min(height, rowBegin + blockRows);
81 146 const AlbedoCellVisitor visit = makeVisitor(rowBegin, rowEnd);
82
2/2
✓ Branch 0 taken 4156 times.
✓ Branch 1 taken 146 times.
4302 for (std::size_t row = rowBegin; row < rowEnd; ++row) {
83
1/1
✓ Branch 1 taken 4156 times.
4156 VisitRow(bands, row, visit);
84 }
85 146 });
86 }
87
88 84 void AlbedoSource::Fill(std::vector<Raster<float>>& bands, const LayerContext& context) const {
89
1/1
✓ Branch 1 taken 84 times.
84 ForEachAlbedoCell(bands, context.threads, [&](AlbedoSample& value, double x, double y) {
90 3803700 value = ValueAt(x, y, context);
91 });
92 84 }
93
94 9 void AlbedoLayer::Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const {
95 9 const std::size_t count = bands.size();
96
1/1
✓ Branch 2 taken 9 times.
9 ForEachAlbedoCell(bands, context.threads, [&](AlbedoSample& value, double x, double y) {
97 837800 ApplyAt(value, count, x, y, context);
98 837789 });
99 9 }
100
101 namespace {
102
103 1737 std::size_t CheckAlbedo(const Albedo& albedo, const LayerContext& context) {
104
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1736 times.
1737 if (!albedo.source) {
105
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An albedo needs a source");
106 }
107
2/2
✓ Branch 0 taken 1704 times.
✓ Branch 1 taken 1735 times.
3439 for (const auto& layer : albedo.layers) {
108
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1703 times.
1704 if (!layer) {
109
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An albedo layer is null");
110 }
111 }
112 1735 const std::size_t bands = albedo.source->Bands();
113
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1734 times.
1735 if (bands != 1 && bands != kMaxAlbedoBands) {
114
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An albedo source must have 1 or 3 bands");
115 }
116 1734 ValidateAlbedoContext(context);
117 1732 return bands;
118 }
119
120 } // namespace
121
122 89 std::vector<Raster<float>> EvaluateAlbedo(const Albedo& albedo, const RasterGrid& grid,
123 LayerContext context) {
124
3/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 88 times.
89 if (grid.width == 0 || grid.height == 0) {
125
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Cannot evaluate an albedo over an empty grid");
126 }
127 88 RequireNonNegative(context.spacing, "Albedo sample spacing");
128 87 context.spacing = ResolvedSpacing(grid, context.spacing, albedo.surface);
129
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 36 times.
87 if (context.surface == nullptr) {
130 51 context.surface = &albedo.surface;
131 }
132 87 const std::size_t count = CheckAlbedo(albedo, context);
133 84 std::vector<Raster<float>> bands;
134
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 84 times.
206 for (std::size_t band = 0; band < count; ++band) {
135
1/1
✓ Branch 1 taken 122 times.
122 Raster<float> raster(grid, 0.0f);
136
1/1
✓ Branch 1 taken 122 times.
122 raster.SetNoData(std::numeric_limits<float>::quiet_NaN());
137
1/1
✓ Branch 1 taken 122 times.
122 bands.push_back(std::move(raster));
138 122 }
139
1/1
✓ Branch 1 taken 84 times.
84 albedo.source->Fill(bands, context);
140
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 84 times.
144 for (std::size_t n = 0; n < albedo.layers.size(); ++n) {
141 60 const Stopwatch stopwatch;
142
1/1
✓ Branch 1 taken 60 times.
60 albedo.layers[n]->Apply(bands, context);
143
1/1
✓ Branch 1 taken 60 times.
60 LogDebug("albedo", "Applied albedo layer ", n + 1, " in ",
144
1/1
✓ Branch 2 taken 60 times.
120 FormatDuration(stopwatch.Milliseconds()));
145 }
146 84 return bands;
147 }
148
149 1650 AlbedoSample EvaluateAlbedoAt(const Albedo& albedo, double x, double y, LayerContext context) {
150
2/2
✓ Branch 0 taken 1645 times.
✓ Branch 1 taken 5 times.
1650 if (context.surface == nullptr) {
151 1645 context.surface = &albedo.surface;
152 }
153 1650 const std::size_t count = CheckAlbedo(albedo, context);
154 1648 AlbedoSample value = albedo.source->ValueAt(x, y, context);
155
2/2
✓ Branch 0 taken 3276 times.
✓ Branch 1 taken 1648 times.
4924 for (std::size_t band = count; band < kMaxAlbedoBands; ++band) {
156 3276 value[band] = 0.0f;
157 }
158
2/2
✓ Branch 0 taken 1642 times.
✓ Branch 1 taken 1648 times.
3290 for (const auto& layer : albedo.layers) {
159 1642 layer->ApplyAt(value, count, x, y, context);
160 }
161 1648 return value;
162 }
163
164 } // namespace terraingen
165