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 | 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 | 1 | return field == other.field && measure == other.measure && | |
| 40 | 1 | minDiameter == other.minDiameter && maxDiameter == other.maxDiameter && | |
| 41 | 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 | 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 | 1 | return field == other.field && measure == other.measure && | |
| 69 | 2 | referenceDiameter == other.referenceDiameter && minExponent == other.minExponent && | |
| 70 | 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 | 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 | return m_measure; | |
| 118 | } | ||
| 119 | |||
| 120 | /// The largest scale anywhere, at least 1. | ||
| 121 | 1041189 | double Max() const { | |
| 122 | 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 |