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 | 36 | m_segments(std::move(segments)) { | |
| 18 | 36 | const DiameterRange& range = ValidRange(); | |
| 19 | 36 | if (m_segments.empty()) { | |
| 20 | 1 | throw std::invalid_argument("A piecewise power law needs at least one segment"); | |
| 21 | } | ||
| 22 | 107 | for (const PowerLawSegment& segment : m_segments) { | |
| 23 | 219 | if (!IsPositive(segment.coefficient) || !IsPositive(segment.exponent)) { | |
| 24 | 2 | throw std::invalid_argument("Segment coefficients and exponents must be positive"); | |
| 25 | } | ||
| 26 | } | ||
| 27 | 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 | 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 | 83034 | const PowerLawSegment& segment = m_segments[i]; | |
| 53 | 83034 | return std::fmax(minDiameter, segment.start) < high && | |
| 54 | 82934 | target >= segment.Cumulative(high); | |
| 55 | }); | ||
| 56 | 80350 | const PowerLawSegment& segment = m_segments[index]; | |
| 57 | 80350 | const double low = std::fmax(minDiameter, segment.start); | |
| 58 | 80350 | if (target > segment.Cumulative(low)) { | |
| 59 | return low; | ||
| 60 | } | ||
| 61 | 79452 | return segment.Invert(target); | |
| 62 | } | ||
| 63 | |||
| 64 | } // namespace terraingen | ||
| 65 |