./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
149 / 151
98.7%
Branches:
133 / 164
81.1%
Functions:
23 / 23
100.0%

src/sources/source_chain.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/sources/source_chain.h"
4
5 #include "sources/lattice.h"
6 #include "sources/seam_field.h"
7 #include "terraingen/random/stream.h"
8 #include "terraingen/util/parallel.h"
9 #include "util/checks.h"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <iterator>
14 #include <limits>
15 #include <optional>
16 #include <stdexcept>
17 #include <string>
18 #include <utility>
19 #include <vector>
20
21 namespace terraingen {
22
23 namespace {
24
25 /// Posts a seam lattice adds beyond the reach on every side, for the stencils and interpolation.
26 constexpr double kSeamBorderPosts = 3.0;
27
28 /// The most posts a seam lattice may have along a side.
29 constexpr double kMaxSeamLatticeSide = 2147483647.0;
30
31 /// `value` shifted by `correction`, or `value` itself where the correction is 0.
32 87945 float Seamed(float value, double correction) {
33
2/2
✓ Branch 0 taken 55891 times.
✓ Branch 1 taken 32054 times.
87945 return correction == 0.0 ? value : static_cast<float>(static_cast<double>(value) + correction);
34 }
35
36 /// The smallest window holding every cell of `heights` without data; empty without one.
37 31 PixelWindow MissingWindow(const Raster<float>& heights) {
38 31 PixelWindow window{heights.Width(), 0, heights.Height(), 0};
39
2/2
✓ Branch 0 taken 1564 times.
✓ Branch 1 taken 31 times.
1595 for (std::size_t row = 0; row < heights.Height(); ++row) {
40 1564 const Span<const float> values = heights.Row(row);
41
11/28
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1340 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1340 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1340 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1340 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 1484 times.
✓ Branch 15 taken 1526 times.
✓ Branch 16 taken 1474 times.
✓ Branch 17 taken 10 times.
✓ Branch 18 taken 1474 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1474 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
5886 const auto missing = [&](float value) { return heights.IsMissing(value); };
42
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1536 times.
1564 const float* first = std::find_if(values.begin(), values.end(), missing);
43
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1536 times.
1564 if (first == values.end()) {
44 28 continue;
45 }
46
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1506 times.
1536 const auto last = std::find_if(std::make_reverse_iterator(values.end()),
47 1536 std::make_reverse_iterator(first), missing);
48 3072 window.colBegin =
49
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1506 times.
1536 std::min(window.colBegin, static_cast<std::size_t>(first - values.begin()));
50
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1506 times.
1536 window.colEnd =
51
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1506 times.
1536 std::max(window.colEnd, static_cast<std::size_t>(last.base() - values.begin()));
52
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1506 times.
1536 window.rowBegin = std::min(window.rowBegin, row);
53 1536 window.rowEnd = row + 1;
54 }
55 31 return window;
56 }
57
58 /// `measure` of the first of `sources` with a height at (`x`, `y`) at an infinite spacing, or
59 /// infinity without one. Sources give a finite measure only where they have a height, so only a
60 /// source measuring infinity is asked for its height.
61 template <typename Measure>
62 130 double FirstWithHeight(const std::vector<std::shared_ptr<const Source>>& sources, double x,
63 double y, const Measure& measure) {
64 130 constexpr double kInfinity = std::numeric_limits<double>::infinity();
65
3/4
double terraingen::(anonymous namespace)::FirstWithHeight<terraingen::SourceChain::PostSpacingAt(double, double) const::{lambda(terraingen::Source const&)#1}>(std::vector<std::shared_ptr<terraingen::Source const>, std::allocator<std::shared_ptr<terraingen::Source const> > > const&, double, double, terraingen::SourceChain::PostSpacingAt(double, double) const::{lambda(terraingen::Source const&)#1} const&):
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
double terraingen::(anonymous namespace)::FirstWithHeight<terraingen::SourceChain::ResolutionAt(double, double) const::{lambda(terraingen::Source const&)#1}>(std::vector<std::shared_ptr<terraingen::Source const>, std::allocator<std::shared_ptr<terraingen::Source const> > > const&, double, double, terraingen::SourceChain::ResolutionAt(double, double) const::{lambda(terraingen::Source const&)#1} const&):
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 21 times.
267 for (const auto& source : sources) {
66
4/4
double terraingen::(anonymous namespace)::FirstWithHeight<terraingen::SourceChain::PostSpacingAt(double, double) const::{lambda(terraingen::Source const&)#1}>(std::vector<std::shared_ptr<terraingen::Source const>, std::allocator<std::shared_ptr<terraingen::Source const> > > const&, double, double, terraingen::SourceChain::PostSpacingAt(double, double) const::{lambda(terraingen::Source const&)#1} const&):
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
double terraingen::(anonymous namespace)::FirstWithHeight<terraingen::SourceChain::ResolutionAt(double, double) const::{lambda(terraingen::Source const&)#1}>(std::vector<std::shared_ptr<terraingen::Source const>, std::allocator<std::shared_ptr<terraingen::Source const> > > const&, double, double, terraingen::SourceChain::ResolutionAt(double, double) const::{lambda(terraingen::Source const&)#1} const&):
✓ Branch 1 taken 137 times.
✓ Branch 2 taken 103 times.
246 const double value = measure(*source);
67
8/8
double terraingen::(anonymous namespace)::FirstWithHeight<terraingen::SourceChain::PostSpacingAt(double, double) const::{lambda(terraingen::Source const&)#1}>(std::vector<std::shared_ptr<terraingen::Source const>, std::allocator<std::shared_ptr<terraingen::Source const> > > const&, double, double, terraingen::SourceChain::PostSpacingAt(double, double) const::{lambda(terraingen::Source const&)#1} const&):
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 3 times.
double terraingen::(anonymous namespace)::FirstWithHeight<terraingen::SourceChain::ResolutionAt(double, double) const::{lambda(terraingen::Source const&)#1}>(std::vector<std::shared_ptr<terraingen::Source const>, std::allocator<std::shared_ptr<terraingen::Source const> > > const&, double, double, terraingen::SourceChain::ResolutionAt(double, double) const::{lambda(terraingen::Source const&)#1} const&):
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 103 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 134 times.
246 if (std::isfinite(value) || !std::isnan(source->HeightAt(x, y, kInfinity))) {
68 109 return value;
69 }
70 }
71 return kInfinity;
72 }
73
74 } // namespace
75
76 134 void ValidateChainSeams(const ChainSeams& seams) {
77 134 RequirePositive(seams.width, "The seam width");
78 132 RequirePositive(seams.decayLength, "The seam decay length");
79 129 }
80
81 85 double SeamReach(const ChainSeams& seams) {
82
3/3
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 1 time.
85 switch (seams.mode) {
83 15 case ChainSeamMode::Feather:
84 15 return seams.width;
85 69 case ChainSeamMode::Height:
86 69 case ChainSeamMode::Slope:
87 69 return kSeamReachDecays * seams.decayLength;
88 case ChainSeamMode::None:
89 break;
90 }
91 return 0.0;
92 }
93
94 63 RasterGrid SeamLattice(const RasterGrid& grid, const ChainSeams& seams) {
95 63 ValidateChainSeams(seams);
96 63 const GeoTransform& transform = grid.transform;
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 const double side = std::min(transform.CellWidth(), transform.CellHeight());
98
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (!IsPositive(side)) {
99
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A seam needs grid cells of a positive, finite size");
100 }
101 62 const double pad = std::ceil(SeamReach(seams) / side) + kSeamBorderPosts;
102 62 const double width = static_cast<double>(grid.width) + 2.0 * pad;
103 62 const double height = static_cast<double>(grid.height) + 2.0 * pad;
104
3/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
62 if (!(width <= kMaxSeamLatticeSide && height <= kMaxSeamLatticeSide)) {
105
1/1
✓ Branch 1 taken 1 time.
2 throw std::invalid_argument("A seam reaching " + std::to_string(pad) +
106 " posts beyond the grid needs a lattice more than 2^31 - 1 "
107
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
3 "posts a side");
108 }
109 61 RasterGrid lattice{static_cast<std::size_t>(width), static_cast<std::size_t>(height),
110 61 transform};
111 61 const Vec2d origin = transform.PixelToWorld(-pad, -pad);
112 61 lattice.transform.originX = origin.x;
113 61 lattice.transform.originY = origin.y;
114 61 return lattice;
115 }
116
117 70 SourceChain::SourceChain(std::vector<std::shared_ptr<const Source>> sources, ChainSeams seams)
118
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 69 times.
70 : m_sources(std::move(sources)), m_seams(seams) {
119
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 69 times.
70 if (m_sources.empty()) {
120
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A source chain needs at least one source");
121 }
122
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 68 times.
207 for (const auto& source : m_sources) {
123
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 138 times.
139 if (!source) {
124
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A source chain cannot hold a null source");
125 }
126 }
127
1/1
✓ Branch 1 taken 64 times.
68 ValidateChainSeams(m_seams);
128
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 62 times.
64 if (m_sources.size() < 2) {
129 2 return;
130 }
131
1/1
✓ Branch 1 taken 62 times.
62 const std::optional<RasterGrid> grid = BoundedGrid(*m_sources[m_sources.size() - 2]);
132
4/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 25 times.
62 if (grid && m_seams.mode != ChainSeamMode::None) {
133
1/1
✓ Branch 1 taken 28 times.
29 static_cast<void>(SeamLattice(*grid, m_seams));
134 }
135
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 4 times.
61 m_prefix = m_sources.size() == 2
136
3/4
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
122 ? m_sources.front()
137
5/7
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 57 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
69 : std::make_shared<SourceChain>(std::vector<std::shared_ptr<const Source>>(
138
1/1
✓ Branch 1 taken 4 times.
4 m_sources.begin(), m_sources.end() - 1),
139
2/3
✓ Branch 1 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 61 times.
65 m_seams);
140
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 }
141
142
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 61 times.
✓ Branch 3 taken 2 times.
158 SourceChain::~SourceChain() = default;
143
144 27524 const detail::SeamField& SourceChain::Seam() const {
145 27524 std::call_once(m_seamed, [this] {
146 34 const std::optional<RasterGrid> grid = BoundedGrid(*m_sources[m_sources.size() - 2]);
147
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1 time.
68 m_seam = std::make_shared<const detail::SeamField>(
148
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1 time.
34 grid ? detail::BuildSeamField(*m_prefix, *m_sources.back(), *grid, m_seams)
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 : detail::SeamField{});
150 34 });
151 27524 return *m_seam;
152 }
153
154 44193 void SourceChain::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const {
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44193 times.
44193 if (!m_prefix) {
156 ✗ FillUnmarked(*m_sources.front(), samples, spacing, threads);
157 ✗ return;
158 }
159 44193 FillUnmarked(*m_prefix, samples, spacing, threads);
160
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 44162 times.
44193 if (Raster<float>* grid = samples.Grid()) {
161 31 FillGridGaps(*grid, spacing, threads);
162 } else {
163 44162 FillPointGaps(samples.Points(), samples.Heights(), spacing);
164 }
165 }
166
167 31 void SourceChain::FillGridGaps(Raster<float>& heights, double spacing, unsigned threads) const {
168 31 const PixelWindow gaps = MissingWindow(heights);
169
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 time.
31 if (gaps.Empty()) {
170 1 return;
171 }
172
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5 times.
30 if (!heights.NoData()) {
173 25 heights.SetNoData(std::numeric_limits<float>::quiet_NaN());
174 }
175 30 const RasterGrid grid = heights.Grid();
176 30 const std::optional<RasterGrid> part = detail::ExactSubgrid(grid, gaps);
177
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 const PixelWindow window = part ? gaps : WholeWindow(grid);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 Raster<float> next((part ? *part : grid));
179
1/1
✓ Branch 1 taken 30 times.
30 SampleBlock samples(next);
180
1/1
✓ Branch 1 taken 30 times.
30 FillUnmarked(*m_sources.back(), samples, spacing, threads);
181
2/2
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
90 ParallelFor(window.Height(), threads, [&](std::size_t r) {
182 1536 const std::size_t row = window.rowBegin + r;
183 1536 const detail::SeamField* seam = nullptr;
184
2/2
✓ Branch 0 taken 109653 times.
✓ Branch 1 taken 1534 times.
111187 for (std::size_t col = window.colBegin; col < window.colEnd; ++col) {
185 109653 float& cell = heights(col, row);
186 109514 const float value = next(col - window.colBegin, r);
187
4/4
✓ Branch 0 taken 64071 times.
✓ Branch 1 taken 45569 times.
✓ Branch 2 taken 2160 times.
✓ Branch 3 taken 61911 times.
109640 if (!heights.IsMissing(cell) || next.IsMissing(value)) {
188 47729 continue;
189 }
190
2/2
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 60407 times.
61911 if (!seam) {
191 1504 seam = &Seam();
192 }
193 61911 const Vec2d center = heights.CellCenter(col, row);
194 61911 cell = Seamed(value, seam->At(center.x, center.y));
195 }
196 1534 });
197 30 }
198
199 44162 void SourceChain::FillPointGaps(Span<const Vec2d> points, Span<float> heights,
200 double spacing) const {
201 44162 detail::EvaluateChosen(
202 points.size(),
203 132490 [&](std::size_t i) {
204
2/2
✓ Branch 1 taken 26049 times.
✓ Branch 2 taken 18117 times.
44166 return std::isnan(heights[i]) ? std::optional<Vec2d>(points[i]) : std::nullopt;
205 },
206 114371 [&](Span<const Vec2d> gaps, Span<float> later) {
207 26047 m_sources.back()->HeightsAt(gaps, later, spacing);
208 26047 },
209 44162 [&](std::size_t i, float later) {
210 26049 heights[i] =
211
2/2
✓ Branch 0 taken 26020 times.
✓ Branch 1 taken 29 times.
26049 std::isnan(later) ? later : Seamed(later, Seam().At(points[i].x, points[i].y));
212 26049 });
213 44162 }
214
215 127 double SourceChain::ResolutionAt(double x, double y) const {
216 127 return FirstWithHeight(m_sources, x, y,
217 367 [&](const Source& source) { return source.ResolutionAt(x, y); });
218 }
219
220 3 double SourceChain::PostSpacingAt(double x, double y) const {
221 3 return FirstWithHeight(m_sources, x, y,
222 9 [&](const Source& source) { return source.PostSpacingAt(x, y); });
223 }
224
225 4 std::shared_ptr<const Source> SourceChain::Reseeded(std::uint64_t seed) const {
226 4 std::vector<std::shared_ptr<const Source>> sources = m_sources;
227 bool changed = false;
228
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (std::size_t n = 0; n < sources.size(); ++n) {
229 8 const auto word = static_cast<std::uint32_t>(2 * n);
230
1/1
✓ Branch 1 taken 8 times.
8 const std::uint64_t part = JoinWords(ReseedWord(seed, word), ReseedWord(seed, word + 1));
231
3/3
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 5 times.
8 if (auto reseeded = sources[n]->Reseeded(part)) {
232 3 sources[n] = std::move(reseeded);
233 3 changed = true;
234 8 }
235 }
236
3/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 3 times.
4 return changed ? std::make_shared<SourceChain>(std::move(sources), m_seams) : nullptr;
237 4 }
238
239 } // namespace terraingen
240