./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
97 / 97
100.0%
Branches:
51 / 57
89.5%
Functions:
11 / 11
100.0%

src/analysis/raster_stats.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/analysis/raster_stats.h"
4
5 #include "terraingen/geometry/geometry.h"
6 #include "terraingen/util/parallel.h"
7
8 #include <algorithm>
9 #include <cmath>
10 #include <cstddef>
11 #include <limits>
12 #include <vector>
13
14 namespace terraingen {
15
16 namespace {
17
18 constexpr double kNan = std::numeric_limits<double>::quiet_NaN();
19
20 /// The interpolated `q` quantile of `values`, reordering them; NaN when empty.
21 /// @param q The quantile, from 0 to 1.
22 50 double Quantile(std::vector<double>& values, double q) {
23
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (values.empty()) {
24 return kNan;
25 }
26 50 const double position = q * static_cast<double>(values.size() - 1);
27 50 const auto rank = static_cast<std::size_t>(std::floor(position));
28 50 const auto at = values.begin() + static_cast<std::ptrdiff_t>(rank);
29 50 std::nth_element(values.begin(), at, values.end());
30 50 const double low = *at;
31
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (rank + 1 >= values.size()) {
32 return low;
33 }
34
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const double high = *std::min_element(at + 1, values.end());
35 50 return low + (position - static_cast<double>(rank)) * (high - low);
36 }
37
38 /// The neighbors along one grid axis a difference at `index` spans: both sides when they have
39 /// data, else the side that has and `index` itself; nothing when neither has.
40 /// @param valid Whether the cell `step` cells from `index` has data, for a step of -1 or 1.
41 template <typename Valid>
42 69268 bool DifferenceSpan(std::size_t index, std::size_t size, const Valid& valid, std::size_t& first,
43 std::size_t& second) {
44
4/4
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 34126 times.
✓ Branch 1 taken 555 times.
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 33828 times.
✓ Branch 1 taken 759 times.
69268 const bool before = index > 0 && valid(index - 1);
45
4/4
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 34068 times.
✓ Branch 1 taken 527 times.
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 33811 times.
✓ Branch 1 taken 761 times.
69167 const bool after = index + 1 < size && valid(index + 1);
46
4/4
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 34017 times.
✓ Branch 1 taken 561 times.
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 33786 times.
✓ Branch 1 taken 747 times.
69111 first = before ? index - 1 : index;
47
4/4
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#1} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 34051 times.
✓ Branch 1 taken 527 times.
bool terraingen::(anonymous namespace)::DifferenceSpan<terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2}>(unsigned long, unsigned long, terraingen::(anonymous namespace)::CellSlope(terraingen::Raster<float> const&, terraingen::Surface const&, unsigned long, unsigned long)::{lambda(unsigned long)#2} const&, unsigned long&, unsigned long&):
✓ Branch 0 taken 33790 times.
✓ Branch 1 taken 743 times.
69111 second = after ? index + 1 : index;
48 69111 return before || after;
49 }
50
51 /// The slope in degrees of the heights at cell (`col`, `row`), or NaN where it cannot be taken.
52 34707 double CellSlope(const Raster<float>& heights, const Surface& surface, std::size_t col,
53 std::size_t row) {
54 203947 const auto valid = [&heights](std::size_t c, std::size_t r) {
55 169240 return !heights.IsMissing(heights(c, r));
56 34707 };
57
2/2
✓ Branch 1 taken 34681 times.
✓ Branch 2 taken 13 times.
34707 if (!valid(col, row)) {
58 return kNan;
59 }
60 34681 std::size_t west = 0;
61 34681 std::size_t east = 0;
62 34681 std::size_t north = 0;
63 34681 std::size_t south = 0;
64 69270 const bool across = DifferenceSpan(
65 102875 col, heights.Width(), [&](std::size_t c) { return valid(c, row); }, west, east);
66 69153 const bool down = DifferenceSpan(
67 102228 row, heights.Height(), [&](std::size_t r) { return valid(col, r); }, north, south);
68
1/2
✓ Branch 0 taken 34564 times.
✗ Branch 1 not taken.
34564 if (!across || !down) {
69 return kNan;
70 }
71 34564 const Vec2d center = heights.CellCenter(col, row);
72 34605 const Vec2d a = surface.Offset(center, heights.CellCenter(east, row)) -
73 34564 surface.Offset(center, heights.CellCenter(west, row));
74 34663 const Vec2d b = surface.Offset(center, heights.CellCenter(col, south)) -
75 34587 surface.Offset(center, heights.CellCenter(col, north));
76 34667 const double riseA =
77 34667 static_cast<double>(heights(east, row)) - static_cast<double>(heights(west, row));
78 34644 const double riseB =
79 34644 static_cast<double>(heights(col, south)) - static_cast<double>(heights(col, north));
80 34610 const double det = a.x * b.y - a.y * b.x;
81
2/4
✓ Branch 0 taken 34610 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34612 times.
✗ Branch 3 not taken.
34610 if (det == 0.0 || !std::isfinite(det)) {
82 return kNan;
83 }
84 34612 const double dx = (riseA * b.y - riseB * a.y) / det;
85 34612 const double dy = (a.x * riseB - b.x * riseA) / det;
86 34612 return GradientSlopeDegrees(dx, dy);
87 }
88
89 } // namespace
90
91 32 RasterStats ComputeStats(const Raster<float>& raster) {
92 32 RasterStats stats;
93 61 const auto forEachValid = [&](const auto& visit) {
94
4/4
auto terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(auto:1 const&)#1}::operator()<terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#1}>(terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#1} const&) const:
✓ Branch 0 taken 36647 times.
✓ Branch 1 taken 32 times.
auto terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(auto:1 const&)#1}::operator()<terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#2}>(terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#2} const&) const:
✓ Branch 0 taken 36638 times.
✓ Branch 1 taken 29 times.
73346 for (const float value : raster.Values()) {
95
4/4
auto terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(auto:1 const&)#1}::operator()<terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#1}>(terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#1} const&) const:
✓ Branch 0 taken 36629 times.
✓ Branch 1 taken 18 times.
auto terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(auto:1 const&)#1}::operator()<terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#2}>(terraingen::ComputeStats(terraingen::Raster<float> const&)::{lambda(double)#2} const&) const:
✓ Branch 0 taken 36629 times.
✓ Branch 1 taken 9 times.
73285 if (!raster.IsMissing(value)) {
96 73258 visit(static_cast<double>(value));
97 }
98 }
99 61 };
100 32 double sum = 0.0;
101 32 forEachValid([&](double v) {
102
2/2
✓ Branch 0 taken 36600 times.
✓ Branch 1 taken 29 times.
36629 stats.min = stats.validCount == 0 ? v : std::fmin(stats.min, v);
103
2/2
✓ Branch 0 taken 36600 times.
✓ Branch 1 taken 29 times.
36629 stats.max = stats.validCount == 0 ? v : std::fmax(stats.max, v);
104 36629 sum += v;
105 36629 ++stats.validCount;
106 36629 });
107
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 3 times.
32 if (stats.validCount == 0) {
108 return stats;
109 }
110 29 const auto count = static_cast<double>(stats.validCount);
111 29 stats.mean = sum / count;
112 29 double squares = 0.0;
113 36658 forEachValid([&](double v) { squares += (v - stats.mean) * (v - stats.mean); });
114 29 stats.stddev = std::sqrt(squares / count);
115 29 return stats;
116 }
117
118 27 SlopeStats ComputeSlopeStats(const Raster<float>& heights, const Surface& surface,
119 unsigned threads) {
120 27 const std::size_t width = heights.Width();
121 27 std::vector<double> cells(heights.Size(), kNan);
122
2/2
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
27 ParallelFor(heights.Height(), threads, [&](std::size_t row) {
123
2/2
✓ Branch 0 taken 34687 times.
✓ Branch 1 taken 610 times.
35297 for (std::size_t col = 0; col < width; ++col) {
124 34687 cells[row * width + col] = CellSlope(heights, surface, col, row);
125 }
126 610 });
127 27 std::vector<double> slopes;
128 27 double sum = 0.0;
129
2/2
✓ Branch 0 taken 36635 times.
✓ Branch 1 taken 27 times.
36662 for (const double slope : cells) {
130
2/2
✓ Branch 0 taken 36621 times.
✓ Branch 1 taken 14 times.
36635 if (!std::isnan(slope)) {
131
1/1
✓ Branch 1 taken 36621 times.
36621 slopes.push_back(slope);
132 36621 sum += slope;
133 }
134 }
135 27 SlopeStats stats;
136
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 stats.validCount = slopes.size();
137
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 if (slopes.empty()) {
138 return stats;
139 }
140 25 stats.mean = sum / static_cast<double>(slopes.size());
141 25 stats.median = Quantile(slopes, 0.5);
142 25 stats.p95 = Quantile(slopes, 0.95);
143 25 return stats;
144 27 }
145
146 } // namespace terraingen
147