src/config/boulders.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/boulders.h" | ||
| 4 | |||
| 5 | #include "terraingen/distribution/power_law.h" | ||
| 6 | #include "terraingen/distribution/tabulated.h" | ||
| 7 | #include "terraingen/layers/boulders/boulder_abundance.h" | ||
| 8 | #include "terraingen/mesh/io/mesh_io.h" | ||
| 9 | #include "util/overloaded.h" | ||
| 10 | |||
| 11 | #include <cmath> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <unordered_map> | ||
| 14 | #include <utility> | ||
| 15 | |||
| 16 | namespace terraingen::config { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | /// The distribution a boulder size spec names, or null for the layer's Golombek-Rapp boulders. | ||
| 21 | 196 | std::shared_ptr<SizeFrequencyDistribution> BuildBoulderSizes(const BoulderSizesSpec& spec) { | |
| 22 | 196 | using Result = std::shared_ptr<SizeFrequencyDistribution>; | |
| 23 | 217 | const auto density = [](const DensitySpec& named) { | |
| 24 | 41 | return std::const_pointer_cast<SizeFrequencyDistribution>(BuildDensity(named)); | |
| 25 | }; | ||
| 26 | 196 | return std::visit( | |
| 27 | 391 | Overloaded{ | |
| 28 | 94 | [](const GolombekRappBouldersSpec&) -> Result { return nullptr; }, | |
| 29 | 70 | [](const Sp8023IntercraterBouldersSpec& s) -> Result { | |
| 30 | 140 | return std::make_shared<TabulatedSfd>(Sp8023IntercraterBlocks(s.curve)); | |
| 31 | }, | ||
| 32 | 10 | [](const Sp8023CraterBouldersSpec& s) -> Result { | |
| 33 | 20 | return std::make_shared<TabulatedSfd>(Sp8023CraterBlocks(s.curve)); | |
| 34 | }, | ||
| 35 | 1 | [](const SynthterrainBouldersSpec& s) -> Result { | |
| 36 | 1 | return std::make_shared<PowerLawSfd>(SynthterrainBoulderDistribution(s.model)); | |
| 37 | }, | ||
| 38 | 32 | [&](const PowerLawSpec& s) -> Result { return density(s); }, | |
| 39 | 2 | [&](const PiecewisePowerLawSpec& s) -> Result { return density(s); }, | |
| 40 | 3 | [&](const TabulatedSpec& s) -> Result { return density(s); }, | |
| 41 | 4 | [&](const PiecewiseUniformSpec& s) -> Result { return density(s); }, | |
| 42 | }, | ||
| 43 | 196 | spec); | |
| 44 | } | ||
| 45 | |||
| 46 | 258 | bool IsGolombekRapp(const BoulderSizesSpec& sizes) { | |
| 47 | 258 | return std::holds_alternative<GolombekRappBouldersSpec>(sizes); | |
| 48 | } | ||
| 49 | |||
| 50 | /// @throws ConfigError at a Golombek-Rapp term that is set, such as "coverage", when no size model | ||
| 51 | /// of the layer is Golombek-Rapp and so none would use it. | ||
| 52 | 88 | void CheckGolombekRappTerms(const BoulderLayerSpec& spec, const std::string& path) { | |
| 53 | 88 | const BoulderSizes& sizes = spec.boulders.sizes; | |
| 54 | 88 | const bool layer = IsGolombekRapp(spec.sizes); | |
| 55 | 176 | const bool ejecta = IsGolombekRapp(spec.ejectaSizes.value_or(spec.sizes)); | |
| 56 | 431 | const auto check = [](bool used, const std::optional<double>& value, const std::string& at, | |
| 57 | const char* fix) { | ||
| 58 | 343 | if (!used && value) { | |
| 59 | 18 | throw ConfigError(at, std::string("is only used by Golombek-Rapp sizes; set ") + fix + | |
| 60 | 24 | " or leave it out"); | |
| 61 | } | ||
| 62 | 337 | }; | |
| 63 | 88 | const char* const layerFix = R"("sizes": {"type": "golombek_rapp"})"; | |
| 64 | 90 | check(layer, sizes.coverage, FieldPath(path, "coverage"), layerFix); | |
| 65 | 87 | check(layer || ejecta, sizes.qConstant, FieldPath(path, "qa"), layerFix); | |
| 66 | 86 | check(layer || ejecta, sizes.qCoverageCoefficient, FieldPath(path, "qb"), layerFix); | |
| 67 | 168 | check(ejecta, spec.boulders.ejecta.coverage, FieldPath(FieldPath(path, "ejecta"), "coverage"), | |
| 68 | R"("ejecta_sizes": {"type": "golombek_rapp"})"); | ||
| 69 | 82 | } | |
| 70 | |||
| 71 | /// @throws ConfigError at the Golombek-Rapp terms, a missing coverage, or ejecta without a crater | ||
| 72 | /// layer before; the layer checks the rest of its options as it is built. | ||
| 73 | 89 | void CheckBoulderOptions(const BoulderLayerSpec& spec, const BoulderLayerOptions& options, | |
| 74 | const std::string& path, bool cratersBefore) { | ||
| 75 | 89 | if (!options.generateRandom) { | |
| 76 | return; | ||
| 77 | } | ||
| 78 | 88 | CheckGolombekRappTerms(spec, path); | |
| 79 | 125 | if (IsGolombekRapp(spec.sizes) && !(options.sizes.Coverage() > 0.0)) { | |
| 80 | 2 | throw ConfigError(FieldPath(path, "coverage"), | |
| 81 | "must be above 0 to generate random boulders; set generate_random to " | ||
| 82 | 3 | "false to place only the manual ones"); | |
| 83 | } | ||
| 84 | 81 | if (options.ejecta.ThrowingDiameter() && !cratersBefore) { | |
| 85 | 4 | throw ConfigError(FieldPath(FieldPath(path, "ejecta"), "min_crater_diameter_m"), | |
| 86 | 6 | "ejecta need a crater layer before this one"); | |
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | /// The distribution a boulder size spec at `path` names, extrapolated when `extrapolate` is set, | ||
| 91 | /// or null for Golombek-Rapp sizes. | ||
| 92 | /// @throws ConfigError at `path` for an invalid distribution. | ||
| 93 | 196 | std::shared_ptr<SizeFrequencyDistribution> BuildSizes(const BoulderSizesSpec& spec, | |
| 94 | bool extrapolate, const std::string& path) { | ||
| 95 | 392 | auto built = At(path, [&] { return BuildBoulderSizes(spec); }); | |
| 96 | 195 | if (built && extrapolate) { | |
| 97 | 12 | built->SetAllowExtrapolation(true); | |
| 98 | } | ||
| 99 | 195 | return built; | |
| 100 | } | ||
| 101 | |||
| 102 | /// The rock meshes of a layer's shape files, read in order. | ||
| 103 | /// @throws ConfigError at `path`[i] for an unreadable mesh. | ||
| 104 | std::vector<std::shared_ptr<const SceneMesh>> | ||
| 105 | 97 | ReadShapeFiles(const std::vector<std::string>& files, const std::string& path, | |
| 106 | const std::filesystem::path& directory) { | ||
| 107 | 97 | std::vector<std::shared_ptr<const SceneMesh>> meshes; | |
| 108 | 98 | for (std::size_t i = 0; i < files.size(); ++i) { | |
| 109 | 4 | meshes.push_back(AtFile(ItemPath(path, i), [&] { | |
| 110 | 4 | return std::make_shared<const SceneMesh>(ReadMesh(Resolve(files[i], directory))); | |
| 111 | })); | ||
| 112 | } | ||
| 113 | 96 | return meshes; | |
| 114 | 1 | } | |
| 115 | |||
| 116 | } // namespace | ||
| 117 | |||
| 118 | 99 | BoulderLayerOptions BuildBoulderOptions(const BoulderLayerSpec& spec, const std::string& path, | |
| 119 | const std::filesystem::path& directory) { | ||
| 120 | 99 | BoulderLayerOptions options = spec.boulders; | |
| 121 | 296 | if (options.sizes.distribution != DefaultBoulderSizes() || | |
| 122 | 197 | options.ejecta.distribution != DefaultBoulderSizes()) { | |
| 123 | 2 | throw ConfigError(FieldPath(path, "sizes"), | |
| 124 | "gives the boulder sizes; leave boulders.sizes.distribution and " | ||
| 125 | "boulders.ejecta.distribution at their defaults and set sizes and " | ||
| 126 | 3 | "ejecta_sizes instead"); | |
| 127 | } | ||
| 128 | 98 | options.sizes.distribution = BuildSizes(spec.sizes, spec.extrapolate, FieldPath(path, "sizes")); | |
| 129 | 294 | options.ejecta.distribution = BuildSizes(spec.ejectaSizes.value_or(spec.sizes), | |
| 130 | 196 | spec.extrapolate, FieldPath(path, "ejecta_sizes")); | |
| 131 | 97 | std::vector<std::shared_ptr<const SceneMesh>> meshes = | |
| 132 | 98 | ReadShapeFiles(spec.shapeFiles, FieldPath(path, "shape_files"), directory); | |
| 133 | 96 | std::vector<std::shared_ptr<const SceneMesh>>& library = options.rendering.shapes.library; | |
| 134 | 96 | library.insert(library.end(), meshes.begin(), meshes.end()); | |
| 135 | 96 | if (spec.densityMap) { | |
| 136 | 2 | options.densityMap = BuildMap(*spec.densityMap, FieldPath(path, "density_map"), directory); | |
| 137 | } | ||
| 138 | 96 | if (spec.slopeMap) { | |
| 139 | 4 | options.slopeMap = BuildMap(*spec.slopeMap, FieldPath(path, "slope_map"), directory); | |
| 140 | } | ||
| 141 | 95 | if (spec.completeness) { | |
| 142 | 2 | options.completeness = | |
| 143 | 2 | BuildField(*spec.completeness, FieldPath(path, "completeness"), directory); | |
| 144 | } | ||
| 145 | 95 | return options; | |
| 146 | 100 | } | |
| 147 | |||
| 148 | 93 | void CheckSpec(const BoulderLayerSpec& spec, const std::string& path, LayerChecks& checks) { | |
| 149 | 93 | const std::string manualPath = FieldPath(path, "manual"); | |
| 150 | 93 | std::unordered_map<std::uint64_t, std::size_t> ids; | |
| 151 | 101 | for (std::size_t n = 0; n < spec.boulders.manual.size(); ++n) { | |
| 152 | 20 | At(ItemPath(manualPath, n), [&] { ValidateManualBoulder(spec.boulders.manual[n]); }); | |
| 153 | 12 | const auto added = ids.emplace(ManualBoulderId(spec.boulders.manual[n]), n); | |
| 154 | 10 | if (!added.second) { | |
| 155 | 4 | throw ConfigError(ItemPath(manualPath, n), | |
| 156 | 6 | "shares its ID with " + ItemPath(manualPath, added.first->second)); | |
| 157 | } | ||
| 158 | } | ||
| 159 | 91 | checks.boulders = true; | |
| 160 | 93 | } | |
| 161 | |||
| 162 | 91 | LayerPtr BuildSpec(const BoulderLayerSpec& spec, const std::string& path, | |
| 163 | const LayerInputs& inputs) { | ||
| 164 | 91 | BoulderLayerOptions options = BuildBoulderOptions(spec, path, inputs.directory); | |
| 165 | 89 | CheckBoulderOptions(spec, options, path, !inputs.craters.empty()); | |
| 166 | 80 | const bool throws = options.generateRandom && options.ejecta.ThrowingDiameter().has_value(); | |
| 167 | 78 | return std::make_shared<BoulderLayer>( | |
| 168 | std::move(options), | ||
| 169 | 238 | throws ? inputs.craters : std::vector<std::shared_ptr<const CraterLayer>>{}, inputs.base); | |
| 170 | 89 | } | |
| 171 | |||
| 172 | 78 | void CheckBuilt(const BoulderLayerSpec& /*spec*/, const Layer& built, const std::string& path, | |
| 173 | LayerChecks& checks) { | ||
| 174 | 78 | const BoulderLayerOptions& boulders = dynamic_cast<const BoulderLayer&>(built).Options(); | |
| 175 | 78 | if (!boulders.generateRandom) { | |
| 176 | return; | ||
| 177 | } | ||
| 178 | 77 | const BoulderSizes& sizes = boulders.sizes; | |
| 179 | 207 | for (const OutputGrid& output : checks.outputs) { | |
| 180 | 131 | if (!output.grid) { | |
| 181 | 8 | continue; | |
| 182 | } | ||
| 183 | 246 | const double minDiameter = | |
| 184 | 123 | output.listed | |
| 185 | 123 | ? sizes.minDiameter | |
| 186 | 72 | : std::fmax(sizes.minDiameter, boulders.fade.startSamples * output.spacing); | |
| 187 | 123 | if (minDiameter >= sizes.maxDiameter) { | |
| 188 | 1 | continue; | |
| 189 | } | ||
| 190 | 122 | const double expected = | |
| 191 | 122 | BoulderCandidates(boulders)->ExpectedCount(output.area, minDiameter, sizes.maxDiameter); | |
| 192 | 122 | CheckExpectedCount(expected, "candidate boulders", "raise min_diameter_m or lower coverage", | |
| 193 | 244 | FieldPath(path, "min_diameter_m")); | |
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | } // namespace terraingen::config | ||
| 198 |