./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
121 / 128
94.5%
Branches:
83 / 138
60.1%
Functions:
20 / 20
100.0%

src/config/write_outputs.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/mesh/io/icq.h"
7 #include "terraingen/mesh/io/mesh_io.h"
8 #include "terraingen/util/bits.h"
9 #include "terraingen/util/log.h"
10
11 #include <filesystem>
12 #include <optional>
13 #include <stdexcept>
14 #include <string>
15 #include <type_traits>
16 #include <utility>
17 #include <variant>
18 #include <vector>
19
20 namespace terraingen {
21
22 namespace {
23
24 /// What `WriteOutputs` writes one output from.
25 struct OutputContext {
26 const GeneratedOutputs& generated;
27 /// What was generated over the output's grid; null for an output without one.
28 const GeneratedGrid* grid;
29 };
30
31 /// `result`, which an output at `path` writes.
32 /// @throws std::runtime_error if it was not generated.
33 template <typename T>
34
6/10
terraingen::BoulderFeatures const& terraingen::(anonymous namespace)::Require<terraingen::BoulderFeatures>(std::optional<terraingen::BoulderFeatures> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
terraingen::CraterFeatures const& terraingen::(anonymous namespace)::Require<terraingen::CraterFeatures>(std::optional<terraingen::CraterFeatures> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
terraingen::IcqModel const& terraingen::(anonymous namespace)::Require<terraingen::IcqModel>(std::optional<terraingen::IcqModel> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
terraingen::TerrainReport const& terraingen::(anonymous namespace)::Require<terraingen::TerrainReport>(std::optional<terraingen::TerrainReport> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
terraingen::TriangleMesh const& terraingen::(anonymous namespace)::Require<terraingen::TriangleMesh>(std::optional<terraingen::TriangleMesh> const&, char const*, std::filesystem::__cxx11::path const&):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
39 const T& Require(const std::optional<T>& result, const char* what,
35 const std::filesystem::path& path) {
36
6/10
terraingen::BoulderFeatures const& terraingen::(anonymous namespace)::Require<terraingen::BoulderFeatures>(std::optional<terraingen::BoulderFeatures> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
terraingen::CraterFeatures const& terraingen::(anonymous namespace)::Require<terraingen::CraterFeatures>(std::optional<terraingen::CraterFeatures> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
terraingen::IcqModel const& terraingen::(anonymous namespace)::Require<terraingen::IcqModel>(std::optional<terraingen::IcqModel> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
terraingen::TerrainReport const& terraingen::(anonymous namespace)::Require<terraingen::TerrainReport>(std::optional<terraingen::TerrainReport> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
terraingen::TriangleMesh const& terraingen::(anonymous namespace)::Require<terraingen::TriangleMesh>(std::optional<terraingen::TriangleMesh> const&, char const*, std::filesystem::__cxx11::path const&):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
39 if (!result) {
37
5/25
terraingen::BoulderFeatures const& terraingen::(anonymous namespace)::Require<terraingen::BoulderFeatures>(std::optional<terraingen::BoulderFeatures> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
terraingen::CraterFeatures const& terraingen::(anonymous namespace)::Require<terraingen::CraterFeatures>(std::optional<terraingen::CraterFeatures> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
terraingen::IcqModel const& terraingen::(anonymous namespace)::Require<terraingen::IcqModel>(std::optional<terraingen::IcqModel> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
terraingen::TerrainReport const& terraingen::(anonymous namespace)::Require<terraingen::TerrainReport>(std::optional<terraingen::TerrainReport> const&, char const*, std::filesystem::__cxx11::path const&):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
terraingen::TriangleMesh const& terraingen::(anonymous namespace)::Require<terraingen::TriangleMesh>(std::optional<terraingen::TriangleMesh> const&, char const*, std::filesystem::__cxx11::path const&):
✓ 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.
4 throw std::runtime_error(std::string("No ") + what + " generated for " + path.string());
38 }
39 38 return *result;
40 }
41
42 /// @name Bands
43 /// The bands a GeoTIFF output other than a heightmap writes.
44 /// @{
45 5 const std::vector<Raster<float>>& Bands(const AlbedoOutput&, const GeneratedGrid& grid) {
46 5 return grid.albedo;
47 }
48 1 const std::vector<Raster<float>>& Bands(const MaterialsOutput&, const GeneratedGrid& grid) {
49 1 return grid.materials;
50 }
51 1 const std::vector<Raster<float>>& Bands(const PhotometryOutput&, const GeneratedGrid& grid) {
52 1 return grid.photometry;
53 }
54 /// @}
55
56 /// Each `Write` writes one output to `path` and returns whether it wrote the file.
57 /// @throws std::runtime_error if what it writes was not generated or cannot be written.
58 template <typename Output, std::enable_if_t<std::is_base_of_v<GeoTiffOutput, Output>, int> = 0>
59 21 bool Write(const Output& output, const std::filesystem::path& path, const OutputContext& context) {
60 21 const GeoTiffWriteOptions options{output.compression, output.tileSize};
61 21 const GeneratedGrid& grid = *context.grid;
62 if constexpr (std::is_same_v<Output, HeightmapOutput>) {
63
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13 times.
14 if (grid.heights.Empty()) {
64
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw std::runtime_error("No heights generated for " + path.string());
65 }
66 13 WriteGeoTiff(path, grid.heights, context.generated.geoKeys, options);
67 } else {
68
4/6
bool terraingen::(anonymous namespace)::Write<terraingen::AlbedoOutput, 0>(terraingen::AlbedoOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
bool terraingen::(anonymous namespace)::Write<terraingen::MaterialsOutput, 0>(terraingen::MaterialsOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
bool terraingen::(anonymous namespace)::Write<terraingen::PhotometryOutput, 0>(terraingen::PhotometryOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
7 const std::vector<Raster<float>>& bands = Bands(output, grid);
69
4/6
bool terraingen::(anonymous namespace)::Write<terraingen::AlbedoOutput, 0>(terraingen::AlbedoOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
bool terraingen::(anonymous namespace)::Write<terraingen::MaterialsOutput, 0>(terraingen::MaterialsOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
bool terraingen::(anonymous namespace)::Write<terraingen::PhotometryOutput, 0>(terraingen::PhotometryOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
7 if (bands.empty()) {
70
5/15
bool terraingen::(anonymous namespace)::Write<terraingen::AlbedoOutput, 0>(terraingen::AlbedoOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✓ 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.
bool terraingen::(anonymous namespace)::Write<terraingen::MaterialsOutput, 0>(terraingen::MaterialsOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
bool terraingen::(anonymous namespace)::Write<terraingen::PhotometryOutput, 0>(terraingen::PhotometryOutput const&, std::filesystem::__cxx11::path const&, terraingen::(anonymous namespace)::OutputContext const&):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
4 throw std::runtime_error("No " + std::string(SpecName<Output>::kName) +
71 " generated for " + path.string());
72 }
73 6 WriteGeoTiff(path, bands, context.generated.geoKeys, options);
74 }
75 19 return true;
76 }
77
78 15 bool Write(const CraterListOutput& output, const std::filesystem::path& path,
79 const OutputContext& context) {
80 15 const CraterFeatures& placed = Require(context.grid->craters, "craters", path);
81 15 WriteCraterList(placed.craters, path, output.format, placed.attributes);
82
1/1
✓ Branch 2 taken 15 times.
15 LogInfo("pipeline", "Wrote ", placed.craters.Size(), " craters to ", path.string());
83 15 return true;
84 }
85
86 4 bool Write(const BoulderListOutput& output, const std::filesystem::path& path,
87 const OutputContext& context) {
88 4 const BoulderList& boulders = Require(context.grid->boulders, "boulders", path).boulders;
89 4 WriteBoulderList(boulders, path, output.format);
90
1/1
✓ Branch 2 taken 4 times.
4 LogInfo("pipeline", "Wrote ", boulders.Size(), " boulders to ", path.string());
91 4 return true;
92 }
93
94 7 bool Write(const BoulderModelsOutput& output, const std::filesystem::path& path,
95 const OutputContext& context) {
96 7 const std::vector<BoulderModel>& models =
97 7 Require(context.grid->boulders, "boulders", path).models;
98
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
7 if (models.empty()) {
99
1/1
✓ Branch 2 taken 1 time.
1 LogWarning("pipeline", "No boulder models to write to ", path.string());
100 1 return false;
101 }
102 12 WriteBoulderModels(models, path, output.creaseAngle, context.generated.terrain.Surface(),
103 6 context.grid->grid.Bounds().Center(), output.instanced);
104 12 LogInfo("pipeline", "Wrote ", models.size(), " boulder models to ", path.string());
105 6 return true;
106 }
107
108 2 bool Write(const TerrainMeshOutput& /*output*/, const std::filesystem::path& path,
109 const OutputContext& context) {
110 2 TriangleMesh mesh = Require(context.grid->mesh, "terrain mesh", path);
111
1/1
✓ Branch 1 taken 2 times.
2 const std::size_t triangles = mesh.triangles.size();
112
1/1
✓ Branch 1 taken 2 times.
2 std::vector<Vec3d> normals = VertexNormals(mesh);
113
2/2
✓ Branch 1 taken 2 times.
✓ Branch 5 taken 2 times.
2 WriteMesh(path, {std::move(mesh), std::move(normals), {"terrain", {0.5, 0.5, 0.5}}});
114
1/1
✓ Branch 1 taken 2 times.
2 LogInfo("pipeline", "Wrote a terrain mesh of ", triangles, " triangles to ", path.string());
115 4 return true;
116
2/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
8 }
117
118 8 bool Write(const MeshOutput& output, const std::filesystem::path& path,
119 const OutputContext& context) {
120 8 const GeneratedOutputs& generated = context.generated;
121
3/3
✓ Branch 2 taken 8 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 5 times.
16 if (IsIcqPath(output.path)) {
122 3 WriteIcq(path, Require(generated.bodyIcq, "ICQ shape model", path));
123 6 LogInfo("pipeline", "Wrote an ICQ shape model of Q = ", output.cellsPerFace, " to ",
124
1/1
✓ Branch 1 taken 3 times.
3 path.string());
125 3 return true;
126 }
127 5 const TriangleMesh& mesh = Require(generated.bodyMesh, "body mesh", path);
128 4 ShadedMesh shaded = WithCreases(mesh, kPi);
129
2/2
✓ Branch 1 taken 4 times.
✓ Branch 5 taken 4 times.
4 WriteMesh(path, {std::move(shaded.mesh), std::move(shaded.normals)});
130 4 LogInfo("pipeline", "Wrote a body mesh of ", mesh.triangles.size(), " triangles to ",
131
1/1
✓ Branch 1 taken 4 times.
4 path.string());
132 4 return true;
133
2/6
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
12 }
134
135 3 bool Write(const ReportOutput& /*output*/, const std::filesystem::path& path,
136 const OutputContext& context) {
137 3 WriteReport(path, Require(context.generated.report, "report", path));
138
1/1
✓ Branch 2 taken 3 times.
3 LogInfo("pipeline", "Wrote the report to ", path.string());
139 3 return true;
140 }
141
142 /// The path an output names, empty for none.
143 75 const std::string& OutputPath(const OutputSpec& spec) {
144 150 return std::visit([](const auto& output) -> const std::string& { return output.path; }, spec);
145 }
146
147 } // namespace
148
149 35 std::vector<std::filesystem::path> WriteOutputs(const TerrainGenConfig& value,
150 const GeneratedOutputs& generated,
151 const std::filesystem::path& directory) {
152 35 const ConfigSpec& config = value.Spec();
153
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 32 times.
35 if (generated.outputGrids.size() != config.outputs.size()) {
154
1/1
✓ Branch 2 taken 3 times.
3 throw std::runtime_error("The outputs were generated from another configuration");
155 }
156 32 std::vector<std::filesystem::path> written;
157
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 28 times.
103 for (std::size_t i = 0; i < config.outputs.size(); ++i) {
158
1/1
✓ Branch 1 taken 75 times.
75 const OutputSpec& spec = config.outputs[i];
159
1/1
✓ Branch 1 taken 75 times.
75 const std::string& relative = OutputPath(spec);
160
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 61 times.
75 if (relative.empty()) {
161 14 continue;
162 }
163
1/1
✓ Branch 1 taken 61 times.
61 const std::filesystem::path path = config::Resolve(relative, directory);
164
1/2
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
61 if (path.has_parent_path()) {
165
2/2
✓ Branch 1 taken 61 times.
✓ Branch 4 taken 60 times.
122 std::filesystem::create_directories(path.parent_path());
166 }
167
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 10 times.
60 const std::optional<RasterGrid>& grid = generated.outputGrids[i];
168
3/3
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 50 times.
60 const GeneratedGrid* found = grid ? generated.Find(*grid) : nullptr;
169
4/5
✓ Branch 1 taken 60 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 49 times.
60 if (config::OutputArea(spec) && !found) {
170
0/3
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ throw std::runtime_error("Nothing generated over the grid of " + path.string());
171 }
172 60 const OutputContext context{generated, found};
173
3/3
✓ Branch 11 taken 57 times.
✓ Branch 13 taken 56 times.
✓ Branch 14 taken 1 time.
121 if (std::visit([&](const auto& output) { return Write(output, path, context); }, spec)) {
174
1/1
✓ Branch 1 taken 56 times.
56 written.push_back(path);
175 }
176 61 }
177 28 return written;
178 4 }
179
180 } // namespace terraingen
181