Lagrange
Loading...
Searching...
No Matches
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/legacy/inline.h>
20#include <lagrange/utils/safe_cast.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(
74 "Max color value is > 0.0 but <= 1.0, but colors are saved as char. "
75 "Please convert your colors to the range [0, 255].");
76 }
77 VD = C.template cast<unsigned char>();
78 if (VD.cols() > 3) {
79 VDheader.push_back("alpha");
80 }
81 la_runtime_assert(safe_cast<size_t>(VD.cols()) == VDheader.size());
82 }
83
84 igl::writePLY(
85 filename.string(),
86 V,
87 F,
88 E,
89 N,
90 UV,
91 VD,
92 VDheader,
93 FD,
94 FDheader,
95 ED,
96 EDheader,
97 comments,
98 encoding == FileEncoding::Binary ? igl::FileEncoding::Binary : igl::FileEncoding::Ascii);
99}
100
101} // namespace legacy
102} // namespace lagrange::io
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40
@ UV
Mesh attribute must have exactly 2 channels.
Definition AttributeFwd.h:62
SurfaceMesh< ToScalar, ToIndex > cast(const SurfaceMesh< FromScalar, FromIndex > &source_mesh, const AttributeFilter &convertible_attributes={}, std::vector< std::string > *converted_attributes_names=nullptr)
Cast a mesh to a mesh of different scalar and/or index type.
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:50
Mesh input/output.
Definition detect_file_format.h:18