./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
46 / 49
93.9%
Branches:
30 / 36
83.3%
Functions:
8 / 8
100.0%

src/sources/source.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/sources/source.h"
4
5 #include "sources/lattice.h"
6
7 #include <cmath>
8 #include <limits>
9 #include <optional>
10 #include <stdexcept>
11 #include <string>
12
13 namespace terraingen {
14
15 933 void MarkMissing(Raster<float>& heights, std::optional<float> noData) {
16
2/2
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 666 times.
933 const float fill = noData.value_or(std::numeric_limits<float>::quiet_NaN());
17 933 bool missing = false;
18
2/2
✓ Branch 0 taken 7970735 times.
✓ Branch 1 taken 933 times.
7971668 for (float& value : heights.Values()) {
19
2/2
✓ Branch 0 taken 185888 times.
✓ Branch 1 taken 7784847 times.
7970735 if (std::isnan(value)) {
20 185888 value = fill;
21 185888 missing = true;
22 }
23 }
24
4/4
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 267 times.
✓ Branch 2 taken 556 times.
✓ Branch 3 taken 110 times.
933 heights.SetNoData(noData || !missing ? noData : std::optional<float>(fill));
25 933 }
26
27 180 std::optional<RasterGrid> detail::ExactSubgrid(const RasterGrid& grid, const PixelWindow& cells) {
28 180 const GeoTransform& whole = grid.transform;
29
4/4
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 176 times.
✓ Branch 3 taken 3 times.
359 if (whole.IsRotated() || cells.Empty()) {
30 4 return std::nullopt;
31 }
32 176 const RasterGrid part = Subgrid(grid, cells);
33 6890 const auto center = [](const GeoTransform& transform, std::size_t col, std::size_t row) {
34 6890 return transform.PixelToWorld(static_cast<double>(col) + 0.5,
35 3254 static_cast<double>(row) + 0.5);
36 };
37
2/2
✓ Branch 0 taken 3636 times.
✓ Branch 1 taken 144 times.
3780 for (std::size_t c = 0; c < part.width; ++c) {
38
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 3604 times.
3636 if (center(whole, cells.colBegin + c, 0).x != center(part.transform, c, 0).x) {
39 32 return std::nullopt;
40 }
41 }
42
2/2
✓ Branch 0 taken 3254 times.
✓ Branch 1 taken 142 times.
3396 for (std::size_t r = 0; r < part.height; ++r) {
43
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3252 times.
3254 if (center(whole, 0, cells.rowBegin + r).y != center(part.transform, 0, r).y) {
44 2 return std::nullopt;
45 }
46 }
47 142 return part;
48 }
49
50 2460494 void Source::Fill(SampleBlock& samples, double spacing, unsigned threads) const {
51 2460494 FillSamples(samples, spacing, threads);
52
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 2464486 times.
2465416 if (Raster<float>* grid = samples.Grid()) {
53 930 MarkMissing(*grid, grid->NoData());
54 }
55 2465416 }
56
57 932 void Source::Fill(Raster<float>& heights, double spacing, unsigned threads) const {
58 932 SampleBlock samples(heights);
59 932 Fill(samples, spacing, threads);
60 930 }
61
62 23 void Source::Fill(const RasterGrid& grid, Span<float> heights, double spacing,
63 unsigned threads) const {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (heights.size() != grid.width * grid.height) {
65
0/1
✗ Branch 1 not taken.
✗ throw std::invalid_argument("Filling a source needs " +
66
0/2
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ std::to_string(grid.width * grid.height) + " heights, not " +
67
0/2
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ std::to_string(heights.size()));
68 }
69 23 Raster<float> filled(grid, heights);
70
1/1
✓ Branch 1 taken 23 times.
23 Fill(filled, spacing, threads);
71
2/2
✓ Branch 0 taken 8511 times.
✓ Branch 1 taken 23 times.
8534 for (float& value : heights) {
72
2/2
✓ Branch 0 taken 7721 times.
✓ Branch 1 taken 790 times.
8511 value = filled.IsMissing(value) ? std::numeric_limits<float>::quiet_NaN() : value;
73 }
74 23 }
75
76 404072 float Source::HeightAt(double x, double y, double spacing) const {
77 808144 return detail::PointHeight(x, y, [&](SampleBlock& samples) { Fill(samples, spacing, 1); });
78 }
79
80 2057486 void Source::HeightsAt(Span<const Vec2d> points, Span<float> heights, double spacing) const {
81 2057486 SampleBlock samples(points, heights);
82 2061336 Fill(samples, spacing, 1);
83 2060761 }
84
85 } // namespace terraingen
86