mesh/io/stl.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/mesh/io/formats.h" | ||
| 2 | |||
| 3 | #include <array> | ||
| 4 | #include <cstdint> | ||
| 5 | #include <cstring> | ||
| 6 | #include <fstream> | ||
| 7 | #include <iterator> | ||
| 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 | /// Appends `value`'s bytes, little-endian on the hosts supported. | ||
| 21 | 1035927 | template <typename T> void Put(std::string& bytes, T value) { | |
| 22 | 1035927 | bytes.append(reinterpret_cast<const char*>(&value), sizeof(T)); | |
| 23 | 1035927 | } | |
| 24 | |||
| 25 | 318744 | void PutVector(std::string& bytes, const Vec3d& v) { | |
| 26 | 1274976 | for (const double c : {v.x, v.y, v.z}) { | |
| 27 | 956232 | Put(bytes, static_cast<float>(c)); | |
| 28 | } | ||
| 29 | 318744 | } | |
| 30 | |||
| 31 | /// The unit normal of a triangle, or zero for a degenerate one. | ||
| 32 | 79686 | Vec3d FaceNormal(const Vec3d& a, const Vec3d& b, const Vec3d& c) { | |
| 33 | 79686 | const Vec3d u = b - a; | |
| 34 | 79686 | const Vec3d v = c - a; | |
| 35 | 79686 | const Vec3d n{u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x}; | |
| 36 | 79686 | const double length = Length(n); | |
| 37 | 79686 | return length > 0.0 ? n * (1.0 / length) : Vec3d{}; | |
| 38 | } | ||
| 39 | |||
| 40 | /// Collects triangle corners into a mesh, joining corners at the same position into one vertex. | ||
| 41 | class Welder { | ||
| 42 | public: | ||
| 43 | 55 | void AddTriangle(const std::array<Vec3d, 3>& corners) { | |
| 44 | 55 | std::array<std::uint32_t, 3> triangle{}; | |
| 45 | 220 | for (std::size_t c = 0; c < 3; ++c) { | |
| 46 | 165 | triangle[c] = Vertex(corners[c]); | |
| 47 | } | ||
| 48 | 55 | mesh_.triangles.push_back(triangle); | |
| 49 | 55 | } | |
| 50 | |||
| 51 | 4 | TriangleMesh Take() { | |
| 52 | 4 | return std::move(mesh_); | |
| 53 | } | ||
| 54 | |||
| 55 | private: | ||
| 56 | 165 | std::uint32_t Vertex(const Vec3d& p) { | |
| 57 | 165 | const auto [found, added] = | |
| 58 | ✗ | index_.emplace(std::array<double, 3>{p.x, p.y, p.z}, | |
| 59 | 165 | static_cast<std::uint32_t>(mesh_.positions.size())); | |
| 60 | 165 | if (added) { | |
| 61 | 35 | mesh_.positions.push_back(p); | |
| 62 | } | ||
| 63 | 330 | return found->second; | |
| 64 | } | ||
| 65 | |||
| 66 | TriangleMesh mesh_; | ||
| 67 | std::map<std::array<double, 3>, std::uint32_t> index_; | ||
| 68 | }; | ||
| 69 | |||
| 70 | 162 | Vec3d GetVector(const std::string& bytes, std::size_t offset) { | |
| 71 | 162 | std::array<float, 3> v{}; | |
| 72 | 162 | std::memcpy(v.data(), bytes.data() + offset, sizeof(v)); | |
| 73 | 162 | return {v[0], v[1], v[2]}; | |
| 74 | } | ||
| 75 | |||
| 76 | 3 | TriangleMesh ReadBinary(const std::string& bytes) { | |
| 77 | 3 | std::uint32_t count = 0; | |
| 78 | 3 | std::memcpy(&count, bytes.data() + kHeaderBytes, sizeof(count)); | |
| 79 | 3 | Welder welder; | |
| 80 | 57 | for (std::size_t t = 0; t < count; ++t) { | |
| 81 | 54 | const std::size_t start = kHeaderBytes + 4 + t * kTriangleBytes + 12; | |
| 82 | 54 | welder.AddTriangle( | |
| 83 | 54 | {GetVector(bytes, start), GetVector(bytes, start + 12), GetVector(bytes, start + 24)}); | |
| 84 | } | ||
| 85 | 6 | return welder.Take(); | |
| 86 | 3 | } | |
| 87 | |||
| 88 | 1 | TriangleMesh ReadAscii(const std::string& text) { | |
| 89 | 1 | std::istringstream in(text); | |
| 90 | 1 | Welder welder; | |
| 91 | 1 | std::array<Vec3d, 3> corners{}; | |
| 92 | 1 | std::size_t corner = 0; | |
| 93 | 1 | std::string word; | |
| 94 | 17 | while (in >> word) { | |
| 95 | 16 | if (word != "vertex") { | |
| 96 | 13 | continue; | |
| 97 | } | ||
| 98 | 3 | Vec3d& p = corners[corner]; | |
| 99 | 3 | if (!(in >> p.x >> p.y >> p.z)) { | |
| 100 | ✗ | throw std::runtime_error("A malformed vertex in an ASCII STL"); | |
| 101 | } | ||
| 102 | 3 | if (++corner == 3) { | |
| 103 | 1 | welder.AddTriangle(corners); | |
| 104 | 1 | corner = 0; | |
| 105 | } | ||
| 106 | } | ||
| 107 | 2 | return welder.Take(); | |
| 108 | 1 | } | |
| 109 | |||
| 110 | } // namespace | ||
| 111 | |||
| 112 | 9 | void WriteStl(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes, | |
| 113 | const std::vector<SceneNode>& nodes) { | ||
| 114 | 9 | std::size_t count = 0; | |
| 115 | 122 | for (const SceneNode& node : nodes) { | |
| 116 | 113 | count += meshes[node.mesh].mesh.triangles.size(); | |
| 117 | } | ||
| 118 | 9 | std::string bytes = "TerrainGen"; | |
| 119 | 9 | bytes.resize(kHeaderBytes, ' '); | |
| 120 | 9 | Put(bytes, static_cast<std::uint32_t>(count)); | |
| 121 | 122 | for (const SceneNode& node : nodes) { | |
| 122 | 113 | const TriangleMesh& mesh = meshes[node.mesh].mesh; | |
| 123 | 79799 | for (const auto& t : mesh.triangles) { | |
| 124 | 79686 | const std::array<Vec3d, 3> corners{Placed(node, mesh.positions[t[0]]), | |
| 125 | 79686 | Placed(node, mesh.positions[t[1]]), | |
| 126 | 79686 | Placed(node, mesh.positions[t[2]])}; | |
| 127 | 79686 | PutVector(bytes, FaceNormal(corners[0], corners[1], corners[2])); | |
| 128 | 318744 | for (const Vec3d& corner : corners) { | |
| 129 | 239058 | PutVector(bytes, corner); | |
| 130 | } | ||
| 131 | 79686 | Put(bytes, static_cast<std::uint16_t>(0)); | |
| 132 | } | ||
| 133 | } | ||
| 134 | 9 | Save(path, bytes); | |
| 135 | 9 | } | |
| 136 | |||
| 137 | 4 | SceneMesh ReadStl(const std::filesystem::path& path) { | |
| 138 | 4 | std::ifstream file(path, std::ios::binary); | |
| 139 | 4 | if (!file) { | |
| 140 | ✗ | throw std::runtime_error("Cannot read " + path.string()); | |
| 141 | } | ||
| 142 | 8 | const std::string bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()}; | |
| 143 | 4 | SceneMesh read; | |
| 144 | 4 | std::uint32_t count = 0; | |
| 145 | 4 | if (bytes.size() >= kHeaderBytes + 4) { | |
| 146 | 4 | std::memcpy(&count, bytes.data() + kHeaderBytes, sizeof(count)); | |
| 147 | } | ||
| 148 | const bool binary = | ||
| 149 | 8 | bytes.size() >= kHeaderBytes + 4 && | |
| 150 | 4 | bytes.size() == kHeaderBytes + 4 + static_cast<std::size_t>(count) * kTriangleBytes; | |
| 151 | 4 | read.mesh = binary ? ReadBinary(bytes) : ReadAscii(bytes); | |
| 152 | 8 | return read; | |
| 153 | 4 | } | |
| 154 | |||
| 155 | } // namespace terraingen::detail | ||
| 156 |