./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
111 / 113
98.2%
Branches:
116 / 127
91.3%
Functions:
18 / 18
100.0%

src/config/sources.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "config/sources.h"
4
5 #include "terraingen/sources/shape_model_source.h"
6 #include "terraingen/sources/upsampling.h"
7 #include "util/overloaded.h"
8
9 #include <algorithm>
10 #include <utility>
11
12 namespace terraingen::config {
13
14 namespace {
15
16 /// `seams` with their lengths, given in meters, in `surface`'s units.
17 19 ChainSeams SurfaceSeams(ChainSeams seams, const Surface& surface) {
18
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 18 times.
19 seams.width /= surface.UnitLength();
19
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 18 times.
19 seams.decayLength /= surface.UnitLength();
20 19 return seams;
21 }
22
23 /// The upsampler a spec names, its lengths in the surface's units: a kernel, diamond-square or a
24 /// type registered with `RegisterUpsampler`.
25 /// @throws ConfigError at `path` for an unknown type or settings its type rejects.
26 109 std::shared_ptr<const Upsampler> BuildUpsampler(const UpsamplingSpec& spec, const Surface& surface,
27 const std::string& path) {
28
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 21 times.
109 if (const std::optional<ResampleMethod> method = KernelUpsamplerMethod(spec.type)) {
29 88 return std::make_shared<KernelUpsampler>(*method);
30 }
31
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 12 times.
21 if (spec.type == "diamond_square") {
32 9 DiamondSquareOptions options = spec.diamondSquare;
33
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 options.minPixelSize /= surface.UnitLength();
34 18 return At(path, [&]() -> std::shared_ptr<const Upsampler> {
35 9 ValidateDiamondSquareOptions(options);
36 8 return std::make_shared<DiamondSquareUpsampler>(options);
37 9 });
38 }
39
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 5 times.
12 if (IsUpsamplerRegistered(spec.type)) {
40 14 return At(path, [&] {
41
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 7 times.
14 return MakeRegisteredUpsampler(spec.type, spec.settings.empty() ? "{}" : spec.settings);
42 7 });
43 }
44
1/1
✓ Branch 1 taken 5 times.
5 std::string expected;
45
3/3
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 5 times.
35 for (const std::string& name : KernelUpsamplerNames()) {
46
1/1
✓ Branch 1 taken 30 times.
65 expected += name + ", ";
47 }
48
1/1
✓ Branch 1 taken 5 times.
5 expected += "diamond_square";
49
3/3
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 5 times.
7 for (const std::string& name : RegisteredUpsamplerNames()) {
50
1/1
✓ Branch 1 taken 2 times.
4 expected += ", " + name;
51 5 }
52
1/1
✓ Branch 1 taken 5 times.
10 throw ConfigError(path, "unknown upsampling type \"" + spec.type +
53
3/3
✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
✓ Branch 8 taken 5 times.
20 "\"; expected one of: " + expected);
54 5 }
55
56 /// A raster source: a path to a readable GeoTIFF with the band, in coordinates that suit the
57 /// surface, over a grid of a size a configuration may request.
58 103 std::shared_ptr<const Source> BuildRaster(const RasterSourceSpec& raster,
59 const std::filesystem::path& directory,
60 const std::string& path, const Surface& surface) {
61 103 GeoTiffSourceOptions read = raster.read;
62
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 102 times.
103 if (read.upsampler) {
63
1/1
✓ Branch 1 taken 1 time.
2 throw ConfigError(FieldPath(path, "upsampling"),
64
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
3 "gives the raster's upsampler; leave read.upsampler unset");
65 }
66
3/3
✓ Branch 1 taken 102 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 14 times.
126 const std::string filePath = FieldPath(path, "path");
67
1/1
✓ Branch 1 taken 100 times.
102 CheckNonEmpty(raster.path, filePath);
68
1/1
✓ Branch 1 taken 100 times.
100 const std::filesystem::path file = Resolve(raster.path, directory);
69
2/2
✓ Branch 1 taken 96 times.
✓ Branch 4 taken 96 times.
200 const GeoTiffInfo info = AtFile(filePath, [&] { return ReadGeoTiffInfo(file); });
70
2/2
✓ Branch 1 taken 96 times.
✓ Branch 4 taken 95 times.
97 CheckBand(read.band, info, FieldPath(path, "band"));
71
5/5
✓ Branch 1 taken 95 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 92 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 2 times.
95 if (info.geoKeys.IsGeographic() && !surface.IsBody()) {
72 1 throw ConfigError(filePath,
73 "the raster's CRS is geographic (degrees); craters are sized in "
74 "meters, so reproject it first, for example with gdalwarp -t_srs, "
75
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 "or set the body to a sphere");
76 }
77
6/7
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1 time.
94 if (surface.IsBody() && !info.geoKeys.Empty() && !info.geoKeys.IsGeographic()) {
78 1 throw ConfigError(
79
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 filePath, "on a body the raster must be in longitude and latitude (a geographic CRS)");
80 }
81
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 92 times.
93 if (read.effectivePixelSize) {
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 *read.effectivePixelSize /= surface.UnitLength();
83 }
84
3/4
✓ Branch 1 taken 93 times.
✓ Branch 4 taken 88 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 88 times.
186 read.upsampler = BuildUpsampler(raster.upsampling, surface, FieldPath(path, "upsampling"));
85
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 75 times.
✓ Branch 3 taken 13 times.
✓ Branch 6 taken 75 times.
105 const std::string gridPath = read.crop ? FieldPath(path, "crop") : filePath;
86 88 std::shared_ptr<const Source> source =
87
2/2
✓ Branch 1 taken 81 times.
✓ Branch 4 taken 81 times.
178 At(gridPath, [&] { return std::make_shared<GeoTiffSource>(file, read); });
88
1/1
✓ Branch 1 taken 81 times.
81 const RasterGrid grid = source->Grid();
89
1/1
✓ Branch 1 taken 79 times.
81 CheckGridSize(grid.width, grid.height, gridPath);
90 79 return source;
91
1/2
✓ Branch 3 taken 79 times.
✗ Branch 4 not taken.
371 }
92
93 /// A chain of sources, each checked and built in turn, with a seam lattice of a size a
94 /// configuration may request after each source with bounds but the last.
95 19 std::shared_ptr<const Source> BuildChain(const ChainSourceSpec& chain,
96 const std::filesystem::path& directory,
97 const std::string& path, const Surface& surface) {
98 19 const std::string sources = FieldPath(path, "sources");
99 19 const ChainSeams seams = SurfaceSeams(chain.seams, surface);
100 19 const std::string reachPath =
101 ✗ FieldPath(FieldPath(path, "seams"),
102
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 19 times.
✓ Branch 6 taken 19 times.
42 seams.mode == ChainSeamMode::Feather ? "width_m" : "decay_length_m");
103 19 std::vector<std::shared_ptr<const Source>> members;
104
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 13 times.
46 for (std::size_t n = 0; n < chain.sources.size(); ++n) {
105
3/4
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 29 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 29 times.
66 members.push_back(BuildSource(chain.sources[n], directory, ItemPath(sources, n), surface));
106
1/1
✓ Branch 1 taken 29 times.
29 const std::optional<RasterGrid> grid = BoundedGrid(*members.back());
107
5/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
29 if (grid && seams.mode != ChainSeamMode::None && n + 1 < chain.sources.size()) {
108
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
14 const RasterGrid lattice = At(reachPath, [&] { return SeamLattice(*grid, seams); });
109
1/1
✓ Branch 1 taken 5 times.
7 CheckGridSize(lattice.width, lattice.height, reachPath);
110 }
111 }
112
1/1
✓ Branch 1 taken 13 times.
26 return At(path, [&] { return std::make_shared<SourceChain>(std::move(members), seams); });
113 25 }
114
115 /// Synthetic noise: its stack, and upsampling only with a pixel size.
116 370 std::shared_ptr<const Source> BuildSynthetic(const SyntheticSourceSpec& synthetic,
117 const std::string& path, const Surface& surface) {
118
1/1
✓ Branch 2 taken 370 times.
370 CheckNoiseStack(synthetic.stack, FieldPath(path, "stack"));
119 370 std::shared_ptr<const Upsampler> upsampler;
120
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 362 times.
370 if (synthetic.upsampling) {
121
1/1
✓ Branch 1 taken 8 times.
8 const std::string at = FieldPath(path, "upsampling");
122
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (!synthetic.pixelSize) {
123
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
4 throw ConfigError(at, "needs pixel_size_m: only noise sampled at posts is upsampled");
124 }
125
2/3
✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
13 upsampler = BuildUpsampler(*synthetic.upsampling, surface, at);
126 8 }
127
4/5
✓ Branch 1 taken 367 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 362 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
370 return At(path, [&] {
128 ✗ return std::make_shared<SyntheticSource>(StackFractals(synthetic.stack),
129 734 static_cast<float>(synthetic.elevation),
130 734 synthetic.pixelSize, upsampler, surface);
131
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 362 times.
367 });
132 367 }
133
134 /// A shape model body's own labels, refined by the spec's upsampler.
135 10 std::shared_ptr<const Source> BuildShapeModel(const ShapeModelSourceSpec& shape,
136 const std::string& path, const Surface& surface) {
137 10 const std::shared_ptr<const Upsampler> upsampler =
138
1/1
✓ Branch 2 taken 10 times.
10 BuildUpsampler(shape.upsampling, surface, FieldPath(path, "upsampling"));
139
3/5
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
20 return At(path, [&] { return std::make_shared<ShapeModelSource>(surface, upsampler); });
140 8 }
141
142 } // namespace
143
144 502 std::shared_ptr<const Source> BuildSource(const SourceSpec& spec,
145 const std::filesystem::path& directory,
146 const std::string& path, const Surface& surface) {
147 502 using Result = std::shared_ptr<const Source>;
148 969 return std::visit(Overloaded{
149 103 [&](const RasterSourceSpec& s) -> Result {
150 103 return BuildRaster(s, directory, path, surface);
151 },
152 19 [&](const ChainSourceSpec& s) -> Result {
153 19 return BuildChain(s, directory, path, surface);
154 },
155 370 [&](const SyntheticSourceSpec& s) -> Result {
156 370 return BuildSynthetic(s, path, surface);
157 },
158 10 [&](const ShapeModelSourceSpec& s) -> Result {
159 10 return BuildShapeModel(s, path, surface);
160 },
161 },
162 502 spec);
163 }
164
165 369 bool HasPosts(const SourceSpec& source) {
166 369 return std::visit(Overloaded{
167 [](const RasterSourceSpec&) { return true; },
168 [](const ShapeModelSourceSpec&) { return true; },
169 19 [](const ChainSourceSpec& s) {
170
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 return !s.sources.empty() && HasPosts(s.sources.front());
171 },
172 242 [](const SyntheticSourceSpec& s) { return s.pixelSize.has_value(); },
173 },
174 369 source);
175 }
176
177 333 bool HasMeasuredData(const SourceSpec& source) {
178 333 return std::visit(Overloaded{
179 [](const RasterSourceSpec&) { return true; },
180 [](const SyntheticSourceSpec&) { return false; },
181 [](const ShapeModelSourceSpec&) { return true; },
182 13 [](const ChainSourceSpec& s) {
183 13 return std::any_of(s.sources.begin(), s.sources.end(),
184 13 HasMeasuredData);
185 },
186 },
187 333 source);
188 }
189
190 } // namespace terraingen::config
191