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 | 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 | 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 | 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 | 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 | 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 | 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 | 438530 | for (const Column<Row>& column : columns) { | |
| 132 | 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 | 26 | if (!attributes.empty()) { | |
| 140 | 21 | WriteCsvHeader(file, kAttributeColumns, ","); | |
| 141 | } | ||
| 142 | 26 | file << '\n'; | |
| 143 | 33902 | for (std::size_t n = 0; n < craters.Size(); ++n) { | |
| 144 | 33876 | WriteCsvCells(file, kCraterColumns, craters.At(n), ""); | |
| 145 | 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 | 211 | for (std::size_t n = 0; n < craters.Size(); ++n) { | |
| 155 | 200 | Json entry = Json::object(); | |
| 156 | 200 | AddJsonCells(entry, kCraterColumns, craters.At(n)); | |
| 157 | 200 | if (!attributes.empty()) { | |
| 158 | 192 | AddJsonCells(entry, kAttributeColumns, attributes[n]); | |
| 159 | } | ||
| 160 | 200 | list.push_back(std::move(entry)); | |
| 161 | 200 | } | |
| 162 | 55 | return Json{{"craters", std::move(list)}}; | |
| 163 | 44 | } | |
| 164 | |||
| 165 | 7 | void WriteBoulderCsv(std::ostream& file, const BoulderList& boulders) { | |
| 166 | 7 | WriteCsvHeader(file, kBoulderColumns, ""); | |
| 167 | 7 | file << '\n'; | |
| 168 | 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 | 25643 | for (std::size_t i = 0; i < boulders.Size(); ++i) { | |
| 177 | 25634 | Json entry = Json::object(); | |
| 178 | 25634 | AddJsonCells(entry, kBoulderColumns, boulders.At(i)); | |
| 179 | 25634 | list.push_back(std::move(entry)); | |
| 180 | 25634 | } | |
| 181 | 45 | return Json{{"boulders", std::move(list)}}; | |
| 182 | 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 | 129 | const std::size_t end = std::min(line.find(',', start), line.size()); | |
| 190 | 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 | 129 | cell = first == std::string::npos ? std::string() : cell.substr(first, last - first + 1); | |
| 194 | 129 | if (cell.size() >= 2 && cell.front() == '"' && cell.back() == '"') { | |
| 195 | 1 | cell = cell.substr(1, cell.size() - 2); | |
| 196 | } | ||
| 197 | 129 | cells.push_back(std::move(cell)); | |
| 198 | 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 | 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 | 58 | if (text.empty() || end != text.c_str() + text.size()) { | |
| 216 | 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 | 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 | 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 | 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 | 200 | const auto found = m_columns.find(name); | |
| 254 | 100 | return found == m_columns.end() || found->second >= m_cells.size() | |
| 255 | 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 | 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 | 3218 | if (found == m_entry.end() || found->is_null()) { | |
| 277 | 325 | return std::nullopt; | |
| 278 | } | ||
| 279 | 1284 | if (found->is_boolean()) { | |
| 280 | 637 | return found->get<bool>() ? 1.0 : 0.0; | |
| 281 | } | ||
| 282 | 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 | 324 | if (found == m_entry.end() || found->is_null()) { | |
| 292 | 1 | return 0; | |
| 293 | } | ||
| 294 | 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 | 172 | crater.ageGyr = row.Number("age_gyr").value_or(0.0); | |
| 319 | 172 | crater.id = row.Id(); | |
| 320 | 170 | crater.manual = row.Number("manual").value_or(0.0) != 0.0; | |
| 321 | 170 | crater.replace = row.Number("replace").value_or(0.0) != 0.0; | |
| 322 | 170 | crater.depth = row.Number("depth_m"); | |
| 323 | 170 | if (!crater.depth) { | |
| 324 | 169 | if (const std::optional<double> ratio = row.Number("depth_ratio")) { | |
| 325 | 162 | crater.depth = *ratio * crater.diameter; | |
| 326 | } | ||
| 327 | } | ||
| 328 | 170 | if (crater.depth) { | |
| 329 | 163 | crater.rimHeight = row.Number("rim_height_m"); | |
| 330 | 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 | 8 | std::string line; | |
| 340 | 8 | std::map<std::string, std::size_t> columns; | |
| 341 | 8 | if (std::getline(file, line)) { | |
| 342 | 8 | const std::vector<std::string> names = CsvCells(line); | |
| 343 | 61 | for (std::size_t n = 0; n < names.size(); ++n) { | |
| 344 | 106 | columns.emplace(Lower(names[n]), n); | |
| 345 | } | ||
| 346 | 8 | } | |
| 347 | 8 | CraterList craters; | |
| 348 | 8 | std::size_t row = 0; | |
| 349 | 26 | while (std::getline(file, line)) { | |
| 350 | 13 | if (line.find_first_not_of(" \t\r") == std::string::npos) { | |
| 351 | 1 | continue; | |
| 352 | } | ||
| 353 | 12 | const std::vector<std::string> cells = CsvCells(line); | |
| 354 | 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 | 12 | const Json json = Json::parse(text, nullptr, false); | |
| 362 | 12 | if (json.is_discarded()) { | |
| 363 | 1 | throw std::invalid_argument("Crater list is not valid JSON"); | |
| 364 | } | ||
| 365 | 11 | const Json* list = &json; | |
| 366 | 11 | if (json.is_object()) { | |
| 367 | 7 | const auto found = json.find("craters"); | |
| 368 | 14 | if (found == json.end()) { | |
| 369 | 1 | throw std::invalid_argument("Crater list JSON needs a \"craters\" array"); | |
| 370 | } | ||
| 371 | 6 | list = &*found; | |
| 372 | } | ||
| 373 | 10 | if (!list->is_array()) { | |
| 374 | 1 | throw std::invalid_argument("Crater list JSON needs a \"craters\" array"); | |
| 375 | } | ||
| 376 | 9 | CraterList craters; | |
| 377 | 9 | craters.Reserve(list->size()); | |
| 378 | 170 | for (std::size_t row = 0; row < list->size(); ++row) { | |
| 379 | 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 | 20 | return HasExtension(path, {".json"}) ? ReadCraterJson(text) : ReadCraterCsv(text); | |
| 390 | 9 | } catch (const std::invalid_argument& error) { | |
| 391 | 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 | 38 | if (!attributes.empty() && attributes.size() != craters.Size()) { | |
| 398 | 1 | throw std::invalid_argument("Crater attributes must be empty or one per crater"); | |
| 399 | } | ||
| 400 | 37 | std::ostringstream text; | |
| 401 | 37 | switch (format) { | |
| 402 | 26 | case ListFormat::Csv: | |
| 403 | 26 | WriteCraterCsv(text, craters, attributes); | |
| 404 | break; | ||
| 405 | 11 | case ListFormat::Json: | |
| 406 | 33 | text << CraterJson(craters, attributes).dump(2) << '\n'; | |
| 407 | 11 | break; | |
| 408 | } | ||
| 409 | 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 | 16 | switch (format) { | |
| 416 | 7 | case ListFormat::Csv: | |
| 417 | 7 | WriteBoulderCsv(text, boulders); | |
| 418 | break; | ||
| 419 | 9 | case ListFormat::Json: | |
| 420 | 27 | text << BoulderJson(boulders).dump(2) << '\n'; | |
| 421 | 9 | break; | |
| 422 | } | ||
| 423 | 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 | 16 | if (!(creaseAngle >= 0.0 && creaseAngle <= kMaxCreaseAngle)) { | |
| 430 | 2 | throw std::invalid_argument("Crease angle must be between 0 and 180 degrees"); | |
| 431 | } | ||
| 432 | 14 | if (boulders.empty()) { | |
| 433 | 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 | 294 | for (const BoulderModel& boulder : boulders) { | |
| 440 | 563 | const BoulderPlacement placement = PlacementAtSite( | |
| 441 | 281 | boulder.placement, {boulder.boulder.x, boulder.boulder.y}, site, surface); | |
| 442 | 281 | if (!instanced) { | |
| 443 | 31 | TriangleMesh placed = boulder.shape->mesh; | |
| 444 | 344 | for (Vec3d& p : placed.positions) { | |
| 445 | 313 | p = placement.Apply(p); | |
| 446 | } | ||
| 447 | 31 | ShadedMesh shaded = WithCreases(placed, radians); | |
| 448 | 31 | nodes.push_back({meshes.size(), {1.0, 1.0, 1.0}, {}}); | |
| 449 | 62 | meshes.push_back( | |
| 450 | 31 | {std::move(shaded.mesh), std::move(shaded.normals), boulder.shape->material}); | |
| 451 | 31 | continue; | |
| 452 | 62 | } | |
| 453 | 250 | auto [found, added] = indices.emplace(boulder.shape.get(), meshes.size()); | |
| 454 | 250 | if (added) { | |
| 455 | 208 | ShadedMesh shaded = WithCreases(boulder.shape->mesh, radians); | |
| 456 | 416 | meshes.push_back( | |
| 457 | 208 | {std::move(shaded.mesh), std::move(shaded.normals), boulder.shape->material}); | |
| 458 | 208 | } | |
| 459 | 250 | nodes.push_back({found->second, placement.scale, placement.pose}); | |
| 460 | } | ||
| 461 | 13 | WriteScene(path, meshes, nodes); | |
| 462 | 253 | } | |
| 463 | |||
| 464 | } // namespace terraingen | ||
| 465 |