src/layers/density_map.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/density_map.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/layer_fields.h" | ||
| 6 | #include "terraingen/util/bits.h" | ||
| 7 | #include "util/numerics.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <stdexcept> | ||
| 12 | #include <utility> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | /// Simpson intervals in log diameter for an areal fraction. | ||
| 19 | constexpr int kArealSteps = 2048; | ||
| 20 | |||
| 21 | /// The share of ground covered by features of `distribution` in [`low`, `high`) meters: the | ||
| 22 | /// integral of pi D^2 / 4 dN/dD, taken in log diameter. | ||
| 23 | 1 | double ArealFraction(const SizeFrequencyDistribution& distribution, double low, double high) { | |
| 24 | 1 | double sum = 0.0; | |
| 25 | 1 | ForEachLogSimpsonNode(low, high, kArealSteps, [&](double d, double weight) { | |
| 26 | 2049 | sum += weight * 0.25 * kPi * d * d * distribution.Incremental(d); | |
| 27 | 2049 | }); | |
| 28 | 1 | return sum; | |
| 29 | } | ||
| 30 | |||
| 31 | } // namespace | ||
| 32 | |||
| 33 | 52 | void ValidateDensityMap(const DensityMap& map) { | |
| 34 | 52 | if (!map.field) { | |
| 35 | 2 | throw std::invalid_argument("A density map needs a field"); | |
| 36 | } | ||
| 37 | 50 | CheckRanges(map); | |
| 38 | 49 | if (map.measure != DensityMeasure::Scale && | |
| 39 | 16 | !(map.minDiameter > 0.0 && map.maxDiameter > map.minDiameter)) { | |
| 40 | 4 | throw std::invalid_argument("A density map's diameters must satisfy 0 < min < max"); | |
| 41 | } | ||
| 42 | 45 | } | |
| 43 | |||
| 44 | 539 | double MapMeasure(const DensityMap& map, const SizeFrequencyDistribution& distribution) { | |
| 45 | 539 | switch (map.measure) { | |
| 46 | case DensityMeasure::Scale: | ||
| 47 | return 1.0; | ||
| 48 | 524 | case DensityMeasure::Count: | |
| 49 | 524 | return distribution.ExpectedCount(1.0, map.minDiameter, map.maxDiameter); | |
| 50 | 2 | case DensityMeasure::ArealFraction: { | |
| 51 | 2 | const double high = std::fmin(map.maxDiameter, distribution.ValidRange().max); | |
| 52 | 2 | if (!std::isfinite(high)) { | |
| 53 | 1 | throw std::domain_error("An areal fraction needs a finite largest diameter"); | |
| 54 | } | ||
| 55 | 1 | return ArealFraction(distribution, map.minDiameter, high); | |
| 56 | } | ||
| 57 | } | ||
| 58 | return 1.0; | ||
| 59 | } | ||
| 60 | |||
| 61 | 18 | void ValidateSizeSlopeMap(const SizeSlopeMap& map) { | |
| 62 | 18 | if (!map.field) { | |
| 63 | 1 | throw std::invalid_argument("A size slope map needs a field"); | |
| 64 | } | ||
| 65 | 17 | CheckRanges(map); | |
| 66 | 14 | if (!(map.maxExponent > map.minExponent)) { | |
| 67 | 2 | throw std::invalid_argument("A size slope map's exponents must satisfy min < max"); | |
| 68 | } | ||
| 69 | 12 | } | |
| 70 | |||
| 71 | 72195 | std::optional<double> SizeSlopeAt(const SizeSlopeMap& map, double x, double y) { | |
| 72 | 72195 | const double value = map.field->ValueAt(x, y); | |
| 73 | 72195 | if (std::isnan(value)) { | |
| 74 | 1 | return std::nullopt; | |
| 75 | } | ||
| 76 | 72194 | double exponent = value; | |
| 77 | 72194 | if (map.measure == SizeSlopeMeasure::MeanDiameter) { | |
| 78 | 2 | const double above = value - map.referenceDiameter; | |
| 79 | 2 | exponent = above > 0.0 ? value / above : map.maxExponent; | |
| 80 | } | ||
| 81 | 144388 | return std::clamp(exponent, map.minExponent, map.maxExponent); | |
| 82 | } | ||
| 83 | |||
| 84 | 2122124 | double MapValueAt(const DensityMap& map, double x, double y) { | |
| 85 | 2122124 | const double value = map.field->ValueAt(x, y); | |
| 86 | 2122124 | return map.measure == DensityMeasure::Count ? value / map.areaUnit : value; | |
| 87 | } | ||
| 88 | |||
| 89 | 16 | double MapMax(const DensityMap& map) { | |
| 90 | 16 | const double top = map.field->Max(); | |
| 91 | 16 | return map.measure == DensityMeasure::Count ? top / map.areaUnit : top; | |
| 92 | } | ||
| 93 | |||
| 94 | 15 | DensityScale::DensityScale(DensityMap map, const SizeFrequencyDistribution& distribution) | |
| 95 | 15 | : m_map(std::move(map)) { | |
| 96 | 15 | ValidateDensityMap(m_map); | |
| 97 | 15 | m_measure = MapMeasure(m_map, distribution); | |
| 98 | 15 | if (!(m_measure > 0.0 && std::isfinite(m_measure))) { | |
| 99 | 1 | throw std::invalid_argument("A density map must measure some of its size distribution"); | |
| 100 | } | ||
| 101 | 14 | m_max = std::fmax(1.0, MapMax(m_map) / m_measure); | |
| 102 | 15 | } | |
| 103 | |||
| 104 | 527334 | double DensityScale::At(double x, double y) const { | |
| 105 | 527334 | return Scale(MapValueAt(m_map, x, y)); | |
| 106 | } | ||
| 107 | |||
| 108 | 1041187 | double DensityScale::Scale(double value) const { | |
| 109 | 1041187 | if (std::isnan(value)) { | |
| 110 | return 1.0; | ||
| 111 | } | ||
| 112 | 2081778 | return std::clamp(value / m_measure, 0.0, m_max); | |
| 113 | } | ||
| 114 | |||
| 115 | } // namespace terraingen | ||
| 116 |