./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
58 / 58
100.0%
Branches:
54 / 61
88.5%
Functions:
8 / 8
100.0%

src/sources/synthetic_source.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/sources/synthetic_source.h"
4
5 #include "sources/lattice.h"
6
7 #include "terraingen/noise/validate.h"
8 #include "terraingen/random/stream.h"
9 #include "util/checks.h"
10
11 #include <cmath>
12 #include <limits>
13 #include <stdexcept>
14 #include <string>
15 #include <utility>
16
17 namespace terraingen {
18
19 26 SyntheticSource::NoisePosts::NoisePosts(const SyntheticSource& source, double pixelSize)
20 26 : m_source(source),
21 26 m_transform(GeoTransform::NorthUp(-0.5 * pixelSize, 0.5 * pixelSize, pixelSize)) {}
22
23 34776 void SyntheticSource::NoisePosts::Read(std::int64_t col0, std::int64_t row0, std::size_t width,
24 std::size_t height, Span<float> heights) const {
25 34776 std::vector<Vec2d> points(width * height);
26
2/2
✓ Branch 0 taken 66815 times.
✓ Branch 1 taken 34776 times.
101591 for (std::size_t r = 0; r < height; ++r) {
27 66815 const double row = static_cast<double>(row0 + static_cast<std::int64_t>(r)) + 0.5;
28
2/2
✓ Branch 0 taken 2155138 times.
✓ Branch 1 taken 66815 times.
2221953 for (std::size_t c = 0; c < width; ++c) {
29 2155138 const double col = static_cast<double>(col0 + static_cast<std::int64_t>(c)) + 0.5;
30 2155138 points[r * width + c] = m_transform.PixelToWorld(col, row);
31 }
32 }
33
1/1
✓ Branch 2 taken 34776 times.
34776 m_source.NoiseAt(points, heights);
34 34776 }
35
36 432 SyntheticSource::SyntheticSource(std::vector<Fractal> fractals, float elevation,
37 std::optional<double> pixelSize,
38 432 std::shared_ptr<const Upsampler> upsampler, Surface surface)
39
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 405 times.
432 : m_fractals(std::move(fractals)), m_elevation(elevation), m_givenPixelSize(pixelSize),
40
4/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 405 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 26 times.
433 m_pixelSize(pixelSize ? std::optional<double>(*pixelSize / surface.UnitLength())
41 : std::nullopt),
42
3/3
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 405 times.
✓ Branch 4 taken 430 times.
864 m_surface(surface), m_givenUpsampler(std::move(upsampler)) {
43
1/1
✓ Branch 1 taken 430 times.
432 ValidateFractals(m_fractals);
44
1/1
✓ Branch 1 taken 429 times.
430 RequireFinite(static_cast<double>(m_elevation), "Elevation");
45
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 27 times.
429 if (!m_pixelSize) {
46
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 400 times.
402 if (m_givenUpsampler) {
47 2 throw std::invalid_argument(
48
1/1
✓ Branch 2 taken 2 times.
2 "A synthetic source upsamples only posts, so an upsampler needs a pixel size");
49 }
50 return;
51 }
52
1/1
✓ Branch 1 taken 26 times.
27 RequirePositive(*m_pixelSize, "Pixel size");
53 26 m_posts.emplace(*this, *m_pixelSize);
54
5/7
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
✓ Branch 3 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
65 m_upsampler = FittedUpsampler(m_givenUpsampler, *m_posts);
55
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
8 }
56
57 22 std::shared_ptr<const Source> SyntheticSource::Reseeded(std::uint64_t seed) const {
58 22 std::vector<Fractal> fractals = m_fractals;
59
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 22 times.
47 for (std::size_t n = 0; n < fractals.size(); ++n) {
60 25 fractals[n].seed = ReseedWord(seed, static_cast<std::uint32_t>(n));
61 }
62
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 std::shared_ptr<const Upsampler> upsampler = m_givenUpsampler;
63
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (upsampler) {
64
1/1
✓ Branch 1 taken 3 times.
3 if (auto reseeded = upsampler->Reseeded(
65
2/3
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 JoinWords(ReseedWord(seed, kMaxFractals), ReseedWord(seed, kMaxFractals + 1)))) {
66 3 upsampler = std::move(reseeded);
67 3 }
68 }
69
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
22 if (fractals.empty() && upsampler == m_givenUpsampler) {
70 2 return nullptr;
71 }
72
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
20 return std::make_shared<SyntheticSource>(std::move(fractals), m_elevation, m_givenPixelSize,
73
1/1
✓ Branch 1 taken 20 times.
20 std::move(upsampler), m_surface);
74 22 }
75
76 125332 void SyntheticSource::NoiseAt(Span<const Vec2d> points, Span<float> heights) const {
77 125332 FractalStackRows(m_fractals, {}, m_surface, points, heights);
78
2/2
✓ Branch 0 taken 3620613 times.
✓ Branch 1 taken 125424 times.
3746037 for (float& height : heights) {
79 3620613 height = m_elevation + height;
80 }
81 125424 }
82
83 392 RasterGrid SyntheticSource::Grid() const {
84
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 375 times.
392 return {0, 0, m_posts ? m_posts->Transform() : GeoTransform::NorthUp(0.0, 0.0, 1.0)};
85 }
86
87 91787 void SyntheticSource::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const {
88
2/2
✓ Branch 0 taken 1237 times.
✓ Branch 1 taken 90550 times.
91787 if (m_posts) {
89 1237 m_upsampler->Fill(*m_posts, samples, spacing, threads);
90 1237 return;
91 }
92 90550 detail::FillByTiles(samples, threads, [&](Span<const Vec2d> points, Span<float> heights) {
93
1/1
✓ Branch 2 taken 225 times.
90610 NoiseAt(points, heights);
94 225 });
95 }
96
97 7 double SyntheticSource::PostSpacingAt(double /*x*/, double /*y*/) const {
98
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 return m_pixelSize.value_or(std::numeric_limits<double>::infinity());
99 }
100
101 } // namespace terraingen
102