./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
57 / 57
100.0%
Branches:
58 / 59
98.3%
Functions:
6 / 6
100.0%

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
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 26 times.
27 if (bands.empty()) {
20
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A piecewise uniform distribution needs at least one band");
21 }
22 26 double top = 0.0;
23
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (const UniformBand& band : bands) {
24
5/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 39 times.
✓ Branch 5 taken 1 time.
123 if (!IsPositive(band.min) || !IsPositive(band.max) || !(band.max > band.min)) {
25 3 throw std::invalid_argument(
26
1/1
✓ Branch 2 taken 3 times.
3 "Band diameters must be positive and finite, with max above min");
27 }
28
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 1 time.
39 if (!std::isfinite(band.density) || band.density < 0.0) {
29
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Band densities must be finite and >= 0");
30 }
31
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 6 times.
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
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 1 time.
21 if (!std::isfinite(total) || total < 0.0) {
39
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A band total must be finite and >= 0");
40 }
41 19 double sum = 0.0;
42
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 19 times.
54 for (const UniformBand& band : bands) {
43 35 sum += band.density;
44 }
45
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 18 times.
19 if (!(sum > 0.0)) {
46
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A piecewise uniform distribution needs a positive density");
47 }
48
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (total > 0.0) {
49
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
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
1/1
✓ Branch 3 taken 18 times.
27 m_bands(ScaleBands(std::move(bands), total)) {
61 18 SetAllowExtrapolation(true);
62
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 18 times.
52 for (const UniformBand& band : m_bands) {
63
1/1
✓ Branch 1 taken 34 times.
34 m_edges.push_back(band.min);
64
1/1
✓ Branch 1 taken 34 times.
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
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 18 times.
81 for (const double edge : m_edges) {
69
1/1
✓ Branch 1 taken 63 times.
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
2/2
✓ Branch 0 taken 240779 times.
✓ Branch 1 taken 120385 times.
361164 for (const UniformBand& band : m_bands) {
76
2/2
✓ Branch 0 taken 40036 times.
✓ Branch 1 taken 200743 times.
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
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 for (const UniformBand& band : m_bands) {
84
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
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
2/2
✓ Branch 0 taken 40104 times.
✓ Branch 1 taken 50303 times.
90407 [target](double value) { return value > target; });
95
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 40103 times.
40105 const auto i = static_cast<std::size_t>(below - m_edgeCumulative.begin());
96
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 40103 times.
40105 if (i == 0) {
97 2 return m_edges.front();
98 }
99
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 40102 times.
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