./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
149 / 149
100.0%
Branches:
112 / 125
89.6%
Functions:
27 / 27
100.0%

src/sources/raster_source.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/sources/raster_source.h"
4
5 #include "sources/lattice.h"
6 #include "sources/raster_heights.h"
7 #include "terraingen/util/bits.h"
8 #include "terraingen/util/parallel.h"
9 #include "util/checks.h"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <limits>
14 #include <stdexcept>
15 #include <utility>
16 #include <vector>
17
18 namespace terraingen {
19
20 namespace {
21
22 /// A turn of longitude, in degrees.
23 constexpr double kTurnDegrees = 360.0;
24
25 /// The heights over the whole of a raster.
26 std::shared_ptr<const detail::RasterHeights>
27 122 WholeHeights(Raster<float> heights, const RasterSourceOptions& options, const GeoKeys& keys) {
28
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 121 times.
122 if (heights.Empty()) {
29
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A raster source needs a raster with cells");
30 }
31 121 detail::ValidateRasterSourceOptions(options);
32 116 const RasterPlacement placement = RasterPlacement::Whole(heights);
33 116 const bool wraps = detail::WrapsLongitude(keys, placement.whole);
34 116 const std::optional<float> noData = heights.NoData();
35 116 return std::make_shared<const detail::RasterHeights>(
36
3/4
✓ Branch 1 taken 116 times.
✓ Branch 4 taken 116 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 116 times.
232 std::make_shared<RasterPosts>(std::make_shared<const Raster<float>>(std::move(heights)),
37 placement, wraps),
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
348 placement.whole, noData, options, WholeWindow(placement.whole), "the raster", wraps);
39 }
40
41 } // namespace
42
43 namespace detail {
44
45 222 bool WrapsLongitude(const GeoKeys& keys, const RasterGrid& lattice) {
46 222 const GeoTransform& transform = lattice.transform;
47 222 const double span = std::fabs(static_cast<double>(lattice.width) * transform.pixelWidth);
48
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 209 times.
222 return keys.IsGeographic() && !transform.IsRotated() &&
49
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 std::fabs(span - kTurnDegrees) <= 1e-9 * kTurnDegrees;
50 }
51
52 281 void ValidateRasterSourceOptions(const RasterSourceOptions& options) {
53
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 265 times.
281 if (options.effectivePixelSize) {
54 16 RequirePositive(*options.effectivePixelSize, "Effective pixel size");
55 }
56 275 }
57
58 104 PixelWindow NativeWindow(const RasterGrid& native, int reach, const PixelWindow& cells) {
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 const double margin = static_cast<double>(std::max(reach, 0)) + 1.0;
60 104 const std::size_t columns = native.width + 1;
61 104 const std::size_t rows = native.height + 1;
62 104 return {ClampIndex(static_cast<double>(cells.colBegin) - margin, columns),
63 104 ClampIndex(static_cast<double>(cells.colEnd) + margin, columns),
64 104 ClampIndex(static_cast<double>(cells.rowBegin) - margin, rows),
65 104 ClampIndex(static_cast<double>(cells.rowEnd) + margin, rows)};
66 }
67
68 220 RasterHeights::RasterHeights(std::shared_ptr<const PostLattice> posts, const RasterGrid& lattice,
69 std::optional<float> noData, const RasterSourceOptions& options,
70 220 const PixelWindow& cells, std::string name, bool wrapLongitude)
71
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 136 times.
220 : m_posts(std::move(posts)), m_lattice(lattice), m_noData(noData), m_cells(cells),
72
3/3
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 136 times.
✓ Branch 3 taken 220 times.
304 m_upsampler(FittedUpsampler(options.upsampler, *m_posts)), m_name(std::move(name)),
73 220 m_wrapLongitude(wrapLongitude) {
74
1/1
✓ Branch 1 taken 220 times.
220 m_cellSpacing = PostSpacing(m_lattice.transform);
75
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 211 times.
220 m_resolution = options.effectivePixelSize.value_or(m_cellSpacing);
76
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
220 }
77
78 1332353 bool RasterHeights::Covers(double col, double row) const {
79 2622807 return col >= static_cast<double>(m_cells.colBegin) &&
80
2/2
✓ Branch 0 taken 1263893 times.
✓ Branch 1 taken 26561 times.
1290454 row >= static_cast<double>(m_cells.rowBegin) &&
81
6/6
✓ Branch 0 taken 1290454 times.
✓ Branch 1 taken 41899 times.
✓ Branch 2 taken 1247617 times.
✓ Branch 3 taken 16276 times.
✓ Branch 4 taken 11312 times.
✓ Branch 5 taken 1236305 times.
2596246 col < static_cast<double>(m_cells.colEnd) && row < static_cast<double>(m_cells.rowEnd);
82 }
83
84 425 PixelWindow RasterHeights::CoveredWindow(const RasterGrid& grid) const {
85 425 const PixelWindow meets = PixelWindowIn(grid, Subgrid(m_lattice, m_cells).Bounds());
86
2/2
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 1 time.
425 if (meets.Empty()) {
87 2 return {};
88 }
89
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 323 times.
423 return {meets.colBegin > 0 ? meets.colBegin - 1 : 0, std::min(meets.colEnd + 1, grid.width),
90
6/6
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 101 times.
✓ Branch 3 taken 322 times.
✓ Branch 4 taken 399 times.
✓ Branch 5 taken 24 times.
1173 meets.rowBegin > 0 ? meets.rowBegin - 1 : 0, std::min(meets.rowEnd + 1, grid.height)};
91 }
92
93 427 void RasterHeights::FillUnwrapped(Raster<float>& heights, double spacing, unsigned threads) const {
94
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 2 times.
427 const GeoTransform& target = heights.Transform();
95
3/4
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
427 if (target.IsRotated() && !(AtPostSpacing(m_lattice.transform, spacing) &&
96
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 detail::OffsetOnLattice(m_lattice.transform, target))) {
97
1/1
✓ Branch 1 taken 2 times.
4 throw std::invalid_argument("A rotated grid off the lattice of " + m_name +
98
1/1
✓ Branch 2 taken 2 times.
6 " cannot be filled");
99 }
100 425 const RasterGrid grid = heights.Grid();
101 425 const PixelWindow window = CoveredWindow(grid);
102
2/2
✓ Branch 0 taken 317 times.
✓ Branch 1 taken 108 times.
425 const std::optional<RasterGrid> part =
103
2/2
✓ Branch 0 taken 317 times.
✓ Branch 1 taken 108 times.
425 window == WholeWindow(grid) ? std::nullopt : detail::ExactSubgrid(grid, window);
104
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 319 times.
425 Raster<float> covered;
105
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 319 times.
425 if (part) {
106
1/1
✓ Branch 1 taken 106 times.
106 covered = Raster<float>(*part);
107
1/1
✓ Branch 1 taken 106 times.
106 m_upsampler->Fill(*m_posts, covered, spacing, threads);
108
2/2
✓ Branch 0 taken 317 times.
✓ Branch 1 taken 2 times.
319 } else if (!window.Empty()) {
109
1/1
✓ Branch 1 taken 317 times.
317 m_upsampler->Fill(*m_posts, heights, spacing, threads);
110 }
111
2/2
✓ Branch 1 taken 425 times.
✓ Branch 4 taken 425 times.
425 ParallelFor(heights.Height(), threads, [&](std::size_t row) {
112
2/2
✓ Branch 0 taken 880514 times.
✓ Branch 1 taken 10719 times.
891233 for (std::size_t col = 0; col < heights.Width(); ++col) {
113
2/2
✓ Branch 0 taken 195279 times.
✓ Branch 1 taken 685235 times.
880514 heights(col, row) = HeightIn(heights, col, row, window, part ? &covered : nullptr);
114 }
115 10719 });
116 425 heights.SetNoData(m_noData);
117 425 }
118
119 882664 float RasterHeights::HeightIn(const Raster<float>& heights, std::size_t col, std::size_t row,
120 const PixelWindow& window, const Raster<float>* covered) const {
121 882664 constexpr float kMissing = std::numeric_limits<float>::quiet_NaN();
122
6/6
✓ Branch 0 taken 828039 times.
✓ Branch 1 taken 54625 times.
✓ Branch 2 taken 791943 times.
✓ Branch 3 taken 36096 times.
✓ Branch 4 taken 771807 times.
✓ Branch 5 taken 20136 times.
882664 if (col < window.colBegin || col >= window.colEnd || row < window.rowBegin ||
123
2/2
✓ Branch 0 taken 760236 times.
✓ Branch 1 taken 11571 times.
771807 row >= window.rowEnd) {
124 return kMissing;
125 }
126 760236 const Vec2d center = heights.CellCenter(col, row);
127 760236 const Vec2d pixel = m_lattice.transform.WorldToPixel(center.x, center.y);
128
2/2
✓ Branch 0 taken 753009 times.
✓ Branch 1 taken 7481 times.
760490 if (!Covers(pixel.x, pixel.y)) {
129 return kMissing;
130 }
131
2/2
✓ Branch 0 taken 75741 times.
✓ Branch 1 taken 677268 times.
1430273 return covered != nullptr ? (*covered)(col - window.colBegin, row - window.rowBegin)
132 677268 : heights(col, row);
133 }
134
135 571652 double RasterHeights::Turns(double x) const {
136
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 571310 times.
571652 if (!m_wrapLongitude) {
137 return 0.0;
138 }
139 342 const GeoTransform& transform = m_lattice.transform;
140 342 const double left =
141 684 std::min(transform.originX,
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
342 transform.originX + static_cast<double>(m_lattice.width) * transform.pixelWidth);
143 342 return std::floor((x - left) / kTurnDegrees);
144 }
145
146 308181 void RasterHeights::Fill(SampleBlock& samples, double spacing, unsigned threads) const {
147
2/2
✓ Branch 0 taken 422 times.
✓ Branch 1 taken 307759 times.
308181 if (Raster<float>* grid = samples.Grid()) {
148 422 FillGrid(*grid, spacing, threads);
149 } else {
150 307759 FillPoints(samples.Points(), samples.Heights(), spacing);
151 }
152 308170 }
153
154 422 void RasterHeights::FillGrid(Raster<float>& heights, double spacing, unsigned threads) const {
155
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 413 times.
422 const std::size_t width = heights.Width();
156 520 const auto turnsAt = [&](std::size_t col) { return Turns(heights.CellCenter(col, 0).x); };
157
4/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 413 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
422 if (!m_wrapLongitude || heights.Empty() || heights.Transform().IsRotated() ||
158
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
9 (turnsAt(0) == 0.0 && turnsAt(width - 1) == 0.0)) {
159 417 FillUnwrapped(heights, spacing, threads);
160 415 return;
161 }
162
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 for (std::size_t begin = 0; begin < width;) {
163 10 const double turns = turnsAt(begin);
164 10 std::size_t end = begin + 1;
165
4/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 70 times.
80 while (end < width && turnsAt(end) == turns) {
166 70 ++end;
167 }
168 10 GeoTransform transform = heights.Transform();
169 10 transform.originX +=
170 10 static_cast<double>(begin) * transform.pixelWidth - turns * kTurnDegrees;
171 10 Raster<float> part(RasterGrid{end - begin, heights.Height(), transform});
172
1/1
✓ Branch 1 taken 10 times.
10 FillUnwrapped(part, spacing, threads);
173
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 10 times.
58 for (std::size_t row = 0; row < part.Height(); ++row) {
174 48 std::copy_n(part.Row(row).begin(), part.Width(), heights.Row(row).begin() + begin);
175 }
176 10 begin = end;
177 10 }
178 5 heights.SetNoData(m_noData);
179 }
180
181 571556 Vec2d RasterHeights::Unwrapped(const Vec2d& point) const {
182 571556 return {point.x - Turns(point.x) * kTurnDegrees, point.y};
183 }
184
185 307759 void RasterHeights::FillPoints(Span<const Vec2d> points, Span<float> heights,
186 double spacing) const {
187 307759 std::fill(heights.begin(), heights.end(), std::numeric_limits<float>::quiet_NaN());
188 307759 detail::EvaluateChosen(
189 points.size(),
190 931158 [&](std::size_t i) -> std::optional<Vec2d> {
191 315646 const Vec2d at = Unwrapped(points[i]);
192 315647 const Vec2d pixel = m_lattice.transform.WorldToPixel(at.x, at.y);
193
2/2
✓ Branch 0 taken 254534 times.
✓ Branch 1 taken 61113 times.
315647 return Covers(pixel.x, pixel.y) ? std::optional<Vec2d>(at) : std::nullopt;
194 },
195 862607 [&](Span<const Vec2d> covered, Span<float> values) {
196 247095 m_upsampler->HeightsAt(*m_posts, covered, values, spacing);
197 247091 },
198 562291 [&](std::size_t i, float value) { heights[i] = value; });
199 307753 }
200
201 255908 bool RasterHeights::HasHeight(double x, double y) const {
202 255908 const Vec2d at = Unwrapped({x, y});
203 255908 const Vec2d pixel = m_lattice.transform.WorldToPixel(at.x, at.y);
204
2/2
✓ Branch 0 taken 228465 times.
✓ Branch 1 taken 27453 times.
255918 if (!Covers(pixel.x, pixel.y)) {
205 return false;
206 }
207 228465 float post = 0.0f;
208 228465 m_posts->Read(static_cast<std::int64_t>(std::floor(pixel.x)),
209 static_cast<std::int64_t>(std::floor(pixel.y)), 1, 1, Span<float>(&post, 1));
210 228465 return !std::isnan(post);
211 }
212
213 255900 double RasterHeights::ResolutionAt(double x, double y) const {
214
2/2
✓ Branch 1 taken 228136 times.
✓ Branch 2 taken 27773 times.
255900 return HasHeight(x, y) ? m_resolution : std::numeric_limits<double>::infinity();
215 }
216
217 6 double RasterHeights::PostSpacingAt(double x, double y) const {
218
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 return HasHeight(x, y) ? m_cellSpacing : std::numeric_limits<double>::infinity();
219 }
220
221 255899 double RasterHeightsSource::ResolutionAt(double x, double y) const {
222 255899 return Heights().ResolutionAt(x, y);
223 }
224
225 6 double RasterHeightsSource::PostSpacingAt(double x, double y) const {
226 6 return Heights().PostSpacingAt(x, y);
227 }
228
229 308180 void RasterHeightsSource::FillSamples(SampleBlock& samples, double spacing,
230 unsigned threads) const {
231 308180 Heights().Fill(samples, spacing, threads);
232 308171 }
233
234 } // namespace detail
235
236 122 RasterSource::RasterSource(Raster<float> heights, const RasterSourceOptions& options, GeoKeys keys)
237 122 : RasterHeightsSource(heights.Grid(), WholeWindow(heights.Grid()), std::move(keys)),
238
2/2
✓ Branch 4 taken 122 times.
✓ Branch 8 taken 116 times.
250 m_heights(WholeHeights(std::move(heights), options, Keys())) {}
239
240 1 const std::shared_ptr<const Upsampler>& RasterSource::UpsamplerInUse() const {
241 1 return m_heights->UpsamplerInUse();
242 }
243
244 178288 const detail::RasterHeights& RasterSource::Heights() const {
245 178288 return *m_heights;
246 }
247
248 } // namespace terraingen
249