./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
71 / 73
97.3%
Branches:
77 / 88
87.5%
Functions:
11 / 12
91.7%

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/1
✓ Branch 2 taken 1 time.
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
2/2
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 128 times.
474 if (config.source) {
33 346 return config.source;
34 }
35
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 124 times.
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
3/3
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 124 times.
✓ Branch 3 taken 350 times.
474 sources.posts = spec && HasPosts(*spec);
49
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 469 times.
474 if (external) {
50 5 sources.terrain = external;
51 5 sources.given = external;
52 5 sources.measured = external;
53 5 return sources;
54 }
55
1/1
✓ Branch 1 taken 469 times.
469 sources.terrain =
56
7/8
✓ Branch 1 taken 469 times.
✓ Branch 4 taken 469 times.
✓ Branch 7 taken 350 times.
✓ Branch 8 taken 119 times.
✓ Branch 10 taken 438 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 438 times.
✓ Branch 20 taken 438 times.
1031 BuildSource(spec.value_or(SourceSpec{SyntheticSourceSpec{}}), directory, "source", surface);
57
1/1
✓ Branch 1 taken 438 times.
438 summary.source = BoundedGrid(*sources.terrain);
58
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 119 times.
438 if (spec) {
59 319 sources.given = sources.terrain;
60
5/7
✓ Branch 1 taken 319 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 238 times.
✓ Branch 5 taken 81 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 319 times.
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
1/1
✓ Branch 1 taken 474 times.
690 CheckConfigRanges(config);
70
1/1
✓ Branch 1 taken 474 times.
474 ConfigSummary summary;
71
1/1
✓ Branch 1 taken 474 times.
474 Surface surface = BuildSurface(config.body, directory);
72
1/1
✓ Branch 1 taken 443 times.
474 const LayerSources sources = BuildLayerSources(config, directory, external, surface, summary);
73
1/1
✓ Branch 1 taken 396 times.
443 const OutputNeeds needs{config.albedo.has_value(), config.materials.has_value()};
74
1/1
✓ Branch 1 taken 396 times.
443 std::vector<OutputGrid> outputs = CheckOutputs(config.outputs, needs, surface, summary.source);
75
2/2
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 396 times.
872 for (const OutputGrid& output : outputs) {
76
1/1
✓ Branch 1 taken 476 times.
476 summary.outputGrids.push_back(output.grid);
77 }
78
1/1
✓ Branch 1 taken 345 times.
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
4/9
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 345 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 345 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 345 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
704 std::move(summary), std::move(outputs)};
82
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 300 times.
345 if (config.albedo) {
83
1/1
✓ Branch 1 taken 37 times.
45 CheckAlbedo(*config.albedo, config.layers, directory);
84 }
85
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 328 times.
337 if (config.materials) {
86
1/1
✓ Branch 1 taken 3 times.
9 CheckMaterials(*config.materials, HasOutput<PhotometryOutput>(config.outputs),
87 checked.terrain);
88 }
89 331 return checked;
90
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 331 times.
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
3/3
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 99 times.
✓ Branch 3 taken 138 times.
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
2/3
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
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
3/3
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 78 times.
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
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 7 times.
8 if (!source) {
113
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("BuildTerrain needs a source");
114 }
115
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (config.source) {
116
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
4 throw ConfigError("source", "must be left out when the caller gives the source");
117 }
118
3/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 4 times.
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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const std::filesystem::path directory = ConfigDirectory(config, given);
125
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13 times.
14 if (!config.albedo) {
126
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError("albedo", "the configuration has no albedo");
127 }
128
3/3
✓ Branch 1 taken 13 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
30 config::CheckConfigRanges(*config.albedo, "albedo");
129
1/1
✓ Branch 1 taken 12 times.
12 config::CheckAlbedo(*config.albedo, config.layers, directory);
130
1/1
✓ Branch 1 taken 9 times.
12 return config::BuildValidAlbedo(*config.albedo, config.layers, terrain, directory);
131 9 }
132
133 } // namespace terraingen
134