./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
67 / 67
100.0%
Branches:
60 / 62
96.8%
Functions:
6 / 6
100.0%

src/layers/noise_layer.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/noise_layer.h"
4
5 #include "layers/kernel.h"
6 #include "terraingen/layers/source_resolved.h"
7 #include "terraingen/noise/surface_noise.h"
8 #include "terraingen/noise/validate.h"
9 #include "terraingen/random/stream.h"
10 #include "terraingen/raster/cell_loops.h"
11 #include "terraingen/util/parallel.h"
12
13 #include <algorithm>
14 #include <cmath>
15 #include <cstddef>
16 #include <cstdint>
17 #include <memory>
18 #include <utility>
19 #include <vector>
20
21 namespace terraingen {
22
23 namespace {
24
25 /// Adds the noise of `fractals` (`FractalStackAt`) to each sample of `samples` holding data, to
26 /// the same bits: a grid row by row over `threads` threads, or the points in one run
27 /// (`FractalStackRows`).
28 2196915 void AddNoiseRows(SampleBlock& samples, unsigned threads, const std::vector<Fractal>& fractals,
29 const Vec2d& origin, const Surface& surface) {
30
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 2196666 times.
2196915 if (Raster<float>* grid = samples.Grid()) {
31
1/1
✓ Branch 2 taken 249 times.
249 ParallelFor(grid->Height(), threads, [&](std::size_t row) {
32 18343 std::vector<Vec2d> points(grid->Width());
33
1/1
✓ Branch 1 taken 18344 times.
18348 std::vector<float> noise(grid->Width());
34
2/2
✓ Branch 0 taken 4714166 times.
✓ Branch 1 taken 18344 times.
4732510 for (std::size_t col = 0; col < points.size(); ++col) {
35 4714166 points[col] = grid->CellCenter(col, row);
36 }
37
1/1
✓ Branch 3 taken 18345 times.
18344 FractalStackRows(fractals, origin, surface, points, noise);
38
2/2
✓ Branch 0 taken 4740976 times.
✓ Branch 1 taken 18349 times.
4759325 for (std::size_t col = 0; col < points.size(); ++col) {
39 4740976 float& cell = (*grid)(col, row);
40
4/4
✓ Branch 0 taken 31278 times.
✓ Branch 1 taken 4709702 times.
✓ Branch 2 taken 31273 times.
✓ Branch 3 taken 5 times.
4772258 if (!grid->IsNoData(cell)) {
41 4740975 cell = cell + noise[col];
42 }
43 }
44 18349 });
45 249 return;
46 }
47 2196666 std::vector<Vec2d> points(samples.Size());
48
1/1
✓ Branch 1 taken 2192826 times.
2192835 std::vector<float> noise(samples.Size());
49
2/2
✓ Branch 0 taken 2941629 times.
✓ Branch 1 taken 2196638 times.
5138267 for (std::size_t i = 0; i < points.size(); ++i) {
50
1/1
✓ Branch 1 taken 2945441 times.
2941629 points[i] = samples.Position(i);
51 }
52
1/1
✓ Branch 3 taken 2197004 times.
2196638 FractalStackRows(fractals, origin, surface, points, noise);
53
2/2
✓ Branch 0 taken 2945455 times.
✓ Branch 1 taken 2202207 times.
5147662 for (std::size_t i = 0; i < points.size(); ++i) {
54
2/3
✓ Branch 1 taken 2951436 times.
✓ Branch 3 taken 2952917 times.
✗ Branch 4 not taken.
2945455 if (!samples.IsNoData(i)) {
55 2952917 samples.Height(i) = samples.Height(i) + noise[i];
56 }
57 }
58 2202207 }
59
60 } // namespace
61
62
1/1
✓ Branch 2 taken 389 times.
392 NoiseLayer::NoiseLayer(NoiseLayerOptions options) : m_options(std::move(options)) {
63
1/1
✓ Branch 1 taken 389 times.
392 ValidateFractals(m_options.fractals, 1);
64
1/1
✓ Branch 1 taken 388 times.
389 ValidateSourceResolvesPixels(m_options.sourceResolvesPixels);
65 388 m_kernel = MakeNoiseLayerKernel(m_options.fractals);
66
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 385 times.
388 if (!m_options.sourceResolvesPixels) {
67 return;
68 }
69
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 for (const Fractal& fractal : m_options.fractals) {
70
1/1
✓ Branch 1 taken 4 times.
4 m_wavelengths.push_back(static_cast<double>(fractal.wavelength));
71 }
72 3 std::sort(m_wavelengths.begin(), m_wavelengths.end());
73 3 m_wavelengths.erase(std::unique(m_wavelengths.begin(), m_wavelengths.end()),
74 3 m_wavelengths.end());
75
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 3 times.
10 for (std::size_t kept = 0; kept <= m_wavelengths.size(); ++kept) {
76
1/1
✓ Branch 1 taken 7 times.
7 std::vector<Fractal> stack = m_options.fractals;
77
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
17 for (Fractal& fractal : stack) {
78
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 1 time.
10 if (kept < m_wavelengths.size() &&
79
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 time.
6 static_cast<double>(fractal.wavelength) >= m_wavelengths[kept]) {
80 5 fractal.enabled = false;
81 }
82 }
83
1/1
✓ Branch 1 taken 7 times.
7 m_stacks.push_back(std::move(stack));
84 7 }
85 12 }
86
87 20 LayerPtr NoiseLayer::Reseeded(std::uint64_t seed) const {
88 20 NoiseLayerOptions options = m_options;
89
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 20 times.
43 for (std::size_t n = 0; n < options.fractals.size(); ++n) {
90 23 options.fractals[n].seed = ReseedWord(seed, static_cast<std::uint32_t>(n));
91 }
92
1/1
✓ Branch 1 taken 20 times.
20 return std::make_shared<NoiseLayer>(std::move(options));
93 20 }
94
95 2187885 void NoiseLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const {
96 2187885 const Surface& surface = context.SurfaceOrPlane();
97 2195691 const Vec2d origin = context.ReferencePoint();
98
4/4
✓ Branch 0 taken 2030011 times.
✓ Branch 1 taken 166785 times.
✓ Branch 2 taken 380 times.
✓ Branch 3 taken 2029631 times.
2196796 if (context.source != nullptr && m_options.sourceResolvesPixels) {
99 380 MapSamples(samples, context.threads, [&](float height, const Vec2d& p) {
100 635 const double resolved = SourceResolvedSize(
101 635 context.source, m_options.sourceResolvesPixels, surface, p.x, p.y);
102 635 const auto kept =
103 635 std::isnan(resolved)
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 635 times.
1270 ? m_wavelengths.size()
105 : static_cast<std::size_t>(
106 635 std::lower_bound(m_wavelengths.begin(), m_wavelengths.end(), resolved) -
107 635 m_wavelengths.begin());
108 635 return height + FractalStackAt(m_stacks[kept], origin, surface, p.x, p.y);
109 });
110 395 return;
111 }
112
2/2
✓ Branch 0 taken 2193138 times.
✓ Branch 1 taken 3278 times.
2196416 Raster<float>* grid = samples.Grid();
113
8/8
✓ Branch 0 taken 2193138 times.
✓ Branch 1 taken 3278 times.
✓ Branch 2 taken 247 times.
✓ Branch 3 taken 2192891 times.
✓ Branch 4 taken 17 times.
✓ Branch 5 taken 230 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 15 times.
2196433 if (!surface.IsBody() && grid != nullptr && context.backend != Backend::Cpu &&
114 17 RunKernel(context.backend, CompiledKernel<NoiseLayerKernel>(), AsBytes(Span(&m_kernel, 1)),
115 *grid, origin)) {
116 15 return;
117 }
118 2196401 AddNoiseRows(samples, context.threads, m_options.fractals, origin, surface);
119 }
120
121 } // namespace terraingen
122