src/distribution/summed.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/distribution/summed.h" | ||
| 4 | |||
| 5 | #include "distribution/composition.h" | ||
| 6 | #include "util/checks.h" | ||
| 7 | |||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | /// The kind of `terms`, after checking them. | ||
| 15 | 447 | SfdKind CheckedKind(const std::vector<WeightedSfd>& terms) { | |
| 16 | 450 | const SfdKind kind = detail::CommonKind(detail::Distributions(terms), "A summed distribution"); | |
| 17 | 967 | for (const WeightedSfd& term : terms) { | |
| 18 | 524 | RequirePositive(term.weight, "Summed distribution weights"); | |
| 19 | } | ||
| 20 | 443 | return kind; | |
| 21 | } | ||
| 22 | |||
| 23 | } // namespace | ||
| 24 | |||
| 25 | 447 | SummedSfd::SummedSfd(std::vector<WeightedSfd> terms) | |
| 26 | 447 | : SummedSfd(CheckedKind(terms), std::move(terms)) {} | |
| 27 | |||
| 28 | 443 | SummedSfd::SummedSfd(SfdKind kind, std::vector<WeightedSfd>&& terms) | |
| 29 | : SizeFrequencyDistribution( | ||
| 30 | kind, detail::IntersectRanges(terms, detail::UsableRange, | ||
| 31 | "Summed terms must have overlapping valid ranges")), | ||
| 32 | 885 | m_terms(std::move(terms)) {} | |
| 33 | |||
| 34 | 11096829 | double SummedSfd::CumulativeAt(double diameter) const { | |
| 35 | 11096829 | double sum = 0.0; | |
| 36 | 24764175 | for (const WeightedSfd& term : m_terms) { | |
| 37 | 13667343 | sum += term.weight * UncheckedCumulative(*term.distribution, diameter); | |
| 38 | } | ||
| 39 | 11096832 | return sum; | |
| 40 | } | ||
| 41 | |||
| 42 | 711409 | double SummedSfd::IncrementalAt(double diameter) const { | |
| 43 | 711409 | double sum = 0.0; | |
| 44 | 1591914 | for (const WeightedSfd& term : m_terms) { | |
| 45 | 880505 | sum += term.weight * UncheckedIncremental(*term.distribution, diameter); | |
| 46 | } | ||
| 47 | 711409 | return sum; | |
| 48 | } | ||
| 49 | |||
| 50 | } // namespace terraingen | ||
| 51 |