src/config/terrain.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/terrain.h" | ||
| 4 | |||
| 5 | #include "terraingen/config/build.h" | ||
| 6 | |||
| 7 | #include <stdexcept> | ||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | 1 | TerrainGenConfig::TerrainGenConfig() : TerrainGenConfig(ConfigSpec{}) {} | |
| 13 | |||
| 14 | 994 | TerrainGenConfig::TerrainGenConfig(ConfigSpec spec) | |
| 15 | 994 | : m_spec(std::make_shared<const ConfigSpec>(std::move(spec))) {} | |
| 16 | |||
| 17 | ✗ | const std::filesystem::path& TerrainGenConfig::Directory() const { | |
| 18 | ✗ | return m_spec->directory; | |
| 19 | } | ||
| 20 | |||
| 21 | 967 | const ConfigSpec& TerrainGenConfig::Spec() const { | |
| 22 | 967 | return *m_spec; | |
| 23 | } | ||
| 24 | |||
| 25 | namespace config { | ||
| 26 | |||
| 27 | namespace { | ||
| 28 | |||
| 29 | /// The source `config` gives, else a "shape_model" source on a shape-model body; none on other | ||
| 30 | /// bodies, which then start from the default synthetic source. | ||
| 31 | 474 | std::optional<SourceSpec> EffectiveSource(const ConfigSpec& config) { | |
| 32 | 474 | if (config.source) { | |
| 33 | 346 | return config.source; | |
| 34 | } | ||
| 35 | 128 | if (std::holds_alternative<ShapeModelSurfaceSpec>(config.body)) { | |
| 36 | 8 | return SourceSpec{ShapeModelSourceSpec{}}; | |
| 37 | } | ||
| 38 | 124 | return std::nullopt; | |
| 39 | } | ||
| 40 | |||
| 41 | /// The terrain's source, the configuration's or the default synthetic noise unless the caller | ||
| 42 | /// gives one, and what the layers are built on. | ||
| 43 | 474 | LayerSources BuildLayerSources(const ConfigSpec& config, const std::filesystem::path& directory, | |
| 44 | const std::shared_ptr<const Source>& external, | ||
| 45 | const Surface& surface, ConfigSummary& summary) { | ||
| 46 | 474 | const std::optional<SourceSpec> spec = EffectiveSource(config); | |
| 47 | 474 | LayerSources sources; | |
| 48 | 474 | sources.posts = spec && HasPosts(*spec); | |
| 49 | 474 | if (external) { | |
| 50 | 5 | sources.terrain = external; | |
| 51 | 5 | sources.given = external; | |
| 52 | 5 | sources.measured = external; | |
| 53 | 5 | return sources; | |
| 54 | } | ||
| 55 | 469 | sources.terrain = | |
| 56 | 1031 | BuildSource(spec.value_or(SourceSpec{SyntheticSourceSpec{}}), directory, "source", surface); | |
| 57 | 438 | summary.source = BoundedGrid(*sources.terrain); | |
| 58 | 438 | if (spec) { | |
| 59 | 319 | sources.given = sources.terrain; | |
| 60 | 400 | sources.measured = HasMeasuredData(*spec) ? sources.terrain : nullptr; | |
| 61 | } | ||
| 62 | return sources; | ||
| 63 | 474 | } | |
| 64 | |||
| 65 | } // namespace | ||
| 66 | |||
| 67 | 582 | CheckedTerrain BuildCheckedTerrain(const ConfigSpec& config, const std::filesystem::path& directory, | |
| 68 | const std::shared_ptr<const Source>& external) { | ||
| 69 | 690 | CheckConfigRanges(config); | |
| 70 | 474 | ConfigSummary summary; | |
| 71 | 474 | Surface surface = BuildSurface(config.body, directory); | |
| 72 | 474 | const LayerSources sources = BuildLayerSources(config, directory, external, surface, summary); | |
| 73 | 443 | const OutputNeeds needs{config.albedo.has_value(), config.materials.has_value()}; | |
| 74 | 443 | std::vector<OutputGrid> outputs = CheckOutputs(config.outputs, needs, surface, summary.source); | |
| 75 | 872 | for (const OutputGrid& output : outputs) { | |
| 76 | 476 | summary.outputGrids.push_back(output.grid); | |
| 77 | } | ||
| 78 | 396 | BuiltLayers built = BuildLayers(config.layers, outputs, sources, directory, surface); | |
| 79 | 345 | summary.expectedCraters = built.expectedCraters; | |
| 80 | 345 | CheckedTerrain checked{Terrain(std::move(surface), sources.terrain, std::move(built.layers)), | |
| 81 | 704 | std::move(summary), std::move(outputs)}; | |
| 82 | 345 | if (config.albedo) { | |
| 83 | 45 | CheckAlbedo(*config.albedo, config.layers, directory); | |
| 84 | } | ||
| 85 | 337 | if (config.materials) { | |
| 86 | 9 | CheckMaterials(*config.materials, HasOutput<PhotometryOutput>(config.outputs), | |
| 87 | checked.terrain); | ||
| 88 | } | ||
| 89 | 331 | return checked; | |
| 90 | 665 | } | |
| 91 | |||
| 92 | } // namespace config | ||
| 93 | |||
| 94 | 378 | ConfigSummary ValidateConfig(const TerrainGenConfig& value, const std::filesystem::path& given) { | |
| 95 | 378 | const ConfigSpec& config = value.Spec(); | |
| 96 | 894 | return config::BuildCheckedTerrain(config, ConfigDirectory(config, given)).summary; | |
| 97 | } | ||
| 98 | |||
| 99 | 1 | Surface BuildSurface(const TerrainGenConfig& value, const std::filesystem::path& given) { | |
| 100 | 1 | const ConfigSpec& config = value.Spec(); | |
| 101 | 3 | return BuildSurface(config.body, ConfigDirectory(config, given)); | |
| 102 | } | ||
| 103 | |||
| 104 | 86 | Terrain BuildTerrain(const TerrainGenConfig& value, const std::filesystem::path& given) { | |
| 105 | 86 | const ConfigSpec& config = value.Spec(); | |
| 106 | 250 | return config::BuildCheckedTerrain(config, ConfigDirectory(config, given)).terrain; | |
| 107 | } | ||
| 108 | |||
| 109 | 8 | Terrain BuildTerrain(const TerrainGenConfig& value, std::shared_ptr<const Source> source, | |
| 110 | const std::filesystem::path& given) { | ||
| 111 | 8 | const ConfigSpec& config = value.Spec(); | |
| 112 | 8 | if (!source) { | |
| 113 | 1 | throw std::invalid_argument("BuildTerrain needs a source"); | |
| 114 | } | ||
| 115 | 7 | if (config.source) { | |
| 116 | 4 | throw ConfigError("source", "must be left out when the caller gives the source"); | |
| 117 | } | ||
| 118 | 14 | return config::BuildCheckedTerrain(config, ConfigDirectory(config, given), source).terrain; | |
| 119 | } | ||
| 120 | |||
| 121 | 14 | Albedo BuildAlbedo(const TerrainGenConfig& value, const Terrain& terrain, | |
| 122 | const std::filesystem::path& given) { | ||
| 123 | 14 | const ConfigSpec& config = value.Spec(); | |
| 124 | 14 | const std::filesystem::path directory = ConfigDirectory(config, given); | |
| 125 | 14 | if (!config.albedo) { | |
| 126 | 2 | throw ConfigError("albedo", "the configuration has no albedo"); | |
| 127 | } | ||
| 128 | 30 | config::CheckConfigRanges(*config.albedo, "albedo"); | |
| 129 | 12 | config::CheckAlbedo(*config.albedo, config.layers, directory); | |
| 130 | 12 | return config::BuildValidAlbedo(*config.albedo, config.layers, terrain, directory); | |
| 131 | 9 | } | |
| 132 | |||
| 133 | } // namespace terraingen | ||
| 134 |