./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
3 / 3
100.0%
Branches:
-
-%
Functions:
1 / 1, 1 excl
100.0%

include/terraingen/distribution/piecewise_uniform.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Size-frequency distributions made of bands with diameters spread uniformly.
5 #pragma once
6
7 #include "terraingen/distribution/size_frequency.h"
8
9 #include <vector>
10
11 namespace terraingen {
12
13 /// One band of a piecewise uniform distribution: `density` features with diameters spread
14 /// uniformly over [`min`, `max`) meters.
15 8 struct UniformBand {
16 double min = 1.0; ///< Smallest diameter, meters; positive.
17 double max = 2.0; ///< Largest diameter, meters; above `min`.
18 double density = 0.01; ///< Features per m^2 (or per m^2 per Gyr), or a relative weight; >= 0.
19
20 /// Memberwise equality.
21 bool operator==(const UniformBand& other) const {
22 return min == other.min && max == other.max && density == other.density;
23 }
24 bool operator!=(const UniformBand& other) const {
25 return !(*this == other);
26 }
27 };
28
29 /// A distribution of diameter bands, each holding features per m^2 with diameters uniform across
30 /// it; overlapping bands add.
31 /// N(>=D) is 0 above the largest band, so extrapolation is allowed from the start.
32 class PiecewiseUniformSfd final : public SizeFrequencyDistribution {
33 public:
34 /// The distribution of `bands`.
35 /// @param total When positive, the features per m^2 (or per m^2 per Gyr) of all bands
36 /// together, shared in proportion to each band's `density`; 0 takes the densities as given.
37 /// @throws std::invalid_argument for no bands, a band whose diameters are not positive and
38 /// finite with `max` above `min`, a negative or non-finite density or total, or no features.
39 PiecewiseUniformSfd(std::vector<UniformBand> bands, SfdKind kind = SfdKind::Density,
40 double total = 0.0);
41
42 /// The bands, with densities per m^2 (or per m^2 per Gyr) after any `total` is applied.
43 3 const std::vector<UniformBand>& Bands() const {
44 3 return m_bands;
45 }
46
47 protected:
48 double CumulativeAt(double diameter) const override;
49 double IncrementalAt(double diameter) const override;
50 double InvertCumulative(double target, double minDiameter, double maxDiameter) const override;
51
52 private:
53 std::vector<UniformBand> m_bands;
54 std::vector<double> m_edges;
55 std::vector<double> m_edgeCumulative;
56 };
57
58 } // namespace terraingen
59