./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
30 / 30
100.0%
Branches:
22 / 23
95.7%
Functions:
7 / 7
100.0%

src/distribution/spliced.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/distribution/spliced.h"
4
5 #include "distribution/composition.h"
6
7 #include <cmath>
8 #include <stdexcept>
9 #include <utility>
10
11 namespace terraingen {
12
13 namespace {
14
15 /// Whether `distribution` may be evaluated from `low` to `high` meters.
16 1478 bool Covers(const SizeFrequencyDistribution& distribution, double low, double high) {
17 1478 const DiameterRange usable = detail::UsableRange(distribution);
18
3/4
✓ Branch 0 taken 1478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1477 times.
1478 return usable.min <= low && usable.max >= high;
19 }
20
21 } // namespace
22
23 746 SplicedSfd::SplicedSfd(std::vector<SfdPiece> pieces, double maxDiameter, double tolerance)
24 : SizeFrequencyDistribution(
25
1/1
✓ Branch 1 taken 746 times.
1492 detail::CommonKind(detail::Distributions(pieces), "A spliced distribution"),
26 detail::PieceRange(pieces, maxDiameter)),
27
3/3
✓ Branch 3 taken 742 times.
✓ Branch 6 taken 742 times.
✓ Branch 10 taken 742 times.
2238 m_pieces(std::move(pieces)) {
28
1/1
✓ Branch 1 taken 742 times.
742 const DiameterRange& range = ValidRange();
29
3/3
✓ Branch 1 taken 742 times.
✓ Branch 4 taken 742 times.
✓ Branch 8 taken 740 times.
744 detail::CheckStarts(detail::Starts(m_pieces), range, "Piece");
30
2/2
✓ Branch 0 taken 1478 times.
✓ Branch 1 taken 739 times.
2217 for (std::size_t i = 0; i < m_pieces.size(); ++i) {
31
2/2
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 740 times.
1478 const double end = i + 1 < m_pieces.size() ? m_pieces[i + 1].start : range.max;
32
3/3
✓ Branch 1 taken 1478 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1477 times.
1478 if (!Covers(*m_pieces[i].distribution, m_pieces[i].start, end)) {
33
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Each piece must be valid over its diameter interval");
34 }
35 }
36
1/1
✓ Branch 1 taken 737 times.
739 detail::CheckJoins(
37 m_pieces,
38 737 [](const SfdPiece& piece, double diameter) {
39 737 return UncheckedCumulative(*piece.distribution, diameter);
40 },
41 tolerance, false);
42 742 }
43
44 2970999 std::size_t SplicedSfd::PieceAt(double diameter) const {
45 2970999 return detail::PieceIndex(m_pieces, diameter);
46 }
47
48 2458996 double SplicedSfd::CumulativeAt(double diameter) const {
49 2458996 return UncheckedCumulative(*m_pieces[PieceAt(diameter)].distribution, diameter);
50 }
51
52 456078 double SplicedSfd::IncrementalAt(double diameter) const {
53 456078 return UncheckedIncremental(*m_pieces[PieceAt(diameter)].distribution, diameter);
54 }
55
56 60200 double SplicedSfd::InvertCumulative(double target, double minDiameter, double maxDiameter) const {
57 180600 const std::size_t index = detail::WalkPieces(
58 60200 m_pieces, PieceAt(minDiameter), maxDiameter, [&](std::size_t i, double high) {
59 60150 return target >= UncheckedCumulative(*m_pieces[i].distribution, high);
60 });
61 60200 const SizeFrequencyDistribution& piece = *m_pieces[index].distribution;
62 60200 const auto [low, high] = detail::PieceInterval(m_pieces, index, minDiameter, maxDiameter);
63
2/2
✓ Branch 1 taken 59678 times.
✓ Branch 2 taken 522 times.
60200 if (target >= UncheckedCumulative(piece, low)) {
64 return low;
65 }
66 59678 return UncheckedInverse(piece, target, low, high);
67 }
68
69 } // namespace terraingen
70