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