./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
58 / 58
100.0%
Branches:
46 / 49
93.9%
Functions:
11 / 11
100.0%

src/layers/density_map.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/density_map.h"
4
5 #include "terraingen/layers/layer_fields.h"
6 #include "terraingen/util/bits.h"
7 #include "util/numerics.h"
8
9 #include <algorithm>
10 #include <cmath>
11 #include <stdexcept>
12 #include <utility>
13
14 namespace terraingen {
15
16 namespace {
17
18 /// Simpson intervals in log diameter for an areal fraction.
19 constexpr int kArealSteps = 2048;
20
21 /// The share of ground covered by features of `distribution` in [`low`, `high`) meters: the
22 /// integral of pi D^2 / 4 dN/dD, taken in log diameter.
23 1 double ArealFraction(const SizeFrequencyDistribution& distribution, double low, double high) {
24 1 double sum = 0.0;
25 1 ForEachLogSimpsonNode(low, high, kArealSteps, [&](double d, double weight) {
26 2049 sum += weight * 0.25 * kPi * d * d * distribution.Incremental(d);
27 2049 });
28 1 return sum;
29 }
30
31 } // namespace
32
33 52 void ValidateDensityMap(const DensityMap& map) {
34
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 50 times.
52 if (!map.field) {
35
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A density map needs a field");
36 }
37 50 CheckRanges(map);
38
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 33 times.
49 if (map.measure != DensityMeasure::Scale &&
39
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 12 times.
16 !(map.minDiameter > 0.0 && map.maxDiameter > map.minDiameter)) {
40
1/1
✓ Branch 2 taken 4 times.
4 throw std::invalid_argument("A density map's diameters must satisfy 0 < min < max");
41 }
42 45 }
43
44 539 double MapMeasure(const DensityMap& map, const SizeFrequencyDistribution& distribution) {
45
3/3
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 13 times.
539 switch (map.measure) {
46 case DensityMeasure::Scale:
47 return 1.0;
48 524 case DensityMeasure::Count:
49 524 return distribution.ExpectedCount(1.0, map.minDiameter, map.maxDiameter);
50 2 case DensityMeasure::ArealFraction: {
51 2 const double high = std::fmin(map.maxDiameter, distribution.ValidRange().max);
52
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (!std::isfinite(high)) {
53
1/1
✓ Branch 2 taken 1 time.
1 throw std::domain_error("An areal fraction needs a finite largest diameter");
54 }
55 1 return ArealFraction(distribution, map.minDiameter, high);
56 }
57 }
58 return 1.0;
59 }
60
61 18 void ValidateSizeSlopeMap(const SizeSlopeMap& map) {
62
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 17 times.
18 if (!map.field) {
63
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A size slope map needs a field");
64 }
65 17 CheckRanges(map);
66
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (!(map.maxExponent > map.minExponent)) {
67
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A size slope map's exponents must satisfy min < max");
68 }
69 12 }
70
71 72195 std::optional<double> SizeSlopeAt(const SizeSlopeMap& map, double x, double y) {
72 72195 const double value = map.field->ValueAt(x, y);
73
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 72194 times.
72195 if (std::isnan(value)) {
74 1 return std::nullopt;
75 }
76 72194 double exponent = value;
77
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72192 times.
72194 if (map.measure == SizeSlopeMeasure::MeanDiameter) {
78 2 const double above = value - map.referenceDiameter;
79
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 exponent = above > 0.0 ? value / above : map.maxExponent;
80 }
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72194 times.
144388 return std::clamp(exponent, map.minExponent, map.maxExponent);
82 }
83
84 2122124 double MapValueAt(const DensityMap& map, double x, double y) {
85 2122124 const double value = map.field->ValueAt(x, y);
86
2/2
✓ Branch 0 taken 1037976 times.
✓ Branch 1 taken 1084148 times.
2122124 return map.measure == DensityMeasure::Count ? value / map.areaUnit : value;
87 }
88
89 16 double MapMax(const DensityMap& map) {
90 16 const double top = map.field->Max();
91
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 return map.measure == DensityMeasure::Count ? top / map.areaUnit : top;
92 }
93
94 15 DensityScale::DensityScale(DensityMap map, const SizeFrequencyDistribution& distribution)
95
1/1
✓ Branch 1 taken 15 times.
15 : m_map(std::move(map)) {
96
1/1
✓ Branch 1 taken 15 times.
15 ValidateDensityMap(m_map);
97
1/1
✓ Branch 1 taken 15 times.
15 m_measure = MapMeasure(m_map, distribution);
98
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
15 if (!(m_measure > 0.0 && std::isfinite(m_measure))) {
99
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A density map must measure some of its size distribution");
100 }
101
1/1
✓ Branch 1 taken 14 times.
14 m_max = std::fmax(1.0, MapMax(m_map) / m_measure);
102
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
15 }
103
104 527334 double DensityScale::At(double x, double y) const {
105 527334 return Scale(MapValueAt(m_map, x, y));
106 }
107
108 1041187 double DensityScale::Scale(double value) const {
109
2/2
✓ Branch 0 taken 1040889 times.
✓ Branch 1 taken 298 times.
1041187 if (std::isnan(value)) {
110 return 1.0;
111 }
112
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1040888 times.
2081778 return std::clamp(value / m_measure, 0.0, m_max);
113 }
114
115 } // namespace terraingen
116