./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
19 / 19
100.0%
Branches:
3 / 3
100.0%
Functions:
4 / 4, 1 excl
100.0%

src/mesh/io/formats.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief The format writers and readers behind `WriteScene` and `ReadMesh` (gltf.cpp, obj.cpp,
5 /// stl.cpp), and what they share.
6 #pragma once
7
8 #include "terraingen/mesh/io/mesh_io.h"
9 #include "terraingen/util/bits.h"
10 #include "util/text.h"
11
12 #include <array>
13 #include <filesystem>
14 #include <string>
15 #include <vector>
16
17 namespace terraingen::detail {
18
19 static_assert(kLittleEndian, "The binary mesh writers assume a little-endian host");
20
21 /// Appends `value`'s bytes to `bytes`, little-endian.
22 4865420 template <typename T> void Put(std::string& bytes, T value) {
23 4865420 bytes.append(reinterpret_cast<const char*>(&value), sizeof(T));
24 4865420 }
25
26 /// TerrainGen's z-up (x, y, z) in the y-up frame of OBJ and glTF: (x, z, -y).
27 666551 inline Vec3d YUp(const Vec3d& p) {
28
2/2
✓ Branch 1 taken 12571 times.
✓ Branch 4 taken 3385 times.
666551 return {p.x, p.z, -p.y};
29 }
30
31 /// A y-up point back in TerrainGen's z-up frame.
32 24123 inline Vec3d ZUp(const Vec3d& p) {
33
1/1
✓ Branch 1 taken 9487 times.
24123 return {p.x, -p.z, p.y};
34 }
35
36 /// A row-major rotation matrix in the y-up frame.
37 173 inline std::array<double, 9> YUp(const std::array<double, 9>& r) {
38 173 return {r[0], r[2], -r[1], r[6], r[8], -r[7], -r[3], -r[5], r[4]};
39 }
40
41 /// The unit quaternion (x, y, z, w) of a proper rotation, row-major 3 x 3, as glTF nodes store it.
42 std::array<double, 4> RotationQuaternion(const std::array<double, 9>& rotation);
43
44 /// `p` of mesh `node.mesh` placed by the node: scaled, then posed.
45 Vec3d Placed(const SceneNode& node, const Vec3d& p);
46
47 /// The normal `n` of a vertex of the node's mesh after `Placed`, unit length.
48 Vec3d PlacedNormal(const SceneNode& node, const Vec3d& n);
49
50 /// `WriteScene` for .obj, with the materials in an .mtl of the same stem beside it.
51 void WriteObj(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes,
52 const std::vector<SceneNode>& nodes);
53
54 /// `WriteScene` for .gltf (with a .bin beside it), or .glb when `binary`.
55 void WriteGltf(const std::filesystem::path& path, bool binary, const std::vector<SceneMesh>& meshes,
56 const std::vector<SceneNode>& nodes);
57
58 /// `WriteScene` for .stl: every node's mesh placed and merged into one binary STL, z-up.
59 void WriteStl(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes,
60 const std::vector<SceneNode>& nodes);
61
62 /// `ReadMesh` for binary or ASCII .stl, corners at the same position joined into one vertex.
63 SceneMesh ReadStl(const std::filesystem::path& path);
64
65 /// `ReadMesh` for .obj.
66 SceneMesh ReadObj(const std::filesystem::path& path);
67
68 /// `ReadMesh` for .gltf and .glb.
69 SceneMesh ReadGltf(const std::filesystem::path& path);
70
71 } // namespace terraingen::detail
72