./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
46 / 46
100.0%
Branches:
47 / 49
95.9%
Functions:
9 / 9
100.0%

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
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 862 times.
881 if (hurst) {
10 19 effective.gain = GainFromHurst(*hurst, fractal.lacunarity);
11 }
12
2/2
✓ Branch 0 taken 822 times.
✓ Branch 1 taken 59 times.
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
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 365 times.
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
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 603 times.
988 for (const FractalSpec& spec : stack) {
29
1/1
✓ Branch 2 taken 385 times.
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
6/6
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 189 times.
✓ Branch 4 taken 4 times.
✓ Branch 6 taken 189 times.
✓ Branch 9 taken 8 times.
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
3/4
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 502 times.
509 if (!fits(width) || !fits(height)) {
54
3/3
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
21 throw ConfigError(path, std::to_string(width) + "x" + std::to_string(height) +
55
1/1
✓ Branch 1 taken 7 times.
14 " cells; each side must be " +
56
3/3
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 7 times.
✓ Branch 8 taken 7 times.
28 CountText(kMinConfigGridSize, kMaxConfigGridSize));
57 }
58 502 }
59
60 101 void CheckBand(std::size_t band, const GeoTiffInfo& info, const std::string& path) {
61
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 99 times.
101 if (band >= info.bands) {
62
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 throw ConfigError(path, "the file has " + std::to_string(info.bands) +
63
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
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
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 459 times.
460 if (expected > kMaxExpectedCount) {
70
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
3 throw ConfigError(path, "about " + std::to_string(static_cast<long long>(expected)) + " " +
71
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
5 std::string(items) + " expected, more than the limit of " +
72 2 std::to_string(static_cast<long long>(kMaxExpectedCount)) +
73
5/5
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
✓ Branch 14 taken 1 time.
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
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
22 if (spec.path.empty()) {
80 18 return std::make_shared<ConstantField>(spec.value);
81 }
82 4 return AtFile(
83
1/1
✓ Branch 2 taken 1 time.
6 path, [&] { return ReadRasterField(Resolve(spec.path, directory), spec.read, spec.band); });
84 }
85
86 } // namespace config
87
88 } // namespace terraingen
89