./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
40 / 40
100.0%
Branches:
46 / 47
97.9%
Functions:
6 / 6
100.0%

src/distribution/tabulated.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/distribution/tabulated.h"
4
5 #include "util/checks.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <stdexcept>
10 #include <utility>
11
12 namespace terraingen {
13
14 namespace {
15
16 /// The table's diameter range in meters, after checking the table and units.
17 307 DiameterRange TableRange(const std::vector<double>& diameters,
18 const std::vector<double>& cumulative, double diameterUnit,
19 double areaUnit) {
20
4/4
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 304 times.
307 if (diameters.size() < 2 || diameters.size() != cumulative.size()) {
21
1/1
✓ Branch 2 taken 3 times.
3 throw std::invalid_argument("A table needs at least two diameters with one value each");
22 }
23
3/4
✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 302 times.
✓ Branch 3 taken 1 time.
607 if (!IsPositive(diameterUnit) || !IsPositive(areaUnit)) {
24
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Diameter and area units must be positive and finite");
25 }
26
2/2
✓ Branch 0 taken 4462 times.
✓ Branch 1 taken 295 times.
4757 for (std::size_t i = 0; i < diameters.size(); ++i) {
27
4/4
✓ Branch 0 taken 4461 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4459 times.
✓ Branch 3 taken 1 time.
8922 if (!IsPositive(diameters[i]) || !IsPositive(cumulative[i])) {
28
1/1
✓ Branch 2 taken 4 times.
4 throw std::invalid_argument("Table diameters and values must be positive and finite");
29 }
30
4/4
✓ Branch 0 taken 4158 times.
✓ Branch 1 taken 300 times.
✓ Branch 2 taken 4156 times.
✓ Branch 3 taken 2 times.
4458 if (i > 0 && !(diameters[i] > diameters[i - 1])) {
31
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Table diameters must strictly increase");
32 }
33
4/4
✓ Branch 0 taken 4156 times.
✓ Branch 1 taken 300 times.
✓ Branch 2 taken 4155 times.
✓ Branch 3 taken 1 time.
4456 if (i > 0 && cumulative[i] > cumulative[i - 1]) {
34
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A cumulative distribution cannot increase with diameter");
35 }
36 }
37 295 return DiameterRange{diameters.front() * diameterUnit, diameters.back() * diameterUnit};
38 }
39
40 } // namespace
41
42 307 TabulatedSfd::TabulatedSfd(std::vector<double> diameters, std::vector<double> cumulative,
43 307 SfdKind kind, double diameterUnit, double areaUnit)
44 : SizeFrequencyDistribution(kind, TableRange(diameters, cumulative, diameterUnit, areaUnit)),
45 307 m_diameters(std::move(diameters)), m_cumulative(std::move(cumulative)) {
46
2/2
✓ Branch 0 taken 4450 times.
✓ Branch 1 taken 295 times.
4745 for (std::size_t i = 0; i < m_diameters.size(); ++i) {
47
2/2
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 295 times.
4450 m_diameters[i] *= diameterUnit;
48 4450 m_cumulative[i] /= areaUnit;
49
2/2
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 295 times.
4450 if (i > 0) {
50
1/1
✓ Branch 1 taken 4155 times.
4155 m_slopes.push_back(std::log(m_cumulative[i] / m_cumulative[i - 1]) /
51
1/1
✓ Branch 1 taken 4155 times.
4155 std::log(m_diameters[i] / m_diameters[i - 1]));
52 }
53 }
54 295 }
55
56 24887777 std::size_t TabulatedSfd::IntervalAt(double diameter) const {
57 24887777 const auto upper = std::upper_bound(m_diameters.begin(), m_diameters.end(), diameter);
58
2/2
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 25065641 times.
25066824 const auto index = static_cast<std::size_t>(upper - m_diameters.begin());
59
2/2
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 25065641 times.
25066824 return std::clamp<std::size_t>(index, 1, m_diameters.size() - 1) - 1;
60 }
61
62 14160102 double TabulatedSfd::CumulativeAt(double diameter) const {
63 14160102 const std::size_t i = IntervalAt(diameter);
64 14150966 return m_cumulative[i] * std::pow(diameter / m_diameters[i], m_slopes[i]);
65 }
66
67 10980075 double TabulatedSfd::IncrementalAt(double diameter) const {
68 10980075 return -m_slopes[IntervalAt(diameter)] * CumulativeAt(diameter) / diameter;
69 }
70
71 40324 double TabulatedSfd::InvertCumulative(double target, double /*minDiameter*/,
72 double /*maxDiameter*/) const {
73 40324 const auto above = std::partition_point(m_cumulative.begin(), m_cumulative.end(),
74
2/2
✓ Branch 0 taken 40339 times.
✓ Branch 1 taken 72686 times.
113025 [target](double value) { return value > target; });
75
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 40281 times.
40324 const auto index = static_cast<std::size_t>(above - m_cumulative.begin());
76
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 40281 times.
40324 const std::size_t i = std::clamp<std::size_t>(index, 1, m_cumulative.size() - 1) - 1;
77
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 40322 times.
40324 if (m_slopes[i] == 0.0) {
78 2 return m_diameters[i];
79 }
80 40322 return m_diameters[i] * std::pow(target / m_cumulative[i], 1.0 / m_slopes[i]);
81 }
82
83 } // namespace terraingen
84