src/config/common.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/common.h" | ||
| 4 | |||
| 5 | namespace terraingen { | ||
| 6 | |||
| 7 | 881 | Fractal FractalSpec::Effective() const { | |
| 8 | 881 | Fractal effective = fractal; | |
| 9 | 881 | if (hurst) { | |
| 10 | 19 | effective.gain = GainFromHurst(*hurst, fractal.lacunarity); | |
| 11 | } | ||
| 12 | 881 | if (amplitude) { | |
| 13 | 822 | effective.amplitude = *amplitude; | |
| 14 | } | ||
| 15 | 881 | return effective; | |
| 16 | } | ||
| 17 | |||
| 18 | 395 | Fractal FractalSpec::EffectiveRelief() const { | |
| 19 | 395 | Fractal effective = Effective(); | |
| 20 | 395 | if (!amplitude) { | |
| 21 | 30 | effective.amplitude = kNoiseAmplitudeRatio * fractal.wavelength; | |
| 22 | } | ||
| 23 | 395 | return effective; | |
| 24 | } | ||
| 25 | |||
| 26 | 603 | std::vector<Fractal> StackFractals(const std::vector<FractalSpec>& stack) { | |
| 27 | 603 | std::vector<Fractal> fractals; | |
| 28 | 988 | for (const FractalSpec& spec : stack) { | |
| 29 | 385 | fractals.push_back(spec.EffectiveRelief()); | |
| 30 | } | ||
| 31 | 603 | return fractals; | |
| 32 | } | ||
| 33 | |||
| 34 | namespace config { | ||
| 35 | |||
| 36 | namespace { | ||
| 37 | |||
| 38 | /// The most craters or candidate boulders a layer may be expected to draw over one output, as | ||
| 39 | /// `CraterGenerationOptions::maxExpectedCraters` caps craters. | ||
| 40 | constexpr double kMaxExpectedCount = 5e7; | ||
| 41 | |||
| 42 | } // namespace | ||
| 43 | |||
| 44 | 197 | std::filesystem::path Resolve(const std::string& path, const std::filesystem::path& directory) { | |
| 45 | 197 | const std::filesystem::path resolved = path; | |
| 46 | 197 | return resolved.is_relative() && !directory.empty() ? directory / resolved : resolved; | |
| 47 | 197 | } | |
| 48 | |||
| 49 | 509 | void CheckGridSize(std::size_t width, std::size_t height, const std::string& path) { | |
| 50 | 1011 | const auto fits = [](std::size_t side) { | |
| 51 | 1011 | return side >= kMinConfigGridSize && side <= kMaxConfigGridSize; | |
| 52 | }; | ||
| 53 | 509 | if (!fits(width) || !fits(height)) { | |
| 54 | 21 | throw ConfigError(path, std::to_string(width) + "x" + std::to_string(height) + | |
| 55 | 14 | " cells; each side must be " + | |
| 56 | 28 | CountText(kMinConfigGridSize, kMaxConfigGridSize)); | |
| 57 | } | ||
| 58 | 502 | } | |
| 59 | |||
| 60 | 101 | void CheckBand(std::size_t band, const GeoTiffInfo& info, const std::string& path) { | |
| 61 | 101 | if (band >= info.bands) { | |
| 62 | 4 | throw ConfigError(path, "the file has " + std::to_string(info.bands) + | |
| 63 | 8 | (info.bands == 1 ? " band" : " bands")); | |
| 64 | } | ||
| 65 | 99 | } | |
| 66 | |||
| 67 | 460 | void CheckExpectedCount(double expected, std::string_view items, std::string_view remedy, | |
| 68 | const std::string& path) { | ||
| 69 | 460 | if (expected > kMaxExpectedCount) { | |
| 70 | 3 | throw ConfigError(path, "about " + std::to_string(static_cast<long long>(expected)) + " " + | |
| 71 | 5 | std::string(items) + " expected, more than the limit of " + | |
| 72 | 2 | std::to_string(static_cast<long long>(kMaxExpectedCount)) + | |
| 73 | 5 | "; " + std::string(remedy)); | |
| 74 | } | ||
| 75 | 459 | } | |
| 76 | |||
| 77 | 22 | std::shared_ptr<const SurfaceField> BuildField(const FieldSpec& spec, const std::string& path, | |
| 78 | const std::filesystem::path& directory) { | ||
| 79 | 22 | if (spec.path.empty()) { | |
| 80 | 18 | return std::make_shared<ConstantField>(spec.value); | |
| 81 | } | ||
| 82 | 4 | return AtFile( | |
| 83 | 6 | path, [&] { return ReadRasterField(Resolve(spec.path, directory), spec.read, spec.band); }); | |
| 84 | } | ||
| 85 | |||
| 86 | } // namespace config | ||
| 87 | |||
| 88 | } // namespace terraingen | ||
| 89 |