./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
62 / 63
98.4%
Branches:
33 / 40
82.5%
Functions:
7 / 7
100.0%

src/pipeline/report.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/pipeline/report.h"
4
5 #include "terraingen/analysis/raster_stats.h"
6 #include "terraingen/geometry/geometry.h"
7
8 #include <algorithm>
9 #include <cmath>
10 #include <stdexcept>
11
12 namespace terraingen {
13
14 namespace {
15
16 constexpr double kNan = std::numeric_limits<double>::quiet_NaN();
17
18 27 void AddHeightStatistics(const Raster<float>& heights, GridReport& report) {
19 27 const RasterStats stats = ComputeStats(heights);
20
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 26 times.
27 if (stats.validCount == 0) {
21 1 return;
22 }
23 26 report.heightMin = stats.min;
24 26 report.heightMax = stats.max;
25 26 report.heightMean = stats.mean;
26 26 report.heightStd = stats.stddev;
27 }
28
29 27 void AddSlopeStatistics(const Raster<float>& heights, const Surface& surface, unsigned threads,
30 GridReport& report) {
31 27 const SlopeStats slopes = ComputeSlopeStats(heights, surface, threads);
32 27 report.slopeMeanDeg = slopes.mean;
33 27 report.slopeMedianDeg = slopes.median;
34 27 report.slopeP95Deg = slopes.p95;
35 27 }
36
37 /// The power-of-2 bin a positive, finite diameter falls in: floor(log2(`diameter`)).
38 189 int DiameterBin(double diameter) {
39 189 int exponent = 0;
40 189 std::frexp(diameter, &exponent);
41 189 return exponent - 1;
42 }
43
44 /// The count `target` expects over `area` square meters in [`low`, `high`) meters, or NaN.
45 39 double ExpectedCount(const SizeFrequencyDistribution* target, double area, double low,
46 double high) {
47
5/6
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
39 if (target == nullptr || target->Kind() != SfdKind::Density || !(area >= 0.0) ||
48
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !std::isfinite(area)) {
49 return kNan;
50 }
51 31 try {
52
1/1
✓ Branch 1 taken 28 times.
31 return target->ExpectedCount(area, low, high);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } catch (const std::domain_error&) {
54 3 return kNan;
55 3 }
56 }
57
58 13 void AddCraterBins(const CraterFeatures& craters, const SizeFrequencyDistribution* target,
59 double area, GridReport& report) {
60 13 std::vector<int> bins;
61
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 13 times.
202 for (const double diameter : craters.craters.Diameters()) {
62
2/4
✓ Branch 0 taken 189 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
✗ Branch 3 not taken.
189 if (diameter > 0.0 && std::isfinite(diameter)) {
63
1/1
✓ Branch 2 taken 189 times.
189 bins.push_back(DiameterBin(diameter));
64 }
65 }
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (bins.empty()) {
67 ✗ return;
68 }
69 13 const auto [smallest, largest] = std::minmax_element(bins.begin(), bins.end());
70 13 const int first = *smallest;
71
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (int bin = first; bin <= *largest; ++bin) {
72 39 CraterBin entry;
73 39 entry.minDiameter = std::ldexp(1.0, bin);
74 39 entry.maxDiameter = std::ldexp(1.0, bin + 1);
75
1/1
✓ Branch 1 taken 39 times.
39 entry.expected = ExpectedCount(target, area, entry.minDiameter, entry.maxDiameter);
76
1/1
✓ Branch 1 taken 39 times.
39 report.craterBins.push_back(entry);
77 }
78
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 13 times.
202 for (const int bin : bins) {
79 189 ++report.craterBins[static_cast<std::size_t>(bin - first)].count;
80 }
81 13 }
82
83 7 void AddBoulders(const BoulderFeatures& boulders, double area, GridReport& report) {
84 7 report.boulderCount = boulders.boulders.Size();
85 7 report.boulderModelCount = boulders.models.size();
86 7 double footprints = 0.0;
87
2/2
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 7 times.
771 for (const double diameter : boulders.boulders.Diameters()) {
88 764 footprints += 0.25 * kPi * diameter * diameter;
89 }
90
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 report.boulderCover = area > 0.0 ? footprints / area : kNan;
91 7 }
92
93 } // namespace
94
95 27 GridReport ReportGrid(const Raster<float>& heights, const Surface& surface,
96 const CraterFeatures* craters, const BoulderFeatures* boulders,
97 const SizeFrequencyDistribution* craterTarget, unsigned threads) {
98 27 GridReport report;
99 27 report.grid = heights.Grid();
100
1/1
✓ Branch 1 taken 27 times.
27 const double area = report.grid.SurfaceArea(surface);
101
1/1
✓ Branch 1 taken 27 times.
27 AddHeightStatistics(heights, report);
102
1/1
✓ Branch 1 taken 27 times.
27 AddSlopeStatistics(heights, surface, threads, report);
103
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 14 times.
27 if (craters != nullptr) {
104
1/1
✓ Branch 1 taken 13 times.
13 AddCraterBins(*craters, craterTarget, area, report);
105 }
106
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 20 times.
27 if (boulders != nullptr) {
107 7 AddBoulders(*boulders, area, report);
108 }
109 27 return report;
110 }
111
112 } // namespace terraingen
113