./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
59 / 59
100.0%
Branches:
71 / 73
97.3%
Functions:
8 / 8
100.0%

src/mesh/io/mesh_io.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/mesh/io/mesh_io.h"
4
5 #include "mesh/io/formats.h"
6 #include "terraingen/mesh/io/icq.h"
7 #include "util/text.h"
8
9 #include <cmath>
10 #include <stdexcept>
11 #include <string>
12
13 namespace terraingen {
14
15 namespace {
16
17 /// The extension of `path`, lower case.
18 92 std::string Extension(const std::filesystem::path& path) {
19
1/1
✓ Branch 2 taken 92 times.
368 return Lower(path.extension().string());
20 }
21
22 /// Throws for no meshes or nodes, an empty or invalid mesh, mismatched normals, a node naming no
23 /// mesh, or a node scale or pose that cannot be written.
24 55 void CheckScene(const std::vector<SceneMesh>& meshes, const std::vector<SceneNode>& nodes) {
25
3/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
55 if (meshes.empty() || nodes.empty()) {
26
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A mesh scene needs meshes and nodes");
27 }
28
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 52 times.
313 for (const SceneMesh& scene : meshes) {
29
3/4
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 260 times.
✗ Branch 3 not taken.
261 if (scene.mesh.positions.empty() || scene.mesh.triangles.empty()) {
30
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Cannot write an empty mesh");
31 }
32
4/4
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 227 times.
✓ Branch 3 taken 1 time.
260 if (!scene.normals.empty() && scene.normals.size() != scene.mesh.positions.size()) {
33
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Normals must be empty or one per vertex");
34 }
35 259 ValidateMesh(scene.mesh);
36 }
37
2/2
✓ Branch 0 taken 299 times.
✓ Branch 1 taken 49 times.
348 for (const SceneNode& node : nodes) {
38
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 298 times.
299 if (node.mesh >= meshes.size()) {
39
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A scene node names a mesh the scene does not have");
40 }
41 298 RequireScaleFactors(node.scale, "Node scales");
42 297 RequireRotation(node.pose.rotation);
43 }
44 49 }
45
46 } // namespace
47
48 namespace detail {
49
50 106765 Vec3d Placed(const SceneNode& node, const Vec3d& p) {
51 106765 return node.pose.Apply({p.x * node.scale.x, p.y * node.scale.y, p.z * node.scale.z});
52 }
53
54 3385 Vec3d PlacedNormal(const SceneNode& node, const Vec3d& n) {
55 3385 const Vec3d rotated =
56 3385 node.pose.Rotate({n.x / node.scale.x, n.y / node.scale.y, n.z / node.scale.z});
57 3385 return rotated * (1.0 / Length(rotated));
58 }
59
60 } // namespace detail
61
62 52 bool IsMeshPath(const std::filesystem::path& path) {
63 52 return HasExtension(path, {".obj", ".gltf", ".glb", ".stl", ".icq"});
64 }
65
66 58 void WriteScene(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes,
67 const std::vector<SceneNode>& nodes) {
68 58 const std::string extension = Extension(path);
69
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 3 times.
58 const bool gltf = extension == ".gltf" || extension == ".glb";
70
6/6
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 32 times.
58 if (extension != ".obj" && extension != ".stl" && !gltf) {
71 3 throw std::invalid_argument("Mesh files must end in .obj, .gltf, .glb or .stl: " +
72
3/3
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
6 path.string());
73 }
74
1/1
✓ Branch 1 taken 49 times.
55 CheckScene(meshes, nodes);
75
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 34 times.
49 if (extension == ".obj") {
76
1/1
✓ Branch 1 taken 15 times.
15 detail::WriteObj(path, meshes, nodes);
77
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 26 times.
34 } else if (extension == ".stl") {
78
1/1
✓ Branch 1 taken 8 times.
8 detail::WriteStl(path, meshes, nodes);
79 } else {
80
1/1
✓ Branch 1 taken 25 times.
26 detail::WriteGltf(path, extension == ".glb", meshes, nodes);
81 }
82 48 }
83
84 36 void WriteMesh(const std::filesystem::path& path, const SceneMesh& mesh) {
85
6/6
✓ Branch 2 taken 36 times.
✓ Branch 5 taken 31 times.
✓ Branch 8 taken 31 times.
✓ Branch 9 taken 31 times.
✓ Branch 13 taken 5 times.
✓ Branch 14 taken 5 times.
118 WriteScene(path, {mesh}, {SceneNode{}});
86
1/1
✓ Branch 1 taken 36 times.
67 }
87
88 34 SceneMesh ReadMesh(const std::filesystem::path& path) {
89 34 const std::string extension = Extension(path);
90
1/1
✓ Branch 1 taken 34 times.
34 SceneMesh read;
91
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 19 times.
34 if (extension == ".obj") {
92
1/1
✓ Branch 1 taken 13 times.
15 read = detail::ReadObj(path);
93
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
19 } else if (extension == ".gltf" || extension == ".glb") {
94
1/1
✓ Branch 1 taken 10 times.
11 read = detail::ReadGltf(path);
95
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 } else if (extension == ".stl") {
96
1/1
✓ Branch 1 taken 5 times.
5 read = detail::ReadStl(path);
97
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 } else if (extension == ".icq") {
98
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 read.mesh = IcqToMesh(ReadIcq(path));
99 } else {
100 1 throw std::invalid_argument("Meshes are read from .obj, .gltf, .glb, .stl or .icq: " +
101
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 path.string());
102 }
103
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29 times.
30 if (read.mesh.triangles.empty()) {
104
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
3 throw std::runtime_error("No triangles in " + path.string());
105 }
106
1/1
✓ Branch 1 taken 29 times.
29 ValidateMesh(read.mesh);
107 29 return read;
108 34 }
109
110 } // namespace terraingen
111