include/terraingen/distribution/piecewise_power_law.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Size-frequency distributions made of power-law segments. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/distribution/power_law.h" | ||
| 8 | #include "terraingen/distribution/size_frequency.h" | ||
| 9 | |||
| 10 | #include <limits> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | /// How the pieces of a distribution must meet at their joins. | ||
| 16 | ✗ | struct SfdJoins { | |
| 17 | /// Allows a downward jump at a join larger than `tolerance` (for legacy specifications). | ||
| 18 | bool allowDiscontinuities = false; | ||
| 19 | double tolerance = 1e-3; ///< Relative mismatch allowed at a join. | ||
| 20 | }; | ||
| 21 | |||
| 22 | /// A cumulative distribution of power-law segments, each applying from its `start` to the next. | ||
| 23 | /// Segments meet within a tolerance unless `SfdJoins::allowDiscontinuities`; a downward jump puts | ||
| 24 | /// that share of the population exactly at the join diameter. | ||
| 25 | 36 | class PiecewisePowerLawSfd final : public SizeFrequencyDistribution { | |
| 26 | public: | ||
| 27 | /// The distribution of `segments`, valid from the first `start` to `maxDiameter` meters. | ||
| 28 | /// @param segments At least one segment, with strictly increasing `start`. | ||
| 29 | /// @throws std::invalid_argument for bad segments, a rising join, or a break `joins` forbids. | ||
| 30 | PiecewisePowerLawSfd(std::vector<PowerLawSegment> segments, SfdKind kind = SfdKind::Density, | ||
| 31 | double maxDiameter = std::numeric_limits<double>::infinity(), | ||
| 32 | SfdJoins joins = {}); | ||
| 33 | |||
| 34 | /// The segments in increasing `start` order. | ||
| 35 | 1 | const std::vector<PowerLawSegment>& Segments() const { | |
| 36 | 1 | return m_segments; | |
| 37 | } | ||
| 38 | |||
| 39 | protected: | ||
| 40 | double CumulativeAt(double diameter) const override; | ||
| 41 | double IncrementalAt(double diameter) const override; | ||
| 42 | double InvertCumulative(double target, double minDiameter, double maxDiameter) const override; | ||
| 43 | |||
| 44 | private: | ||
| 45 | const PowerLawSegment& SegmentAt(double diameter) const; | ||
| 46 | |||
| 47 | std::vector<PowerLawSegment> m_segments; | ||
| 48 | }; | ||
| 49 | |||
| 50 | } // namespace terraingen | ||
| 51 |