14#include <lagrange/fs/filesystem.h>
16#include <lagrange/MeshTrait.h>
17#include <lagrange/create_mesh.h>
18#include <lagrange/legacy/inline.h>
21#include <lagrange/utils/warnoff.h>
22#include <igl/readPLY.h>
23#include <lagrange/utils/warnon.h>
34template <
typename MeshType>
35std::unique_ptr<MeshType>
load_mesh_ply(
const fs::path& filename)
37 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
38 using VertexArray =
typename MeshType::VertexArray;
39 using FacetArray =
typename MeshType::FacetArray;
40 using AttributeArray =
typename MeshType::AttributeArray;
41 using Index =
typename MeshType::Index;
45 Eigen::Matrix<Index, Eigen::Dynamic, Eigen::Dynamic> E;
50 std::vector<std::string> VDheader;
53 std::vector<std::string> FDheader;
56 std::vector<std::string> EDheader;
57 std::vector<std::string> comments;
76 auto mesh =
create_mesh(std::move(V), std::move(F));
78 logger().debug(
"Setting vertex normal");
79 mesh->add_vertex_attribute(
"normal");
80 mesh->import_vertex_attribute(
"normal", N);
83 auto has_attribute = [&](
const std::string& name) {
84 return std::find(VDheader.begin(), VDheader.end(), name) != VDheader.end();
88 if (has_attribute(
"red") && has_attribute(
"green") && has_attribute(
"blue")) {
89 bool has_alpha = has_attribute(
"alpha");
90 int n = (has_alpha ? 4 : 3);
93 for (
size_t i = 0; i < VDheader.size(); ++i) {
94 if (VDheader[i] ==
"red") {
96 color.col(0) = VD.col(i);
97 }
else if (VDheader[i] ==
"green") {
99 color.col(1) = VD.col(i);
100 }
else if (VDheader[i] ==
"blue") {
102 color.col(2) = VD.col(i);
103 }
else if (VDheader[i] ==
"alpha") {
105 color.col(3) = VD.col(i);
107 logger().warn(
"Unknown vertex attribute: {}", VDheader[i]);
110 logger().debug(
"Setting vertex color");
111 mesh->add_vertex_attribute(
"color");
112 mesh->import_vertex_attribute(
"color", color);
Index get_num_vertices() const
Retrieves the number of vertices.
Definition: SurfaceMesh.h:671
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
@ UV
Mesh attribute must have exactly 2 channels.
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Mesh input/output.
Definition: detect_file_format.h:18
MeshType load_mesh_ply(std::istream &input_stream, const LoadOptions &options={})
Loads a mesh from a stream in PLY format.
Definition: load_mesh_ply.cpp:301
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