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 | 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 | 1492 | detail::CommonKind(detail::Distributions(pieces), "A spliced distribution"), | |
| 26 | detail::PieceRange(pieces, maxDiameter)), | ||
| 27 | 2238 | m_pieces(std::move(pieces)) { | |
| 28 | 742 | const DiameterRange& range = ValidRange(); | |
| 29 | 744 | detail::CheckStarts(detail::Starts(m_pieces), range, "Piece"); | |
| 30 | 2217 | for (std::size_t i = 0; i < m_pieces.size(); ++i) { | |
| 31 | 1478 | const double end = i + 1 < m_pieces.size() ? m_pieces[i + 1].start : range.max; | |
| 32 | 1478 | if (!Covers(*m_pieces[i].distribution, m_pieces[i].start, end)) { | |
| 33 | 1 | throw std::invalid_argument("Each piece must be valid over its diameter interval"); | |
| 34 | } | ||
| 35 | } | ||
| 36 | 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 | 60200 | if (target >= UncheckedCumulative(piece, low)) { | |
| 64 | return low; | ||
| 65 | } | ||
| 66 | 59678 | return UncheckedInverse(piece, target, low, high); | |
| 67 | } | ||
| 68 | |||
| 69 | } // namespace terraingen | ||
| 70 |