src/layers/boulders/boulder_abundance.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder_abundance.h" | ||
| 4 | |||
| 5 | #include "layers/boulders/boulder_clusters.h" | ||
| 6 | #include "terraingen/distribution/golombek_rapp.h" | ||
| 7 | #include "terraingen/distribution/power_law.h" | ||
| 8 | #include "terraingen/distribution/summed.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <cmath> | ||
| 12 | #include <stdexcept> | ||
| 13 | #include <utility> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// Coverages the Golombek-Rapp measures are tabulated at, log-spaced. | ||
| 20 | constexpr int kCoverageSteps = 512; | ||
| 21 | constexpr double kLeastCoverage = 1e-6; | ||
| 22 | constexpr double kMostCoverage = 0.999; | ||
| 23 | |||
| 24 | /// What `map` measures of `distribution`, with the areal fraction of Golombek-Rapp boulders in | ||
| 25 | /// closed form so that it needs no largest diameter. | ||
| 26 | 1542 | double BoulderMeasure(const DensityMap& map, const SizeFrequencyDistribution& distribution) { | |
| 27 | 1542 | const auto* rapp = dynamic_cast<const GolombekRappSfd*>(&distribution); | |
| 28 | 1542 | if (rapp != nullptr && map.measure == DensityMeasure::ArealFraction) { | |
| 29 | 1024 | const double beyond = | |
| 30 | 1024 | std::isinf(map.maxDiameter) ? 0.0 : rapp->FractionalArea(map.maxDiameter); | |
| 31 | 1024 | return rapp->FractionalArea(map.minDiameter) - beyond; | |
| 32 | } | ||
| 33 | 518 | return MapMeasure(map, distribution); | |
| 34 | } | ||
| 35 | |||
| 36 | /// The largest background abundance with patches averaging `patchMean` and a density map whose | ||
| 37 | /// largest abundance is `mapMax`, if there is a map. | ||
| 38 | 745 | double MaxBackground(const BoulderLayerOptions& options, double patchMean, | |
| 39 | std::optional<double> mapMax) { | ||
| 40 | 745 | const double a = options.bedrock.abundance; | |
| 41 | 745 | double patches = options.patches.spread; | |
| 42 | 745 | double bedrock = options.bedrock.exposure ? std::fmax(1.0, a) : 1.0; | |
| 43 | 745 | double map = 1.0; | |
| 44 | 745 | if (mapMax) { | |
| 45 | 12 | patches /= patchMean; | |
| 46 | 12 | bedrock = options.bedrock.exposure ? std::fmax(1.0, a) / std::fmin(1.0, a) : 1.0; | |
| 47 | 12 | map = *mapMax; | |
| 48 | } | ||
| 49 | 745 | return patches * bedrock * MaxClusterAbundance(options.clusters) * map; | |
| 50 | } | ||
| 51 | |||
| 52 | } // namespace | ||
| 53 | |||
| 54 | 18 | BoulderMapAbundance::BoulderMapAbundance(const BoulderLayerOptions& options) { | |
| 55 | 18 | if (!options.densityMap) { | |
| 56 | 1 | throw std::invalid_argument("Boulder map abundance needs a density map"); | |
| 57 | } | ||
| 58 | 17 | m_map = *options.densityMap; | |
| 59 | 17 | ValidateDensityMap(m_map); | |
| 60 | 17 | const BoulderSizes& sizes = options.sizes; | |
| 61 | 17 | if (m_map.total && options.ejecta.ThrowingDiameter()) { | |
| 62 | 3 | m_ejectaMeasure = BoulderMeasure(m_map, *EjectaSizes(options)); | |
| 63 | } | ||
| 64 | 17 | if (sizes.distribution || m_map.measure == DensityMeasure::Scale) { | |
| 65 | 14 | m_measure = sizes.distribution ? BoulderMeasure(m_map, *sizes.distribution) : 1.0; | |
| 66 | 14 | if (!(m_measure > 0.0 && std::isfinite(m_measure))) { | |
| 67 | ✗ | throw std::invalid_argument("A boulder density map must measure some of the sizes"); | |
| 68 | } | ||
| 69 | } else { | ||
| 70 | 3 | m_coverage = sizes.Coverage(); | |
| 71 | 1539 | for (int i = 0; i < kCoverageSteps; ++i) { | |
| 72 | 1536 | const double k = | |
| 73 | 1536 | kLeastCoverage * std::pow(kMostCoverage / kLeastCoverage, | |
| 74 | 1536 | static_cast<double>(i) / (kCoverageSteps - 1)); | |
| 75 | 1536 | m_coverages.push_back(k); | |
| 76 | 1536 | m_measures.push_back(BoulderMeasure( | |
| 77 | 3072 | m_map, GolombekRappSfd(k, sizes.QConstant(), sizes.QCoverageCoefficient()))); | |
| 78 | } | ||
| 79 | 3 | if (!(m_measures.back() > 0.0)) { | |
| 80 | ✗ | throw std::invalid_argument("A boulder density map must measure some of the sizes"); | |
| 81 | } | ||
| 82 | } | ||
| 83 | 17 | const double top = m_map.field->Max(); | |
| 84 | 17 | const double largest = m_map.measure == DensityMeasure::Count ? top / m_map.areaUnit : top; | |
| 85 | 17 | m_max = std::fmax(1.0, At(largest, 0.0)); | |
| 86 | 20 | } | |
| 87 | |||
| 88 | 14 | double BoulderMapAbundance::Coverage(double target) const { | |
| 89 | 14 | if (target <= m_measures.front()) { | |
| 90 | 2 | return m_coverages.front() * target / m_measures.front(); | |
| 91 | } | ||
| 92 | 12 | if (target >= m_measures.back()) { | |
| 93 | 1 | return m_coverages.back(); | |
| 94 | } | ||
| 95 | 11 | const auto above = std::lower_bound(m_measures.begin(), m_measures.end(), target); | |
| 96 | 11 | const auto i = static_cast<std::size_t>(above - m_measures.begin()); | |
| 97 | 11 | const double t = (target - m_measures[i - 1]) / (m_measures[i] - m_measures[i - 1]); | |
| 98 | 11 | return m_coverages[i - 1] + t * (m_coverages[i] - m_coverages[i - 1]); | |
| 99 | } | ||
| 100 | |||
| 101 | 1080973 | double BoulderMapAbundance::At(double value, double ejecta) const { | |
| 102 | 1080973 | if (std::isnan(value)) { | |
| 103 | return 1.0; | ||
| 104 | } | ||
| 105 | 1050972 | if (m_map.measure == DensityMeasure::Scale) { | |
| 106 | 1050952 | return std::fmax(0.0, value); | |
| 107 | } | ||
| 108 | 20 | const double target = value - ejecta * m_ejectaMeasure; | |
| 109 | 20 | if (!(target > 0.0)) { | |
| 110 | return 0.0; | ||
| 111 | } | ||
| 112 | 19 | return m_coverages.empty() ? target / m_measure : Coverage(target) / m_coverage; | |
| 113 | } | ||
| 114 | |||
| 115 | 15 | double MeanPatchAbundance(const BoulderPatches& patches) { | |
| 116 | 15 | if (!(patches.spread > 1.0)) { | |
| 117 | return 1.0; | ||
| 118 | } | ||
| 119 | 4 | return LatticeMean(32.0, [&](double x, double y) { | |
| 120 | 262144 | return std::pow(patches.spread, PatchNoise({}, x, y, 1.0, kBoulderPatchStream)); | |
| 121 | }); | ||
| 122 | } | ||
| 123 | |||
| 124 | 745 | BoulderBackground::BoulderBackground(const BoulderLayerOptions& options) { | |
| 125 | 745 | if (options.densityMap) { | |
| 126 | 12 | patchMean = MeanPatchAbundance(options.patches); | |
| 127 | 12 | map.emplace(options); | |
| 128 | } | ||
| 129 | 745 | max = MaxBackground(options, patchMean, map ? std::optional<double>(map->Max()) : std::nullopt); | |
| 130 | 745 | } | |
| 131 | |||
| 132 | 443 | double MaxBackgroundAbundance(const BoulderLayerOptions& options) { | |
| 133 | 443 | return BoulderBackground(options).max; | |
| 134 | } | ||
| 135 | |||
| 136 | 104 | std::shared_ptr<const SizeFrequencyDistribution> EjectaSizes(const BoulderLayerOptions& options) { | |
| 137 | 104 | const BoulderEjecta& ejecta = options.ejecta; | |
| 138 | 104 | if (ejecta.distribution) { | |
| 139 | 42 | return ejecta.distribution; | |
| 140 | } | ||
| 141 | 86 | return std::make_shared<GolombekRappSfd>(ejecta.Coverage(), options.sizes.QConstant(), | |
| 142 | 124 | options.sizes.QCoverageCoefficient()); | |
| 143 | } | ||
| 144 | |||
| 145 | std::shared_ptr<const SizeFrequencyDistribution> | ||
| 146 | 133 | BoulderCandidates(const BoulderLayerOptions& options) { | |
| 147 | 133 | ValidateBoulderLayerOptions(options); | |
| 148 | 132 | if (!options.generateRandom) { | |
| 149 | 2 | throw std::invalid_argument( | |
| 150 | 2 | "A boulder layer without generateRandom draws no candidate boulders"); | |
| 151 | } | ||
| 152 | 130 | return BoulderCandidates(options, MaxBackgroundAbundance(options)); | |
| 153 | } | ||
| 154 | |||
| 155 | std::shared_ptr<const SizeFrequencyDistribution> | ||
| 156 | 432 | BoulderCandidates(const BoulderLayerOptions& options, double richest) { | |
| 157 | 432 | const BoulderSizes& sizes = options.sizes; | |
| 158 | 432 | std::vector<WeightedSfd> terms; | |
| 159 | 432 | if (sizes.distribution) { | |
| 160 | 135 | terms.push_back({richest, sizes.distribution}); | |
| 161 | 135 | if (options.slopeMap) { | |
| 162 | 8 | const SizeSlopeMap& map = *options.slopeMap; | |
| 163 | 8 | const double d0 = map.referenceDiameter; | |
| 164 | 8 | const double count = richest * sizes.distribution->Cumulative(d0); | |
| 165 | 8 | const double low = map.minExponent; | |
| 166 | 8 | const double high = map.maxExponent; | |
| 167 | 8 | terms.push_back( | |
| 168 | 8 | {1.0, std::make_shared<PowerLawSfd>(count * std::pow(d0, low) * high / low, low)}); | |
| 169 | 8 | terms.push_back({1.0, std::make_shared<PowerLawSfd>(count * std::pow(d0, high), high)}); | |
| 170 | } | ||
| 171 | } else { | ||
| 172 | 297 | terms.push_back( | |
| 173 | 564 | {1.0, std::make_shared<GolombekRappSfd>(sizes.Coverage() * richest, sizes.QConstant(), | |
| 174 | 594 | sizes.QCoverageCoefficient())}); | |
| 175 | } | ||
| 176 | 432 | if (options.ejecta.ThrowingDiameter()) { | |
| 177 | 60 | terms.push_back({1.0, EjectaSizes(options)}); | |
| 178 | } | ||
| 179 | 432 | return std::make_shared<SummedSfd>(std::move(terms)); | |
| 180 | 1372 | } | |
| 181 | |||
| 182 | } // namespace terraingen | ||
| 183 |