./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
235 / 238
98.7%
Branches:
207 / 263
78.7%
Functions:
27 / 27
100.0%

src/config/generate.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/config/generate.h"
4
5 #include "config/terrain.h"
6 #include "terraingen/config/build.h"
7 #include "terraingen/config/json.h"
8 #include "terraingen/gpu/backend.h"
9 #include "terraingen/mesh/io/icq.h"
10 #include "terraingen/mesh/mesh_build.h"
11 #include "terraingen/pipeline/body_mesh.h"
12 #include "terraingen/util/log.h"
13
14 #include <algorithm>
15 #include <cmath>
16 #include <cstdint>
17 #include <iomanip>
18 #include <memory>
19 #include <optional>
20 #include <sstream>
21 #include <stdexcept>
22 #include <string>
23 #include <type_traits>
24 #include <utility>
25 #include <variant>
26 #include <vector>
27
28 namespace terraingen {
29
30 namespace {
31
32 /// Whether an output needs the heights over its grid: a raster or a terrain mesh.
33 179 bool NeedsHeights(const OutputSpec& output) {
34 179 return config::HoldsAny<HeightmapOutput, AlbedoOutput, TerrainMeshOutput, MaterialsOutput,
35 179 PhotometryOutput>(output);
36 }
37
38 /// One step of generating a terrain and the time it took over every grid.
39 671 struct StepTime {
40 std::string name;
41 double milliseconds = 0.0;
42 };
43
44 /// The steps after the layers, in order.
45 enum class Step { Albedo, Materials, Features, Meshes };
46
47 /// A sample spacing in meters to three significant figures, or in kilometers from 1 km.
48 104 std::string FormatSpacing(double meters) {
49 104 std::ostringstream text;
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 text << std::setprecision(3);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (meters >= 1000.0) {
52
0/2
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ text << meters / 1000.0 << " km";
53 } else {
54
2/2
✓ Branch 1 taken 104 times.
✓ Branch 4 taken 104 times.
104 text << meters << " m";
55 }
56
1/1
✓ Branch 1 taken 104 times.
208 return text.str();
57 104 }
58
59 /// Logs the time of each step that ran and its share of `total` milliseconds.
60 110 void LogStepTimes(const std::vector<StepTime>& steps, std::size_t grids, double total) {
61 110 std::size_t width = 0;
62
2/2
✓ Branch 0 taken 665 times.
✓ Branch 1 taken 110 times.
775 for (const StepTime& step : steps) {
63
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 412 times.
918 width = std::max(width, step.name.size());
64 }
65
3/3
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 101 times.
✓ Branch 3 taken 110 times.
119 LogInfo("pipeline", "Time per step over ", grids, grids == 1 ? " grid" : " grids", ", of ",
66 110 FormatDuration(total), ":");
67
2/2
✓ Branch 0 taken 665 times.
✓ Branch 1 taken 110 times.
775 for (const StepTime& step : steps) {
68
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 269 times.
665 if (step.milliseconds <= 0.0) {
69 396 continue;
70 }
71 269 std::ostringstream line;
72
2/2
✓ Branch 1 taken 269 times.
✓ Branch 5 taken 269 times.
269 line << " " << std::left << std::setw(static_cast<int>(width)) << step.name << std::right
73
2/2
✓ Branch 1 taken 269 times.
✓ Branch 4 taken 269 times.
538 << std::setw(10) << FormatDuration(step.milliseconds) << std::setw(5)
74
2/2
✓ Branch 1 taken 269 times.
✓ Branch 4 taken 269 times.
269 << std::llround(100.0 * step.milliseconds / std::fmax(total, 1e-9)) << "%";
75
2/2
✓ Branch 1 taken 269 times.
✓ Branch 4 taken 269 times.
269 LogInfo("pipeline", line.str());
76 269 }
77 110 }
78
79 /// The distinct grids of `grids`, in order.
80 111 std::vector<RasterGrid> DistinctGrids(const std::vector<std::optional<RasterGrid>>& grids) {
81 111 std::vector<RasterGrid> distinct;
82
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 111 times.
304 for (const std::optional<RasterGrid>& grid : grids) {
83
4/4
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 63 times.
193 if (grid && std::find(distinct.begin(), distinct.end(), *grid) == distinct.end()) {
84
1/1
✓ Branch 1 taken 116 times.
116 distinct.push_back(*grid);
85 }
86 }
87 111 return distinct;
88 }
89
90 /// Generates what each output asks for over its grid (`GeneratedOutputs::outputGrids`), once per
91 /// grid and result, timing each step.
92 class OutputGenerator {
93 public:
94 /// @param outputs What each output covers (`config::CheckedTerrain::outputs`).
95 111 OutputGenerator(const ConfigSpec& config, GeneratedOutputs& generated,
96 const std::filesystem::path& directory, unsigned threads,
97 std::vector<config::OutputGrid> outputs)
98 111 : m_config(config), m_generated(generated), m_directory(directory), m_threads(threads),
99
1/1
✓ Branch 1 taken 111 times.
111 m_outputs(std::move(outputs)), m_grids(DistinctGrids(generated.outputGrids)) {
100
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 111 times.
338 for (std::size_t n = 0; n < config.layers.size(); ++n) {
101 227 m_steps.push_back(
102
2/2
✓ Branch 1 taken 227 times.
✓ Branch 4 taken 227 times.
454 {"layer " + std::to_string(n + 1) + " " + LayerType(config.layers[n])});
103 }
104
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 111 times.
555 for (const char* name : {"albedo", "materials", "features", "meshes"}) {
105 444 m_steps.push_back({name});
106 }
107
5/5
✓ Branch 1 taken 227 times.
✓ Branch 4 taken 227 times.
✓ Branch 7 taken 227 times.
✓ Branch 10 taken 444 times.
✓ Branch 13 taken 444 times.
1453 }
108
109 /// Generates what output `i` asks for, unless an earlier output did.
110 193 void Generate(std::size_t i) {
111
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
193 const OutputSpec& output = m_config.outputs[i];
112
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
193 if (const auto* mesh = std::get_if<MeshOutput>(&output)) {
113 7 AddBodyMesh(*mesh);
114 7 return;
115 }
116
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 7 times.
186 const std::optional<RasterGrid>& grid = m_generated.outputGrids[i];
117
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 7 times.
186 if (!grid) {
118 return;
119 }
120 179 GeneratedGrid& target = Target(*grid);
121
4/4
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 15 times.
179 if (NeedsHeights(output) && target.heights.Empty()) {
122 104 AddHeights(target);
123 }
124
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 170 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
179 if (std::holds_alternative<AlbedoOutput>(output) && target.albedo.empty()) {
125 9 AddAlbedo(target);
126 }
127 179 AddMaterials(output, target);
128 179 AddFeatures(output, m_outputs[i].featureSpacing, target);
129
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
186 if (const auto* mesh = std::get_if<TerrainMeshOutput>(&output); mesh && !target.mesh) {
130 7 AddTerrainMesh(*mesh, target);
131 }
132 }
133
134 /// Logs the time of each step, of `total` milliseconds, when anything was generated.
135 111 void LogTimes(double total) const {
136
6/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
111 if (!m_generated.grids.empty() || m_generated.bodyMesh || m_generated.bodyIcq) {
137 110 LogStepTimes(m_steps, m_generated.grids.size(), total);
138 }
139 111 }
140
141 /// Each layer's type and its time summed over every evaluation, in order.
142 7 std::vector<LayerTiming> LayerTimings() const {
143 7 std::vector<LayerTiming> timings;
144
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7 times.
25 for (std::size_t n = 0; n < m_config.layers.size(); ++n) {
145
2/2
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
18 timings.push_back({LayerType(m_config.layers[n]), m_steps[n].milliseconds / 1000.0});
146 }
147 7 return timings;
148
2/2
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
18 }
149
150 private:
151 285 void Timed(std::size_t step, double milliseconds) {
152 285 m_steps[step].milliseconds += milliseconds;
153
1/1
✓ Branch 2 taken 285 times.
285 LogInfo("pipeline", " ", m_steps[step].name, " in ", FormatDuration(milliseconds));
154 285 }
155
156 77 void Timed(Step step, double milliseconds) {
157 77 Timed(m_config.layers.size() + static_cast<std::size_t>(step), milliseconds);
158 77 }
159
160 /// The generated grid over `grid`, added empty the first time.
161 179 GeneratedGrid& Target(const RasterGrid& grid) {
162
2/2
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 63 times.
179 if (GeneratedGrid* existing = m_generated.Find(grid)) {
163 return *existing;
164 }
165 116 GeneratedGrid added;
166 116 added.grid = grid;
167
1/1
✓ Branch 1 taken 116 times.
116 m_generated.grids.push_back(std::move(added));
168 116 return m_generated.grids.back();
169 116 }
170
171 /// How grids are evaluated: on the configured threads and backend.
172 105 EvaluationOptions Options() const {
173
1/1
✓ Branch 1 taken 105 times.
105 return EvaluationOptions{}.Threads(m_threads).On(m_generated.backend);
174 }
175
176 104 void AddHeights(GeneratedGrid& target) {
177 104 const Terrain& terrain = m_generated.terrain;
178 104 const auto index = static_cast<std::size_t>(
179 104 std::find(m_grids.begin(), m_grids.end(), target.grid) - m_grids.begin());
180 104 LogInfo("pipeline", "Grid ", index + 1, " of ", m_grids.size(), ": ", target.grid.width,
181
1/1
✓ Branch 1 taken 104 times.
104 "x", target.grid.height, " at ",
182 104 FormatSpacing(target.grid.Spacing(terrain.Surface())));
183 104 EvaluationOptions options = Options();
184 416 options.onLayer = [this](std::size_t step, double milliseconds) {
185 208 Timed(step, milliseconds);
186 104 };
187
1/1
✓ Branch 1 taken 104 times.
104 target.heights = terrain.Evaluate(target.grid, options);
188 104 }
189
190 9 void AddAlbedo(GeneratedGrid& target) {
191 9 const Stopwatch stopwatch;
192 9 const Terrain& terrain = m_generated.terrain;
193
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!m_albedo) {
194 9 m_albedo =
195 9 config::BuildValidAlbedo(*m_config.albedo, m_config.layers, terrain, m_directory);
196 }
197 9 target.albedo = EvaluateAlbedo(*m_albedo, target.grid, terrain.Context(0.0, m_threads));
198 9 Timed(Step::Albedo, stopwatch.Milliseconds());
199 9 }
200
201 /// Adds the material map a materials or photometry output asks for, and the photometry.
202 179 void AddMaterials(const OutputSpec& output, GeneratedGrid& target) {
203
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 177 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
179 if (config::HoldsAny<MaterialsOutput, PhotometryOutput>(output) &&
204
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 target.materials.empty()) {
205 1 const Stopwatch stopwatch;
206 1 target.materials = EvaluateMaterials(m_generated.terrain, m_config.materials->options,
207
1/1
✓ Branch 2 taken 1 time.
1 target.grid, Options());
208 1 Timed(Step::Materials, stopwatch.Milliseconds());
209 }
210
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
179 if (std::holds_alternative<PhotometryOutput>(output) && target.photometry.empty()) {
211 1 const Stopwatch stopwatch;
212 4 target.photometry = EvaluatePhotometry(
213 1 target.materials, config::BuildPhotometry(m_config.materials->photometry));
214 1 Timed(Step::Materials, stopwatch.Milliseconds());
215 }
216 179 }
217
218 /// Places the craters a crater list asks for, or the boulders a boulder list or models do, at
219 /// `spacing` meters (0 for the grid's own).
220 179 void AddFeatures(const OutputSpec& output, double spacing, GeneratedGrid& target) {
221 179 const Terrain& terrain = m_generated.terrain;
222
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 1 time.
179 if (std::holds_alternative<CraterListOutput>(output) && !target.craters) {
223 42 const Stopwatch stopwatch;
224 42 target.craters = terrain.Craters(target.grid, spacing);
225 42 Timed(Step::Features, stopwatch.Milliseconds());
226 }
227
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 162 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 6 times.
179 if (config::HoldsAny<BoulderListOutput, BoulderModelsOutput>(output) && !target.boulders) {
228 11 const Stopwatch stopwatch;
229 11 target.boulders = terrain.Boulders(target.grid, spacing);
230 11 Timed(Step::Features, stopwatch.Milliseconds());
231 }
232 179 }
233
234 7 void AddTerrainMesh(const TerrainMeshOutput& output, GeneratedGrid& target) {
235 7 const Stopwatch stopwatch;
236 7 BuildMeshOptions options;
237 7 options.skirt = output.skirt;
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 for (const ExtentSpec& cutout : output.cutouts) {
239
0/1
✗ Branch 1 not taken.
✗ options.cutouts.push_back(config::ExtentBox(cutout));
240 }
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 const Vec2d site = output.siteDeg ? Vec2d{(*output.siteDeg)[0], (*output.siteDeg)[1]}
242
1/1
✓ Branch 1 taken 7 times.
7 : target.grid.Bounds().Center();
243
1/1
✓ Branch 1 taken 7 times.
7 target.mesh = BuildMesh(target.heights, m_generated.terrain.Surface(), site, options);
244
1/1
✓ Branch 2 taken 7 times.
7 Timed(Step::Meshes, stopwatch.Milliseconds());
245 7 }
246
247 7 void AddBodyMesh(const MeshOutput& output) {
248 7 const Stopwatch stopwatch;
249 7 const Terrain& terrain = m_generated.terrain;
250
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 const double spacing = output.spacing.value_or(0.0);
251
3/3
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 4 times.
14 if (IsIcqPath(output.path)) {
252
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!m_generated.bodyIcq) {
253 3 m_generated.bodyIcq = BodyIcq(terrain, output.cellsPerFace, spacing, m_threads);
254 3 Timed(Step::Meshes, stopwatch.Milliseconds());
255 }
256
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 } else if (!m_generated.bodyMesh) {
257 3 m_generated.bodyMesh = BodyMesh(terrain, output.cellsPerFace, spacing, m_threads);
258 3 Timed(Step::Meshes, stopwatch.Milliseconds());
259 }
260 7 }
261
262 const ConfigSpec& m_config;
263 GeneratedOutputs& m_generated;
264 const std::filesystem::path& m_directory;
265 unsigned m_threads;
266 std::vector<config::OutputGrid> m_outputs;
267 std::vector<RasterGrid> m_grids;
268 std::vector<StepTime> m_steps;
269 std::optional<Albedo> m_albedo;
270 };
271
272 /// A field-list `IO` that finds a layer's top-level "seed", or its inner layer's in a masked
273 /// layer.
274 class SeedFinder {
275 public:
276 template <typename T>
277
3/20
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 8 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
15 void operator()(std::string_view key, T& value, std::string_view /*description*/ = {},
278 std::string_view /*unit*/ = {}, const FieldRange& /*range*/ = {}) {
279 if constexpr (std::is_same_v<T, std::uint32_t>) {
280
3/20
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
14 if (key == "seed") {
281 11 seed = value;
282 }
283 } else if constexpr (std::is_same_v<T, Boxed<LayerSpec>>) {
284 1 Find(*value);
285 }
286 1 }
287
288 /// Visits the fields of `layer`.
289 19 void Find(LayerSpec layer) {
290
1/1
✓ Branch 1 taken 1 time.
20 std::visit([this](auto& spec) { Fields(*this, spec); }, layer);
291 }
292
293 std::optional<std::uint32_t> seed;
294 };
295
296 /// The top-level "seed" of each layer that has one, in order, a masked layer's from inside.
297 7 std::vector<std::uint32_t> LayerSeeds(const ConfigSpec& config) {
298 7 std::vector<std::uint32_t> seeds;
299
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7 times.
25 for (const LayerSpec& layer : config.layers) {
300 18 SeedFinder finder;
301
2/2
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
36 finder.Find(layer);
302
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
18 if (finder.seed) {
303
1/1
✓ Branch 1 taken 11 times.
11 seeds.push_back(*finder.seed);
304 }
305 }
306 7 return seeds;
307 }
308
309 /// The density of the terrain's first top-level crater layer, or null without one.
310 7 std::shared_ptr<const SizeFrequencyDistribution> CraterTarget(const Terrain& terrain) {
311
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 for (const LayerPtr& layer : terrain.Layers()) {
312
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
14 if (const auto* craters = dynamic_cast<const CraterLayer*>(layer.get())) {
313
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return craters->Models().density;
314 }
315 }
316 ✗ return nullptr;
317 }
318
319 /// The report of what was generated from `config`, with each layer's summed `timings`.
320 7 TerrainReport BuildReport(const ConfigSpec& config, const GeneratedOutputs& generated,
321 std::vector<LayerTiming> timings, unsigned threads) {
322 7 TerrainReport report;
323 7 const std::shared_ptr<const SizeFrequencyDistribution> target = CraterTarget(generated.terrain);
324
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7 times.
16 for (const GeneratedGrid& grid : generated.grids) {
325
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 if (grid.heights.Empty()) {
326 1 continue;
327 }
328
5/5
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 2 times.
✓ Branch 5 taken 8 times.
25 report.grids.push_back(ReportGrid(
329
3/3
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 8 times.
8 grid.heights, generated.terrain.Surface(), grid.craters ? &*grid.craters : nullptr,
330
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 grid.boulders ? &*grid.boulders : nullptr, target.get(), threads));
331 }
332 7 report.layers = std::move(timings);
333
2/2
✓ Branch 1 taken 7 times.
✓ Branch 6 taken 7 times.
14 report.backend = std::string(BackendName(generated.backend));
334 7 report.threads = threads;
335
1/3
✓ Branch 1 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
7 report.seeds = LayerSeeds(config);
336 7 report.seconds = generated.seconds;
337
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return report;
338 7 }
339
340 } // namespace
341
342 16 const GeneratedGrid& GeneratedOutputs::Grid() const {
343
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
16 if (grids.size() != 1) {
344
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 throw std::runtime_error("The outputs cover " + std::to_string(grids.size()) +
345
1/1
✓ Branch 2 taken 2 times.
6 " grids, not one; find a grid by its RasterGrid instead");
346 }
347 14 return grids.front();
348 }
349
350 233 const GeneratedGrid* GeneratedOutputs::Find(const RasterGrid& grid) const {
351
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 117 times.
247 for (const GeneratedGrid& generated : grids) {
352
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 116 times.
130 if (generated.grid == grid) {
353 233 return &generated;
354 }
355 }
356 return nullptr;
357 }
358
359 180 GeneratedGrid* GeneratedOutputs::Find(const RasterGrid& grid) {
360 180 return const_cast<GeneratedGrid*>(std::as_const(*this).Find(grid));
361 }
362
363 113 GeneratedOutputs GenerateFromConfig(const TerrainGenConfig& value,
364 const std::filesystem::path& given, unsigned threads,
365 std::optional<Backend> backend) {
366 113 const ConfigSpec& config = value.Spec();
367
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 27 times.
113 const std::filesystem::path directory = ConfigDirectory(config, given);
368
1/1
✓ Branch 1 taken 111 times.
115 config::CheckedTerrain checked = config::BuildCheckedTerrain(config, directory);
369 111 GeneratedOutputs generated{std::move(checked.terrain)};
370 111 generated.outputGrids = std::move(checked.summary.outputGrids);
371
4/4
✓ Branch 1 taken 111 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 106 times.
✓ Branch 6 taken 111 times.
116 generated.backend = ResolveBackend(backend.value_or(DefaultBackend()));
372 111 const Stopwatch stopwatch;
373
1/1
✓ Branch 1 taken 111 times.
111 OutputGenerator outputGenerator(config, generated, directory, threads,
374
1/1
✓ Branch 1 taken 111 times.
111 std::move(checked.outputs));
375
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 111 times.
304 for (std::size_t i = 0; i < config.outputs.size(); ++i) {
376
1/1
✓ Branch 1 taken 193 times.
193 outputGenerator.Generate(i);
377 }
378
1/1
✓ Branch 1 taken 111 times.
111 generated.geoKeys = generated.terrain.Keys();
379 111 generated.seconds = stopwatch.Milliseconds() / 1000.0;
380
1/1
✓ Branch 2 taken 111 times.
111 outputGenerator.LogTimes(stopwatch.Milliseconds());
381
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 104 times.
111 if (config::HasOutput<ReportOutput>(config.outputs)) {
382
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 generated.report = BuildReport(config, generated, outputGenerator.LayerTimings(), threads);
383 }
384 222 return generated;
385 222 }
386
387 10 std::string Summary(const GeneratedOutputs& generated) {
388 10 std::vector<const Raster<float>*> heights;
389
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 for (const GeneratedGrid& grid : generated.grids) {
390
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!grid.heights.Empty()) {
391
1/1
✓ Branch 1 taken 10 times.
10 heights.push_back(&grid.heights);
392 }
393 }
394
1/1
✓ Branch 1 taken 10 times.
10 std::ostringstream text;
395
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
10 text << std::setprecision(3);
396
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
10 if (heights.empty()) {
397
1/1
✓ Branch 1 taken 1 time.
1 text << "Generated the outputs, with no heights to evaluate,";
398
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 } else if (heights.size() == 1) {
399
5/5
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
8 text << "Generated a " << heights[0]->Width() << "x" << heights[0]->Height() << " terrain";
400 } else {
401
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 text << "Generated " << heights.size() << " grids (";
402
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 for (std::size_t i = 0; i < heights.size(); ++i) {
403
6/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
✓ Branch 12 taken 2 times.
3 text << (i > 0 ? ", " : "") << heights[i]->Width() << "x" << heights[i]->Height();
404 }
405
1/1
✓ Branch 1 taken 1 time.
1 text << ")";
406 }
407
4/4
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
20 text << " in " << generated.seconds << " s on " << DescribeBackend(generated.backend);
408
1/1
✓ Branch 1 taken 10 times.
20 return text.str();
409 10 }
410
411 } // namespace terraingen
412