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 | 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 | 55 | if (meshes.empty() || nodes.empty()) { | |
| 26 | 1 | throw std::invalid_argument("A mesh scene needs meshes and nodes"); | |
| 27 | } | ||
| 28 | 313 | for (const SceneMesh& scene : meshes) { | |
| 29 | 261 | if (scene.mesh.positions.empty() || scene.mesh.triangles.empty()) { | |
| 30 | 1 | throw std::invalid_argument("Cannot write an empty mesh"); | |
| 31 | } | ||
| 32 | 260 | if (!scene.normals.empty() && scene.normals.size() != scene.mesh.positions.size()) { | |
| 33 | 1 | throw std::invalid_argument("Normals must be empty or one per vertex"); | |
| 34 | } | ||
| 35 | 259 | ValidateMesh(scene.mesh); | |
| 36 | } | ||
| 37 | 348 | for (const SceneNode& node : nodes) { | |
| 38 | 299 | if (node.mesh >= meshes.size()) { | |
| 39 | 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 | 58 | const bool gltf = extension == ".gltf" || extension == ".glb"; | |
| 70 | 58 | if (extension != ".obj" && extension != ".stl" && !gltf) { | |
| 71 | 3 | throw std::invalid_argument("Mesh files must end in .obj, .gltf, .glb or .stl: " + | |
| 72 | 6 | path.string()); | |
| 73 | } | ||
| 74 | 55 | CheckScene(meshes, nodes); | |
| 75 | 49 | if (extension == ".obj") { | |
| 76 | 15 | detail::WriteObj(path, meshes, nodes); | |
| 77 | 34 | } else if (extension == ".stl") { | |
| 78 | 8 | detail::WriteStl(path, meshes, nodes); | |
| 79 | } else { | ||
| 80 | 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 | 118 | WriteScene(path, {mesh}, {SceneNode{}}); | |
| 86 | 67 | } | |
| 87 | |||
| 88 | 34 | SceneMesh ReadMesh(const std::filesystem::path& path) { | |
| 89 | 34 | const std::string extension = Extension(path); | |
| 90 | 34 | SceneMesh read; | |
| 91 | 34 | if (extension == ".obj") { | |
| 92 | 15 | read = detail::ReadObj(path); | |
| 93 | 19 | } else if (extension == ".gltf" || extension == ".glb") { | |
| 94 | 11 | read = detail::ReadGltf(path); | |
| 95 | 8 | } else if (extension == ".stl") { | |
| 96 | 5 | read = detail::ReadStl(path); | |
| 97 | 3 | } else if (extension == ".icq") { | |
| 98 | 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 | 2 | path.string()); | |
| 102 | } | ||
| 103 | 30 | if (read.mesh.triangles.empty()) { | |
| 104 | 3 | throw std::runtime_error("No triangles in " + path.string()); | |
| 105 | } | ||
| 106 | 29 | ValidateMesh(read.mesh); | |
| 107 | 29 | return read; | |
| 108 | 34 | } | |
| 109 | |||
| 110 | } // namespace terraingen | ||
| 111 |