mesh/io/formats.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @file | ||
| 2 | /// @brief The format writers and readers behind `WriteScene` and `ReadMesh` (gltf.cpp, obj.cpp, | ||
| 3 | /// stl.cpp), and what they share. | ||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include "terraingen/mesh/io/mesh_io.h" | ||
| 7 | |||
| 8 | #include <array> | ||
| 9 | #include <filesystem> | ||
| 10 | #include <string> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace terraingen::detail { | ||
| 14 | |||
| 15 | /// TerrainGen's z-up (x, y, z) in the y-up frame of OBJ and glTF: (x, z, -y). | ||
| 16 | 724444 | inline Vec3d YUp(const Vec3d& p) { | |
| 17 | 724444 | return {p.x, p.z, -p.y}; | |
| 18 | } | ||
| 19 | |||
| 20 | /// A y-up point back in TerrainGen's z-up frame. | ||
| 21 | 18507 | inline Vec3d ZUp(const Vec3d& p) { | |
| 22 | 18507 | return {p.x, -p.z, p.y}; | |
| 23 | } | ||
| 24 | |||
| 25 | /// A row-major rotation matrix in the y-up frame. | ||
| 26 | 360 | inline std::array<double, 9> YUp(const std::array<double, 9>& r) { | |
| 27 | 360 | return {r[0], r[2], -r[1], r[6], r[8], -r[7], -r[3], -r[5], r[4]}; | |
| 28 | } | ||
| 29 | |||
| 30 | /// `p` of mesh `node.mesh` placed by the node: scaled, then posed. | ||
| 31 | Vec3d Placed(const SceneNode& node, const Vec3d& p); | ||
| 32 | |||
| 33 | /// The normal `n` of a vertex of the node's mesh after `Placed`, unit length. | ||
| 34 | Vec3d PlacedNormal(const SceneNode& node, const Vec3d& n); | ||
| 35 | |||
| 36 | /// Writes `bytes` to `path`. | ||
| 37 | /// @throws std::runtime_error if the file cannot be written. | ||
| 38 | void Save(const std::filesystem::path& path, const std::string& bytes); | ||
| 39 | |||
| 40 | /// `WriteScene` for .obj, with the materials in an .mtl of the same stem beside it. | ||
| 41 | void WriteObj(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes, | ||
| 42 | const std::vector<SceneNode>& nodes); | ||
| 43 | |||
| 44 | /// `WriteScene` for .gltf (with a .bin beside it) or .glb. | ||
| 45 | void WriteGltf(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes, | ||
| 46 | const std::vector<SceneNode>& nodes); | ||
| 47 | |||
| 48 | /// `WriteScene` for .stl: every node's mesh placed and merged into one binary STL, z-up. | ||
| 49 | void WriteStl(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes, | ||
| 50 | const std::vector<SceneNode>& nodes); | ||
| 51 | |||
| 52 | /// `ReadMesh` for binary or ASCII .stl, corners at the same position joined into one vertex. | ||
| 53 | SceneMesh ReadStl(const std::filesystem::path& path); | ||
| 54 | |||
| 55 | /// `ReadMesh` for .obj. | ||
| 56 | SceneMesh ReadObj(const std::filesystem::path& path); | ||
| 57 | |||
| 58 | /// `ReadMesh` for .gltf and .glb. | ||
| 59 | SceneMesh ReadGltf(const std::filesystem::path& path); | ||
| 60 | |||
| 61 | } // namespace terraingen::detail | ||
| 62 |