./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
31 / 31
100.0%
Branches:
29 / 31
93.5%
Functions:
6 / 6
100.0%

src/distribution/piecewise_power_law.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/distribution/piecewise_power_law.h"
4
5 #include "distribution/composition.h"
6 #include "util/checks.h"
7
8 #include <cmath>
9 #include <stdexcept>
10 #include <utility>
11
12 namespace terraingen {
13
14 36 PiecewisePowerLawSfd::PiecewisePowerLawSfd(std::vector<PowerLawSegment> segments, SfdKind kind,
15 36 double maxDiameter, SfdJoins joins)
16 : SizeFrequencyDistribution(kind, detail::PieceRange(segments, maxDiameter)),
17
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 35 times.
36 m_segments(std::move(segments)) {
18
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 35 times.
36 const DiameterRange& range = ValidRange();
19
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 35 times.
36 if (m_segments.empty()) {
20
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A piecewise power law needs at least one segment");
21 }
22
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 33 times.
107 for (const PowerLawSegment& segment : m_segments) {
23
2/4
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
219 if (!IsPositive(segment.coefficient) || !IsPositive(segment.exponent)) {
24
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Segment coefficients and exponents must be positive");
25 }
26 }
27
4/4
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 8 taken 30 times.
✓ Branch 13 taken 27 times.
36 detail::CheckStarts(detail::Starts(m_segments), range, "Segment");
28 30 detail::CheckJoins(
29 m_segments,
30 72 [](const PowerLawSegment& segment, double diameter) {
31 36 return segment.Cumulative(diameter);
32 },
33
1/1
✓ Branch 1 taken 27 times.
30 joins.tolerance, joins.allowDiscontinuities);
34 36 }
35
36 361550 const PowerLawSegment& PiecewisePowerLawSfd::SegmentAt(double diameter) const {
37 361550 return m_segments[detail::PieceIndex(m_segments, diameter)];
38 }
39
40 361528 double PiecewisePowerLawSfd::CumulativeAt(double diameter) const {
41 361528 return SegmentAt(diameter).Cumulative(diameter);
42 }
43
44 22 double PiecewisePowerLawSfd::IncrementalAt(double diameter) const {
45 22 return SegmentAt(diameter).Incremental(diameter);
46 }
47
48 80350 double PiecewisePowerLawSfd::InvertCumulative(double target, double minDiameter,
49 double maxDiameter) const {
50 80350 const std::size_t index =
51 80350 detail::WalkPieces(m_segments, 0, maxDiameter, [&](std::size_t i, double high) {
52
2/2
✓ Branch 0 taken 82934 times.
✓ Branch 1 taken 100 times.
83034 const PowerLawSegment& segment = m_segments[i];
53
4/4
✓ Branch 0 taken 82934 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 5475 times.
✓ Branch 3 taken 77459 times.
83034 return std::fmax(minDiameter, segment.start) < high &&
54
2/2
✓ Branch 0 taken 5475 times.
✓ Branch 1 taken 77459 times.
82934 target >= segment.Cumulative(high);
55 });
56
2/2
✓ Branch 0 taken 79452 times.
✓ Branch 1 taken 898 times.
80350 const PowerLawSegment& segment = m_segments[index];
57 80350 const double low = std::fmax(minDiameter, segment.start);
58
2/2
✓ Branch 0 taken 79452 times.
✓ Branch 1 taken 898 times.
80350 if (target > segment.Cumulative(low)) {
59 return low;
60 }
61 79452 return segment.Invert(target);
62 }
63
64 } // namespace terraingen
65