Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
load_mesh.impl.h
1/*
2 * Copyright 2021 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/io/load_mesh.h>
15
16#include <lagrange/Mesh.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/combine_mesh_list.h>
19#include <lagrange/create_mesh.h>
20#include <lagrange/fs/file_utils.h>
21#include <lagrange/io/legacy/load_mesh_ext.h>
22#include <lagrange/io/legacy/load_mesh_ply.h>
23#include <lagrange/legacy/inline.h>
24
25// clang-format off
26#include <lagrange/utils/warnoff.h>
27#include <igl/read_triangle_mesh.h>
28#include <lagrange/utils/warnon.h>
29// clang-format on
30
31#include <tiny_obj_loader.h>
32
33namespace lagrange::io {
34LAGRANGE_LEGACY_INLINE
35namespace legacy {
36
37template <typename MeshType>
38std::unique_ptr<MeshType> load_mesh_basic(const fs::path& filename)
39{
40 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
41 typename MeshType::VertexArray vertices;
42 typename MeshType::FacetArray facets;
43
44 igl::read_triangle_mesh(filename.string(), vertices, facets);
45
46 return create_mesh(vertices, facets);
47}
48extern template std::unique_ptr<lagrange::TriangleMesh3D> load_mesh_basic(const fs::path&);
49extern template std::unique_ptr<lagrange::TriangleMesh3Df> load_mesh_basic(const fs::path&);
50
51template <typename MeshType>
52std::vector<std::unique_ptr<MeshType>> load_obj_meshes(const fs::path& filename)
53{
54 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
55
56 if (!std::is_same<tinyobj::real_t, double>::value) {
57 logger().warn("Tinyobjloader compiled to use float, loss of precision may occur.");
58 }
59
60 return load_mesh_ext<MeshType>(filename).meshes;
61}
62extern template std::vector<std::unique_ptr<lagrange::TriangleMesh3D>> load_obj_meshes(const fs::path&);
63extern template std::vector<std::unique_ptr<lagrange::TriangleMesh3Df>> load_obj_meshes(const fs::path&);
64
65template <typename MeshType>
66std::unique_ptr<MeshType> load_obj_mesh(const fs::path& filename)
67{
68 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
69
70 auto res = load_obj_meshes<MeshType>(filename);
71 if (res.empty()) {
72 return nullptr;
73 } else if (res.size() == 1) {
74 return std::move(res[0]);
75 } else {
76 logger().debug("Combining {} meshes into one.", res.size());
77 return combine_mesh_list(res, true);
78 }
79}
80extern template std::unique_ptr<lagrange::TriangleMesh3D> load_obj_mesh(const fs::path&);
81extern template std::unique_ptr<lagrange::TriangleMesh3Df> load_obj_mesh(const fs::path&);
82
83template <
84 typename MeshType,
85 std::enable_if_t<lagrange::MeshTraitHelper::is_mesh<MeshType>::value>* /*= nullptr*/>
86std::unique_ptr<MeshType> load_mesh(const fs::path& filename)
87{
88 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
89
90 if (filename.extension() == ".obj") {
91 return load_obj_mesh<MeshType>(filename);
92 } else if (filename.extension() == ".ply") {
93 return load_mesh_ply<MeshType>(filename);
94 } else {
95 return load_mesh_basic<MeshType>(filename);
96 }
97}
98extern template std::unique_ptr<lagrange::TriangleMesh3D> load_mesh(const fs::path&);
99extern template std::unique_ptr<lagrange::TriangleMesh3Df> load_mesh(const fs::path&);
100
101
102} // namespace legacy
103} // namespace lagrange::io
Definition: Mesh.h:48
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
Mesh input/output.
Definition: detect_file_format.h:18
MeshType load_mesh(std::istream &input_stream, const LoadOptions &options={})
Load a mesh from a stream.
Definition: load_mesh.cpp:37
auto create_mesh(const Eigen::MatrixBase< DerivedV > &vertices, const Eigen::MatrixBase< DerivedF > &facets)
This function create a new mesh given the vertex and facet arrays by copying data into the Mesh objec...
Definition: create_mesh.h:39