./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
27 / 27
100.0%
Branches:
83 / 162
51.2%
Functions:
4 / 4, 3 excl
100.0%

include/terraingen/sources/raster_source.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Heights from a raster held in memory.
5 #pragma once
6
7 #include "terraingen/raster/cell_loops.h"
8 #include "terraingen/raster/geotiff/geotiff.h"
9 #include "terraingen/raster/resample.h"
10 #include "terraingen/sources/source.h"
11 #include "terraingen/sources/upsampling.h"
12
13 #include <memory>
14 #include <optional>
15 #include <utility>
16
17 namespace terraingen {
18
19 /// How a `RasterSource` reads its raster.
20
44/123
✓ Branch 0 taken 69 times.
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 102 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
✗ Branch 3 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 4 taken 114 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 5 taken 45 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 4 times.
✓ Branch 7 taken 154 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✓ Branch 9 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 10 not taken.
✗ Branch 10 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 11 taken 7 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 14 taken 2 times.
✗ Branch 15 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 taken 1 time.
✗ Branch 20 not taken.
✓ Branch 21 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 time.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 time.
✓ Branch 24 taken 1 time.
✗ Branch 26 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 1 time.
✗ Branch 35 not taken.
✓ Branch 36 taken 1 time.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✓ Branch 43 taken 1 time.
✗ Branch 45 not taken.
✓ Branch 46 taken 1 time.
✗ Branch 48 not taken.
✓ Branch 49 taken 1 time.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 60 not taken.
✓ Branch 61 taken 4 times.
✗ Branch 62 not taken.
✓ Branch 63 taken 1 time.
✗ Branch 65 not taken.
✓ Branch 66 taken 1 time.
✗ Branch 68 not taken.
✓ Branch 69 taken 1 time.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 73 not taken.
✗ Branch 74 not taken.
✗ Branch 75 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
✓ Branch 78 taken 1 time.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✗ Branch 82 not taken.
✗ Branch 83 not taken.
✗ Branch 85 not taken.
✓ Branch 86 taken 1 time.
✓ Branch 88 taken 1 time.
✗ Branch 89 not taken.
✗ Branch 91 not taken.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✗ Branch 94 not taken.
✓ Branch 95 taken 4 times.
✓ Branch 96 taken 1 time.
✗ Branch 97 not taken.
✗ Branch 98 not taken.
✗ Branch 99 not taken.
✓ Branch 100 taken 1 time.
✗ Branch 102 not taken.
✗ Branch 103 not taken.
587 struct RasterSourceOptions {
21 /// The cell size in CRS units the heights resolve, for a DEM that was upsampled or smoothed
22 /// before it was saved: `ResolutionAt` gives it in place of the cells' size. Unset uses the
23 /// cells'.
24 std::optional<double> effectivePixelSize{};
25 /// How the cells become heights between them and at sample spacings finer than the cells;
26 /// null for cubic interpolation (`DefaultUpsampler`).
27 std::shared_ptr<const Upsampler> upsampler{};
28
29 /// Memberwise equality, the upsamplers compared by identity.
30 bool operator==(const RasterSourceOptions& other) const {
31 return effectivePixelSize == other.effectivePixelSize && upsampler == other.upsampler;
32 }
33 bool operator!=(const RasterSourceOptions& other) const {
34 return !(*this == other);
35 }
36 };
37
38 namespace detail {
39
40 class RasterHeights;
41
42 /// A source serving the upsampled heights of a window of a lattice: what `RasterSource`
43 /// and `GeoTiffSource` share.
44 10 class RasterHeightsSource : public Source {
45 public:
46 /// The lattice's cells covered.
47 476 RasterGrid Grid() const final {
48
24/24
<unknown function 20>:
✓ Branch 1 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 4 times.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 12 times.
<unknown function 47>:
✓ Branch 1 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 24 times.
✓ Branch 10 taken 24 times.
✓ Branch 13 taken 24 times.
✓ Branch 16 taken 24 times.
✓ Branch 19 taken 24 times.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
448 return Subgrid(m_lattice, m_cells);
49 }
50
51 /// The top-left corner of the whole lattice, with or without a window of it.
52 749 Vec2d ReferencePoint() const final {
53
6/6
<unknown function 20>:
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 4 times.
<unknown function 47>:
✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
749 return {m_lattice.transform.originX, m_lattice.transform.originY};
54 }
55
56 /// The projection given at construction.
57 267 GeoKeys Keys() const final {
58
5/5
<unknown function 20>:
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 4 times.
<unknown function 47>:
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
terraingen::detail::RasterHeightsSource::Keys() const:
✓ Branch 1 taken 122 times.
267 return m_keys;
59 }
60
61 /// The effective pixel size, or else the cell size in CRS units, the larger side of a cell;
62 /// infinity off the covered cells and where the cell under (`x`, `y`) holds no data.
63 /// @throws GeoTiffError if a file read for the heights cannot be read.
64 double ResolutionAt(double x, double y) const final;
65
66 /// The cell size in CRS units, the larger side of a cell; infinity off the covered cells and
67 /// where the cell under (`x`, `y`) holds no data.
68 /// @throws GeoTiffError if a file read for the heights cannot be read.
69 double PostSpacingAt(double x, double y) const final;
70
71 protected:
72 /// @param lattice The whole grid the heights lie on.
73 /// @param cells The window of `lattice` covered.
74 /// @param keys The projection outputs are written with.
75 285 RasterHeightsSource(const RasterGrid& lattice, const PixelWindow& cells, GeoKeys keys)
76 285 : m_lattice(lattice), m_cells(cells), m_keys(std::move(keys)) {}
77
78 /// The whole grid the heights lie on.
79 104 const RasterGrid& Lattice() const {
80
1/1
✓ Branch 1 taken 104 times.
104 return m_lattice;
81 }
82
83 /// The window of the lattice covered.
84 208 const PixelWindow& Cells() const {
85
3/3
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 38 times.
✓ Branch 3 taken 104 times.
208 return m_cells;
86 }
87
88 /// Copies cells on the lattice at its cell size or coarser, otherwise upsamples.
89 /// @throws std::invalid_argument for a rotated grid off the lattice.
90 /// @throws GeoTiffError if a file read for the heights cannot be read.
91 void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const final;
92
93 /// The heights served.
94 virtual const RasterHeights& Heights() const = 0;
95
96 private:
97 RasterGrid m_lattice;
98 PixelWindow m_cells;
99 GeoKeys m_keys;
100 };
101
102 } // namespace detail
103
104 /// Heights from a raster held in memory, in a projected CRS or the surface's coordinates.
105 ///
106 /// The heights are used as given, with the raster's no-data value. On the source's own lattice
107 /// at its cell size or coarser `Fill` copies cells exactly; elsewhere, and at finer spacings,
108 /// the upsampler gives each cell from the raster's cells, its posts. Cells beyond the raster are
109 /// no-data. With geographic `keys` and columns spanning 360 degrees the raster wraps in
110 /// longitude, so a file from 0 to 360 also serves -180 to 0.
111 class RasterSource final : public detail::RasterHeightsSource {
112 public:
113 /// The source of `heights`.
114 /// @param keys The projection outputs are written with.
115 /// @throws std::invalid_argument for an empty raster, invalid options, or as
116 /// `Upsampler::FitTo`.
117 explicit RasterSource(Raster<float> heights, const RasterSourceOptions& options = {},
118 GeoKeys keys = {});
119
120 /// The upsampler in use, fitted to the raster (`Upsampler::FitTo`).
121 const std::shared_ptr<const Upsampler>& UpsamplerInUse() const;
122
123 protected:
124 const detail::RasterHeights& Heights() const override;
125
126 private:
127 std::shared_ptr<const detail::RasterHeights> m_heights;
128 };
129
130 } // namespace terraingen
131