./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
6 / 6
100.0%
Branches:
17 / 19
89.5%
Functions:
2 / 2, 1 excl
100.0%

include/terraingen/mesh/io/mesh_io.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Reading and writing triangle meshes and instanced scenes as OBJ with MTL, glTF and STL.
5 #pragma once
6
7 #include "terraingen/mesh/mesh.h"
8
9 #include <array>
10 #include <cstddef>
11 #include <filesystem>
12 #include <string>
13 #include <vector>
14
15 namespace terraingen {
16
17 /// A mesh's material: a name and a diffuse color.
18
11/11
✓ Branch 1 taken 1342 times.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 5 times.
✓ Branch 4 taken 1342 times.
✓ Branch 4 taken 31 times.
✓ Branch 5 taken 3 times.
✓ Branch 7 taken 208 times.
✓ Branch 8 taken 3 times.
✓ Branch 10 taken 208 times.
✓ Branch 17 taken 12 times.
✓ Branch 20 taken 12 times.
1823 struct MeshMaterial {
19 std::string name = "default"; ///< Name in OBJ/MTL and glTF.
20 std::array<double, 3> color{0.5, 0.5, 0.5}; ///< Linear RGB, each in [0, 1].
21
22 /// Memberwise equality.
23 75 bool operator==(const MeshMaterial& other) const {
24
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 60 times.
90 return name == other.name && color == other.color;
25 }
26 bool operator!=(const MeshMaterial& other) const {
27 return !(*this == other);
28 }
29 };
30
31 /// A mesh with optional normals and its material.
32 struct SceneMesh {
33 TriangleMesh mesh; ///< The mesh, in meters.
34 std::vector<Vec3d> normals; ///< Empty, or one unit normal per vertex.
35 MeshMaterial material{}; ///< Written as an MTL material or a glTF base color.
36 };
37
38 /// An instance of a scene mesh: its vertices scaled per axis by `scale`, then moved by
39 /// `pose`, as glTF composes a node's scale, rotation and translation.
40 struct SceneNode {
41 std::size_t mesh = 0; ///< Index into the scene's meshes.
42 Vec3d scale{1.0, 1.0, 1.0}; ///< Per-axis scale, nonzero and finite.
43 RigidTransform pose{}; ///< Rotation and translation applied after the scale.
44 };
45
46 /// Whether `path` names a mesh format `ReadMesh` reads by its extension, in any case: .obj,
47 /// .gltf, .glb, .stl or .icq. `WriteScene` writes all but .icq (see `WriteIcq`).
48 bool IsMeshPath(const std::filesystem::path& path);
49
50 /// Writes meshes drawn once per node as glTF or OBJ (y-up, as (x, z, -y)) or binary STL (merged,
51 /// z-up, no materials), by extension.
52 /// @throws std::invalid_argument for another extension, or invalid meshes, nodes or poses.
53 /// @throws std::runtime_error if a file cannot be written.
54 void WriteScene(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes,
55 const std::vector<SceneNode>& nodes);
56
57 /// Writes one mesh, as `WriteScene` with a single node.
58 /// @throws std::invalid_argument and std::runtime_error as `WriteScene`.
59 void WriteMesh(const std::filesystem::path& path, const SceneMesh& mesh);
60
61 /// Writes `mesh` alone, without normals and with the default material.
62 /// @throws std::invalid_argument and std::runtime_error as `WriteScene`.
63 9 inline void WriteMesh(const std::filesystem::path& path, const TriangleMesh& mesh) {
64
4/6
✓ Branch 2 taken 9 times.
✓ Branch 5 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 3 times.
15 WriteMesh(path, SceneMesh{mesh, {}, {}});
65 6 }
66
67 /// Reads every triangle of an .obj, .gltf, .glb, .stl or .icq (kilometers) file in meters and
68 /// z-up, with the first material's diffuse color where there is one; normals are not read.
69 /// @throws std::invalid_argument for another extension.
70 /// @throws std::runtime_error if the file cannot be read or holds no triangles.
71 SceneMesh ReadMesh(const std::filesystem::path& path);
72
73 } // namespace terraingen
74