include/terraingen/distribution/spliced.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Size-frequency distributions spliced from others over consecutive diameter intervals. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/distribution/size_frequency.h" | ||
| 8 | |||
| 9 | #include <cstddef> | ||
| 10 | #include <limits> | ||
| 11 | #include <memory> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | /// One piece of a `SplicedSfd`: `distribution` applies from `start` meters up to the next | ||
| 17 | /// piece's start. | ||
| 18 | 5949 | struct SfdPiece { | |
| 19 | double start = 0.0; ///< First diameter, meters. | ||
| 20 | std::shared_ptr<const SizeFrequencyDistribution> distribution; ///< Applies from `start`. | ||
| 21 | }; | ||
| 22 | |||
| 23 | /// A cumulative distribution made of other distributions over consecutive diameter | ||
| 24 | /// intervals, such as a production function extended below its fitted range by a power law. | ||
| 25 | /// | ||
| 26 | /// As in `PiecewisePowerLawSfd`, the lower piece applies at a join, and pieces must meet there | ||
| 27 | /// within a relative tolerance. | ||
| 28 | 1533 | class SplicedSfd final : public SizeFrequencyDistribution { | |
| 29 | public: | ||
| 30 | /// The distribution of `pieces`, valid from the first `start` to `maxDiameter` meters. | ||
| 31 | /// @param pieces At least one piece, with strictly increasing `start`. | ||
| 32 | /// @param tolerance Relative mismatch allowed at a join. | ||
| 33 | /// @throws std::invalid_argument for malformed or mixed pieces, or a join that rises or breaks. | ||
| 34 | explicit SplicedSfd(std::vector<SfdPiece> pieces, | ||
| 35 | double maxDiameter = std::numeric_limits<double>::infinity(), | ||
| 36 | double tolerance = 1e-3); | ||
| 37 | |||
| 38 | /// The pieces in increasing `start` order. | ||
| 39 | 1 | const std::vector<SfdPiece>& Pieces() const { | |
| 40 | 1 | return m_pieces; | |
| 41 | } | ||
| 42 | |||
| 43 | protected: | ||
| 44 | double CumulativeAt(double diameter) const override; | ||
| 45 | double IncrementalAt(double diameter) const override; | ||
| 46 | double InvertCumulative(double target, double minDiameter, double maxDiameter) const override; | ||
| 47 | |||
| 48 | private: | ||
| 49 | std::size_t PieceAt(double diameter) const; | ||
| 50 | |||
| 51 | std::vector<SfdPiece> m_pieces; | ||
| 52 | }; | ||
| 53 | |||
| 54 | } // namespace terraingen | ||
| 55 |