include/terraingen/random/value_distribution.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `ValueDistribution`, a clamped distribution a parameter is drawn from. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <limits> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | class RandomStream; | ||
| 14 | |||
| 15 | /// A distribution a parameter is drawn from, clamped to [`min`, `max`]. | ||
| 16 | 978704 | struct ValueDistribution { | |
| 17 | /// The family of the distribution, which sets the meaning of `a` and `b`. | ||
| 18 | enum class Kind { | ||
| 19 | Constant, ///< Always `a`. | ||
| 20 | Uniform, ///< Uniform on [`a`, `b`]. | ||
| 21 | Normal, ///< Mean `a`, standard deviation `b`. | ||
| 22 | LogNormal, ///< Median `a`, standard deviation of the natural logarithm `b`. | ||
| 23 | Piecewise, ///< Uniform within each bin of `edges`, with bin shares from `weights`. | ||
| 24 | }; | ||
| 25 | Kind kind = Kind::Constant; ///< The family. | ||
| 26 | double a = 0.0; ///< First parameter, per `kind`. | ||
| 27 | double b = 0.0; ///< Second parameter, per `kind`. | ||
| 28 | double min = -std::numeric_limits<double>::infinity(); ///< Lower clamp. | ||
| 29 | double max = std::numeric_limits<double>::infinity(); ///< Upper clamp. | ||
| 30 | std::vector<double> edges{}; ///< Piecewise bin edges, strictly increasing. | ||
| 31 | std::vector<double> weights{}; ///< Piecewise relative weight of each bin. | ||
| 32 | |||
| 33 | /// Always `value`. | ||
| 34 | 236 | static ValueDistribution Constant(double value) { | |
| 35 | 236 | return {Kind::Constant, value}; | |
| 36 | 186 | } | |
| 37 | /// Uniform on [`low`, `high`]. | ||
| 38 | 495 | static ValueDistribution Uniform(double low, double high) { | |
| 39 | 495 | return {Kind::Uniform, low, high}; | |
| 40 | 384 | } | |
| 41 | /// Normal of `mean` and standard deviation `sd`; needs a finite `max` (`Clamped`). | ||
| 42 | 656 | static ValueDistribution Normal(double mean, double sd) { | |
| 43 | 656 | return {Kind::Normal, mean, sd}; | |
| 44 | 348 | } | |
| 45 | /// Log-normal of `median` and standard deviation of the natural logarithm `logSd`; needs a | ||
| 46 | /// finite `max` (`Clamped`). | ||
| 47 | 292 | static ValueDistribution LogNormal(double median, double logSd) { | |
| 48 | 292 | return {Kind::LogNormal, median, logSd}; | |
| 49 | 246 | } | |
| 50 | /// Uniform within each bin of `edges`, with bin shares from `weights`. | ||
| 51 | 2 | static ValueDistribution Piecewise(std::vector<double> edges, std::vector<double> weights) { | |
| 52 | 2 | ValueDistribution distribution{Kind::Piecewise}; | |
| 53 | 2 | distribution.edges = std::move(edges); | |
| 54 | 2 | distribution.weights = std::move(weights); | |
| 55 | 2 | return distribution; | |
| 56 | } | ||
| 57 | |||
| 58 | /// This distribution clamped to [`low`, `high`], for chaining: | ||
| 59 | /// `ValueDistribution::Normal(10.7, 3.1).Clamped(6.0, 20.0)`. | ||
| 60 | 948 | ValueDistribution Clamped(double low, double high) const { | |
| 61 | 304 | ValueDistribution clamped = *this; | |
| 62 | 948 | clamped.min = low; | |
| 63 | 948 | clamped.max = high; | |
| 64 | 304 | return clamped; | |
| 65 | } | ||
| 66 | |||
| 67 | /// A value drawn from two independent uniforms `u1` and `u2` in [0, 1) (Box-Muller for the | ||
| 68 | /// normal kinds, inversion of `u1` for piecewise), clamped to [`min`, `max`]. | ||
| 69 | double Sample(double u1, double u2) const; | ||
| 70 | |||
| 71 | /// `Sample` of the next two uniforms of `random`, in order. | ||
| 72 | double Draw(RandomStream& random) const; | ||
| 73 | |||
| 74 | /// The smallest value `Sample` can return. | ||
| 75 | double Lowest() const; | ||
| 76 | |||
| 77 | /// The largest value `Sample` can return. | ||
| 78 | double Highest() const; | ||
| 79 | |||
| 80 | /// Memberwise equality. | ||
| 81 | 30 | bool operator==(const ValueDistribution& other) const { | |
| 82 | 30 | return kind == other.kind && a == other.a && b == other.b && min == other.min && | |
| 83 | 60 | max == other.max && edges == other.edges && weights == other.weights; | |
| 84 | } | ||
| 85 | 2 | bool operator!=(const ValueDistribution& other) const { | |
| 86 | 2 | return !(*this == other); | |
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | /// Checks a value distribution. | ||
| 91 | /// @throws std::invalid_argument for non-finite or out-of-range parameters, an unbounded normal or | ||
| 92 | /// log-normal, bad piecewise edges or weights, or edges or weights on another kind. | ||
| 93 | void ValidateValueDistribution(const ValueDistribution& distribution); | ||
| 94 | |||
| 95 | } // namespace terraingen | ||
| 96 |