src/distribution/piecewise_uniform.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/distribution/piecewise_uniform.h" | ||
| 4 | |||
| 5 | #include "util/checks.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cmath> | ||
| 9 | #include <cstddef> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <utility> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | /// The diameters the bands cover, up to the largest `max`, after checking the bands. | ||
| 18 | 27 | DiameterRange BandRange(const std::vector<UniformBand>& bands) { | |
| 19 | 27 | if (bands.empty()) { | |
| 20 | 1 | throw std::invalid_argument("A piecewise uniform distribution needs at least one band"); | |
| 21 | } | ||
| 22 | 26 | double top = 0.0; | |
| 23 | 63 | for (const UniformBand& band : bands) { | |
| 24 | 123 | if (!IsPositive(band.min) || !IsPositive(band.max) || !(band.max > band.min)) { | |
| 25 | 3 | throw std::invalid_argument( | |
| 26 | 3 | "Band diameters must be positive and finite, with max above min"); | |
| 27 | } | ||
| 28 | 39 | if (!std::isfinite(band.density) || band.density < 0.0) { | |
| 29 | 2 | throw std::invalid_argument("Band densities must be finite and >= 0"); | |
| 30 | } | ||
| 31 | 68 | top = std::max(top, band.max); | |
| 32 | } | ||
| 33 | 21 | return DiameterRange{0.0, top}; | |
| 34 | } | ||
| 35 | |||
| 36 | /// `bands` with densities scaled to sum to `total` when it is positive. | ||
| 37 | 21 | std::vector<UniformBand> ScaleBands(std::vector<UniformBand> bands, double total) { | |
| 38 | 21 | if (!std::isfinite(total) || total < 0.0) { | |
| 39 | 2 | throw std::invalid_argument("A band total must be finite and >= 0"); | |
| 40 | } | ||
| 41 | 19 | double sum = 0.0; | |
| 42 | 54 | for (const UniformBand& band : bands) { | |
| 43 | 35 | sum += band.density; | |
| 44 | } | ||
| 45 | 19 | if (!(sum > 0.0)) { | |
| 46 | 1 | throw std::invalid_argument("A piecewise uniform distribution needs a positive density"); | |
| 47 | } | ||
| 48 | 18 | if (total > 0.0) { | |
| 49 | 24 | for (UniformBand& band : bands) { | |
| 50 | 16 | band.density *= total / sum; | |
| 51 | } | ||
| 52 | } | ||
| 53 | 18 | return bands; | |
| 54 | } | ||
| 55 | |||
| 56 | } // namespace | ||
| 57 | |||
| 58 | 27 | PiecewiseUniformSfd::PiecewiseUniformSfd(std::vector<UniformBand> bands, SfdKind kind, double total) | |
| 59 | : SizeFrequencyDistribution(kind, BandRange(bands)), | ||
| 60 | 27 | m_bands(ScaleBands(std::move(bands), total)) { | |
| 61 | 18 | SetAllowExtrapolation(true); | |
| 62 | 52 | for (const UniformBand& band : m_bands) { | |
| 63 | 34 | m_edges.push_back(band.min); | |
| 64 | 34 | m_edges.push_back(band.max); | |
| 65 | } | ||
| 66 | 18 | std::sort(m_edges.begin(), m_edges.end()); | |
| 67 | 18 | m_edges.erase(std::unique(m_edges.begin(), m_edges.end()), m_edges.end()); | |
| 68 | 81 | for (const double edge : m_edges) { | |
| 69 | 63 | m_edgeCumulative.push_back(CumulativeAt(edge)); | |
| 70 | } | ||
| 71 | 18 | } | |
| 72 | |||
| 73 | 120385 | double PiecewiseUniformSfd::CumulativeAt(double diameter) const { | |
| 74 | 120385 | double total = 0.0; | |
| 75 | 361164 | for (const UniformBand& band : m_bands) { | |
| 76 | 481558 | total += band.density * std::clamp((band.max - diameter) / (band.max - band.min), 0.0, 1.0); | |
| 77 | } | ||
| 78 | 120385 | return total; | |
| 79 | } | ||
| 80 | |||
| 81 | 5 | double PiecewiseUniformSfd::IncrementalAt(double diameter) const { | |
| 82 | 5 | double total = 0.0; | |
| 83 | 18 | for (const UniformBand& band : m_bands) { | |
| 84 | 13 | if (diameter >= band.min && diameter < band.max) { | |
| 85 | 5 | total += band.density / (band.max - band.min); | |
| 86 | } | ||
| 87 | } | ||
| 88 | 5 | return total; | |
| 89 | } | ||
| 90 | |||
| 91 | 40105 | double PiecewiseUniformSfd::InvertCumulative(double target, double /*minDiameter*/, | |
| 92 | double /*maxDiameter*/) const { | ||
| 93 | 40105 | const auto below = std::partition_point(m_edgeCumulative.begin(), m_edgeCumulative.end(), | |
| 94 | 90407 | [target](double value) { return value > target; }); | |
| 95 | 40105 | const auto i = static_cast<std::size_t>(below - m_edgeCumulative.begin()); | |
| 96 | 40105 | if (i == 0) { | |
| 97 | 2 | return m_edges.front(); | |
| 98 | } | ||
| 99 | 40103 | if (i == m_edges.size()) { | |
| 100 | 1 | return m_edges.back(); | |
| 101 | } | ||
| 102 | 40102 | const double high = m_edgeCumulative[i - 1]; | |
| 103 | 40102 | const double low = m_edgeCumulative[i]; | |
| 104 | 40102 | return m_edges[i - 1] + (high - target) / (high - low) * (m_edges[i] - m_edges[i - 1]); | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace terraingen | ||
| 108 |