./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
144 / 144
100.0%
Branches:
28 / 35
80.0%
Functions:
24 / 24, 1 excl
100.0%

src/config/albedo.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief A configuration's albedo: its source and layers, their fields, checks and building.
5 #pragma once
6
7 #include "config/layers.h"
8
9 #include "terraingen/albedo/albedo.h"
10 #include "terraingen/albedo/albedo_fields.h"
11 #include "terraingen/albedo/synthetic.h"
12 #include "terraingen/pipeline/terrain.h"
13
14 #include <cstddef>
15 #include <filesystem>
16 #include <optional>
17 #include <string>
18 #include <string_view>
19 #include <variant>
20 #include <vector>
21
22 namespace terraingen {
23
24 /// Albedo source "synthetic": `SyntheticAlbedo`, a base color with a noise stack per channel (each
25 /// 0 to `kMaxFractals` layers), measured from the source's reference point.
26 struct SyntheticAlbedoSpec {
27 /// The color; the noise stacks below, each layer with its Hurst exponent, give its noise.
28 SyntheticAlbedoOptions options{};
29 std::vector<AlbedoFractalSpec> hueNoiseDegrees;
30 std::vector<AlbedoFractalSpec> saturationNoise;
31 std::vector<AlbedoFractalSpec> valueNoise;
32
33 /// The albedo options, with each noise layer's Hurst exponent applied.
34 SyntheticAlbedoOptions Options() const;
35 };
36
37 /// Albedo source "raster": `RasterAlbedo` from one or three bands of a GeoTIFF in the terrain's
38 /// projected coordinates, such as an orbital normal albedo map. Relative paths resolve against the
39 /// configuration's folder; samples are scaled as the file's GDAL metadata says, or by `scale` and
40 /// `offset` when given, for maps stored as integers.
41
8/13
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 16 not taken.
✓ Branch 17 taken 1 time.
✗ Branch 20 not taken.
✓ Branch 21 taken 1 time.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 time.
170 struct RasterAlbedoSpec {
42 std::string path;
43 std::vector<std::size_t> bands{0}; ///< One or three bands, from 0.
44 std::optional<double> scale{};
45 std::optional<double> offset{};
46 RasterAlbedoOptions options{};
47 };
48
49 /// An albedo source, named in JSON by its "type".
50 using AlbedoSourceSpec = std::variant<SyntheticAlbedoSpec, RasterAlbedoSpec>;
51
52 /// Albedo layer "craters": `CraterAlbedo`, bright ejecta and rays of the craters of one top-level
53 /// crater layer, or of every one.
54
3/4
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 47 times.
✓ Branch 9 taken 1 time.
✗ Branch 11 not taken.
1006 struct CraterAlbedoSpec {
55 /// Index of the top-level crater layer, from 0; unset for every crater layer.
56 std::optional<std::size_t> craterLayer;
57 CraterAlbedoOptions options{};
58 };
59
60 /// Albedo layer "blast_zones": `BlastZoneAlbedo`, the zones landers' engines scoured.
61
8/9
✓ Branch 3 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 26 times.
✓ Branch 4 taken 13 times.
✓ Branch 6 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 18 taken 8 times.
172 struct BlastZoneAlbedoSpec {
62 std::vector<BlastZone> zones;
63 };
64
65 /// Albedo layer "rocks": `RockAlbedo`, the rock of the terrain's boulder layers, each read on the
66 /// ground beneath it.
67 struct RockAlbedoSpec {
68 /// Index of the top-level boulder layer, from 0; unset for every top-level boulder layer.
69 std::optional<std::size_t> boulderLayer;
70 RockAlbedoOptions options{};
71 };
72
73 /// An albedo layer, named in JSON by its "type". "splotches" (`SplotchAlbedo`) and "slopes"
74 /// (`SlopeAlbedo`) hold their library options directly.
75 using AlbedoLayerSpec = std::variant<CraterAlbedoSpec, SplotchAlbedoOptions, BlastZoneAlbedoSpec,
76 RockAlbedoSpec, SlopeAlbedoOptions>;
77
78 /// The albedo of the terrain: a source and the layers that change it, evaluated over the
79 /// terrain's grid.
80
5/5
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 11 taken 6 times.
✓ Branch 12 taken 21 times.
✓ Branch 14 taken 1 time.
499 struct AlbedoSpec {
81 AlbedoSourceSpec source{};
82 std::vector<AlbedoLayerSpec> layers;
83 };
84
85 template <> struct SpecName<SyntheticAlbedoSpec> {
86 static constexpr std::string_view kName = "synthetic";
87 static constexpr std::string_view kDescription =
88 "A base color in hue, saturation and value, each varied by noise: an albedo for terrain "
89 "without data.";
90 };
91 164 template <typename IO> void Fields(IO& io, SyntheticAlbedoSpec& spec) {
92 164 SyntheticAlbedoColorFields(io, spec.options);
93 162 io("hue_noise_deg", spec.hueNoiseDegrees,
94 "Noise layers whose sum in degrees is added to the hue; used with rgb only.", "",
95 162 NoiseStackLength(0));
96 161 io("saturation_noise", spec.saturationNoise,
97 "Noise layers added to the saturation, which is then clamped to [0, 1]; used with rgb "
98 "only.",
99 161 "", NoiseStackLength(0));
100 160 io("value_noise", spec.valueNoise,
101 "Noise layers: the value is multiplied by 1 plus their sum and clamped to [0, 1], so their "
102 "amplitude sets the relative contrast.",
103 160 "", NoiseStackLength(0));
104 158 }
105
106 template <> struct SpecName<RasterAlbedoSpec> {
107 static constexpr std::string_view kName = "raster";
108 static constexpr std::string_view kDescription =
109 "Albedo read from one or three bands of a GeoTIFF in the terrain's projection, such as an "
110 "orbital albedo map.";
111 };
112 52 template <typename IO> void Fields(IO& io, RasterAlbedoSpec& spec) {
113
4/4
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::RasterAlbedoSpec&):
✓ Branch 2 taken 12 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::RasterAlbedoSpec&):
✓ Branch 1 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::RasterAlbedoSpec&):
✓ Branch 1 taken 2 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::RasterAlbedoSpec&):
✓ Branch 1 taken 25 times.
64 io("path", spec.path,
114 52 config::InputPathHelp("GeoTIFF albedo map in the terrain's projected coordinates"), "",
115 52 Required());
116 52 io("bands", spec.bands,
117 "Bands of the file to read, counting from 0: one for a single albedo band, or three for "
118 "red, green and blue.",
119 52 "", LengthEither(1, kMaxAlbedoBands));
120 51 io("scale", spec.scale,
121 "Factor each sample is multiplied by, replacing the band's scale from the file's GDAL "
122 "metadata, as for maps stored as integers. Unset keeps the file's.");
123 50 io("offset", spec.offset,
124 "Added to each scaled sample, replacing the band's offset from the file's GDAL metadata. "
125 "Unset keeps the file's.");
126 49 io("resampling", spec.options.method,
127 "Interpolation used to sample the map at the terrain's points.");
128 49 io("detail", spec.options.detail,
129 "Fractal detail below the map's resolution, continuing the map's own measured roughness "
130 "to finer scales.");
131 48 }
132
133 template <> struct SpecName<CraterAlbedoSpec> {
134 static constexpr std::string_view kName = "craters";
135 static constexpr std::string_view kDescription =
136 "Bright ejecta, interiors and rays of the craters of a crater layer, fading with crater "
137 "age.";
138 };
139 111 template <typename IO> void Fields(IO& io, CraterAlbedoSpec& spec) {
140 111 io("crater_layer", spec.craterLayer,
141 "Which top-level crater layer's craters to brighten, counting from 0; unset uses every "
142 "top-level crater layer.");
143 111 Fields(io, spec.options);
144 109 }
145
146 template <> struct SpecName<SplotchAlbedoOptions> {
147 static constexpr std::string_view kName = "splotches";
148 static constexpr std::string_view kDescription =
149 "Dark and bright splotches that secondary impacts of loose regolith leave over the whole "
150 "surface, after Speyerer et al. (2016).";
151 };
152
153 template <> struct SpecName<BlastZoneAlbedoSpec> {
154 static constexpr std::string_view kName = "blast_zones";
155 static constexpr std::string_view kDescription =
156 "Brighter zones of regolith around landers scoured by their descent engines, after Clegg "
157 "et al. (2014) and Clegg-Watkins et al. (2016).";
158 };
159 44 template <typename IO> void Fields(IO& io, BlastZoneAlbedoSpec& spec) {
160 44 io("zones", spec.zones, "One blast zone per lander.");
161 43 }
162
163 template <> struct SpecName<RockAlbedoSpec> {
164 static constexpr std::string_view kName = "rocks";
165 static constexpr std::string_view kDescription =
166 "Rock brighter than the regolith: the boulders of boulder layers where resolved, and a "
167 "speckle of the share of each sample covered by those too small to resolve.";
168 };
169 52 template <typename IO> void Fields(IO& io, RockAlbedoSpec& spec) {
170 52 io("boulder_layer", spec.boulderLayer,
171 "Which top-level boulder layer's rock to brighten, counting from 0; unset uses every "
172 "top-level boulder layer.");
173 52 Fields(io, spec.options);
174 51 }
175
176 template <> struct SpecName<SlopeAlbedoOptions> {
177 static constexpr std::string_view kName = "slopes";
178 static constexpr std::string_view kDescription =
179 "Steep ground kept immature and bright by regolith moving downslope, after Ikeda et al. "
180 "(2022) and Lucey et al. (2014).";
181 };
182
183 144 template <typename IO> void Fields(IO& io, AlbedoSpec& spec) {
184 144 io("source", spec.source,
185 "Base albedo of the terrain, before its layers: a synthetic color or a raster map.");
186 133 io("layers", spec.layers,
187 "Layers applied to the source's albedo in order, such as bright crater ejecta and rays.");
188 127 }
189
190 namespace config {
191
192 /// Checks an albedo against the layers it reads and its files.
193 /// @throws ConfigError naming the first invalid value under "albedo".
194 void CheckAlbedo(const AlbedoSpec& albedo, const std::vector<LayerSpec>& layers,
195 const std::filesystem::path& directory);
196
197 /// The albedo `albedo` describes for a terrain built from `layers`, already checked.
198 /// @throws ConfigError if the terrain's layers are not the ones `layers` build.
199 Albedo BuildValidAlbedo(const AlbedoSpec& albedo, const std::vector<LayerSpec>& layers,
200 const Terrain& terrain, const std::filesystem::path& directory);
201
202 } // namespace config
203
204 } // namespace terraingen
205