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 | 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 | 39 | if (target == nullptr || target->Kind() != SfdKind::Density || !(area >= 0.0) || | |
| 48 | 31 | !std::isfinite(area)) { | |
| 49 | return kNan; | ||
| 50 | } | ||
| 51 | 31 | try { | |
| 52 | 31 | return target->ExpectedCount(area, low, high); | |
| 53 | 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 | 202 | for (const double diameter : craters.craters.Diameters()) { | |
| 62 | 189 | if (diameter > 0.0 && std::isfinite(diameter)) { | |
| 63 | 189 | bins.push_back(DiameterBin(diameter)); | |
| 64 | } | ||
| 65 | } | ||
| 66 | 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 | 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 | 39 | entry.expected = ExpectedCount(target, area, entry.minDiameter, entry.maxDiameter); | |
| 76 | 39 | report.craterBins.push_back(entry); | |
| 77 | } | ||
| 78 | 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 | 771 | for (const double diameter : boulders.boulders.Diameters()) { | |
| 88 | 764 | footprints += 0.25 * kPi * diameter * diameter; | |
| 89 | } | ||
| 90 | 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 | 27 | const double area = report.grid.SurfaceArea(surface); | |
| 101 | 27 | AddHeightStatistics(heights, report); | |
| 102 | 27 | AddSlopeStatistics(heights, surface, threads, report); | |
| 103 | 27 | if (craters != nullptr) { | |
| 104 | 13 | AddCraterBins(*craters, craterTarget, area, report); | |
| 105 | } | ||
| 106 | 27 | if (boulders != nullptr) { | |
| 107 | 7 | AddBoulders(*boulders, area, report); | |
| 108 | } | ||
| 109 | 27 | return report; | |
| 110 | } | ||
| 111 | |||
| 112 | } // namespace terraingen | ||
| 113 |