src/distribution/composition.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "distribution/composition.h" | ||
| 4 | |||
| 5 | #include <stdexcept> | ||
| 6 | |||
| 7 | namespace terraingen::detail { | ||
| 8 | |||
| 9 | 1641 | SfdKind CommonKind(Span<const SizeFrequencyDistribution* const> distributions, | |
| 10 | const std::string& what) { | ||
| 11 | 1641 | if (distributions.empty()) { | |
| 12 | 14 | throw std::invalid_argument(what + " needs at least one distribution"); | |
| 13 | } | ||
| 14 | 4523 | for (const SizeFrequencyDistribution* distribution : distributions) { | |
| 15 | 2899 | if (distribution == nullptr) { | |
| 16 | 12 | throw std::invalid_argument(what + " cannot hold a null distribution"); | |
| 17 | } | ||
| 18 | 2893 | if (distribution->Kind() != distributions.front()->Kind()) { | |
| 19 | 8 | throw std::invalid_argument(what + " needs distributions of one kind"); | |
| 20 | } | ||
| 21 | } | ||
| 22 | 1624 | return distributions.front()->Kind(); | |
| 23 | } | ||
| 24 | |||
| 25 | 2005 | DiameterRange UsableRange(const SizeFrequencyDistribution& distribution) { | |
| 26 | 2005 | return distribution.AllowsExtrapolation() ? DiameterRange{} : distribution.ValidRange(); | |
| 27 | } | ||
| 28 | |||
| 29 | 884 | DiameterRange IntersectRanges(Span<const DiameterRange> ranges, const std::string& message) { | |
| 30 | 884 | DiameterRange range; | |
| 31 | 2289 | for (const DiameterRange& each : ranges) { | |
| 32 | 1405 | range.min = std::fmax(range.min, each.min); | |
| 33 | 1405 | range.max = std::fmin(range.max, each.max); | |
| 34 | } | ||
| 35 | 884 | if (!(range.min < range.max)) { | |
| 36 | 3 | throw std::invalid_argument(message); | |
| 37 | } | ||
| 38 | 881 | return range; | |
| 39 | } | ||
| 40 | |||
| 41 | 780 | void CheckStarts(Span<const double> starts, const DiameterRange& range, const std::string& what) { | |
| 42 | 1559 | if (starts.empty() || starts.front() != range.min) { | |
| 43 | 4 | throw std::invalid_argument(what + " starts must begin at the range minimum"); | |
| 44 | } | ||
| 45 | 1555 | for (std::size_t i = 1; i < starts.size(); ++i) { | |
| 46 | 784 | if (!(std::isfinite(starts[i]) && starts[i] > starts[i - 1] && starts[i] < range.max)) { | |
| 47 | 14 | throw std::invalid_argument(what + " starts must increase within the range"); | |
| 48 | } | ||
| 49 | } | ||
| 50 | 771 | } | |
| 51 | |||
| 52 | 779 | void CheckJoin(double below, double above, double start, double tolerance, | |
| 53 | bool allowDiscontinuities) { | ||
| 54 | 779 | if (above > below * (1.0 + tolerance)) { | |
| 55 | 5 | throw std::invalid_argument("A cumulative distribution cannot rise at the join at " + | |
| 56 | 20 | std::to_string(start) + " m"); | |
| 57 | } | ||
| 58 | 774 | if (!allowDiscontinuities && above < below * (1.0 - tolerance)) { | |
| 59 | 6 | throw std::invalid_argument("The pieces are discontinuous at " + std::to_string(start) + | |
| 60 | 9 | " m"); | |
| 61 | } | ||
| 62 | 771 | } | |
| 63 | |||
| 64 | } // namespace terraingen::detail | ||
| 65 |