./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
298 / 298
100.0%
Branches:
250 / 276
90.6%
Functions:
35 / 35
100.0%

src/io/feature_lists.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/io/feature_lists.h"
4
5 #include "terraingen/geometry/geometry.h"
6 #include "terraingen/mesh/io/mesh_io.h"
7 #include "util/overloaded.h"
8 #include "util/text.h"
9
10 #include <nlohmann/json.hpp>
11
12 #include <algorithm>
13 #include <array>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <map>
17 #include <optional>
18 #include <sstream>
19 #include <stdexcept>
20 #include <string>
21 #include <variant>
22 #include <vector>
23
24 namespace terraingen {
25
26 65 ListFormat InferListFormat(std::string_view path) {
27
3/3
✓ Branch 2 taken 65 times.
✓ Branch 4 taken 56 times.
✓ Branch 5 taken 9 times.
65 return HasExtension(std::filesystem::path(path), {".json"}) ? ListFormat::Json
28 130 : ListFormat::Csv;
29 }
30
31 namespace {
32
33 using Json = nlohmann::ordered_json;
34
35 /// One value of a feature list: a CSV cell and a JSON value.
36 using Cell = std::variant<double, std::optional<double>, std::uint64_t, int, bool>;
37
38 /// A column of a feature list: its name and how a row gives its value.
39 template <typename Row> struct Column {
40 const char* name;
41 Cell (*get)(const Row&);
42 };
43
44 /// The columns of every crater.
45 const std::array<Column<Crater>, 7> kCraterColumns{{
46 {"x", [](const Crater& c) { return Cell(c.x); }},
47 {"y", [](const Crater& c) { return Cell(c.y); }},
48 {"diameter", [](const Crater& c) { return Cell(c.diameter); }},
49 {"age_gyr", [](const Crater& c) { return Cell(c.ageGyr); }},
50 {"id", [](const Crater& c) { return Cell(c.id); }},
51 {"manual", [](const Crater& c) { return Cell(c.manual); }},
52 {"replace", [](const Crater& c) { return Cell(c.replace); }},
53 }};
54
55 /// The columns added for craters with attributes.
56 const std::array<Column<CraterAttributes>, 5> kAttributeColumns{{
57 {"floor_depth", [](const CraterAttributes& a) { return Cell(a.floorDepth); }},
58 {"rim_height", [](const CraterAttributes& a) { return Cell(a.rimHeight); }},
59 {"depth_ratio", [](const CraterAttributes& a) { return Cell(a.depthRatio); }},
60 {"freshness", [](const CraterAttributes& a) { return Cell(a.freshness); }},
61 {"complex", [](const CraterAttributes& a) { return Cell(a.complex); }},
62 }};
63
64 /// The columns of every boulder.
65 const std::array<Column<Boulder>, 16> kBoulderColumns{{
66 {"x", [](const Boulder& b) { return Cell(b.x); }},
67 {"y", [](const Boulder& b) { return Cell(b.y); }},
68 {"ground", [](const Boulder& b) { return Cell(b.ground); }},
69 {"diameter", [](const Boulder& b) { return Cell(b.diameter); }},
70 {"height", [](const Boulder& b) { return Cell(b.height); }},
71 {"burial", [](const Boulder& b) { return Cell(b.burial); }},
72 {"yaw_deg", [](const Boulder& b) { return Cell(b.yaw * kRadian); }},
73 {"lean_deg", [](const Boulder& b) { return Cell(b.lean * kRadian); }},
74 {"id", [](const Boulder& b) { return Cell(b.id); }},
75 {"model", [](const Boulder& b) { return Cell(b.model); }},
76 {"wear", [](const Boulder& b) { return Cell(b.wear); }},
77 {"parent", [](const Boulder& b) { return Cell(b.parent); }},
78 {"piece", [](const Boulder& b) { return Cell(static_cast<std::uint64_t>(b.piece)); }},
79 {"rest", [](const Boulder& b) { return Cell(static_cast<int>(b.rest)); }},
80 {"tilt_deg", [](const Boulder& b) { return Cell(b.tilt * kRadian); }},
81 {"tilt_azimuth_deg", [](const Boulder& b) { return Cell(b.tiltAzimuth * kRadian); }},
82 }};
83
84 /// A cell as CSV writes it: doubles exactly, an unknown value empty and booleans as 1 or 0.
85 532773 std::string CsvText(const Cell& cell) {
86 532773 return std::visit(Overloaded{
87 332338 [](double value) { return FormatNumber(value, 17); },
88 14171 [](const std::optional<double>& value) {
89
2/2
✓ Branch 0 taken 14170 times.
✓ Branch 1 taken 1 time.
14171 return value ? FormatNumber(*value, 17) : std::string();
90 },
91 76389 [](std::uint64_t value) { return std::to_string(value); },
92 14171 [](int value) { return std::to_string(value); },
93
2/2
✓ Branch 0 taken 95616 times.
✓ Branch 1 taken 88 times.
191320 [](bool value) { return std::string(value ? "1" : "0"); },
94 },
95 532773 cell);
96 }
97
98 /// A cell as JSON writes it, an unknown value null.
99 412504 Json JsonValue(const Cell& cell) {
100 412504 return std::visit(
101 412504 Overloaded{
102
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 20002 times.
25634 [](const std::optional<double>& value) { return value ? Json(*value) : Json(nullptr); },
103 386870 [](const auto& value) { return Json(value); },
104 },
105 412504 cell);
106 }
107
108 /// Writes the names of `columns` as CSV cells, the first after `separator` and the rest after ",".
109 template <typename Row, std::size_t N>
110 54 void WriteCsvHeader(std::ostream& file, const std::array<Column<Row>, N>& columns,
111 const char* separator) {
112
6/6
void terraingen::(anonymous namespace)::WriteCsvHeader<terraingen::Boulder, 16ul>(std::ostream&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Boulder>, 16ul> const&, char const*):
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 7 times.
void terraingen::(anonymous namespace)::WriteCsvHeader<terraingen::Crater, 7ul>(std::ostream&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Crater>, 7ul> const&, char const*):
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 26 times.
void terraingen::(anonymous namespace)::WriteCsvHeader<terraingen::CraterAttributes, 5ul>(std::ostream&, std::array<terraingen::(anonymous namespace)::Column<terraingen::CraterAttributes>, 5ul> const&, char const*):
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 21 times.
453 for (const Column<Row>& column : columns) {
113 399 file << separator << column.name;
114 399 separator = ",";
115 }
116 54 }
117
118 /// Writes the cells of `row` as CSV, the first after `separator` and the rest after ",".
119 template <typename Row, std::size_t N>
120 61828 void WriteCsvCells(std::ostream& file, const std::array<Column<Row>, N>& columns, const Row& row,
121 const char* separator) {
122
6/6
void terraingen::(anonymous namespace)::WriteCsvCells<terraingen::Boulder, 16ul>(std::ostream&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Boulder>, 16ul> const&, terraingen::Boulder const&, char const*):
✓ Branch 0 taken 226736 times.
✓ Branch 1 taken 14171 times.
void terraingen::(anonymous namespace)::WriteCsvCells<terraingen::Crater, 7ul>(std::ostream&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Crater>, 7ul> const&, terraingen::Crater const&, char const*):
✓ Branch 0 taken 237132 times.
✓ Branch 1 taken 33876 times.
void terraingen::(anonymous namespace)::WriteCsvCells<terraingen::CraterAttributes, 5ul>(std::ostream&, std::array<terraingen::(anonymous namespace)::Column<terraingen::CraterAttributes>, 5ul> const&, terraingen::CraterAttributes const&, char const*):
✓ Branch 0 taken 68905 times.
✓ Branch 1 taken 13781 times.
594601 for (const Column<Row>& column : columns) {
123 1065546 file << separator << CsvText(column.get(row));
124 532773 separator = ",";
125 }
126 61828 }
127
128 /// Adds the cells of `row` to a JSON object, keyed by column name in order.
129 template <typename Row, std::size_t N>
130 26026 void AddJsonCells(Json& entry, const std::array<Column<Row>, N>& columns, const Row& row) {
131
6/6
void terraingen::(anonymous namespace)::AddJsonCells<terraingen::Boulder, 16ul>(nlohmann::json_abi_v3_12_0::basic_json<nlohmann::json_abi_v3_12_0::ordered_map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> >, void>&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Boulder>, 16ul> const&, terraingen::Boulder const&):
✓ Branch 0 taken 410144 times.
✓ Branch 1 taken 25634 times.
void terraingen::(anonymous namespace)::AddJsonCells<terraingen::Crater, 7ul>(nlohmann::json_abi_v3_12_0::basic_json<nlohmann::json_abi_v3_12_0::ordered_map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> >, void>&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Crater>, 7ul> const&, terraingen::Crater const&):
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 200 times.
void terraingen::(anonymous namespace)::AddJsonCells<terraingen::CraterAttributes, 5ul>(nlohmann::json_abi_v3_12_0::basic_json<nlohmann::json_abi_v3_12_0::ordered_map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> >, void>&, std::array<terraingen::(anonymous namespace)::Column<terraingen::CraterAttributes>, 5ul> const&, terraingen::CraterAttributes const&):
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 192 times.
438530 for (const Column<Row>& column : columns) {
132
3/3
void terraingen::(anonymous namespace)::AddJsonCells<terraingen::Boulder, 16ul>(nlohmann::json_abi_v3_12_0::basic_json<nlohmann::json_abi_v3_12_0::ordered_map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> >, void>&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Boulder>, 16ul> const&, terraingen::Boulder const&):
✓ Branch 3 taken 410144 times.
void terraingen::(anonymous namespace)::AddJsonCells<terraingen::Crater, 7ul>(nlohmann::json_abi_v3_12_0::basic_json<nlohmann::json_abi_v3_12_0::ordered_map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> >, void>&, std::array<terraingen::(anonymous namespace)::Column<terraingen::Crater>, 7ul> const&, terraingen::Crater const&):
✓ Branch 3 taken 1400 times.
void terraingen::(anonymous namespace)::AddJsonCells<terraingen::CraterAttributes, 5ul>(nlohmann::json_abi_v3_12_0::basic_json<nlohmann::json_abi_v3_12_0::ordered_map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> >, void>&, std::array<terraingen::(anonymous namespace)::Column<terraingen::CraterAttributes>, 5ul> const&, terraingen::CraterAttributes const&):
✓ Branch 3 taken 960 times.
412504 entry[column.name] = JsonValue(column.get(row));
133 }
134 26026 }
135
136 26 void WriteCraterCsv(std::ostream& file, const CraterList& craters,
137 const std::vector<CraterAttributes>& attributes) {
138 26 WriteCsvHeader(file, kCraterColumns, "");
139
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 5 times.
26 if (!attributes.empty()) {
140 21 WriteCsvHeader(file, kAttributeColumns, ",");
141 }
142 26 file << '\n';
143
2/2
✓ Branch 1 taken 33876 times.
✓ Branch 2 taken 26 times.
33902 for (std::size_t n = 0; n < craters.Size(); ++n) {
144 33876 WriteCsvCells(file, kCraterColumns, craters.At(n), "");
145
2/2
✓ Branch 0 taken 13781 times.
✓ Branch 1 taken 20095 times.
33876 if (!attributes.empty()) {
146 13781 WriteCsvCells(file, kAttributeColumns, attributes[n], ",");
147 }
148 33876 file << '\n';
149 }
150 26 }
151
152 11 Json CraterJson(const CraterList& craters, const std::vector<CraterAttributes>& attributes) {
153 11 Json list = Json::array();
154
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 11 times.
211 for (std::size_t n = 0; n < craters.Size(); ++n) {
155
1/1
✓ Branch 1 taken 200 times.
200 Json entry = Json::object();
156
2/2
✓ Branch 1 taken 200 times.
✓ Branch 4 taken 200 times.
200 AddJsonCells(entry, kCraterColumns, craters.At(n));
157
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 8 times.
200 if (!attributes.empty()) {
158
1/1
✓ Branch 1 taken 192 times.
192 AddJsonCells(entry, kAttributeColumns, attributes[n]);
159 }
160
1/1
✓ Branch 1 taken 200 times.
200 list.push_back(std::move(entry));
161 200 }
162
2/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
55 return Json{{"craters", std::move(list)}};
163
5/7
✓ Branch 1 taken 11 times.
✓ Branch 5 taken 11 times.
✓ Branch 8 taken 11 times.
✓ Branch 10 taken 22 times.
✓ Branch 11 taken 11 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
44 }
164
165 7 void WriteBoulderCsv(std::ostream& file, const BoulderList& boulders) {
166 7 WriteCsvHeader(file, kBoulderColumns, "");
167 7 file << '\n';
168
2/2
✓ Branch 1 taken 14171 times.
✓ Branch 2 taken 7 times.
14178 for (std::size_t i = 0; i < boulders.Size(); ++i) {
169 14171 WriteCsvCells(file, kBoulderColumns, boulders.At(i), "");
170 14171 file << '\n';
171 }
172 7 }
173
174 9 Json BoulderJson(const BoulderList& boulders) {
175 9 Json list = Json::array();
176
2/2
✓ Branch 0 taken 25634 times.
✓ Branch 1 taken 9 times.
25643 for (std::size_t i = 0; i < boulders.Size(); ++i) {
177
1/1
✓ Branch 1 taken 25634 times.
25634 Json entry = Json::object();
178
2/2
✓ Branch 1 taken 25634 times.
✓ Branch 4 taken 25634 times.
25634 AddJsonCells(entry, kBoulderColumns, boulders.At(i));
179
1/1
✓ Branch 1 taken 25634 times.
25634 list.push_back(std::move(entry));
180 25634 }
181
2/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
45 return Json{{"boulders", std::move(list)}};
182
5/7
✓ Branch 1 taken 9 times.
✓ Branch 5 taken 9 times.
✓ Branch 8 taken 9 times.
✓ Branch 10 taken 18 times.
✓ Branch 11 taken 9 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
36 }
183
184 /// The cells of one CSV line, split at commas, with spaces and enclosing double quotes trimmed.
185 20 std::vector<std::string> CsvCells(const std::string& line) {
186 20 std::vector<std::string> cells;
187 20 std::size_t start = 0;
188 238 while (true) {
189
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 109 times.
129 const std::size_t end = std::min(line.find(',', start), line.size());
190
1/1
✓ Branch 1 taken 129 times.
129 std::string cell = line.substr(start, end - start);
191 129 const std::size_t first = cell.find_first_not_of(" \t\r");
192 129 const std::size_t last = cell.find_last_not_of(" \t\r");
193
3/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 126 times.
✓ Branch 3 taken 126 times.
129 cell = first == std::string::npos ? std::string() : cell.substr(first, last - first + 1);
194
5/6
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 67 times.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
129 if (cell.size() >= 2 && cell.front() == '"' && cell.back() == '"') {
195
1/1
✓ Branch 1 taken 1 time.
1 cell = cell.substr(1, cell.size() - 2);
196 }
197
1/1
✓ Branch 1 taken 129 times.
129 cells.push_back(std::move(cell));
198
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 20 times.
129 if (end == line.size()) {
199 20 return cells;
200 }
201 109 start = end + 1;
202 129 }
203 }
204
205 /// @throws std::invalid_argument "Crater list row <row>: <what>".
206 6 [[noreturn]] void ThrowRowError(std::size_t row, const std::string& what) {
207
4/4
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
✓ Branch 8 taken 6 times.
✓ Branch 11 taken 6 times.
18 throw std::invalid_argument("Crater list row " + std::to_string(row) + ": " + what);
208 }
209
210 /// The number `text` spells in full, for row `row`'s column `name`.
211 /// @throws std::invalid_argument for anything else.
212 58 double ParseNumber(const std::string& text, std::size_t row, const char* name) {
213 58 char* end = nullptr;
214 58 const double value = std::strtod(text.c_str(), &end);
215
3/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 57 times.
58 if (text.empty() || end != text.c_str() + text.size()) {
216
1/1
✓ Branch 2 taken 1 time.
6 ThrowRowError(row, std::string(name) + " '" + text + "' is not a number");
217 }
218 57 return value;
219 }
220
221 /// A row of a CSV crater list, its cells looked up by the header's names.
222 class CsvCraterRow {
223 public:
224 12 CsvCraterRow(const std::map<std::string, std::size_t>& columns,
225 const std::vector<std::string>& cells, std::size_t row)
226 12 : m_columns(columns), m_cells(cells), m_row(row) {}
227
228 /// Column `name`'s number; none where the list lacks it or the cell is empty.
229 90 std::optional<double> Number(const char* name) const {
230 90 const std::string* text = Cell(name);
231
4/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 58 times.
90 if (text == nullptr || text->empty()) {
232 32 return std::nullopt;
233 }
234 58 return ParseNumber(*text, m_row, name);
235 }
236
237 /// The ID column, 0 where the list lacks it or the cell is empty.
238 10 std::uint64_t Id() const {
239 10 const std::string* text = Cell("id");
240
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
10 if (text == nullptr || text->empty()) {
241 return 0;
242 }
243 7 char* end = nullptr;
244 7 const unsigned long long value = std::strtoull(text->c_str(), &end, 10);
245
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 6 times.
7 if (end != text->c_str() + text->size() || text->front() == '-') {
246 3 ThrowRowError(m_row, "id '" + *text + "' is not an unsigned integer");
247 }
248 return static_cast<std::uint64_t>(value);
249 }
250
251 private:
252 100 const std::string* Cell(const char* name) const {
253
2/2
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 32 times.
200 const auto found = m_columns.find(name);
254
3/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
100 return found == m_columns.end() || found->second >= m_cells.size()
255
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 32 times.
100 ? nullptr
256 68 : &m_cells[found->second];
257 }
258
259 const std::map<std::string, std::size_t>& m_columns;
260 const std::vector<std::string>& m_cells;
261 std::size_t m_row;
262 };
263
264 /// An entry of a JSON crater list.
265 class JsonCraterRow {
266 public:
267 164 JsonCraterRow(const Json& entry, std::size_t row) : m_entry(entry), m_row(row) {
268
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 163 times.
164 if (!m_entry.is_object()) {
269 1 ThrowRowError(m_row, "not an object");
270 }
271 163 }
272
273 /// Key `name`'s number, booleans as 1 and 0; none where the entry lacks it or holds null.
274 1609 std::optional<double> Number(const char* name) const {
275 1609 const auto found = m_entry.find(name);
276
4/4
✓ Branch 2 taken 1285 times.
✓ Branch 3 taken 324 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1284 times.
3218 if (found == m_entry.end() || found->is_null()) {
277 325 return std::nullopt;
278 }
279
2/2
✓ Branch 1 taken 321 times.
✓ Branch 2 taken 963 times.
1284 if (found->is_boolean()) {
280
2/2
✓ Branch 2 taken 316 times.
✓ Branch 3 taken 5 times.
637 return found->get<bool>() ? 1.0 : 0.0;
281 }
282
2/2
✓ Branch 1 taken 957 times.
✓ Branch 2 taken 6 times.
963 if (!found->is_number()) {
283 3 ThrowRowError(m_row, std::string(name) + " is not a number");
284 }
285 962 return found->get<double>();
286 }
287
288 /// The ID, 0 where the entry lacks it or holds null.
289 162 std::uint64_t Id() const {
290 162 const auto found = m_entry.find("id");
291
3/4
✓ Branch 2 taken 162 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 161 times.
324 if (found == m_entry.end() || found->is_null()) {
292 1 return 0;
293 }
294
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 160 times.
161 if (!found->is_number_unsigned()) {
295 1 ThrowRowError(m_row, "id is not an unsigned integer");
296 }
297 160 return found->get<std::uint64_t>();
298 }
299
300 private:
301 const Json& m_entry;
302 std::size_t m_row;
303 };
304
305 /// The crater `row` describes (`ReadCraterList`).
306 175 template <typename Row> Crater ParseCrater(const Row& row, std::size_t index) {
307 175 const auto required = [&](const char* name) {
308 const std::optional<double> value = row.Number(name);
309 if (!value) {
310 ThrowRowError(index, std::string("needs ") + name);
311 }
312 return *value;
313 };
314 175 Crater crater;
315 175 crater.x = required("x");
316 174 crater.y = required("y");
317 173 crater.diameter = required("diameter");
318
4/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4 times.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 2 times.
172 crater.ageGyr = row.Number("age_gyr").value_or(0.0);
319 172 crater.id = row.Id();
320
4/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 1 time.
170 crater.manual = row.Number("manual").value_or(0.0) != 0.0;
321
3/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 1 taken 161 times.
✗ Branch 2 not taken.
170 crater.replace = row.Number("replace").value_or(0.0) != 0.0;
322 170 crater.depth = row.Number("depth_m");
323
3/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
170 if (!crater.depth) {
324
4/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 3 times.
169 if (const std::optional<double> ratio = row.Number("depth_ratio")) {
325 162 crater.depth = *ratio * crater.diameter;
326 }
327 }
328
4/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 3 times.
170 if (crater.depth) {
329 163 crater.rimHeight = row.Number("rim_height_m");
330
2/4
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::CsvCraterRow>(terraingen::(anonymous namespace)::CsvCraterRow const&, unsigned long):
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
terraingen::Crater terraingen::(anonymous namespace)::ParseCrater<terraingen::(anonymous namespace)::JsonCraterRow>(terraingen::(anonymous namespace)::JsonCraterRow const&, unsigned long):
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
163 if (!crater.rimHeight) {
331 163 crater.rimHeight = row.Number("rim_height");
332 }
333 }
334 170 return crater;
335 }
336
337 8 CraterList ReadCraterCsv(const std::string& text) {
338 8 std::istringstream file(text);
339
1/1
✓ Branch 1 taken 8 times.
8 std::string line;
340
1/1
✓ Branch 1 taken 8 times.
8 std::map<std::string, std::size_t> columns;
341
2/3
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 if (std::getline(file, line)) {
342
1/1
✓ Branch 1 taken 8 times.
8 const std::vector<std::string> names = CsvCells(line);
343
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 8 times.
61 for (std::size_t n = 0; n < names.size(); ++n) {
344
2/2
✓ Branch 1 taken 53 times.
✓ Branch 4 taken 53 times.
106 columns.emplace(Lower(names[n]), n);
345 }
346 8 }
347 8 CraterList craters;
348 8 std::size_t row = 0;
349
3/3
✓ Branch 2 taken 18 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 5 times.
26 while (std::getline(file, line)) {
350
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 12 times.
13 if (line.find_first_not_of(" \t\r") == std::string::npos) {
351 1 continue;
352 }
353
1/1
✓ Branch 1 taken 12 times.
12 const std::vector<std::string> cells = CsvCells(line);
354
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
12 craters.Append(ParseCrater(CsvCraterRow(columns, cells, row), row));
355 9 ++row;
356 12 }
357 10 return craters;
358 14 }
359
360 12 CraterList ReadCraterJson(const std::string& text) {
361
1/1
✓ Branch 1 taken 12 times.
12 const Json json = Json::parse(text, nullptr, false);
362
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 11 times.
12 if (json.is_discarded()) {
363
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Crater list is not valid JSON");
364 }
365 11 const Json* list = &json;
366
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 if (json.is_object()) {
367 7 const auto found = json.find("craters");
368
3/3
✓ Branch 2 taken 7 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 6 times.
14 if (found == json.end()) {
369
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Crater list JSON needs a \"craters\" array");
370 }
371
1/1
✓ Branch 1 taken 6 times.
6 list = &*found;
372 }
373
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
10 if (!list->is_array()) {
374
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Crater list JSON needs a \"craters\" array");
375 }
376 9 CraterList craters;
377
1/1
✓ Branch 1 taken 9 times.
9 craters.Reserve(list->size());
378
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 6 times.
170 for (std::size_t row = 0; row < list->size(); ++row) {
379
4/4
✓ Branch 1 taken 164 times.
✓ Branch 4 taken 163 times.
✓ Branch 7 taken 161 times.
✓ Branch 10 taken 161 times.
164 craters.Append(ParseCrater(JsonCraterRow((*list)[row], row), row));
380 }
381 6 return craters;
382 15 }
383
384 } // namespace
385
386 22 CraterList ReadCraterList(const std::filesystem::path& path) {
387 22 const std::string text = ReadFile(path);
388 20 try {
389
5/5
✓ Branch 1 taken 20 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 8 times.
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 5 times.
20 return HasExtension(path, {".json"}) ? ReadCraterJson(text) : ReadCraterCsv(text);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } catch (const std::invalid_argument& error) {
391
4/4
✓ Branch 3 taken 9 times.
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
✓ Branch 12 taken 9 times.
36 throw std::invalid_argument(path.string() + ": " + error.what());
392 9 }
393 11 }
394
395 38 void WriteCraterList(const CraterList& craters, const std::filesystem::path& path,
396 ListFormat format, const std::vector<CraterAttributes>& attributes) {
397
4/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 28 times.
38 if (!attributes.empty() && attributes.size() != craters.Size()) {
398
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Crater attributes must be empty or one per crater");
399 }
400 37 std::ostringstream text;
401
2/3
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
37 switch (format) {
402 26 case ListFormat::Csv:
403
1/1
✓ Branch 1 taken 26 times.
26 WriteCraterCsv(text, craters, attributes);
404 break;
405 11 case ListFormat::Json:
406
3/3
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
33 text << CraterJson(craters, attributes).dump(2) << '\n';
407 11 break;
408 }
409
2/2
✓ Branch 1 taken 37 times.
✓ Branch 4 taken 35 times.
39 WriteFile(path, text.str());
410 37 }
411
412 16 void WriteBoulderList(const BoulderList& boulders, const std::filesystem::path& path,
413 ListFormat format) {
414 16 std::ostringstream text;
415
2/3
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
16 switch (format) {
416 7 case ListFormat::Csv:
417
1/1
✓ Branch 1 taken 7 times.
7 WriteBoulderCsv(text, boulders);
418 break;
419 9 case ListFormat::Json:
420
3/3
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
27 text << BoulderJson(boulders).dump(2) << '\n';
421 9 break;
422 }
423
2/2
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 14 times.
18 WriteFile(path, text.str());
424 16 }
425
426 16 void WriteBoulderModels(const std::vector<BoulderModel>& boulders,
427 const std::filesystem::path& path, double creaseAngle,
428 const Surface& surface, const Vec2d& site, bool instanced) {
429
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 14 times.
16 if (!(creaseAngle >= 0.0 && creaseAngle <= kMaxCreaseAngle)) {
430
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Crease angle must be between 0 and 180 degrees");
431 }
432
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13 times.
14 if (boulders.empty()) {
433
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("There are no boulder models to write");
434 }
435 13 const double radians = creaseAngle * kDegree;
436 13 std::vector<SceneMesh> meshes;
437 13 std::map<const BoulderShape*, std::size_t> indices;
438 13 std::vector<SceneNode> nodes;
439
2/2
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 13 times.
294 for (const BoulderModel& boulder : boulders) {
440 563 const BoulderPlacement placement = PlacementAtSite(
441
1/1
✓ Branch 1 taken 281 times.
281 boulder.placement, {boulder.boulder.x, boulder.boulder.y}, site, surface);
442
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 250 times.
281 if (!instanced) {
443
1/1
✓ Branch 1 taken 31 times.
31 TriangleMesh placed = boulder.shape->mesh;
444
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 31 times.
344 for (Vec3d& p : placed.positions) {
445
1/1
✓ Branch 1 taken 313 times.
313 p = placement.Apply(p);
446 }
447
1/1
✓ Branch 1 taken 31 times.
31 ShadedMesh shaded = WithCreases(placed, radians);
448
1/1
✓ Branch 1 taken 31 times.
31 nodes.push_back({meshes.size(), {1.0, 1.0, 1.0}, {}});
449
1/1
✓ Branch 1 taken 31 times.
62 meshes.push_back(
450 31 {std::move(shaded.mesh), std::move(shaded.normals), boulder.shape->material});
451 31 continue;
452 62 }
453
3/3
✓ Branch 1 taken 250 times.
✓ Branch 3 taken 208 times.
✓ Branch 4 taken 42 times.
250 auto [found, added] = indices.emplace(boulder.shape.get(), meshes.size());
454
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 42 times.
250 if (added) {
455
1/1
✓ Branch 1 taken 208 times.
208 ShadedMesh shaded = WithCreases(boulder.shape->mesh, radians);
456
1/1
✓ Branch 1 taken 208 times.
416 meshes.push_back(
457 208 {std::move(shaded.mesh), std::move(shaded.normals), boulder.shape->material});
458 208 }
459
1/1
✓ Branch 1 taken 250 times.
250 nodes.push_back({found->second, placement.scale, placement.pose});
460 }
461
1/1
✓ Branch 1 taken 12 times.
13 WriteScene(path, meshes, nodes);
462
4/8
✓ Branch 1 taken 31 times.
✓ Branch 4 taken 31 times.
✓ Branch 7 taken 208 times.
✓ Branch 10 taken 208 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
253 }
463
464 } // namespace terraingen
465