src/mesh/io/icq.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/mesh/io/icq.h" | ||
| 4 | |||
| 5 | #include "terraingen/mesh/shape_model.h" | ||
| 6 | #include "util/text.h" | ||
| 7 | |||
| 8 | #include <array> | ||
| 9 | #include <cmath> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <cstdio> | ||
| 12 | #include <sstream> | ||
| 13 | #include <stdexcept> | ||
| 14 | #include <string> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | 58 | void CheckModel(const IcqModel& model) { | |
| 21 | 58 | if (model.q == 0) { | |
| 22 | 1 | throw std::invalid_argument("An ICQ model needs Q > 0"); | |
| 23 | } | ||
| 24 | 57 | const std::size_t side = model.q + 1; | |
| 25 | 57 | if (model.vertices.size() != 6 * side * side) { | |
| 26 | 1 | throw std::invalid_argument("An ICQ model needs 6 (Q + 1)^2 vertices"); | |
| 27 | } | ||
| 28 | 834839 | for (const Vec3d& v : model.vertices) { | |
| 29 | 834784 | if (!(std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z))) { | |
| 30 | 1 | throw std::invalid_argument("An ICQ model's vertices must be finite"); | |
| 31 | } | ||
| 32 | } | ||
| 33 | 55 | } | |
| 34 | |||
| 35 | 805212 | double CrossSquared(const Vec3d& a, const Vec3d& b) { | |
| 36 | 805212 | const Vec3d c = Cross(a, b); | |
| 37 | 805212 | return Dot(c, c); | |
| 38 | } | ||
| 39 | |||
| 40 | } // namespace | ||
| 41 | |||
| 42 | 102 | bool IsIcqPath(const std::filesystem::path& path) { | |
| 43 | 102 | return HasExtension(path, {".icq"}); | |
| 44 | } | ||
| 45 | |||
| 46 | 25 | IcqModel ReadIcq(const std::filesystem::path& path, double metersPerUnit) { | |
| 47 | 25 | std::istringstream file(ReadFile(path)); | |
| 48 | 24 | std::string line; | |
| 49 | 24 | long long q = 0; | |
| 50 | 50 | if (!std::getline(file, line) || !(std::istringstream(line) >> q) || q <= 0 || q > 1000000) { | |
| 51 | 9 | throw std::runtime_error("ICQ file " + path.string() + " does not start with Q > 0"); | |
| 52 | } | ||
| 53 | 21 | IcqModel model; | |
| 54 | 21 | model.q = static_cast<std::size_t>(q); | |
| 55 | 21 | const std::size_t count = 6 * (model.q + 1) * (model.q + 1); | |
| 56 | 21 | model.vertices.reserve(count); | |
| 57 | std::size_t number = 1; | ||
| 58 | 213610 | while (model.vertices.size() < count) { | |
| 59 | 213591 | if (!std::getline(file, line)) { | |
| 60 | 3 | throw std::runtime_error("ICQ file " + path.string() + " has " + | |
| 61 | 4 | std::to_string(model.vertices.size()) + " of " + | |
| 62 | 4 | std::to_string(count) + " vertices"); | |
| 63 | } | ||
| 64 | 213590 | ++number; | |
| 65 | 213590 | std::istringstream values(line); | |
| 66 | 213590 | Vec3d v; | |
| 67 | 213590 | if (!(values >> v.x >> v.y >> v.z) || | |
| 68 | 213589 | !(std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z))) { | |
| 69 | 3 | throw std::runtime_error("ICQ file " + path.string() + " line " + | |
| 70 | 4 | std::to_string(number) + " is not three finite numbers"); | |
| 71 | } | ||
| 72 | 213589 | model.vertices.push_back({v.x * metersPerUnit, v.y * metersPerUnit, v.z * metersPerUnit}); | |
| 73 | 213590 | } | |
| 74 | 38 | return model; | |
| 75 | 26 | } | |
| 76 | |||
| 77 | 20 | void WriteIcq(const std::filesystem::path& path, const IcqModel& model, double metersPerUnit) { | |
| 78 | 20 | CheckModel(model); | |
| 79 | 18 | std::string text; | |
| 80 | 18 | std::array<char, 64> buffer{}; | |
| 81 | 18 | std::snprintf(buffer.data(), buffer.size(), "%12zu\n", model.q); | |
| 82 | 18 | text += buffer.data(); | |
| 83 | 411150 | for (const Vec3d& v : model.vertices) { | |
| 84 | 411132 | std::snprintf(buffer.data(), buffer.size(), "%18.9f%18.9f%18.9f\n", v.x / metersPerUnit, | |
| 85 | 411132 | v.y / metersPerUnit, v.z / metersPerUnit); | |
| 86 | 411132 | text += buffer.data(); | |
| 87 | } | ||
| 88 | 18 | WriteFile(path, text); | |
| 89 | 17 | } | |
| 90 | |||
| 91 | namespace { | ||
| 92 | |||
| 93 | /// For each stored vertex, the index of the one copy of it the mesh keeps: shared edge and corner | ||
| 94 | /// labels taken from one face, as Gaskell's converter does. | ||
| 95 | 24 | std::vector<std::size_t> SharedVertices(const IcqModel& model) { | |
| 96 | 24 | const std::size_t q = model.q; | |
| 97 | 24 | std::vector<std::size_t> n(model.vertices.size()); | |
| 98 | 410778 | for (std::size_t k = 0; k < n.size(); ++k) { | |
| 99 | 410754 | n[k] = k; | |
| 100 | } | ||
| 101 | 1310 | const auto at = [&](std::size_t i, std::size_t j, std::size_t f) -> std::size_t& { | |
| 102 | 1310 | return n[model.Index(i, j, f)]; | |
| 103 | }; | ||
| 104 | 667 | for (std::size_t i = 1; i < q; ++i) { | |
| 105 | 643 | at(i, q, 6) = at(q - i, q, 4); | |
| 106 | 643 | at(i, 0, 6) = at(i, q, 2); | |
| 107 | 643 | at(i, 0, 5) = at(q, q - i, 1); | |
| 108 | 643 | at(i, 0, 4) = at(q - i, 0, 1); | |
| 109 | 643 | at(i, 0, 3) = at(0, i, 1); | |
| 110 | 643 | at(i, 0, 2) = at(i, q, 1); | |
| 111 | } | ||
| 112 | 667 | for (std::size_t j = 1; j < q; ++j) { | |
| 113 | 643 | at(q, j, 6) = at(j, q, 5); | |
| 114 | 643 | at(q, j, 5) = at(0, j, 4); | |
| 115 | 643 | at(q, j, 4) = at(0, j, 3); | |
| 116 | 643 | at(q, j, 3) = at(0, j, 2); | |
| 117 | 643 | at(0, j, 6) = at(q - j, q, 3); | |
| 118 | 643 | at(0, j, 5) = at(q, j, 2); | |
| 119 | } | ||
| 120 | 24 | at(0, 0, 3) = at(0, 0, 1); | |
| 121 | 24 | at(q, 0, 4) = at(0, 0, 1); | |
| 122 | 24 | at(0, 0, 2) = at(0, q, 1); | |
| 123 | 24 | at(q, 0, 3) = at(0, q, 1); | |
| 124 | 24 | at(0, 0, 4) = at(q, 0, 1); | |
| 125 | 24 | at(q, 0, 5) = at(q, 0, 1); | |
| 126 | 24 | at(0, 0, 5) = at(q, q, 1); | |
| 127 | 24 | at(q, 0, 2) = at(q, q, 1); | |
| 128 | 24 | at(0, 0, 6) = at(0, q, 2); | |
| 129 | 24 | at(q, q, 3) = at(0, q, 2); | |
| 130 | 24 | at(0, q, 5) = at(q, q, 2); | |
| 131 | 24 | at(q, 0, 6) = at(q, q, 2); | |
| 132 | 24 | at(q, q, 4) = at(0, q, 3); | |
| 133 | 24 | at(0, q, 6) = at(0, q, 3); | |
| 134 | 24 | at(q, q, 5) = at(0, q, 4); | |
| 135 | 24 | at(q, q, 6) = at(0, q, 4); | |
| 136 | 24 | return n; | |
| 137 | } | ||
| 138 | |||
| 139 | } // namespace | ||
| 140 | |||
| 141 | 25 | TriangleMesh IcqToMesh(const IcqModel& model) { | |
| 142 | 25 | CheckModel(model); | |
| 143 | 24 | const std::size_t q = model.q; | |
| 144 | 24 | const std::vector<std::size_t> shared = SharedVertices(model); | |
| 145 | 4026084 | const auto at = [&](std::size_t i, std::size_t j, std::size_t f) { | |
| 146 | 4026060 | return shared[model.Index(i, j, f)]; | |
| 147 | 24 | }; | |
| 148 | 24 | TriangleMesh mesh; | |
| 149 | 24 | std::vector<std::uint32_t> compact(model.vertices.size(), UINT32_MAX); | |
| 150 | 2415660 | const auto vertex = [&](std::size_t i, std::size_t j, std::size_t f) { | |
| 151 | 2415636 | const std::size_t k = at(i, j, f); | |
| 152 | 2415636 | if (compact[k] == UINT32_MAX) { | |
| 153 | 402654 | compact[k] = static_cast<std::uint32_t>(mesh.positions.size()); | |
| 154 | 402654 | mesh.positions.push_back(model.vertices[k]); | |
| 155 | } | ||
| 156 | 2415636 | return compact[k]; | |
| 157 | 24 | }; | |
| 158 | 1610448 | const auto v = [&](std::size_t i, std::size_t j, std::size_t f) { | |
| 159 | 1610424 | return model.vertices[at(i, j, f)]; | |
| 160 | 24 | }; | |
| 161 | 24 | mesh.triangles.reserve(12 * q * q); | |
| 162 | 168 | for (std::size_t f = 1; f <= 6; ++f) { | |
| 163 | 4146 | for (std::size_t i = 0; i < q; ++i) { | |
| 164 | 406608 | for (std::size_t j = 0; j < q; ++j) { | |
| 165 | 402606 | const double z1 = CrossSquared(v(i, j, f), v(i + 1, j + 1, f)); | |
| 166 | 402606 | const double z2 = CrossSquared(v(i + 1, j, f), v(i, j + 1, f)); | |
| 167 | 402606 | if (z1 <= z2) { | |
| 168 | 201287 | mesh.triangles.push_back( | |
| 169 | 201287 | {vertex(i, j, f), vertex(i + 1, j + 1, f), vertex(i + 1, j, f)}); | |
| 170 | 201287 | mesh.triangles.push_back( | |
| 171 | 201287 | {vertex(i, j, f), vertex(i, j + 1, f), vertex(i + 1, j + 1, f)}); | |
| 172 | } else { | ||
| 173 | 201319 | mesh.triangles.push_back( | |
| 174 | 201319 | {vertex(i, j, f), vertex(i, j + 1, f), vertex(i + 1, j, f)}); | |
| 175 | 201319 | mesh.triangles.push_back( | |
| 176 | 201319 | {vertex(i + 1, j, f), vertex(i, j + 1, f), vertex(i + 1, j + 1, f)}); | |
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | 24 | return mesh; | |
| 182 | 24 | } | |
| 183 | |||
| 184 | 13 | RadialShape ToRadialShape(const IcqModel& model) { | |
| 185 | 13 | CheckModel(model); | |
| 186 | 13 | constexpr double kRadial = 1e-6; | |
| 187 | 13 | std::vector<double> radii(model.vertices.size()); | |
| 188 | 13 | bool radial = true; | |
| 189 | 13 | ForEachIcqLabel(model.q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) { | |
| 190 | 12894 | const Vec3d& v = model.vertices[k]; | |
| 191 | 12894 | const double r = Length(v); | |
| 192 | 12894 | const Vec3d d = IcqDirection(model.q, i, j, face); | |
| 193 | 12894 | radial = radial && r > 0.0 && Length(Cross(v, d)) <= kRadial * r && Dot(v, d) >= 0.0; | |
| 194 | 12894 | radii[k] = r; | |
| 195 | 12894 | }); | |
| 196 | 13 | if (!radial) { | |
| 197 | 2 | return ShapeModelFromMesh(IcqToMesh(model), model.q); | |
| 198 | } | ||
| 199 | 12 | return RadialShape(model.q, std::move(radii)); | |
| 200 | 13 | } | |
| 201 | |||
| 202 | 9 | IcqModel ToIcq(const RadialShape& shape) { | |
| 203 | 9 | IcqModel model; | |
| 204 | 9 | model.q = shape.Q(); | |
| 205 | 9 | model.vertices.resize(shape.Radii().size()); | |
| 206 | 9 | ForEachIcqLabel(model.q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) { | |
| 207 | 10422 | model.vertices[k] = IcqDirection(model.q, i, j, face) * shape.Radii()[k]; | |
| 208 | 10422 | }); | |
| 209 | 9 | return model; | |
| 210 | } | ||
| 211 | |||
| 212 | } // namespace terraingen | ||
| 213 |