./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
82 / 87
94.3%
Branches:
27 / 39
69.2%
Functions:
8 / 8
100.0%

src/layers/roughness_estimate.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/roughness_estimate.h"
4
5 #include "terraingen/raster/grid.h"
6 #include "util/checks.h"
7
8 #include <algorithm>
9 #include <cmath>
10 #include <optional>
11 #include <stdexcept>
12 #include <string>
13
14 namespace terraingen {
15
16 namespace {
17
18 /// Largest side, in cells, of the lattice a source is sampled on.
19 constexpr double kLatticeCells = 512.0;
20
21 /// Points a side of the lattice a noise layer's RMS height difference is measured on.
22 constexpr int kNoisePoints = 64;
23
24 /// Spacing of that lattice in wavelengths: the golden ratio conjugate, so the points fall at
25 /// every phase of the layer's octave lattices.
26 constexpr double kNoiseStep = 0.6180339887498949;
27
28 /// The roughness of `heights` on cells `cellSize` meters across by the variogram method.
29 27 BaseRoughness EstimateBaseRoughness(const Raster<float>& heights, double cellSize) {
30 27 RequirePositive(cellSize, "Base roughness cell size");
31 27 const DeviogramFit fit = FitDeviogram(heights);
32
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 24 times.
27 if (fit.lags < 2) {
33 3 throw std::invalid_argument(
34 "Estimating roughness from a base needs relief at lags of 1 and 2 cells; the base "
35
1/1
✓ Branch 2 taken 3 times.
3 "is flat or has too little data, so give the values instead");
36 }
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!(fit.slope > 0.0)) {
38 ✗ throw std::invalid_argument("The base's variogram gives a Hurst exponent of " +
39
0/2
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ std::to_string(fit.slope) +
40
0/1
✗ Branch 2 not taken.
✗ ", not positive, so give the Hurst exponent instead");
41 }
42 24 BaseRoughness roughness;
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 roughness.hurst = std::min(fit.slope, 1.0);
44 24 roughness.deviation = std::exp2(fit.intercept);
45 24 roughness.cellSize = cellSize;
46 24 return roughness;
47 }
48
49 } // namespace
50
51 41 double BaseRoughness::Deviation(double lag) const {
52 41 return deviation * std::pow(lag / cellSize, hurst);
53 }
54
55 22 double BaseRoughness::RmsSlope(double baseline) const {
56 22 return Deviation(baseline) / baseline;
57 }
58
59 29 Raster<float> SampleSourceWindow(const Source& source, double cellSize, const Vec2d& origin) {
60 29 const std::optional<RasterGrid> bounded = BoundedGrid(source);
61 29 Bounds2d bounds;
62
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 time.
29 if (bounded) {
63 28 bounds = bounded->Bounds();
64 } else {
65 1 const Vec2d center = source.ReferencePoint();
66 1 const double half = 0.5 * kLatticeCells * cellSize;
67 1 bounds = {center.x - half, center.y - half, center.x + half, center.y + half};
68 }
69 87 const auto cells = [&](double extent) {
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 return std::clamp(std::floor(extent / cellSize), 3.0, kLatticeCells);
71 29 };
72 29 const double width = cells(bounds.maxX - bounds.minX);
73 29 const double height = cells(bounds.maxY - bounds.minY);
74 29 const double col0 =
75 29 std::round((0.5 * (bounds.minX + bounds.maxX) - origin.x) / cellSize - 0.5 * width);
76 29 const double rowTop =
77 29 std::round((0.5 * (bounds.minY + bounds.maxY) - origin.y) / cellSize + 0.5 * height);
78 29 const RasterGrid grid{static_cast<std::size_t>(width), static_cast<std::size_t>(height),
79 29 GeoTransform::NorthUp(origin.x + (col0 - 0.5) * cellSize,
80 29 origin.y + (rowTop + 0.5) * cellSize, cellSize)};
81 29 Raster<float> heights(grid);
82
1/1
✓ Branch 1 taken 29 times.
29 source.Fill(heights, cellSize);
83 29 return heights;
84 }
85
86 28 BaseRoughness EstimateSourceRoughness(const Source& source, const Surface& surface) {
87 28 const RasterGrid grid = source.Grid();
88
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 26 times.
28 if (!BoundedGrid(source)) {
89 2 const Vec2d center = source.ReferencePoint();
90 2 const double native = source.PostSpacingAt(center.x, center.y);
91
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (!std::isfinite(native)) {
92 1 throw std::invalid_argument(
93 "Estimating roughness needs a source with posts, such as a DEM or synthetic noise "
94
1/1
✓ Branch 2 taken 1 time.
1 "with a pixel size; give the values instead");
95 }
96 1 const Raster<float> heights =
97 1 SampleSourceWindow(source, native, grid.transform.PixelToWorld(0.5, 0.5));
98
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
1 return EstimateBaseRoughness(heights, native * surface.UnitLength());
99 1 }
100 26 const Bounds2d bounds = grid.Bounds();
101 26 const Vec2d center{0.5 * (bounds.minX + bounds.maxX), 0.5 * (bounds.minY + bounds.maxY)};
102 26 const double resolved = source.ResolutionAt(center.x, center.y);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 const double native =
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 std::isfinite(resolved) ? resolved : source.PostSpacingAt(center.x, center.y);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 const double cellSize = std::isfinite(native) ? native : grid.PixelSize();
106 26 const Vec2d corner = grid.transform.PixelToWorld(0.0, 0.0);
107 26 const Vec2d origin{corner.x + 0.5 * cellSize, corner.y - 0.5 * cellSize};
108 26 const Raster<float> heights = SampleSourceWindow(source, cellSize, origin);
109
3/3
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 25 times.
✓ Branch 3 taken 23 times.
27 return EstimateBaseRoughness(heights, cellSize * surface.UnitLength());
110 23 }
111
112 14 float MatchedNoiseAmplitude(const Fractal& fractal, const BaseRoughness& roughness) {
113 14 Fractal unit = fractal;
114 14 unit.amplitude = 1.0f;
115 14 const double wavelength = static_cast<double>(fractal.wavelength);
116 14 double sum = 0.0;
117
2/2
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 14 times.
910 for (int row = 0; row < kNoisePoints; ++row) {
118
2/2
✓ Branch 0 taken 57344 times.
✓ Branch 1 taken 896 times.
58240 for (int col = 0; col < kNoisePoints; ++col) {
119 57344 const double x = kNoiseStep * wavelength * col;
120 57344 const double y = kNoiseStep * wavelength * row;
121 229376 const auto at = [&](double px, double py) {
122 172032 return static_cast<double>(
123 172032 EvaluateFractal(static_cast<float>(px), static_cast<float>(py), unit));
124 57344 };
125 57344 const double here = at(x, y);
126 57344 const double east = at(x + wavelength, y) - here;
127 57344 const double north = at(x, y + wavelength) - here;
128 57344 sum += east * east + north * north;
129 }
130 }
131 14 const double unitDeviation = std::sqrt(sum / (2.0 * kNoisePoints * kNoisePoints));
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!(unitDeviation > 0.0)) {
133 ✗ throw std::invalid_argument(
134 "The noise layer shows no relief to match the base's roughness to; give its "
135
0/1
✗ Branch 2 not taken.
✗ "amplitude instead");
136 }
137 14 return static_cast<float>(roughness.Deviation(wavelength) / unitDeviation);
138 }
139
140 } // namespace terraingen
141