src/mesh/io/stl.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "mesh/io/formats.h" | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstring> | ||
| 8 | #include <map> | ||
| 9 | #include <sstream> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | |||
| 13 | namespace terraingen::detail { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | constexpr std::size_t kHeaderBytes = 80; | ||
| 18 | constexpr std::size_t kTriangleBytes = 50; | ||
| 19 | |||
| 20 | 125592 | void PutVector(std::string& bytes, const Vec3d& v) { | |
| 21 | 502368 | for (const double c : {v.x, v.y, v.z}) { | |
| 22 | 376776 | Put(bytes, static_cast<float>(c)); | |
| 23 | } | ||
| 24 | 125592 | } | |
| 25 | |||
| 26 | /// Collects triangle corners into a mesh, joining corners at the same position into one vertex. | ||
| 27 | 5 | class Welder { | |
| 28 | public: | ||
| 29 | 247 | void AddTriangle(const std::array<Vec3d, 3>& corners) { | |
| 30 | 247 | std::array<std::uint32_t, 3> triangle{}; | |
| 31 | 988 | for (std::size_t c = 0; c < 3; ++c) { | |
| 32 | 741 | triangle[c] = Vertex(corners[c]); | |
| 33 | } | ||
| 34 | 247 | m_mesh.triangles.push_back(triangle); | |
| 35 | 247 | } | |
| 36 | |||
| 37 | 5 | TriangleMesh Take() { | |
| 38 | 5 | return std::move(m_mesh); | |
| 39 | } | ||
| 40 | |||
| 41 | private: | ||
| 42 | 741 | std::uint32_t Vertex(const Vec3d& p) { | |
| 43 | 1482 | const auto [found, added] = | |
| 44 | 1482 | m_index.emplace(std::array<double, 3>{p.x, p.y, p.z}, | |
| 45 | 741 | static_cast<std::uint32_t>(m_mesh.positions.size())); | |
| 46 | 741 | if (added) { | |
| 47 | 133 | m_mesh.positions.push_back(p); | |
| 48 | } | ||
| 49 | 741 | return found->second; | |
| 50 | } | ||
| 51 | |||
| 52 | TriangleMesh m_mesh; | ||
| 53 | std::map<std::array<double, 3>, std::uint32_t> m_index; | ||
| 54 | }; | ||
| 55 | |||
| 56 | 738 | Vec3d GetVector(const std::string& bytes, std::size_t offset) { | |
| 57 | 738 | std::array<float, 3> v{}; | |
| 58 | 738 | std::memcpy(v.data(), bytes.data() + offset, sizeof(v)); | |
| 59 | 738 | return {static_cast<double>(v[0]), static_cast<double>(v[1]), static_cast<double>(v[2])}; | |
| 60 | } | ||
| 61 | |||
| 62 | 4 | TriangleMesh ReadBinary(const std::string& bytes) { | |
| 63 | 4 | std::uint32_t count = 0; | |
| 64 | 4 | std::memcpy(&count, bytes.data() + kHeaderBytes, sizeof(count)); | |
| 65 | 4 | Welder welder; | |
| 66 | 250 | for (std::size_t t = 0; t < count; ++t) { | |
| 67 | 246 | const std::size_t start = kHeaderBytes + 4 + t * kTriangleBytes + 12; | |
| 68 | 246 | welder.AddTriangle( | |
| 69 | 246 | {GetVector(bytes, start), GetVector(bytes, start + 12), GetVector(bytes, start + 24)}); | |
| 70 | } | ||
| 71 | 4 | return welder.Take(); | |
| 72 | 4 | } | |
| 73 | |||
| 74 | 1 | TriangleMesh ReadAscii(const std::string& text) { | |
| 75 | 1 | std::istringstream in(text); | |
| 76 | 1 | Welder welder; | |
| 77 | 1 | std::array<Vec3d, 3> corners{}; | |
| 78 | 1 | std::size_t corner = 0; | |
| 79 | 1 | std::string word; | |
| 80 | 17 | while (in >> word) { | |
| 81 | 16 | if (word != "vertex") { | |
| 82 | 13 | continue; | |
| 83 | } | ||
| 84 | 3 | Vec3d& p = corners[corner]; | |
| 85 | 3 | if (!(in >> p.x >> p.y >> p.z)) { | |
| 86 | ✗ | throw std::runtime_error("A malformed vertex in an ASCII STL"); | |
| 87 | } | ||
| 88 | 3 | if (++corner == 3) { | |
| 89 | 1 | welder.AddTriangle(corners); | |
| 90 | corner = 0; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | 2 | return welder.Take(); | |
| 94 | 1 | } | |
| 95 | |||
| 96 | } // namespace | ||
| 97 | |||
| 98 | 8 | void WriteStl(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes, | |
| 99 | const std::vector<SceneNode>& nodes) { | ||
| 100 | 8 | std::size_t count = 0; | |
| 101 | 107 | for (const SceneNode& node : nodes) { | |
| 102 | 99 | count += meshes[node.mesh].mesh.triangles.size(); | |
| 103 | } | ||
| 104 | 8 | std::string bytes = "TerrainGen"; | |
| 105 | 8 | bytes.resize(kHeaderBytes, ' '); | |
| 106 | 8 | Put(bytes, static_cast<std::uint32_t>(count)); | |
| 107 | 107 | for (const SceneNode& node : nodes) { | |
| 108 | 99 | const TriangleMesh& mesh = meshes[node.mesh].mesh; | |
| 109 | 31497 | for (const auto& t : mesh.triangles) { | |
| 110 | 31398 | const std::array<Vec3d, 3> corners{Placed(node, mesh.positions[t[0]]), | |
| 111 | 31398 | Placed(node, mesh.positions[t[1]]), | |
| 112 | 31398 | Placed(node, mesh.positions[t[2]])}; | |
| 113 | 31398 | PutVector(bytes, Normalized(TriangleAreaNormal(corners[0], corners[1], corners[2]))); | |
| 114 | 125592 | for (const Vec3d& corner : corners) { | |
| 115 | 94194 | PutVector(bytes, corner); | |
| 116 | } | ||
| 117 | 31398 | Put(bytes, static_cast<std::uint16_t>(0)); | |
| 118 | } | ||
| 119 | } | ||
| 120 | 8 | WriteFile(path, bytes); | |
| 121 | 8 | } | |
| 122 | |||
| 123 | 5 | SceneMesh ReadStl(const std::filesystem::path& path) { | |
| 124 | 5 | const std::string bytes = ReadFile(path); | |
| 125 | 5 | SceneMesh read; | |
| 126 | 5 | std::uint32_t count = 0; | |
| 127 | 5 | if (bytes.size() >= kHeaderBytes + 4) { | |
| 128 | 5 | std::memcpy(&count, bytes.data() + kHeaderBytes, sizeof(count)); | |
| 129 | } | ||
| 130 | 5 | const bool binary = | |
| 131 | 5 | bytes.size() >= kHeaderBytes + 4 && | |
| 132 | 5 | bytes.size() == kHeaderBytes + 4 + static_cast<std::size_t>(count) * kTriangleBytes; | |
| 133 | 5 | read.mesh = binary ? ReadBinary(bytes) : ReadAscii(bytes); | |
| 134 | 5 | return read; | |
| 135 | 5 | } | |
| 136 | |||
| 137 | } // namespace terraingen::detail | ||
| 138 |