./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
80 / 81
98.8%
Branches:
48 / 52
92.3%
Functions:
8 / 8
100.0%

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
2/2
✓ Branch 0 taken 376776 times.
✓ Branch 1 taken 125592 times.
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
2/2
✓ Branch 0 taken 741 times.
✓ Branch 1 taken 247 times.
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
2/2
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 608 times.
741 static_cast<std::uint32_t>(m_mesh.positions.size()));
46
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 608 times.
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
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 4 times.
250 for (std::size_t t = 0; t < count; ++t) {
67 246 const std::size_t start = kHeaderBytes + 4 + t * kTriangleBytes + 12;
68
1/1
✓ Branch 1 taken 246 times.
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
3/3
✓ Branch 1 taken 17 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 1 time.
17 while (in >> word) {
81
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
16 if (word != "vertex") {
82 13 continue;
83 }
84
1/1
✓ Branch 1 taken 3 times.
3 Vec3d& p = corners[corner];
85
4/5
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
3 if (!(in >> p.x >> p.y >> p.z)) {
86
0/1
✗ Branch 2 not taken.
✗ throw std::runtime_error("A malformed vertex in an ASCII STL");
87 }
88
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 if (++corner == 3) {
89
1/1
✓ Branch 1 taken 1 time.
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
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 8 times.
107 for (const SceneNode& node : nodes) {
102 99 count += meshes[node.mesh].mesh.triangles.size();
103 }
104 8 std::string bytes = "TerrainGen";
105
1/1
✓ Branch 1 taken 8 times.
8 bytes.resize(kHeaderBytes, ' ');
106
1/1
✓ Branch 1 taken 8 times.
8 Put(bytes, static_cast<std::uint32_t>(count));
107
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 8 times.
107 for (const SceneNode& node : nodes) {
108 99 const TriangleMesh& mesh = meshes[node.mesh].mesh;
109
2/2
✓ Branch 0 taken 31398 times.
✓ Branch 1 taken 99 times.
31497 for (const auto& t : mesh.triangles) {
110
1/1
✓ Branch 1 taken 31398 times.
31398 const std::array<Vec3d, 3> corners{Placed(node, mesh.positions[t[0]]),
111
1/1
✓ Branch 1 taken 31398 times.
31398 Placed(node, mesh.positions[t[1]]),
112
1/1
✓ Branch 1 taken 31398 times.
31398 Placed(node, mesh.positions[t[2]])};
113
1/1
✓ Branch 2 taken 31398 times.
31398 PutVector(bytes, Normalized(TriangleAreaNormal(corners[0], corners[1], corners[2])));
114
2/2
✓ Branch 0 taken 94194 times.
✓ Branch 1 taken 31398 times.
125592 for (const Vec3d& corner : corners) {
115
1/1
✓ Branch 1 taken 94194 times.
94194 PutVector(bytes, corner);
116 }
117
1/1
✓ Branch 1 taken 31398 times.
31398 Put(bytes, static_cast<std::uint16_t>(0));
118 }
119 }
120
1/1
✓ Branch 1 taken 8 times.
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
1/1
✓ Branch 1 taken 5 times.
5 SceneMesh read;
126 5 std::uint32_t count = 0;
127
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (bytes.size() >= kHeaderBytes + 4) {
128 5 std::memcpy(&count, bytes.data() + kHeaderBytes, sizeof(count));
129 }
130 5 const bool binary =
131
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 bytes.size() >= kHeaderBytes + 4 &&
132
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 bytes.size() == kHeaderBytes + 4 + static_cast<std::size_t>(count) * kTriangleBytes;
133
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 1 time.
5 read.mesh = binary ? ReadBinary(bytes) : ReadAscii(bytes);
134 5 return read;
135 5 }
136
137 } // namespace terraingen::detail
138