Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
save_mesh_ply.h
1/*
2 * Copyright 2020 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/fs/filesystem.h>
15#include <lagrange/io/types.h>
16
17#include <lagrange/MeshTrait.h>
18#include <lagrange/create_mesh.h>
19#include <lagrange/utils/safe_cast.h>
20#include <lagrange/legacy/inline.h>
21
22// clang-format off
23#include <lagrange/utils/warnoff.h>
24#include <igl/writePLY.h>
25#include <lagrange/utils/warnon.h>
26// clang-format on
27
28#include <string>
29#include <vector>
30
31namespace lagrange::io {
32LAGRANGE_LEGACY_INLINE
33namespace legacy {
34
36template <
37 typename MeshType,
38 std::enable_if_t<lagrange::MeshTraitHelper::is_mesh<MeshType>::value>* = nullptr>
39void save_mesh_ply(
40 const fs::path& filename,
41 const MeshType& mesh,
42 FileEncoding encoding = FileEncoding::Binary)
43{
44 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
45 using VertexArray = typename MeshType::VertexArray;
46 using AttributeArray = typename MeshType::AttributeArray;
47 using Index = unsigned int;
48
49 const VertexArray& V = mesh.get_vertices();
50 const auto F = mesh.get_facets().template cast<Index>();
51 Eigen::Matrix<Index, Eigen::Dynamic, Eigen::Dynamic> E;
52 AttributeArray N;
53 AttributeArray UV;
54
55 Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic> VD;
56 std::vector<std::string> VDheader;
57
58 AttributeArray FD;
59 std::vector<std::string> FDheader;
60
61 AttributeArray ED;
62 std::vector<std::string> EDheader;
63 std::vector<std::string> comments;
64
65 if (mesh.has_vertex_attribute("normal")) {
66 N = mesh.get_vertex_attribute("normal");
67 }
68
69 if (mesh.has_vertex_attribute("color")) {
70 VDheader = {"red", "green", "blue"};
71 auto C = mesh.get_vertex_attribute("color");
72 if (C.maxCoeff() <= 1.0 && C.maxCoeff() > 0.0) {
73 logger().warn("Max color value is > 0.0 but <= 1.0, but colors are saved as char. "
74 "Please convert your colors to the range [0, 255].");
75 }
76 VD = C.template cast<unsigned char>();
77 if (VD.cols() > 3) {
78 VDheader.push_back("alpha");
79 }
80 la_runtime_assert(safe_cast<size_t>(VD.cols()) == VDheader.size());
81 }
82
83 igl::writePLY(
84 filename.string(),
85 V,
86 F,
87 E,
88 N,
89 UV,
90 VD,
91 VDheader,
92 FD,
93 FDheader,
94 ED,
95 EDheader,
96 comments,
97 encoding == FileEncoding::Binary ? igl::FileEncoding::Binary : igl::FileEncoding::Ascii);
98}
99
100} // namespace legacy
101} // namespace lagrange::io
Definition: Mesh.h:48
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
void save_mesh_ply(std::ostream &output_stream, const SurfaceMesh< Scalar, Index > &mesh, const SaveOptions &options={})
Saves a mesh to a stream in PLY format.
Definition: save_mesh_ply.cpp:214