./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
105 / 105
100.0%
Branches:
53 / 62
85.5%
Functions:
17 / 17, 1 excl
100.0%

src/config/sources.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Where a configuration's heights come from: source specs, their fields and building them.
5 #pragma once
6
7 #include "config/common.h"
8
9 #include "terraingen/geometry/surface.h"
10 #include "terraingen/sources/diamond_square.h"
11 #include "terraingen/sources/geotiff_source.h"
12 #include "terraingen/sources/source.h"
13 #include "terraingen/sources/source_chain.h"
14 #include "terraingen/sources/sources_fields.h"
15 #include "terraingen/sources/synthetic_source.h"
16
17 #include <array>
18 #include <filesystem>
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <string_view>
23 #include <variant>
24 #include <vector>
25
26 namespace terraingen {
27
28 /// How a source's posts become heights between them and at sample spacings finer than them
29 /// (`Upsampler`): `type` names a kernel upsampler (`KernelUpsamplerNames`), "diamond_square", or a
30 /// type registered with `RegisterUpsampler`. In JSON a name alone, or an object with the "type"
31 /// and the type's settings.
32
10/15
✓ Branch 3 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 6 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 1 time.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 time.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 30 taken 1 time.
✗ Branch 50 not taken.
✓ Branch 51 taken 1 time.
378 struct UpsamplingSpec {
33 std::string type = "cubic";
34 DiamondSquareOptions diamondSquare{}; ///< The settings of "diamond_square"; meters.
35 /// A registered type's settings, a JSON object as text without the "type"; empty for none.
36 std::string settings{};
37 };
38
39 /// Source "raster": a heightmap read from a GeoTIFF, in its own projected coordinates
40 /// (`GeoTiffSource`), upsampled as `upsampling` says. Relative paths resolve against the
41 /// configuration's folder.
42 struct RasterSourceSpec {
43 std::string path;
44 /// How the band is read: band, scale, offset, crop and `effectivePixelSize` (meters); its
45 /// upsampler must stay unset, as `upsampling` gives it.
46 GeoTiffSourceOptions read{};
47 UpsamplingSpec upsampling{};
48 };
49
50 /// Source "synthetic": `elevation` plus a stack of noise layers (`SyntheticSource`), evaluated at
51 /// every point, or with `pixelSize` sampled at posts that `upsampling` refines (cubic unset).
52 struct SyntheticSourceSpec {
53 double elevation = 0.0; ///< Meters.
54 /// 0 to `kMaxFractals` layers, by default `DefaultBaseNoise`; an empty stack is flat.
55 std::vector<FractalSpec> stack{DefaultBaseNoise()};
56 std::optional<double> pixelSize{}; ///< Meters between posts; unset for none.
57 std::optional<UpsamplingSpec> upsampling{}; ///< Needs `pixelSize`.
58 };
59
60 /// Source "shape_model": a "shape_model" body's own labels refined by `upsampling`
61 /// (`ShapeModelSource`), the source a shape-model body has unless the configuration gives one.
62
3/4
✓ Branch 5 taken 25 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
442 struct ShapeModelSourceSpec {
63 UpsamplingSpec upsampling{};
64 };
65
66 struct ChainSourceSpec;
67
68 /// Where a terrain's heights come from, its source, named in JSON by its "type".
69 using SourceSpec =
70 std::variant<RasterSourceSpec, ChainSourceSpec, SyntheticSourceSpec, ShapeModelSourceSpec>;
71
72 /// Source "chain": `sources` tried in order, each filling where those before it have no data
73 /// (`SourceChain`), joined as `seams` say; its grid is the first source's.
74
9/12
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 51 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 15 taken 13 times.
✓ Branch 20 taken 8 times.
660 struct ChainSourceSpec {
75 std::vector<SourceSpec> sources;
76 ChainSeams seams{}; ///< `width` and `decayLength` in meters.
77 };
78
79 /// Checks the ranges of an upsampling: diamond-square's settings when it is that type.
80 132 inline void CheckValueRanges(UpsamplingSpec& value, const std::string& path,
81 std::string_view /*unit*/, const FieldRange& /*range*/) {
82
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 119 times.
132 if (value.type == "diamond_square") {
83 26 RangeChecker checker(path);
84
1/1
✓ Branch 1 taken 9 times.
13 Fields(checker, value.diamondSquare);
85 9 }
86 128 }
87
88 /// The description of a source's upsampling.
89 inline constexpr std::string_view kUpsamplingHelp =
90 "How the posts are refined between them and at finer spacings: a kernel name such as cubic "
91 "or lanczos3, or an object with a \"type\" and its settings, such as {\"type\": "
92 "\"diamond_square\"} for fractal detail.";
93
94 template <> struct SpecName<RasterSourceSpec> {
95 static constexpr std::string_view kName = "raster";
96 static constexpr std::string_view kDescription =
97 "A heightmap read from a GeoTIFF in its own projected coordinates, its cells upsampled to "
98 "finer spacings, to add relief to a real DEM.";
99 };
100 281 template <typename IO> void Fields(IO& io, RasterSourceSpec& source) {
101
4/4
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::RasterSourceSpec&):
✓ Branch 2 taken 120 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::RasterSourceSpec&):
✓ Branch 2 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::RasterSourceSpec&):
✓ Branch 2 taken 47 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::RasterSourceSpec&):
✓ Branch 2 taken 101 times.
401 io("path", source.path, config::InputPathHelp("GeoTIFF to read the heights from"), "",
102 281 Required());
103 281 GeoTiffReadFields(io, source.read);
104 275 io("upsampling", source.upsampling, kUpsamplingHelp);
105 265 }
106
107 template <> struct SpecName<ChainSourceSpec> {
108 static constexpr std::string_view kName = "chain";
109 static constexpr std::string_view kDescription =
110 "Several sources tried in order, each filling where those before it have no data; its "
111 "grid is the first one's.";
112 };
113
114 109 template <typename IO> void Fields(IO& io, ChainSourceSpec& source) {
115 109 io("sources", source.sources,
116 "Sources tried in order, each filling where those before it have no data, each upsampled "
117 "as it says; the chain's grid is the first one's.",
118 109 "", MinLength(1));
119 107 io("seams", source.seams,
120 "How each source is matched to the sources before it where it fills their gaps; by "
121 "default each switches outright.");
122 101 }
123
124 template <> struct SpecName<SyntheticSourceSpec> {
125 static constexpr std::string_view kName = "synthetic";
126 static constexpr std::string_view kDescription =
127 "Heights generated from a stack of fractal noise layers over an unbounded plane or the "
128 "whole body; outputs give their own extents and pixel sizes.";
129 };
130 445 template <typename IO> void Fields(IO& io, SyntheticSourceSpec& source) {
131 445 io("elevation_m", source.elevation, "Height in meters added everywhere.", "m");
132
4/4
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 341 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 32 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 58 times.
890 io("stack", source.stack,
133
4/4
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 342 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 32 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 58 times.
890 std::string(kNoiseStackHelp) +
134 " Unset is one layer of fBm noise 400 m in wavelength and 12 m in amplitude; an empty "
135 "stack gives a flat source.",
136 889 "", NoiseStackLength(0));
137 444 io("pixel_size_m", source.pixelSize,
138 "Spacing in meters of posts the noise is sampled at, as for a DEM of that cell size, "
139 "which upsampling refines. Unset evaluates the noise at every point, at any spacing.",
140 888 "m", Above(0.0));
141
4/4
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 339 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 32 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 58 times.
886 io("upsampling", source.upsampling,
142
4/4
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 340 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 32 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::SyntheticSourceSpec&):
✓ Branch 2 taken 58 times.
885 std::string(kUpsamplingHelp) + " Needs pixel_size_m; unset is cubic.");
143 442 }
144
145 template <> struct SpecName<ShapeModelSourceSpec> {
146 static constexpr std::string_view kName = "shape_model";
147 static constexpr std::string_view kDescription =
148 "The shape-model body's own labels refined by an upsampler; the default source on such a "
149 "body.";
150 };
151 50 template <typename IO> void Fields(IO& io, ShapeModelSourceSpec& source) {
152
8/8
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::ShapeModelSourceSpec&):
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 7 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::ShapeModelSourceSpec&):
✓ Branch 2 taken 13 times.
✓ Branch 5 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::ShapeModelSourceSpec&):
✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::ShapeModelSourceSpec&):
✓ Branch 2 taken 25 times.
✓ Branch 5 taken 25 times.
100 io("upsampling", source.upsampling, std::string(kUpsamplingHelp) + " Unset is cubic.");
153 50 }
154
155 namespace config {
156
157 /// The source a spec describes, its lengths in the surface's units, each part checked before it
158 /// is built: a raster's file and grid, a chain's sources and seams, a synthetic stack and every
159 /// upsampling. Its grid is `BoundedGrid` of it: a raster's, a chain's first source's, none for
160 /// synthetic noise or a shape model.
161 /// @param path The spec's JSON path, which errors are reported under.
162 /// @throws ConfigError at `path`, or below it, for what the checks or the sources reject.
163 std::shared_ptr<const Source> BuildSource(const SourceSpec& spec,
164 const std::filesystem::path& directory,
165 const std::string& path, const Surface& surface);
166
167 /// Whether a source has posts that layers can estimate settings from: a raster, a chain whose
168 /// first source has, a shape model, or synthetic noise with a pixel size.
169 bool HasPosts(const SourceSpec& source);
170
171 /// Whether a source holds measured data whose resolution layers read for what it already holds
172 /// (`sourceResolvesPixels`): a raster or a shape model, or a chain holding one.
173 bool HasMeasuredData(const SourceSpec& source);
174
175 } // namespace config
176
177 } // namespace terraingen
178