./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
87 / 87
100.0%
Branches:
75 / 80
93.8%
Functions:
8 / 8
100.0%

src/albedo/raster_albedo.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/raster_albedo.h"
4
5 #include "terraingen/albedo/albedo_fields.h"
6 #include "terraingen/analysis/variogram.h"
7 #include "terraingen/noise/hash.h"
8 #include "terraingen/noise/perlin.h"
9 #include "terraingen/noise/structure_function.h"
10 #include "terraingen/random/streams.h"
11
12 #include <algorithm>
13 #include <array>
14 #include <cmath>
15 #include <limits>
16 #include <stdexcept>
17 #include <utility>
18
19 namespace terraingen {
20
21 namespace {
22
23 /// Step between the seeds of successive detail octaves (2^32 over the golden ratio).
24 constexpr std::uint32_t kOctaveSeedStep = 0x9e3779b9u;
25
26 /// The logarithm of the mean of the bands at each cell, NaN where a band has no data or is not
27 /// positive.
28 18 Raster<float> LogBrightness(const std::vector<Raster<float>>& bands) {
29 18 Raster<float> logs(bands.front().Width(), bands.front().Height());
30
2/2
✓ Branch 1 taken 440 times.
✓ Branch 2 taken 18 times.
458 for (std::size_t row = 0; row < logs.Height(); ++row) {
31
2/2
✓ Branch 0 taken 14742 times.
✓ Branch 1 taken 440 times.
15182 for (std::size_t col = 0; col < logs.Width(); ++col) {
32 14742 double sum = 0.0;
33
2/2
✓ Branch 0 taken 15254 times.
✓ Branch 1 taken 14742 times.
29996 for (const Raster<float>& band : bands) {
34 15254 const float value = band(col, row);
35
4/6
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 14198 times.
✓ Branch 2 taken 15253 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15253 times.
✗ Branch 5 not taken.
31563 sum += band.IsNoData(value) || !std::isfinite(value) || value <= 0.0f
36
2/2
✓ Branch 0 taken 1055 times.
✓ Branch 1 taken 1 time.
16309 ? std::numeric_limits<double>::quiet_NaN()
37 : static_cast<double>(value);
38 }
39 14742 logs(col, row) = static_cast<float>(std::log(sum / static_cast<double>(bands.size())));
40 }
41 }
42 18 return logs;
43 }
44
45 /// The RMS of `PerlinNoise2` over a fixed set of points, so octaves can be given an RMS.
46 224707 double PerlinRms() {
47 7 static const double rms = [] {
48 7 double sum = 0.0;
49 7 constexpr int kSide = 256;
50
2/2
✓ Branch 0 taken 1792 times.
✓ Branch 1 taken 7 times.
1799 for (int j = 0; j < kSide; ++j) {
51
2/2
✓ Branch 0 taken 458752 times.
✓ Branch 1 taken 1792 times.
460544 for (int i = 0; i < kSide; ++i) {
52 458752 const float v = PerlinNoise2(
53 458752 i / 8, j / 8, static_cast<float>(i % 8) / 8.0f + 1.0f / 16.0f,
54 458752 static_cast<float>(j % 8) / 8.0f + 1.0f / 16.0f, kAlbedoDetailRmsStream);
55 458752 sum += static_cast<double>(v) * static_cast<double>(v);
56 }
57 }
58 7 return std::sqrt(sum / (kSide * kSide));
59
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 224695 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 5 times.
224707 }();
60 224707 return rms;
61 }
62
63 } // namespace
64
65 26 void ValidateAlbedoDetail(const AlbedoDetail& detail) {
66 26 CheckRanges(detail);
67 22 ValidateResolutionFade(detail.fade);
68 21 }
69
70 26 RasterAlbedo::RasterAlbedo(std::vector<Raster<float>> bands, RasterAlbedoOptions options)
71
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
26 : m_bands(std::move(bands)), m_options(options) {
72
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
26 if (m_bands.size() != 1 && m_bands.size() != kMaxAlbedoBands) {
73
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An albedo map must have 1 or 3 bands");
74 }
75
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 time.
25 const Raster<float>& first = m_bands.front();
76
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 1 time.
25 if (first.Empty() || first.Transform().IsRotated()) {
77
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("An albedo map must be neither empty nor rotated");
78 }
79
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 22 times.
48 for (const Raster<float>& band : m_bands) {
80
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
26 if (band.Width() != first.Width() || band.Height() != first.Height() ||
81
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 !(band.Transform() == first.Transform())) {
82
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("The bands of an albedo map must share one grid");
83 }
84 }
85
1/1
✓ Branch 1 taken 18 times.
22 ValidateAlbedoDetail(m_options.detail);
86
1/1
✓ Branch 1 taken 18 times.
18 MeasureRoughness();
87
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 17 times.
18 if (m_options.detail.hurst) {
88 1 m_hurst = *m_options.detail.hurst;
89 }
90 26 }
91
92 18 void RasterAlbedo::MeasureRoughness() {
93 18 const Raster<float> logs = LogBrightness(m_bands);
94
1/1
✓ Branch 1 taken 18 times.
18 const DeviogramFit fit = FitDeviogram(logs);
95 18 m_roughness = fit.cellRms / std::sqrt(2.0);
96
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 if (fit.lags >= 2) {
97
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
32 m_hurst = std::clamp(fit.slope, 0.0, 1.0);
98 }
99 18 }
100
101 226334 double RasterAlbedo::DetailSum(double x, double y, double spacing, const Surface& surface) const {
102 226334 const AlbedoDetail& detail = m_options.detail;
103 226334 const Vec2d origin = m_bands.front().CellCenter(0, 0);
104 226334 const double cell = m_bands.front().Grid().Spacing(surface);
105 double sum = 0.0;
106
2/2
✓ Branch 0 taken 842968 times.
✓ Branch 1 taken 543 times.
843511 for (int octave = 0; octave < detail.octaves; ++octave) {
107 842968 const double wavelength = cell / std::exp2(octave + 1);
108 842968 const double weight = ResolutionWeight(wavelength, spacing, detail.fade);
109
2/2
✓ Branch 0 taken 617177 times.
✓ Branch 1 taken 225759 times.
842936 if (weight == 0.0) {
110 break;
111 }
112 617177 const std::uint32_t seed =
113
2/2
✓ Branch 0 taken 1609 times.
✓ Branch 1 taken 615568 times.
617177 PcgHash(detail.seed + kOctaveSeedStep * static_cast<std::uint32_t>(octave));
114 617177 float noise = 0.0f;
115
2/2
✓ Branch 0 taken 1609 times.
✓ Branch 1 taken 615568 times.
617177 if (surface.IsBody()) {
116 1609 const Vec3d p = surface.Position({x, y});
117 1609 noise = PerlinNoise3Precise(p.x / wavelength, p.y / wavelength, p.z / wavelength, seed);
118 } else {
119 615568 noise =
120 615568 PerlinNoise2Precise((x - origin.x) / wavelength, (y - origin.y) / wavelength, seed);
121 }
122 617177 sum += weight * std::exp2(-(octave + 1) * m_hurst) * static_cast<double>(noise);
123 }
124 226302 return sum;
125 }
126
127 228889 AlbedoSample RasterAlbedo::ValueAt(double x, double y, const LayerContext& context) const {
128 228889 AlbedoSample value{};
129
2/2
✓ Branch 0 taken 230589 times.
✓ Branch 1 taken 228081 times.
458670 for (std::size_t band = 0; band < m_bands.size(); ++band) {
130
2/2
✓ Branch 1 taken 826 times.
✓ Branch 2 taken 229781 times.
230589 value[band] = SampleRaster(m_bands[band], x, y, m_options.method);
131
2/2
✓ Branch 0 taken 826 times.
✓ Branch 1 taken 229781 times.
230607 if (std::isnan(value[band])) {
132 826 value.fill(std::numeric_limits<float>::quiet_NaN());
133 826 return value;
134 }
135 }
136 228081 const AlbedoDetail& detail = m_options.detail;
137
5/6
✓ Branch 0 taken 226335 times.
✓ Branch 1 taken 1746 times.
✓ Branch 2 taken 226334 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 226334 times.
228081 if (!detail.enabled || detail.scale == 0.0 || m_roughness == 0.0) {
138 1747 return value;
139 }
140 226334 const Surface& surface = context.SurfaceOrPlane();
141 226334 const double sum = DetailSum(x, y, context.spacing, surface);
142
2/2
✓ Branch 0 taken 1604 times.
✓ Branch 1 taken 224706 times.
226310 const double rms =
143
2/2
✓ Branch 0 taken 1604 times.
✓ Branch 1 taken 224706 times.
226310 surface.IsBody() ? std::sqrt(0.5 * Perlin3StructureFunction(1e6)) : PerlinRms();
144 226294 const double factor = std::exp(detail.scale * m_roughness * sum / rms);
145
2/2
✓ Branch 0 taken 227808 times.
✓ Branch 1 taken 226294 times.
454102 for (std::size_t band = 0; band < m_bands.size(); ++band) {
146 227808 value[band] = static_cast<float>(static_cast<double>(value[band]) * factor);
147 }
148 226294 return value;
149 }
150
151 } // namespace terraingen
152