./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
16 / 20
80.0%
Branches:
93 / 233
39.9%
Functions:
2 / 4, 1 excl
50.0%

include/terraingen/layers/density_map.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Density maps: how a map of feature counts or cover sets a layer's local density.
5 #pragma once
6
7 #include "terraingen/distribution/size_frequency.h"
8 #include "terraingen/layers/field.h"
9
10 #include <limits>
11 #include <memory>
12 #include <optional>
13
14 namespace terraingen {
15
16 /// What a density map's values measure.
17 enum class DensityMeasure {
18 Scale, ///< The local density over the layer's own; 1 keeps it as given.
19 Count, ///< Features in [`minDiameter`, `maxDiameter`) per `areaUnit` square meters.
20 ArealFraction, ///< Share of the ground covered by features in [`minDiameter`, `maxDiameter`).
21 };
22
23 /// A map of a crater or boulder population that sets a layer's density point by point: the size
24 /// distribution keeps its shape and is scaled to measure what the map does, and applies as given
25 /// where the map has no value.
26
53/151
✓ Branch 0 taken 49 times.
✓ Branch 0 taken 34 times.
✗ Branch 0 not taken.
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 5 taken 15 times.
✓ Branch 5 taken 3 times.
✓ Branch 5 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 6 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 7 taken 1 time.
✗ Branch 7 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✓ Branch 8 taken 1 time.
✗ Branch 8 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 10 not taken.
✗ Branch 10 not taken.
✗ Branch 10 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 11 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 1 time.
✗ Branch 13 not taken.
✓ Branch 13 taken 1 time.
✓ Branch 13 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 time.
✓ Branch 15 taken 1 time.
✗ Branch 16 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 17 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 time.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✓ Branch 30 taken 1 time.
✗ Branch 31 not taken.
✓ Branch 32 taken 1 time.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 1 time.
✓ Branch 38 taken 1 time.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 time.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✓ Branch 43 taken 1 time.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✓ Branch 47 taken 1 time.
✓ Branch 49 taken 1 time.
✗ Branch 51 not taken.
✓ Branch 52 taken 1 time.
✗ Branch 53 not taken.
✓ Branch 54 taken 1 time.
✗ Branch 55 not taken.
✓ Branch 56 taken 1 time.
✗ Branch 57 not taken.
✓ Branch 58 taken 1 time.
✗ Branch 59 not taken.
✓ Branch 60 taken 1 time.
✗ Branch 61 not taken.
✓ Branch 62 taken 1 time.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✓ Branch 73 taken 1 time.
✗ Branch 74 not taken.
✗ Branch 75 not taken.
✓ Branch 76 taken 1 time.
✓ Branch 77 taken 1 time.
✗ Branch 78 not taken.
✗ Branch 79 not taken.
✓ Branch 80 taken 1 time.
✓ Branch 81 taken 1 time.
✗ Branch 82 not taken.
✗ Branch 83 not taken.
✓ Branch 84 taken 1 time.
✓ Branch 85 taken 1 time.
✗ Branch 86 not taken.
✗ Branch 87 not taken.
✓ Branch 88 taken 1 time.
✗ Branch 89 not taken.
✗ Branch 90 not taken.
✗ Branch 91 not taken.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✗ Branch 94 not taken.
✗ Branch 95 not taken.
✗ Branch 96 not taken.
✗ Branch 97 not taken.
✗ Branch 98 not taken.
✗ Branch 99 not taken.
✗ Branch 100 not taken.
✓ Branch 101 taken 1 time.
✗ Branch 102 not taken.
375 struct DensityMap {
27 std::shared_ptr<const SurfaceField> field; ///< The map; not null.
28 DensityMeasure measure = DensityMeasure::Scale; ///< What its values measure.
29 double minDiameter = 0.0; ///< Smallest diameter measured (m); positive unless `Scale`.
30 /// Largest diameter measured (m), exclusive; finite for `ArealFraction` unless the size
31 /// distribution's valid range is.
32 double maxDiameter = std::numeric_limits<double>::infinity();
33 double areaUnit = 1.0; ///< Square meters per map area unit for `Count`: 1e6 for per km^2.
34 /// The map measures every feature of its kind, including those a layer adds by other means,
35 /// which are taken out of it; otherwise it measures only what the layer generates.
36 bool total = true;
37
38 1 bool operator==(const DensityMap& other) const {
39
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 return field == other.field && measure == other.measure &&
40
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 minDiameter == other.minDiameter && maxDiameter == other.maxDiameter &&
41
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
2 areaUnit == other.areaUnit && total == other.total;
42 }
43 };
44
45 /// What a size slope map's values measure.
46 enum class SizeSlopeMeasure {
47 Exponent, ///< The cumulative exponent alpha of N(>=D), which falls as D^-alpha.
48 /// The mean diameter (m) of the features at least `referenceDiameter` across, from which
49 /// alpha = mean / (mean - D0) as for a power law above D0.
50 MeanDiameter,
51 };
52
53 /// A map of how steeply feature counts fall with size, such as a mean boulder diameter map.
54 ///
55 /// At each point it bends the layer's size distribution into a power law through the count the
56 /// layer has there at `referenceDiameter`: N(>=D) = N(>=D0) (D / D0)^-alpha. A density map that
57 /// counts from D0 is kept exactly; where the map has no value the sizes keep their own shape.
58
15/27
<unknown function 26>:
✓ Branch 0 taken 10 times.
✗ Branch 0 not taken.
✓ Branch 0 taken 3 times.
✓ Branch 0 taken 14 times.
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 2 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 3 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
terraingen::DensityMap::operator==(terraingen::DensityMap const&) const:
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
103 struct SizeSlopeMap {
59 std::shared_ptr<const SurfaceField> field; ///< The map; not null.
60 SizeSlopeMeasure measure = SizeSlopeMeasure::Exponent; ///< What its values measure.
61 double referenceDiameter = 1.0; ///< D0 (m), where the power law meets the layer's count.
62 /// The exponents alpha is clamped to, which bound the features drawn; positive and below
63 /// `maxExponent`. Generous bounds draw more candidates to thin.
64 double minExponent = 1.0;
65 double maxExponent = 6.0; ///< The largest exponent; finite.
66
67 1 bool operator==(const SizeSlopeMap& other) const {
68
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 return field == other.field && measure == other.measure &&
69
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 referenceDiameter == other.referenceDiameter && minExponent == other.minExponent &&
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 maxExponent == other.maxExponent;
71 }
72 };
73
74 /// Checks a size slope map.
75 /// @throws std::invalid_argument for no field, a reference diameter that is not positive and
76 /// finite, or exponents that are not 0 < min < max finite.
77 void ValidateSizeSlopeMap(const SizeSlopeMap& map);
78
79 /// The exponent alpha `map` gives at surface (x, y), clamped to its bounds; none where it has no
80 /// value.
81 std::optional<double> SizeSlopeAt(const SizeSlopeMap& map, double x, double y);
82
83 /// Checks a density map.
84 /// @throws std::invalid_argument for no field, a diameter range that is not 0 < min < max for a
85 /// `Count` or `ArealFraction`, or an area unit that is not positive and finite.
86 void ValidateDensityMap(const DensityMap& map);
87
88 /// What `map` measures of `distribution` as it is: its count per square meter in the map's range,
89 /// the share of ground its features there cover, or 1 for `Scale`.
90 /// @throws std::domain_error if the range leaves the distribution's valid range, or an
91 /// `ArealFraction` has no finite largest diameter.
92 double MapMeasure(const DensityMap& map, const SizeFrequencyDistribution& distribution);
93
94 /// The map's value at surface (x, y) in square meters for a `Count`; NaN where it has none.
95 double MapValueAt(const DensityMap& map, double x, double y);
96
97 /// The largest value the map reads anywhere, in the units of `MapValueAt`.
98 double MapMax(const DensityMap& map);
99
100 /// The local scale a density map gives a size distribution: the map's value over its measure of
101 /// the distribution, 1 where the map has no value.
102
6/22
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
15 class DensityScale {
103 public:
104 /// @throws std::invalid_argument for an invalid map or one measuring nothing of
105 /// `distribution`.
106 /// @throws std::domain_error as `MapMeasure`.
107 DensityScale(DensityMap map, const SizeFrequencyDistribution& distribution);
108
109 /// The scale at surface (x, y), not negative.
110 double At(double x, double y) const;
111
112 /// The scale where the map reads `value` (`MapValueAt`), clamped to [0, `Max`]; 1 for NaN.
113 double Scale(double value) const;
114
115 /// What the map measures of the distribution at a scale of 1.
116 1 double Measure() const {
117
1/1
✓ Branch 1 taken 1 time.
1 return m_measure;
118 }
119
120 /// The largest scale anywhere, at least 1.
121 1041189 double Max() const {
122
5/6
✓ Branch 0 taken 513850 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 time.
1027711 return m_max;
123 }
124
125 /// The map.
126 const DensityMap& Map() const {
127 return m_map;
128 }
129
130 private:
131 DensityMap m_map;
132 double m_measure = 1.0;
133 double m_max = 1.0;
134 };
135
136 } // namespace terraingen
137