./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
80 / 84
95.2%
Branches:
66 / 81
81.5%
Functions:
7 / 7
100.0%

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
4/4
✓ Branch 0 taken 93867 times.
✓ Branch 1 taken 35891 times.
✓ Branch 2 taken 65752 times.
✓ Branch 3 taken 28115 times.
129758 while (bin + 1 < weights.size() && !(target < weights[bin])) {
27 65752 target -= weights[bin];
28 65752 ++bin;
29 }
30
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 64006 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
64006 while (weights[bin] == 0.0 && bin > 0) {
31 ✗ --bin;
32 ✗ target = weights[bin];
33 }
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64006 times.
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
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 11 times.
16 if (d.edges.size() < 2 || d.weights.size() + 1 != d.edges.size()) {
40 5 throw std::invalid_argument(
41
1/1
✓ Branch 2 taken 5 times.
5 "A piecewise distribution needs at least two edges and one weight per bin");
42 }
43
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 9 times.
30 for (std::size_t i = 0; i + 1 < d.edges.size(); ++i) {
44
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
21 if (!(d.edges[i] < d.edges[i + 1])) {
45
1/1
✓ Branch 2 taken 2 times.
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
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!IsPositive(total)) {
50
1/1
✓ Branch 2 taken 2 times.
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
5/5
✓ Branch 0 taken 876474 times.
✓ Branch 1 taken 224167 times.
✓ Branch 2 taken 111290 times.
✓ Branch 3 taken 64006 times.
✓ Branch 4 taken 60786157 times.
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
2/2
✓ Branch 0 taken 6663 times.
✓ Branch 1 taken 62055431 times.
62062094 return std::clamp(value, min, max);
81 }
82
83 649925 double ValueDistribution::Lowest() const {
84
4/5
✓ Branch 0 taken 647910 times.
✓ Branch 1 taken 1173 times.
✓ Branch 2 taken 841 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
649925 switch (kind) {
85 647910 case Kind::Constant:
86 647910 case Kind::Uniform:
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 647910 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
2 return std::clamp(edges.front(), min, max);
94 }
95 ✗ return min;
96 }
97
98 508073 double ValueDistribution::Highest() const {
99
4/5
✓ Branch 0 taken 12580 times.
✓ Branch 1 taken 493082 times.
✓ Branch 2 taken 2409 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
508073 switch (kind) {
100 12580 case Kind::Constant:
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12580 times.
25160 return std::clamp(a, min, max);
102 493082 case Kind::Uniform:
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 493082 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
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
4/6
✓ Branch 0 taken 811675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 811675 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 811673 times.
811675 if (std::isnan(d.min) || std::isnan(d.max) || d.min > d.max) {
116
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Distribution min must not exceed max");
117 }
118
4/4
✓ Branch 0 taken 645449 times.
✓ Branch 1 taken 166224 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 645444 times.
811673 if (d.kind == ValueDistribution::Kind::Uniform && d.b < d.a) {
119
1/1
✓ Branch 2 taken 5 times.
5 throw std::invalid_argument("A uniform distribution needs b >= a");
120 }
121 811668 if ((d.kind == ValueDistribution::Kind::Normal ||
122
2/2
✓ Branch 0 taken 2032 times.
✓ Branch 1 taken 809636 times.
811668 d.kind == ValueDistribution::Kind::LogNormal) &&
123
4/4
✓ Branch 0 taken 2031 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2021 times.
2032 (d.b < 0.0 || !std::isfinite(d.max))) {
124 11 throw std::invalid_argument(
125
1/1
✓ Branch 2 taken 11 times.
11 "Normal and log-normal distributions need a non-negative spread and a finite max");
126 }
127
4/4
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 810816 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 840 times.
811657 if (d.kind == ValueDistribution::Kind::LogNormal && !(d.a > 0.0)) {
128
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A log-normal distribution needs a positive median");
129 }
130
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 811640 times.
811656 if (d.kind == ValueDistribution::Kind::Piecewise) {
131 16 CheckPiecewise(d);
132
3/4
✓ Branch 0 taken 811639 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 811639 times.
811640 } else if (!(d.edges.empty() && d.weights.empty())) {
133
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Only a piecewise distribution takes edges and weights");
134 }
135 811646 }
136
137 } // namespace terraingen
138