src/random/value_distribution.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/random/value_distribution.h" | ||
| 4 | |||
| 5 | #include "terraingen/random/random_fields.h" | ||
| 6 | #include "terraingen/random/samplers.h" | ||
| 7 | #include "terraingen/random/stream.h" | ||
| 8 | #include "util/checks.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <cmath> | ||
| 12 | #include <cstddef> | ||
| 13 | #include <numeric> | ||
| 14 | #include <stdexcept> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | /// The value at `u` in [0, 1) of the inverse CDF of piecewise-uniform bins; bins without weight | ||
| 21 | /// are never drawn. | ||
| 22 | 64006 | double SamplePiecewise(const std::vector<double>& edges, const std::vector<double>& weights, | |
| 23 | double u) { | ||
| 24 | 64006 | double target = u * std::accumulate(weights.begin(), weights.end(), 0.0); | |
| 25 | 64006 | std::size_t bin = 0; | |
| 26 | 129758 | while (bin + 1 < weights.size() && !(target < weights[bin])) { | |
| 27 | 65752 | target -= weights[bin]; | |
| 28 | 65752 | ++bin; | |
| 29 | } | ||
| 30 | 64006 | while (weights[bin] == 0.0 && bin > 0) { | |
| 31 | ✗ | --bin; | |
| 32 | ✗ | target = weights[bin]; | |
| 33 | } | ||
| 34 | 64006 | const double share = std::clamp(target / weights[bin], 0.0, 1.0); | |
| 35 | 64006 | return edges[bin] + (edges[bin + 1] - edges[bin]) * share; | |
| 36 | } | ||
| 37 | |||
| 38 | 16 | void CheckPiecewise(const ValueDistribution& d) { | |
| 39 | 16 | if (d.edges.size() < 2 || d.weights.size() + 1 != d.edges.size()) { | |
| 40 | 5 | throw std::invalid_argument( | |
| 41 | 5 | "A piecewise distribution needs at least two edges and one weight per bin"); | |
| 42 | } | ||
| 43 | 30 | for (std::size_t i = 0; i + 1 < d.edges.size(); ++i) { | |
| 44 | 21 | if (!(d.edges[i] < d.edges[i + 1])) { | |
| 45 | 2 | throw std::invalid_argument("A piecewise distribution needs strictly increasing edges"); | |
| 46 | } | ||
| 47 | } | ||
| 48 | 9 | const double total = std::accumulate(d.weights.begin(), d.weights.end(), 0.0); | |
| 49 | 9 | if (!IsPositive(total)) { | |
| 50 | 2 | throw std::invalid_argument("A piecewise distribution needs weights summing above zero"); | |
| 51 | } | ||
| 52 | 7 | } | |
| 53 | |||
| 54 | } // namespace | ||
| 55 | |||
| 56 | 191719 | double ValueDistribution::Draw(RandomStream& random) const { | |
| 57 | 191719 | const double u1 = random.NextDouble(); | |
| 58 | 191719 | const double u2 = random.NextDouble(); | |
| 59 | 191719 | return Sample(u1, u2); | |
| 60 | } | ||
| 61 | |||
| 62 | 62062094 | double ValueDistribution::Sample(double u1, double u2) const { | |
| 63 | 62062094 | double value = a; | |
| 64 | 62062094 | switch (kind) { | |
| 65 | case Kind::Constant: | ||
| 66 | break; | ||
| 67 | 876474 | case Kind::Uniform: | |
| 68 | 876474 | value = a + (b - a) * u1; | |
| 69 | 876474 | break; | |
| 70 | 224167 | case Kind::Normal: | |
| 71 | 224167 | value = a + b * StandardNormal(u1, u2); | |
| 72 | 224167 | break; | |
| 73 | 111290 | case Kind::LogNormal: | |
| 74 | 111290 | value = a * std::exp(b * StandardNormal(u1, u2)); | |
| 75 | 111290 | break; | |
| 76 | 64006 | case Kind::Piecewise: | |
| 77 | 64006 | value = SamplePiecewise(edges, weights, u1); | |
| 78 | 64006 | break; | |
| 79 | } | ||
| 80 | 62062094 | return std::clamp(value, min, max); | |
| 81 | } | ||
| 82 | |||
| 83 | 649925 | double ValueDistribution::Lowest() const { | |
| 84 | 649925 | switch (kind) { | |
| 85 | 647910 | case Kind::Constant: | |
| 86 | 647910 | case Kind::Uniform: | |
| 87 | 1295820 | return std::clamp(a, min, max); | |
| 88 | 1173 | case Kind::Normal: | |
| 89 | 1173 | return min; | |
| 90 | 841 | case Kind::LogNormal: | |
| 91 | 841 | return std::fmax(0.0, min); | |
| 92 | 1 | case Kind::Piecewise: | |
| 93 | 2 | return std::clamp(edges.front(), min, max); | |
| 94 | } | ||
| 95 | ✗ | return min; | |
| 96 | } | ||
| 97 | |||
| 98 | 508073 | double ValueDistribution::Highest() const { | |
| 99 | 508073 | switch (kind) { | |
| 100 | 12580 | case Kind::Constant: | |
| 101 | 25160 | return std::clamp(a, min, max); | |
| 102 | 493082 | case Kind::Uniform: | |
| 103 | 986164 | return std::clamp(b, min, max); | |
| 104 | 2409 | case Kind::Normal: | |
| 105 | 2409 | case Kind::LogNormal: | |
| 106 | 2409 | return max; | |
| 107 | 2 | case Kind::Piecewise: | |
| 108 | 4 | return std::clamp(edges.back(), min, max); | |
| 109 | } | ||
| 110 | ✗ | return max; | |
| 111 | } | ||
| 112 | |||
| 113 | 811820 | void ValidateValueDistribution(const ValueDistribution& d) { | |
| 114 | 811820 | CheckRanges(d); | |
| 115 | 811675 | if (std::isnan(d.min) || std::isnan(d.max) || d.min > d.max) { | |
| 116 | 2 | throw std::invalid_argument("Distribution min must not exceed max"); | |
| 117 | } | ||
| 118 | 811673 | if (d.kind == ValueDistribution::Kind::Uniform && d.b < d.a) { | |
| 119 | 5 | throw std::invalid_argument("A uniform distribution needs b >= a"); | |
| 120 | } | ||
| 121 | 811668 | if ((d.kind == ValueDistribution::Kind::Normal || | |
| 122 | 811668 | d.kind == ValueDistribution::Kind::LogNormal) && | |
| 123 | 2032 | (d.b < 0.0 || !std::isfinite(d.max))) { | |
| 124 | 11 | throw std::invalid_argument( | |
| 125 | 11 | "Normal and log-normal distributions need a non-negative spread and a finite max"); | |
| 126 | } | ||
| 127 | 811657 | if (d.kind == ValueDistribution::Kind::LogNormal && !(d.a > 0.0)) { | |
| 128 | 1 | throw std::invalid_argument("A log-normal distribution needs a positive median"); | |
| 129 | } | ||
| 130 | 811656 | if (d.kind == ValueDistribution::Kind::Piecewise) { | |
| 131 | 16 | CheckPiecewise(d); | |
| 132 | 811640 | } else if (!(d.edges.empty() && d.weights.empty())) { | |
| 133 | 1 | throw std::invalid_argument("Only a piecewise distribution takes edges and weights"); | |
| 134 | } | ||
| 135 | 811646 | } | |
| 136 | |||
| 137 | } // namespace terraingen | ||
| 138 |