14 #include <unordered_map>
22 static const std::unordered_map<
24 std::function<bool(
const std::string &,
37 static const std::unordered_map<
39 std::function<bool(
const std::string &,
53 const std::string &
filename,
bool print_progress) {
54 auto mesh = std::make_shared<geometry::TriangleMesh>();
77 std::string filename_ext =
79 if (filename_ext.empty()) {
81 "Read geometry::TriangleMesh failed: unknown file "
100 "Read geometry::TriangleMesh: {:d} triangles and {:d} "
106 "geometry::TriangleMesh appears to be a "
107 "geometry::PointCloud "
108 "(only contains vertices, but no triangles).");
118 bool write_vertex_normals ,
119 bool write_vertex_colors ,
120 bool write_triangle_uvs ,
121 bool print_progress ) {
122 std::string filename_ext =
124 if (filename_ext.empty()) {
126 "Write geometry::TriangleMesh failed: unknown file "
135 write_vertex_normals, write_vertex_colors, write_triangle_uvs,
139 write_vertex_normals, write_vertex_colors,
140 write_triangle_uvs, print_progress);
142 "Write geometry::TriangleMesh: {:d} triangles and {:d} vertices.",
155 if (!(attribute_map.count(
"vertices") > 0) ||
156 !(attribute_map.count(
"triangles") > 0)) {
158 "Read geometry::TriangleMesh failed: Could not find 'vertices' "
159 "or 'triangles' attributes in {}",
165 for (
auto &attr : attribute_map) {
166 if (attr.first ==
"vertices") {
168 }
else if (attr.first ==
"triangles") {
170 }
else if (attr.first ==
"vertex_normals") {
172 }
else if (attr.first ==
"triangle_normals") {
174 }
else if (attr.first ==
"vertex_colors") {
176 }
else if (attr.first ==
"triangle_colors") {
178 }
else if (attr.first ==
"triangle_texture_uvs") {
180 }
else if (attr.first.find(
"tex_") != std::string::npos) {
182 auto key = attr.first.substr(4);
190 if (key ==
"metallic") {
193 }
else if (attr.first.find(
"vertex_") != std::string::npos) {
195 auto key = attr.first.substr(7);
197 }
else if (attr.first.find(
"triangle_") != std::string::npos) {
199 auto key = attr.first.substr(9);
201 }
else if (attr.first ==
"material_name") {
205 const uint8_t *str_ptr = attr.second.GetDataPtr<uint8_t>();
206 std::string mat_name(attr.second.GetShape().GetLength(),
'a');
207 std::memcpy((
void *)mat_name.data(), str_ptr,
208 attr.second.GetShape().GetLength());
220 const bool write_vertex_normals,
221 const bool write_vertex_colors,
222 const bool write_triangle_uvs,
223 const bool print_progress) {
227 "TriangleMesh can't be saved in ASCII fromat as .npz");
232 "TriangleMesh can't be saved in compressed format as .npz");
237 std::set<std::string> known_attributes(
238 {
"positions",
"normals",
"texture_uvs",
"indices",
"colors"});
241 std::unordered_map<std::string, core::Tensor> mesh_attributes;
261 mesh_attributes[
"triangle_texture_uvs"] =
267 if (known_attributes.count(attr.first) > 0) {
270 std::string key_name(
"vertex_");
271 key_name += attr.first;
272 mesh_attributes[key_name] = attr.second;
275 if (known_attributes.count(attr.first) > 0) {
278 std::string key_name(
"triangle_");
279 key_name += attr.first;
280 mesh_attributes[key_name] = attr.second;
287 std::vector<uint8_t> mat_name_vec(
288 {mat.GetMaterialName().begin(), mat.GetMaterialName().end()});
290 mesh_attributes[
"material_name"] = mat_name_tensor;
291 for (
auto &tex : mat.GetTextureMaps()) {
292 std::string key = std::string(
"tex_") + tex.first;
293 mesh_attributes[key] = tex.second.AsTensor();
cmdLineReadable * params[]
int64_t GetLength() const
visualization::rendering::Material & GetMaterial()
Get material associated with this Geometry.
The Image class stores image with customizable rows, cols, channels, dtype and device.
A triangle mesh contains vertices and triangles.
bool HasTriangleNormals() const
void SetTriangleNormals(const core::Tensor &value)
core::Tensor & GetTriangleIndices()
core::Tensor & GetVertexNormals()
static geometry::TriangleMesh FromLegacy(const ccMesh &mesh_legacy, core::Dtype float_dtype=core::Float32, core::Dtype int_dtype=core::Int64, const core::Device &device=core::Device("CPU:0"))
const TensorMap & GetVertexAttr() const
Getter for vertex_attr_ TensorMap. Used in Pybind.
bool HasVertexNormals() const
bool HasVertexPositions() const
core::Tensor & GetVertexPositions()
bool HasTriangleColors() const
core::Tensor & GetTriangleNormals()
core::Tensor & GetVertexColors()
void SetVertexPositions(const core::Tensor &value)
bool HasTriangleIndices() const
void SetTriangleIndices(const core::Tensor &value)
Set the value of the "indices" attribute in triangle_attr_.
ccMesh ToLegacy() const
Convert to a legacy CloudViewer TriangleMesh.
void SetVertexNormals(const core::Tensor &value)
bool HasVertexColors() const
core::Tensor & GetTriangleColors()
bool HasTriangleAttr(const std::string &key) const
void SetVertexAttr(const std::string &key, const core::Tensor &value)
void SetVertexColors(const core::Tensor &value)
void SetTriangleColors(const core::Tensor &value)
void SetTriangleAttr(const std::string &key, const core::Tensor &value)
const TensorMap & GetTriangleAttr() const
Getter for triangle_attr_ TensorMap. Used in Pybind.
void SetMaterialName(const std::string &material_name)
Set material name. The material name should match the name of a built.
void SetDefaultProperties()
void SetScalarProperty(const std::string &key, float value)
void SetTextureMap(const std::string &key, const t::geometry::Image &image)
bool WriteTriangleMesh(const std::string &filename, const ccMesh &mesh, bool write_ascii, bool compressed, bool write_vertex_normals, bool write_vertex_colors, bool write_triangle_uvs, bool print_progress)
bool ReadTriangleMesh(const std::string &filename, ccMesh &mesh, ReadTriangleMeshOptions params)
bool ReadTriangleMesh(const std::string &filename, geometry::TriangleMesh &mesh, cloudViewer::io::ReadTriangleMeshOptions params)
std::unordered_map< std::string, core::Tensor > ReadNpz(const std::string &file_name)
bool WriteTriangleMeshToNPZ(const std::string &filename, const geometry::TriangleMesh &mesh, const bool write_ascii, const bool compressed, const bool write_vertex_normals, const bool write_vertex_colors, const bool write_triangle_uvs, const bool print_progress)
std::shared_ptr< geometry::TriangleMesh > CreateMeshFromFile(const std::string &filename, bool print_progress)
static const std::unordered_map< std::string, std::function< bool(const std::string &, geometry::TriangleMesh &, const cloudViewer::io::ReadTriangleMeshOptions &)> > file_extension_to_trianglemesh_read_function
void WriteNpz(const std::string &file_name, const std::unordered_map< std::string, core::Tensor > &tensor_map)
bool ReadTriangleMeshFromNPZ(const std::string &filename, geometry::TriangleMesh &mesh, const cloudViewer::io::ReadTriangleMeshOptions ¶ms)
static const std::unordered_map< std::string, std::function< bool(const std::string &, const geometry::TriangleMesh &, const bool, const bool, const bool, const bool, const bool, const bool)> > file_extension_to_trianglemesh_write_function
bool WriteTriangleMesh(const std::string &filename, const geometry::TriangleMesh &mesh, bool write_ascii, bool compressed, bool write_vertex_normals, bool write_vertex_colors, bool write_triangle_uvs, bool print_progress)
bool WriteTriangleMeshUsingASSIMP(const std::string &filename, const geometry::TriangleMesh &mesh, const bool write_ascii, const bool compressed, const bool write_vertex_normals, const bool write_vertex_colors, const bool write_triangle_uvs, const bool print_progress)
bool ReadTriangleMeshUsingASSIMP(const std::string &filename, geometry::TriangleMesh &mesh, const cloudViewer::io::ReadTriangleMeshOptions ¶ms)
std::string GetFileExtensionInLowerCase(const std::string &filename)
Generic file read and write utility for python interface.