src/config/layers.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/layers.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/noise_layer.h" | ||
| 6 | #include "terraingen/layers/sand_fill.h" | ||
| 7 | #include "terraingen/layers/sand_shadow.h" | ||
| 8 | #include "terraingen/layers/spectral_continuation.h" | ||
| 9 | #include "terraingen/layers/transforms.h" | ||
| 10 | #include "util/overloaded.h" | ||
| 11 | |||
| 12 | #include <type_traits> | ||
| 13 | #include <utility> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | 233 | std::vector<Fractal> NoiseLayerSpec::Fractals() const { | |
| 18 | 233 | return StackFractals(stack); | |
| 19 | } | ||
| 20 | |||
| 21 | 7 | std::vector<Fractal> NoiseLayerSpec::EstimatedFractals(const BaseRoughness& source) const { | |
| 22 | 7 | std::vector<Fractal> fractals; | |
| 23 | 16 | for (FractalSpec layer : stack) { | |
| 24 | 9 | if (!layer.hurst) { | |
| 25 | 7 | layer.hurst = static_cast<float>(source.hurst); | |
| 26 | } | ||
| 27 | 9 | Fractal effective = layer.EffectiveRelief(); | |
| 28 | 9 | if (!layer.amplitude && effective.enabled) { | |
| 29 | 7 | BaseRoughness continued = source; | |
| 30 | 7 | continued.hurst = static_cast<double>(*layer.hurst); | |
| 31 | 7 | effective.amplitude = MatchedNoiseAmplitude(effective, continued); | |
| 32 | } | ||
| 33 | 9 | fractals.push_back(effective); | |
| 34 | } | ||
| 35 | 7 | return fractals; | |
| 36 | } | ||
| 37 | |||
| 38 | 14 | RoughnessOptions RoughnessLayerSpec::Options() const { | |
| 39 | 14 | RoughnessOptions options = roughness; | |
| 40 | 14 | options.rmsSlope = rmsSlope.value_or(roughness.rmsSlope); | |
| 41 | 14 | options.hurst = hurst.value_or(roughness.hurst); | |
| 42 | 14 | return options; | |
| 43 | } | ||
| 44 | |||
| 45 | 8 | RoughnessOptions RoughnessLayerSpec::EstimatedOptions(BaseRoughness source) const { | |
| 46 | 8 | RoughnessOptions options = Options(); | |
| 47 | 8 | source.hurst = hurst.value_or(source.hurst); | |
| 48 | 8 | options.hurst = source.hurst; | |
| 49 | 8 | options.rmsSlope = rmsSlope.value_or(source.RmsSlope(options.baseline)); | |
| 50 | 8 | return options; | |
| 51 | } | ||
| 52 | |||
| 53 | 257 | std::string LayerType(const LayerSpec& layer) { | |
| 54 | 257 | return std::visit( | |
| 55 | 257 | [](const auto& spec) { | |
| 56 | using Spec = std::decay_t<decltype(spec)>; | ||
| 57 | 257 | std::string type(SpecName<Spec>::kName); | |
| 58 | if constexpr (std::is_same_v<Spec, MaskedLayerSpec>) { | ||
| 59 | 18 | type += " (" + LayerType(*spec.layer) + ")"; | |
| 60 | } | ||
| 61 | 6 | return type; | |
| 62 | ✗ | }, | |
| 63 | 257 | layer); | |
| 64 | } | ||
| 65 | |||
| 66 | namespace config { | ||
| 67 | |||
| 68 | /// @name Layer dispatch | ||
| 69 | /// Checks, builds or checks the built layer of any layer spec, through the overloads of its kind. | ||
| 70 | /// @{ | ||
| 71 | void CheckLayer(const LayerSpec& spec, const std::string& path, LayerChecks& checks); | ||
| 72 | LayerPtr BuildLayer(const LayerSpec& spec, const std::string& path, const LayerInputs& inputs); | ||
| 73 | void CheckBuiltLayer(const LayerSpec& spec, const Layer& built, const std::string& path, | ||
| 74 | LayerChecks& checks); | ||
| 75 | /// @} | ||
| 76 | |||
| 77 | namespace { | ||
| 78 | |||
| 79 | /// The roughness of the source at its native resolution (`EstimateSourceRoughness`). | ||
| 80 | /// @throws std::invalid_argument without a source, or as `EstimateSourceRoughness`. | ||
| 81 | 10 | BaseRoughness SourceRoughness(const LayerInputs& inputs) { | |
| 82 | 10 | if (!inputs.source) { | |
| 83 | ✗ | throw std::invalid_argument("Estimating from the source needs a source"); | |
| 84 | } | ||
| 85 | 10 | return EstimateSourceRoughness(*inputs.source, inputs.base.surface); | |
| 86 | } | ||
| 87 | |||
| 88 | 255 | void CheckSpec(const NoiseLayerSpec& spec, const std::string& path, LayerChecks& checks) { | |
| 89 | 255 | CheckNoiseStack(spec.stack, FieldPath(path, "stack")); | |
| 90 | 255 | CheckEstimateSource(spec.estimateFromSource, path, checks); | |
| 91 | 254 | } | |
| 92 | |||
| 93 | 11 | void CheckSpec(const RoughnessLayerSpec& spec, const std::string& path, LayerChecks& checks) { | |
| 94 | 11 | CheckEstimateSource(spec.estimateFromSource, path, checks); | |
| 95 | 8 | } | |
| 96 | |||
| 97 | 7 | void CheckSpec(const SpectralContinuationLayerSpec& spec, const std::string& path, | |
| 98 | LayerChecks& checks) { | ||
| 99 | 7 | if (!checks.posts && !(spec.pixelSize && spec.slope && spec.octaveRms)) { | |
| 100 | 1 | throw ConfigError(path, "without a source with posts to fit its spectrum to needs " | |
| 101 | 2 | "pixel_size_m, slope and octave_rms_m"); | |
| 102 | } | ||
| 103 | 12 | At(path, [&] { ValidateSpectrumFitBand(spec.fit); }); | |
| 104 | 5 | } | |
| 105 | |||
| 106 | 2 | void CheckSpec(const SandShadowOptions& /*spec*/, const std::string& path, LayerChecks& checks) { | |
| 107 | 2 | if (!checks.boulders) { | |
| 108 | 2 | throw ConfigError(path, "sand shadows need a boulder layer before this one"); | |
| 109 | } | ||
| 110 | 1 | } | |
| 111 | |||
| 112 | 20 | void CheckSpec(const MaskedLayerSpec& spec, const std::string& path, LayerChecks& checks) { | |
| 113 | 60 | At(FieldPath(path, "shape"), [&] { ValidateRegionShape(BuildRegionShape(spec.shape)); }); | |
| 114 | 30 | const bool empty = std::visit( | |
| 115 | 15 | Overloaded{ | |
| 116 | 2 | [](const RectangleRegionSpec& r) { return !(r.maxX > r.minX && r.maxY > r.minY); }, | |
| 117 | 11 | [](const CircleRegionSpec& c) { return !(c.radius > 0.0); }, | |
| 118 | [](const PolygonRegionSpec&) { return false; }, | ||
| 119 | }, | ||
| 120 | 15 | spec.shape); | |
| 121 | 15 | if (empty) { | |
| 122 | 4 | throw ConfigError(FieldPath(path, "shape"), | |
| 123 | "required: the shape has no area; give the rectangle's bounds or the " | ||
| 124 | 6 | "circle's radius"); | |
| 125 | } | ||
| 126 | 13 | LayerChecks inner = checks; | |
| 127 | 26 | CheckLayer(*spec.layer, FieldPath(path, "layer"), inner); | |
| 128 | 13 | } | |
| 129 | |||
| 130 | 12 | void CheckBuilt(const MaskedLayerSpec& spec, const Layer& built, const std::string& path, | |
| 131 | LayerChecks& checks) { | ||
| 132 | 12 | LayerChecks inner = checks; | |
| 133 | 12 | CheckBuiltLayer(*spec.layer, dynamic_cast<const MaskedLayer&>(built).Inner(), | |
| 134 | 24 | FieldPath(path, "layer"), inner); | |
| 135 | 12 | } | |
| 136 | |||
| 137 | 233 | LayerPtr BuildSpec(const NoiseLayerSpec& spec, const std::string& /*path*/, | |
| 138 | const LayerInputs& inputs) { | ||
| 139 | 233 | const std::vector<Fractal> fractals = | |
| 140 | 233 | spec.estimateFromSource ? spec.EstimatedFractals(SourceRoughness(inputs)) : spec.Fractals(); | |
| 141 | 466 | return std::make_shared<NoiseLayer>(NoiseLayerOptions{fractals, spec.sourceResolvesPixels}); | |
| 142 | 233 | } | |
| 143 | |||
| 144 | 8 | LayerPtr BuildSpec(const RoughnessLayerSpec& spec, const std::string& /*path*/, | |
| 145 | const LayerInputs& inputs) { | ||
| 146 | 8 | const RoughnessOptions options = | |
| 147 | 8 | spec.estimateFromSource ? spec.EstimatedOptions(SourceRoughness(inputs)) : spec.Options(); | |
| 148 | 8 | return std::make_shared<RoughnessLayer>(options); | |
| 149 | } | ||
| 150 | |||
| 151 | /// A spectral continuation layer, fitted to the source wherever the spec gives no slope or | ||
| 152 | /// amplitude; a valid configuration has a source whenever the spec leaves one out. | ||
| 153 | 5 | LayerPtr BuildSpec(const SpectralContinuationLayerSpec& spec, const std::string& /*path*/, | |
| 154 | const LayerInputs& inputs) { | ||
| 155 | 5 | const Surface& surface = inputs.base.surface; | |
| 156 | 5 | SpectralContinuationOptions options = spec.options; | |
| 157 | 5 | const RasterGrid grid = inputs.source ? inputs.source->Grid() : RasterGrid{}; | |
| 158 | 5 | options.pixelSize = spec.pixelSize.value_or(grid.PixelSize() * surface.UnitLength()); | |
| 159 | 5 | PowerLawSpectrum fit{options.slope, options.octaveRms}; | |
| 160 | 5 | if (!(spec.slope && spec.octaveRms)) { | |
| 161 | 2 | fit = EstimatePowerLawSpectrum(SampleSourceWindow(*inputs.source, | |
| 162 | 2 | options.pixelSize / surface.UnitLength(), | |
| 163 | 2 | grid.transform.PixelToWorld(0.5, 0.5)), | |
| 164 | 2 | spec.fit); | |
| 165 | } | ||
| 166 | 5 | options.slope = spec.slope.value_or(fit.slope); | |
| 167 | 5 | options.octaveRms = spec.octaveRms.value_or(fit.octaveRms); | |
| 168 | 5 | return std::make_shared<SpectralContinuationLayer>(options); | |
| 169 | } | ||
| 170 | |||
| 171 | 13 | LayerPtr BuildSpec(const MaskedLayerSpec& spec, const std::string& path, | |
| 172 | const LayerInputs& inputs) { | ||
| 173 | 26 | return std::make_shared<MaskedLayer>(BuildLayer(*spec.layer, FieldPath(path, "layer"), inputs), | |
| 174 | 37 | BuildRegionShape(spec.shape), spec.feather, spec.blend); | |
| 175 | } | ||
| 176 | |||
| 177 | 8 | LayerPtr BuildSpec(const DuneOptions& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 178 | 8 | return std::make_shared<DuneLayer>(spec); | |
| 179 | } | ||
| 180 | |||
| 181 | 5 | LayerPtr BuildSpec(const BedformOptions& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 182 | 5 | return std::make_shared<BedformLayer>(spec); | |
| 183 | } | ||
| 184 | |||
| 185 | 2 | LayerPtr BuildSpec(const PavementOptions& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 186 | 2 | return std::make_shared<PavementLayer>(spec); | |
| 187 | } | ||
| 188 | |||
| 189 | 2 | LayerPtr BuildSpec(const StrataOptions& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 190 | 2 | return std::make_shared<StrataLayer>(spec); | |
| 191 | } | ||
| 192 | |||
| 193 | 1 | LayerPtr BuildSpec(const SandShadowOptions& spec, const std::string& /*path*/, | |
| 194 | const LayerInputs& inputs) { | ||
| 195 | 1 | return std::make_shared<SandShadowLayer>(inputs.boulders, spec); | |
| 196 | } | ||
| 197 | |||
| 198 | 1 | LayerPtr BuildSpec(const SandFillOptions& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 199 | 1 | return std::make_shared<SandFillLayer>(spec); | |
| 200 | } | ||
| 201 | |||
| 202 | 4 | LayerPtr BuildSpec(const ScaleLayerSpec& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 203 | 4 | return std::make_shared<ScaleLayer>(spec.scale, spec.offset); | |
| 204 | } | ||
| 205 | |||
| 206 | 5 | LayerPtr BuildSpec(const ClampLayerSpec& spec, const std::string& /*path*/, const LayerInputs&) { | |
| 207 | 5 | return std::make_shared<ClampLayer>(spec.min, spec.max); | |
| 208 | } | ||
| 209 | |||
| 210 | /// The secondaries the secondary layers after the crater layer `layers[index]` throw from it, up | ||
| 211 | /// to the next crater layer; none when `layers[index]` is not a crater layer. | ||
| 212 | 640 | std::vector<SecondaryCraterOptions> ThrownSecondaries(const std::vector<LayerSpec>& layers, | |
| 213 | std::size_t index) { | ||
| 214 | 640 | std::vector<SecondaryCraterOptions> thrown; | |
| 215 | 640 | if (!std::holds_alternative<CraterLayerSpec>(layers[index])) { | |
| 216 | return thrown; | ||
| 217 | } | ||
| 218 | 264 | for (std::size_t j = index + 1; | |
| 219 | 382 | j < layers.size() && !std::holds_alternative<CraterLayerSpec>(layers[j]); ++j) { | |
| 220 | 236 | if (const auto* secondaries = std::get_if<SecondaryLayerSpec>(&layers[j])) { | |
| 221 | 7 | thrown.push_back(secondaries->secondaries); | |
| 222 | } | ||
| 223 | } | ||
| 224 | return thrown; | ||
| 225 | } | ||
| 226 | |||
| 227 | } // namespace | ||
| 228 | |||
| 229 | 712 | void CheckLayer(const LayerSpec& spec, const std::string& path, LayerChecks& checks) { | |
| 230 | 1397 | std::visit([&](const auto& layer) { CheckSpec(layer, path, checks); }, spec); | |
| 231 | 690 | } | |
| 232 | |||
| 233 | 653 | LayerPtr BuildLayer(const LayerSpec& spec, const std::string& path, const LayerInputs& inputs) { | |
| 234 | 1306 | return At(path, [&] { | |
| 235 | 1306 | return std::visit([&](const auto& layer) { return BuildSpec(layer, path, inputs); }, spec); | |
| 236 | 653 | }); | |
| 237 | } | ||
| 238 | |||
| 239 | 626 | void CheckBuiltLayer(const LayerSpec& spec, const Layer& built, const std::string& path, | |
| 240 | LayerChecks& checks) { | ||
| 241 | 348 | std::visit([&](const auto& layer) { CheckBuilt(layer, built, path, checks); }, spec); | |
| 242 | 623 | } | |
| 243 | |||
| 244 | 266 | void CheckEstimateSource(bool estimate, const std::string& path, const LayerChecks& checks) { | |
| 245 | 266 | if (estimate && !checks.posts) { | |
| 246 | 8 | throw ConfigError(FieldPath(path, "estimate_from_source"), | |
| 247 | "needs a source with posts to estimate from, a raster or synthetic noise " | ||
| 248 | 12 | "with a pixel size; without one give the values instead"); | |
| 249 | } | ||
| 250 | 262 | } | |
| 251 | |||
| 252 | 36 | RegionShape BuildRegionShape(const RegionShapeSpec& spec) { | |
| 253 | 36 | return std::visit( | |
| 254 | 36 | Overloaded{ | |
| 255 | 5 | [](const RectangleRegionSpec& s) { return RegionShape::Rectangle(s); }, | |
| 256 | 22 | [](const CircleRegionSpec& s) { return RegionShape::Circle({s.x, s.y}, s.radius); }, | |
| 257 | 9 | [](const PolygonRegionSpec& s) { | |
| 258 | 22 | const auto vertices = [](const std::vector<std::array<double, 2>>& pairs) { | |
| 259 | 13 | std::vector<Vec2d> ring; | |
| 260 | 13 | ring.reserve(pairs.size()); | |
| 261 | 59 | for (const std::array<double, 2>& p : pairs) { | |
| 262 | 46 | ring.push_back({p[0], p[1]}); | |
| 263 | } | ||
| 264 | 13 | return ring; | |
| 265 | ✗ | }; | |
| 266 | 9 | std::vector<std::vector<Vec2d>> holes; | |
| 267 | 9 | holes.reserve(s.holes.size()); | |
| 268 | 13 | for (const auto& hole : s.holes) { | |
| 269 | 8 | holes.push_back(vertices(hole)); | |
| 270 | } | ||
| 271 | 18 | return RegionShape::Polygon(vertices(s.points), std::move(holes)); | |
| 272 | 9 | }, | |
| 273 | }, | ||
| 274 | 36 | spec); | |
| 275 | } | ||
| 276 | |||
| 277 | 396 | BuiltLayers BuildLayers(const std::vector<LayerSpec>& layers, | |
| 278 | const std::vector<OutputGrid>& outputs, const LayerSources& sources, | ||
| 279 | const std::filesystem::path& directory, const Surface& surface) { | ||
| 280 | 396 | LayerChecks checks{outputs}; | |
| 281 | 396 | checks.posts = sources.posts; | |
| 282 | 1073 | for (std::size_t i = 0; i < layers.size(); ++i) { | |
| 283 | 1449 | CheckLayer(layers[i], ItemPath("layers", i), checks); | |
| 284 | } | ||
| 285 | 374 | BuiltLayers built; | |
| 286 | 374 | std::vector<std::shared_ptr<const CraterLayer>> craters; | |
| 287 | 374 | std::shared_ptr<const BoulderLayer> boulders; | |
| 288 | 985 | for (std::size_t i = 0; i < layers.size(); ++i) { | |
| 289 | 1309 | const std::string path = ItemPath("layers", i); | |
| 290 | 640 | const LayerInputs inputs{TerrainBase{surface, sources.measured}, | |
| 291 | directory, | ||
| 292 | craters, | ||
| 293 | boulders, | ||
| 294 | 640 | sources.given, | |
| 295 | 1309 | ThrownSecondaries(layers, i)}; | |
| 296 | 640 | const LayerPtr layer = BuildLayer(layers[i], path, inputs); | |
| 297 | 614 | built.layers.push_back(layer); | |
| 298 | 614 | CheckBuiltLayer(layers[i], *layer, path, checks); | |
| 299 | 611 | if (std::holds_alternative<CraterLayerSpec>(layers[i])) { | |
| 300 | 507 | craters.push_back(std::static_pointer_cast<const CraterLayer>(layer)); | |
| 301 | } | ||
| 302 | 611 | if (std::holds_alternative<BoulderLayerSpec>(layers[i])) { | |
| 303 | 152 | boulders = std::static_pointer_cast<const BoulderLayer>(layer); | |
| 304 | } | ||
| 305 | 1283 | } | |
| 306 | 345 | if (!checks.expectedCraters.empty()) { | |
| 307 | 390 | built.expectedCraters = | |
| 308 | 390 | *std::max_element(checks.expectedCraters.begin(), checks.expectedCraters.end()); | |
| 309 | } | ||
| 310 | 345 | return built; | |
| 311 | 403 | } | |
| 312 | |||
| 313 | } // namespace config | ||
| 314 | |||
| 315 | } // namespace terraingen | ||
| 316 |