./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
18 / 20
90.0%
Branches:
12 / 18
66.7%
Functions:
3 / 4, 2 excl
75.0%

include/terraingen/sources/source_chain.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Sources tried in order, each filling the gaps of those before it.
5 #pragma once
6
7 #include "terraingen/raster/grid.h"
8 #include "terraingen/sources/source.h"
9
10 #include <cstdint>
11 #include <memory>
12 #include <mutex>
13 #include <vector>
14
15 namespace terraingen {
16
17 namespace detail {
18 class SeamField;
19 } // namespace detail
20
21 /// How a `SourceChain` joins a source to the gaps of the sources before it.
22 enum class ChainSeamMode {
23 None, ///< Each cell takes the first source with data, unchanged.
24 Feather, ///< Fades the edge's height difference out across a band of the gap.
25 Height, ///< Shifts the later source by the edge's height difference, spread smoothly (C0).
26 Slope, ///< Shifts the later source so heights and slopes both match at the edge (C1).
27 };
28
29 /// How a `SourceChain` joins each source to the gaps of those before it.
30 ///
31 /// Along the edge of each gap the earlier source's height minus the later one's is measured at
32 /// the earlier source's posts. `Feather` adds that difference, carried from the nearest edge post,
33 /// to the later source and fades it to 0 over `width`. `Height` spreads it into the gap as the
34 /// solution of (Laplacian - 1/L^2) d = 0 with L the `decayLength`, so the later source keeps its
35 /// relief and only its level shifts; `Slope` solves (Laplacian - 1/L^2)^2 d = 0, matching slopes as
36 /// well. In a hole the shift spans the hole; beyond a source's edge it fades out over L and is 0
37 /// from `kSeamReachDecays` L on.
38 6 struct ChainSeams {
39 ChainSeamMode mode = ChainSeamMode::None;
40 /// Width of `Feather`'s band, in the sources' units (meters on a plane, degrees on a body);
41 /// positive and finite.
42 double width = 50.0;
43 /// Decay length L of `Height` and `Slope`, in the sources' units; positive and finite.
44 double decayLength = 50.0;
45
46 /// Memberwise equality.
47 6 bool operator==(const ChainSeams& other) const {
48
6/12
<unknown function 38>:
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
<unknown function 47>:
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
6 return mode == other.mode && width == other.width && decayLength == other.decayLength;
49 }
50 bool operator!=(const ChainSeams& other) const {
51 return !(*this == other);
52 }
53 };
54
55 /// Decay lengths from a gap's edge beyond which `Height` and `Slope` add nothing.
56 inline constexpr double kSeamReachDecays = 3.0;
57
58 /// Checks seam options.
59 /// @throws std::invalid_argument for a `width` or `decayLength` that is not positive and finite.
60 void ValidateChainSeams(const ChainSeams& seams);
61
62 /// The distance from a gap's edge in the sources' units over which `seams` change the later
63 /// source: `width` for `Feather`, `kSeamReachDecays` times `decayLength` for `Height` and `Slope`,
64 /// 0 for `None`.
65 double SeamReach(const ChainSeams& seams);
66
67 /// The lattice a seam at the edge of a source covering `grid` is solved on: `grid`'s posts,
68 /// widened on every side by enough posts to cover `SeamReach` and 3 more.
69 /// @throws std::invalid_argument as `ValidateChainSeams`, for a grid whose cells are not of a
70 /// positive and finite size, or for a lattice more than 2^31 - 1 posts a side.
71 RasterGrid SeamLattice(const RasterGrid& grid, const ChainSeams& seams);
72
73 /// Sources tried in order, each filling where those before it have no data, such as a site
74 /// DEM over a regional one or a DEM over a flat datum. The sources must share coordinates; the
75 /// chain takes its grid and reference point from the first.
76 ///
77 /// With seams (`ChainSeams`) each source is matched to the chain of those before it where it
78 /// fills their gaps. The match is solved once, on first use, on the posts of the source before it
79 /// (`SeamLattice`, at the cell size of its grid), and read between the posts by Catmull-Rom
80 /// interpolation, so points, tiles and grids still agree. A source without bounds gives no lattice
81 /// and the source after it is not matched. On a body the lattice's degrees of longitude count as
82 /// degrees of latitude. No-data inside the later source stays no-data.
83 class SourceChain final : public Source {
84 public:
85 /// The chain of `sources`, first first, joined as `seams` says.
86 /// @throws std::invalid_argument for no sources or a null one, as `ValidateChainSeams`, or as
87 /// `SeamLattice` for a seam at the edge of a source with bounds.
88 explicit SourceChain(std::vector<std::shared_ptr<const Source>> sources, ChainSeams seams = {});
89
90 ~SourceChain() override;
91
92 /// The first source's grid.
93 15 RasterGrid Grid() const override {
94
1/1
✓ Branch 1 taken 1 time.
15 return m_sources.front()->Grid();
95 }
96
97 /// The first source's reference point.
98 1 Vec2d ReferencePoint() const override {
99
1/1
✓ Branch 1 taken 1 time.
1 return m_sources.front()->ReferencePoint();
100 }
101
102 /// The first source's projection.
103 15 GeoKeys Keys() const override {
104 15 return m_sources.front()->Keys();
105 }
106
107 /// The resolution of the first source with a height at (`x`, `y`) at its coarsest, an
108 /// infinite spacing; infinity if no source has one.
109 double ResolutionAt(double x, double y) const override;
110
111 /// The post spacing of the first source with a height at (`x`, `y`) at its coarsest, an
112 /// infinite spacing; infinity if no source has one.
113 double PostSpacingAt(double x, double y) const override;
114
115 /// The sources in the order they are tried.
116 const std::vector<std::shared_ptr<const Source>>& Sources() const {
117
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
3 return m_sources;
118 }
119
120 /// How each source is joined to the gaps of those before it.
121 5 const ChainSeams& Seams() const {
122
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 time.
5 return m_seams;
123 }
124
125 /// A chain of the same seams whose source n is reseeded with
126 /// `JoinWords(ReseedWord(seed, 2 n), ReseedWord(seed, 2 n + 1))` (`Source::Reseeded`), keeping
127 /// the sources without seeds; null when no source has seeds.
128 std::shared_ptr<const Source> Reseeded(std::uint64_t seed) const override;
129
130 protected:
131 /// Fills from the sources before the last, then each sample still without data from the
132 /// last, matched by the seams; a grid keeps the first source's no-data value. Each source
133 /// upsamples its own posts.
134 void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const override;
135
136 private:
137 /// The correction added to the last source, solved on first use.
138 const detail::SeamField& Seam() const;
139 /// Fills the cells of `heights` without data from the last source, filled as a grid.
140 void FillGridGaps(Raster<float>& heights, double spacing, unsigned threads) const;
141 /// Fills the `heights` without data at `points` from the last source, at those points.
142 void FillPointGaps(Span<const Vec2d> points, Span<float> heights, double spacing) const;
143
144 std::vector<std::shared_ptr<const Source>> m_sources;
145 ChainSeams m_seams;
146 /// The chain of every source but the last, which the last fills and is matched to; null with
147 /// one source.
148 std::shared_ptr<const Source> m_prefix;
149 mutable std::once_flag m_seamed;
150 mutable std::shared_ptr<const detail::SeamField> m_seam;
151 };
152
153 } // namespace terraingen
154