src/config/albedo.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/albedo.h" | ||
| 4 | |||
| 5 | #include "terraingen/albedo/blast_zones.h" | ||
| 6 | #include "terraingen/albedo/crater_albedo.h" | ||
| 7 | #include "terraingen/albedo/raster_albedo.h" | ||
| 8 | #include "terraingen/albedo/rock_albedo.h" | ||
| 9 | #include "terraingen/albedo/slope_albedo.h" | ||
| 10 | #include "terraingen/albedo/splotches.h" | ||
| 11 | #include "util/overloaded.h" | ||
| 12 | |||
| 13 | #include <utility> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// Each layer of `stack` with its Hurst exponent applied, appended to `layers`. | ||
| 20 | template <typename Layers> | ||
| 21 | 213 | void AppendEffective(const std::vector<AlbedoFractalSpec>& stack, Layers& layers) { | |
| 22 | 258 | for (const AlbedoFractalSpec& layer : stack) { | |
| 23 | 90 | layers.push_back(layer.Effective()); | |
| 24 | } | ||
| 25 | 213 | } | |
| 26 | |||
| 27 | } // namespace | ||
| 28 | |||
| 29 | 71 | SyntheticAlbedoOptions SyntheticAlbedoSpec::Options() const { | |
| 30 | 71 | SyntheticAlbedoOptions built = options; | |
| 31 | 71 | AppendEffective(hueNoiseDegrees, built.hueNoiseDegrees); | |
| 32 | 71 | AppendEffective(saturationNoise, built.saturationNoise); | |
| 33 | 71 | AppendEffective(valueNoise, built.valueNoise); | |
| 34 | 71 | return built; | |
| 35 | } | ||
| 36 | |||
| 37 | namespace config { | ||
| 38 | |||
| 39 | namespace { | ||
| 40 | |||
| 41 | /// The top-level crater and boulder layers albedo layers can read. | ||
| 42 | struct AlbedoChecks { | ||
| 43 | std::size_t craterLayers = 0; | ||
| 44 | std::size_t boulderLayers = 0; | ||
| 45 | }; | ||
| 46 | |||
| 47 | 50 | void CheckAlbedoSource(const SyntheticAlbedoSpec& spec, const std::filesystem::path& /*directory*/, | |
| 48 | const std::string& path) { | ||
| 49 | 50 | CheckNoiseStack(spec.hueNoiseDegrees, FieldPath(path, "hue_noise_deg")); | |
| 50 | 50 | CheckNoiseStack(spec.saturationNoise, FieldPath(path, "saturation_noise")); | |
| 51 | 50 | CheckNoiseStack(spec.valueNoise, FieldPath(path, "value_noise")); | |
| 52 | 100 | At(path, [&] { ValidateSyntheticAlbedoOptions(spec.Options()); }); | |
| 53 | 50 | } | |
| 54 | |||
| 55 | 7 | void CheckAlbedoSource(const RasterAlbedoSpec& spec, const std::filesystem::path& directory, | |
| 56 | const std::string& path) { | ||
| 57 | 7 | const std::string file = FieldPath(path, "path"); | |
| 58 | 7 | CheckNonEmpty(spec.path, file); | |
| 59 | 6 | const GeoTiffInfo info = | |
| 60 | 17 | AtFile(file, [&] { return ReadGeoTiffInfo(Resolve(spec.path, directory)); }); | |
| 61 | 9 | for (const std::size_t band : spec.bands) { | |
| 62 | 10 | CheckBand(band, info, FieldPath(path, "bands")); | |
| 63 | } | ||
| 64 | 4 | if (info.grid.transform.IsRotated()) { | |
| 65 | 2 | throw ConfigError(file, "rotated albedo maps are not supported"); | |
| 66 | } | ||
| 67 | 11 | At(FieldPath(path, "detail"), [&] { ValidateAlbedoDetail(spec.options.detail); }); | |
| 68 | 5 | } | |
| 69 | |||
| 70 | 31 | void CheckAlbedoLayer(const CraterAlbedoSpec& craters, const std::string& path, | |
| 71 | const AlbedoChecks& checks) { | ||
| 72 | 31 | if (checks.craterLayers == 0) { | |
| 73 | 2 | throw ConfigError(path, "crater albedo needs a crater layer"); | |
| 74 | } | ||
| 75 | 30 | if (craters.craterLayer && *craters.craterLayer >= checks.craterLayers) { | |
| 76 | 2 | throw ConfigError(FieldPath(path, "crater_layer"), | |
| 77 | 3 | "there are " + std::to_string(checks.craterLayers) + " crater layers"); | |
| 78 | } | ||
| 79 | 58 | At(path, [&] { ValidateCraterAlbedoOptions(craters.options); }); | |
| 80 | 29 | } | |
| 81 | |||
| 82 | 8 | void CheckAlbedoLayer(const RockAlbedoSpec& rocks, const std::string& path, | |
| 83 | const AlbedoChecks& checks) { | ||
| 84 | 8 | if (checks.boulderLayers == 0) { | |
| 85 | 2 | throw ConfigError(path, "rock albedo needs a top-level boulder layer"); | |
| 86 | } | ||
| 87 | 7 | if (rocks.boulderLayer && *rocks.boulderLayer >= checks.boulderLayers) { | |
| 88 | 2 | throw ConfigError(FieldPath(path, "boulder_layer"), | |
| 89 | 3 | "there are " + std::to_string(checks.boulderLayers) + " boulder layers"); | |
| 90 | } | ||
| 91 | 12 | At(path, [&] { ValidateRockAlbedoOptions(rocks.options); }); | |
| 92 | 6 | } | |
| 93 | |||
| 94 | 2 | void CheckAlbedoLayer(const SplotchAlbedoOptions& splotches, const std::string& path, | |
| 95 | const AlbedoChecks&) { | ||
| 96 | 4 | At(path, [&] { ValidateSplotchAlbedoOptions(splotches); }); | |
| 97 | 2 | } | |
| 98 | |||
| 99 | ✗ | void CheckAlbedoLayer(const BlastZoneAlbedoSpec& zones, const std::string& path, | |
| 100 | const AlbedoChecks&) { | ||
| 101 | ✗ | for (std::size_t z = 0; z < zones.zones.size(); ++z) { | |
| 102 | ✗ | At(ItemPath(FieldPath(path, "zones"), z), [&] { ValidateBlastZone(zones.zones[z]); }); | |
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | 2 | void CheckAlbedoLayer(const SlopeAlbedoOptions& slopes, const std::string& path, | |
| 107 | const AlbedoChecks&) { | ||
| 108 | 4 | At(path, [&] { ValidateSlopeAlbedoOptions(slopes); }); | |
| 109 | 2 | } | |
| 110 | |||
| 111 | /// The bands of a raster albedo map, scaled as its metadata or the spec says. | ||
| 112 | 2 | std::vector<Raster<float>> LoadAlbedoBands(const RasterAlbedoSpec& spec, | |
| 113 | const std::filesystem::path& directory) { | ||
| 114 | 2 | const std::filesystem::path path = Resolve(spec.path, directory); | |
| 115 | 2 | const GeoTiffInfo info = ReadGeoTiffInfo(path); | |
| 116 | 2 | std::vector<Raster<float>> bands; | |
| 117 | 4 | for (const std::size_t band : spec.bands) { | |
| 118 | 2 | Raster<float> raster = ReadGeoTiff(path, band).raster; | |
| 119 | 2 | const BandScaling scaling{spec.scale.value_or(info.bandScaling[band].scale), | |
| 120 | 2 | spec.offset.value_or(info.bandScaling[band].offset)}; | |
| 121 | 2 | if (!scaling.Identity()) { | |
| 122 | 81 | for (float& value : raster.Values()) { | |
| 123 | 80 | if (!raster.IsNoData(value)) { | |
| 124 | 80 | value = static_cast<float>(static_cast<double>(value) * scaling.scale + | |
| 125 | scaling.offset); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | 2 | bands.push_back(std::move(raster)); | |
| 130 | 2 | } | |
| 131 | 2 | return bands; | |
| 132 | 4 | } | |
| 133 | |||
| 134 | 21 | std::shared_ptr<const AlbedoSource> BuildAlbedoSource(const AlbedoSourceSpec& spec, | |
| 135 | const std::filesystem::path& directory) { | ||
| 136 | 21 | using Result = std::shared_ptr<const AlbedoSource>; | |
| 137 | 42 | return std::visit(Overloaded{ | |
| 138 | 19 | [&](const SyntheticAlbedoSpec& s) -> Result { | |
| 139 | 38 | return std::make_shared<SyntheticAlbedo>(s.Options()); | |
| 140 | }, | ||
| 141 | 2 | [&](const RasterAlbedoSpec& s) -> Result { | |
| 142 | 2 | return std::make_shared<RasterAlbedo>(LoadAlbedoBands(s, directory), | |
| 143 | 4 | s.options); | |
| 144 | }, | ||
| 145 | }, | ||
| 146 | 21 | spec); | |
| 147 | } | ||
| 148 | |||
| 149 | /// What albedo layers are built from: the terrain, and the layers it was built from. | ||
| 150 | struct AlbedoInputs { | ||
| 151 | const std::vector<LayerSpec>& layers; | ||
| 152 | const Terrain& terrain; | ||
| 153 | }; | ||
| 154 | |||
| 155 | /// The terrain's layer `index`, which the configuration built as a `T`. | ||
| 156 | /// @throws ConfigError if the terrain has no `T` there, as when it was changed after building. | ||
| 157 | template <typename T> | ||
| 158 | 20 | std::shared_ptr<const T> TerrainLayer(const AlbedoInputs& inputs, std::size_t index) { | |
| 159 | 20 | const std::vector<LayerPtr>& layers = inputs.terrain.Layers(); | |
| 160 | 2 | std::shared_ptr<const T> layer = | |
| 161 | 20 | index < layers.size() ? std::dynamic_pointer_cast<const T>(layers[index]) : nullptr; | |
| 162 | 20 | if (!layer) { | |
| 163 | 6 | throw ConfigError(ItemPath("layers", index), | |
| 164 | "the terrain's layer here is not the one the configuration builds; " | ||
| 165 | "pass the terrain BuildTerrain built from this configuration"); | ||
| 166 | } | ||
| 167 | 17 | return layer; | |
| 168 | 3 | } | |
| 169 | |||
| 170 | 14 | void AddAlbedoLayer(const CraterAlbedoSpec& craters, const AlbedoInputs& inputs, Albedo& albedo) { | |
| 171 | 14 | const std::vector<std::size_t> indices = LayerIndices<CraterLayerSpec>(inputs.layers); | |
| 172 | 30 | for (std::size_t i = 0; i < indices.size(); ++i) { | |
| 173 | 17 | if (!craters.craterLayer || *craters.craterLayer == i) { | |
| 174 | 14 | albedo.layers.push_back(std::make_shared<CraterAlbedo>( | |
| 175 | 15 | TerrainLayer<CraterLayer>(inputs, indices[i]), craters.options)); | |
| 176 | } | ||
| 177 | } | ||
| 178 | 14 | } | |
| 179 | |||
| 180 | 4 | void AddAlbedoLayer(const RockAlbedoSpec& rocks, const AlbedoInputs& inputs, Albedo& albedo) { | |
| 181 | 4 | std::vector<RockSource> sources; | |
| 182 | 7 | for (const std::size_t i : LayerIndices<BoulderLayerSpec>(inputs.layers)) { | |
| 183 | 3 | sources.push_back({TerrainLayer<BoulderLayer>(inputs, i), | |
| 184 | 9 | std::make_shared<const Terrain>(inputs.terrain.Beneath(i))}); | |
| 185 | 4 | } | |
| 186 | 2 | if (rocks.boulderLayer) { | |
| 187 | 2 | sources = {sources[*rocks.boulderLayer]}; | |
| 188 | } | ||
| 189 | 2 | albedo.layers.push_back(std::make_shared<RockAlbedo>(rocks.options, std::move(sources))); | |
| 190 | 12 | } | |
| 191 | |||
| 192 | 1 | void AddAlbedoLayer(const SplotchAlbedoOptions& splotches, const AlbedoInputs&, Albedo& albedo) { | |
| 193 | 1 | albedo.layers.push_back(std::make_shared<SplotchAlbedo>(splotches)); | |
| 194 | 1 | } | |
| 195 | |||
| 196 | ✗ | void AddAlbedoLayer(const BlastZoneAlbedoSpec& zones, const AlbedoInputs&, Albedo& albedo) { | |
| 197 | ✗ | albedo.layers.push_back(std::make_shared<BlastZoneAlbedo>(zones.zones)); | |
| 198 | } | ||
| 199 | |||
| 200 | 1 | void AddAlbedoLayer(const SlopeAlbedoOptions& slopes, const AlbedoInputs&, Albedo& albedo) { | |
| 201 | 1 | albedo.layers.push_back(std::make_shared<SlopeAlbedo>(slopes)); | |
| 202 | 1 | } | |
| 203 | |||
| 204 | } // namespace | ||
| 205 | |||
| 206 | 57 | void CheckAlbedo(const AlbedoSpec& albedo, const std::vector<LayerSpec>& layers, | |
| 207 | const std::filesystem::path& directory) { | ||
| 208 | 110 | std::visit([&](const auto& source) { CheckAlbedoSource(source, directory, "albedo.source"); }, | |
| 209 | 57 | albedo.source); | |
| 210 | 53 | const AlbedoChecks checks{CountLayers<CraterLayerSpec>(layers), | |
| 211 | 53 | CountLayers<BoulderLayerSpec>(layers)}; | |
| 212 | 92 | for (std::size_t i = 0; i < albedo.layers.size(); ++i) { | |
| 213 | 86 | const std::string path = ItemPath("albedo.layers", i); | |
| 214 | 86 | std::visit([&](const auto& layer) { CheckAlbedoLayer(layer, path, checks); }, | |
| 215 | 43 | albedo.layers[i]); | |
| 216 | 39 | } | |
| 217 | 49 | } | |
| 218 | |||
| 219 | 21 | Albedo BuildValidAlbedo(const AlbedoSpec& spec, const std::vector<LayerSpec>& layers, | |
| 220 | const Terrain& terrain, const std::filesystem::path& directory) { | ||
| 221 | 21 | Albedo albedo; | |
| 222 | 21 | albedo.surface = terrain.Surface(); | |
| 223 | 21 | albedo.source = BuildAlbedoSource(spec.source, directory); | |
| 224 | 21 | const AlbedoInputs inputs{layers, terrain}; | |
| 225 | 38 | for (const AlbedoLayerSpec& layer : spec.layers) { | |
| 226 | 40 | std::visit([&](const auto& s) { AddAlbedoLayer(s, inputs, albedo); }, layer); | |
| 227 | } | ||
| 228 | 18 | return albedo; | |
| 229 | 3 | } | |
| 230 | |||
| 231 | } // namespace config | ||
| 232 | |||
| 233 | } // namespace terraingen | ||
| 234 |