./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
49 / 51
96.1%
Branches:
55 / 149
36.9%
Functions:
5 / 6, 4 excl
83.3%

include/terraingen/sources/upsampling.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Upsamplers: how a source's posts become heights at any point and sample spacing.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/raster/cell_loops.h"
9 #include "terraingen/raster/geo_transform.h"
10 #include "terraingen/raster/grid.h"
11 #include "terraingen/raster/raster.h"
12 #include "terraingen/raster/resample.h"
13 #include "terraingen/sources/sample_block.h"
14 #include "terraingen/util/span.h"
15
16 #include <cstddef>
17 #include <cstdint>
18 #include <functional>
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <vector>
23
24 namespace terraingen {
25
26 /// The posts of a source: heights on a regular lattice, such as a DEM's cells or noise sampled at
27 /// a fixed spacing. Implementations must be thread-safe.
28
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 1 time.
452 class PostLattice {
29 public:
30 300 virtual ~PostLattice() = default;
31
32 /// Where the posts lie: post (col, row) is at the center of cell (col, row) of a grid with
33 /// this transform.
34 virtual GeoTransform Transform() const = 0;
35
36 /// The posts held, which an upsampler may estimate settings from; none for a lattice without
37 /// bounds, which holds a post everywhere.
38 2 virtual std::optional<PixelWindow> Extent() const {
39 2 return std::nullopt;
40 }
41
42 /// Reads posts [`col0`, `col0` + `width`) x [`row0`, `row0` + `height`) row by row into
43 /// `heights`, of `width` x `height` values, NaN where a post has no data. Posts beyond a
44 /// bounded lattice repeat its nearest edge post.
45 virtual void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height,
46 Span<float> heights) const = 0;
47 };
48
49 /// Where a raster that is a window of a larger one, such as a window read from a file, lies in
50 /// it.
51 struct RasterPlacement {
52 RasterGrid whole{}; ///< The larger raster's grid.
53 std::size_t col = 0; ///< The window's first column in it.
54 std::size_t row = 0; ///< The window's first row in it.
55
56 /// A raster placed in itself.
57 139 static RasterPlacement Whole(const Raster<float>& raster) {
58
5/10
<unknown function 28>:
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
terraingen::PostLattice::Extent() const:
✓ Branch 0 taken 1 time.
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
139 return {raster.Grid(), 0, 0};
59 }
60 };
61
62 /// The posts of a raster that is a window of a larger lattice, such as a window read from a
63 /// file: posts beyond the larger lattice repeat its edge, and those beyond the window its edge, so
64 /// reads within the window give the same values as the whole.
65
22/95
<unknown function 28>:
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
<unknown function 30>:
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
terraingen::PostLattice::Extent() const:
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 19 taken 1 time.
✓ Branch 20 taken 1 time.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 21 taken 1 time.
✗ Branch 22 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 time.
✗ Branch 28 not taken.
✓ Branch 28 taken 1 time.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 31 not taken.
✓ Branch 32 taken 1 time.
✗ Branch 33 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 1 time.
✗ Branch 38 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✓ Branch 45 taken 1 time.
✗ Branch 46 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✓ Branch 54 taken 1 time.
✗ Branch 55 not taken.
✓ Branch 56 taken 1 time.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✓ Branch 67 taken 1 time.
✗ Branch 68 not taken.
✓ Branch 69 taken 1 time.
✗ Branch 70 not taken.
✓ Branch 71 taken 1 time.
✗ Branch 72 not taken.
✗ Branch 74 not taken.
✗ Branch 75 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
156 class RasterPosts final : public PostLattice {
66 public:
67 /// @param window A non-empty, unrotated raster; its no-data cells read as NaN.
68 /// @param placement Where `window` lies in the whole lattice.
69 /// @param wrapColumns Whether columns repeat with the lattice's width, as longitudes around a
70 /// body do, rather than repeating the edge posts; `window` then spans every column.
71 /// @throws std::invalid_argument for a null or empty window, or a wrapping one that does not
72 /// span every column.
73 RasterPosts(std::shared_ptr<const Raster<float>> window, const RasterPlacement& placement,
74 bool wrapColumns = false);
75
76 /// The whole lattice's transform.
77 186824 GeoTransform Transform() const override {
78
1/1
✓ Branch 1 taken 1 time.
186824 return m_placement.whole.transform;
79 }
80
81 /// The window's cells in the whole lattice.
82 std::optional<PixelWindow> Extent() const override;
83
84 void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height,
85 Span<float> heights) const override;
86
87 private:
88 std::shared_ptr<const Raster<float>> m_window;
89 RasterPlacement m_placement;
90 bool m_wrapColumns = false;
91 };
92
93 /// The spacing of a lattice's posts: the longer side of a cell of `transform`, in its units.
94 double PostSpacing(const GeoTransform& transform);
95
96 /// Whether `spacing` is no finer than the posts of a lattice placed by `transform`, to within
97 /// rounding, measuring a post's cell as grids measure theirs (`RasterGrid::PixelSize`).
98 bool AtPostSpacing(const GeoTransform& transform, double spacing);
99
100 /// How a source's posts become heights at any point and at any sample spacing in the posts'
101 /// units: interpolation, or refinement that adds detail as the spacing shrinks. Heights must
102 /// depend only on the posts, the position and the spacing, and implementations must be
103 /// thread-safe.
104 ///
105 /// A post keeps its own height at the posts' spacing or coarser, so an upsampler only decides
106 /// heights off the posts and below their spacing, in `FillSamples`, the one method it evaluates.
107
5/5
<unknown function 107>:
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 25 times.
terraingen::PostLattice::Extent() const:
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
372 class Upsampler {
108 public:
109
11/18
✓ Branch 0 taken 1 time.
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✓ Branch 14 taken 1 time.
✗ Branch 16 not taken.
✓ Branch 17 taken 1 time.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 time.
✗ Branch 28 not taken.
✓ Branch 29 taken 1 time.
✗ Branch 30 not taken.
✓ Branch 31 taken 1 time.
318 virtual ~Upsampler() = default;
110
111 /// Sets every sample of `samples` to its height from `posts` at a sample spacing of
112 /// `spacing`, NaN where it has none: a post's own where the sample is centered on one and the
113 /// spacing is no finer than the posts (`AtPostSpacing`), otherwise `FillSamples`'. A grid on
114 /// the posts' lattice at their spacing or coarser copies them.
115 /// @param threads Threads to fill a grid over; 0 for every hardware thread.
116 void Fill(const PostLattice& posts, SampleBlock& samples, double spacing,
117 unsigned threads) const;
118
119 /// `Fill` of every cell of `heights`, placed by its transform.
120 /// @param threads Threads to fill over; 0 for every hardware thread.
121 void Fill(const PostLattice& posts, Raster<float>& heights, double spacing,
122 unsigned threads) const;
123
124 /// The height `Fill` gives a cell centered at world (`x`, `y`), to the same bits.
125 float HeightAt(const PostLattice& posts, double x, double y, double spacing) const;
126
127 /// Sets each of `heights` to `HeightAt` the world point of `points` at its index, to the same
128 /// bits, in one `Fill` on one thread.
129 /// @throws std::invalid_argument for spans of different sizes.
130 void HeightsAt(const PostLattice& posts, Span<const Vec2d> points, Span<float> heights,
131 double spacing) const;
132
133 /// Posts either side of a point that `FillSamples` reads, so that a source reading a window
134 /// of its posts reads enough around it: 2 by default.
135 ✗ virtual int Reach() const {
136 ✗ return 2;
137 }
138
139 /// An upsampler for `posts` with any settings this one leaves to be estimated from them filled
140 /// in, such as diamond-square's roughness; null when this one serves as it is, the default.
141 333 virtual std::shared_ptr<const Upsampler> FitTo(const PostLattice& /*posts*/) const {
142 333 return nullptr;
143 }
144
145 /// A copy of the upsampler whose seeds draw from `seed` (`ReseedWord`); null for one without
146 /// seeds, the default, which a reseeded source keeps.
147 2 virtual std::shared_ptr<const Upsampler> Reseeded(std::uint64_t /*seed*/) const {
148 2 return nullptr;
149 }
150
151 protected:
152 /// Sets every sample of `samples` to the height from `posts` there at a sample spacing of
153 /// `spacing`, NaN where it has none, the same bits for a grid's cell as for a point at its
154 /// center.
155 /// @param threads Threads to split a grid over; 0 for every hardware thread.
156 virtual void FillSamples(const PostLattice& posts, SampleBlock& samples, double spacing,
157 unsigned threads) const = 0;
158 };
159
160 /// Interpolation of the posts with a kernel (`ResampleMethod`), at every spacing alike: `Cubic`,
161 /// the default, gives the same bits as `SampleRaster` of the posts.
162
2/10
terraingen::PostLattice::Extent() const:
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
terraingen::Upsampler::Reseeded(unsigned long) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
225 class KernelUpsampler final : public Upsampler {
163 public:
164
6/7
terraingen::PostLattice::Extent() const:
✓ Branch 1 taken 1 time.
terraingen::Upsampler::Reseeded(unsigned long) const:
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 17 taken 5 times.
226 explicit KernelUpsampler(ResampleMethod method = ResampleMethod::Cubic) : m_method(method) {}
165
166 /// The kernel's radius (`KernelRadius`).
167 94 int Reach() const override {
168
1/1
✓ Branch 1 taken 5 times.
94 return KernelRadius(m_method);
169 }
170
171 protected:
172 /// The posts interpolated at each sample as `SampleRaster` interpolates a raster, NaN where a
173 /// post weighed holds no data. Points close together read the posts once,
174 /// reusing a row's kernel weights in the rows below that share them.
175 /// @throws std::invalid_argument for a rotated lattice.
176 void FillSamples(const PostLattice& posts, SampleBlock& samples, double spacing,
177 unsigned threads) const override;
178
179 private:
180 ResampleMethod m_method;
181 };
182
183 /// The upsampler sources use unless given one: cubic interpolation, shared.
184 std::shared_ptr<const Upsampler> DefaultUpsampler();
185
186 /// `upsampler`, or `DefaultUpsampler()` when null, fitted to `posts` (`Upsampler::FitTo`).
187 /// @throws std::invalid_argument as `Upsampler::FitTo`.
188 std::shared_ptr<const Upsampler> FittedUpsampler(std::shared_ptr<const Upsampler> upsampler,
189 const PostLattice& posts);
190
191 /// The built-in kernel upsamplers' names, as configurations give them: nearest, bilinear, cubic,
192 /// catmull_rom, lanczos3 and cubic_bspline.
193 const std::vector<std::string>& KernelUpsamplerNames();
194
195 /// The kernel a built-in kernel upsampler's name gives, or none for another name.
196 std::optional<ResampleMethod> KernelUpsamplerMethod(const std::string& name);
197
198 /// Makes an upsampler from its settings: a JSON object as text, "{}" without any.
199 using UpsamplerFactory =
200 std::function<std::shared_ptr<const Upsampler>(const std::string& settings)>;
201
202 /// Registers an upsampler type of your own under `name`, replacing any registered under it
203 /// before, so that configurations can name it as `"upsampling": "name"` or
204 /// `{"type": "name", ...settings}`.
205 /// @throws std::invalid_argument for an empty name, a built-in's name (the kernel names and
206 /// "diamond_square"), or an empty factory.
207 void RegisterUpsampler(const std::string& name, UpsamplerFactory factory);
208
209 /// Whether an upsampler type is registered under `name` (`RegisterUpsampler`).
210 bool IsUpsamplerRegistered(const std::string& name);
211
212 /// The names upsampler types are registered under, sorted.
213 std::vector<std::string> RegisteredUpsamplerNames();
214
215 /// The upsampler the type registered under `name` makes with `settings`.
216 /// @throws std::invalid_argument for a name not registered, or a factory that gives null.
217 std::shared_ptr<const Upsampler> MakeRegisteredUpsampler(const std::string& name,
218 const std::string& settings = "{}");
219
220 } // namespace terraingen
221