1 // ----------------------------------------------------------------------------
2 // -                        Open3D: www.open3d.org                            -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include "open3d_core.h"
28 #include "open3d_core_trampoline.h"
29 
30 #include <Core/Geometry/TriangleMesh.h>
31 #include <IO/ClassIO/TriangleMeshIO.h>
32 using namespace three;
33 
pybind_trianglemesh(py::module & m)34 void pybind_trianglemesh(py::module &m)
35 {
36     py::class_<TriangleMesh, PyGeometry3D<TriangleMesh>,
37             std::shared_ptr<TriangleMesh>, Geometry3D> trianglemesh(m,
38             "TriangleMesh");
39     py::detail::bind_default_constructor<TriangleMesh>(trianglemesh);
40     py::detail::bind_copy_functions<TriangleMesh>(trianglemesh);
41     trianglemesh
42         .def("__repr__", [](const TriangleMesh &mesh) {
43             return std::string("TriangleMesh with ") +
44                     std::to_string(mesh.vertices_.size()) + " points and " +
45                     std::to_string(mesh.triangles_.size()) + " triangles.";
46         })
47         .def(py::self + py::self)
48         .def(py::self += py::self)
49         .def("compute_triangle_normals", &TriangleMesh::ComputeTriangleNormals,
50                 "Function to compute triangle normals, usually called before rendering",
51                 "normalized"_a = true)
52         .def("compute_vertex_normals", &TriangleMesh::ComputeVertexNormals,
53                 "Function to compute vertex normals, usually called before rendering",
54                 "normalized"_a = true)
55         .def("purge", &TriangleMesh::Purge,
56                 "Function to remove duplicated and non-manifold vertices/triangles")
57         .def("has_vertices", &TriangleMesh::HasVertices)
58         .def("has_triangles", &TriangleMesh::HasTriangles)
59         .def("has_vertex_normals", &TriangleMesh::HasVertexNormals)
60         .def("has_vertex_colors", &TriangleMesh::HasVertexColors)
61         .def("has_triangle_normals", &TriangleMesh::HasTriangleNormals)
62         .def("normalize_normals", &TriangleMesh::NormalizeNormals)
63         .def("paint_uniform_color", &TriangleMesh::PaintUniformColor)
64         .def_readwrite("vertices", &TriangleMesh::vertices_)
65         .def_readwrite("vertex_normals", &TriangleMesh::vertex_normals_)
66         .def_readwrite("vertex_colors", &TriangleMesh::vertex_colors_)
67         .def_readwrite("triangles", &TriangleMesh::triangles_)
68         .def_readwrite("triangle_normals", &TriangleMesh::triangle_normals_);
69 }
70 
pybind_trianglemesh_methods(py::module & m)71 void pybind_trianglemesh_methods(py::module &m)
72 {
73     m.def("read_triangle_mesh", [](const std::string &filename) {
74         TriangleMesh mesh;
75         ReadTriangleMesh(filename, mesh);
76         return mesh;
77     }, "Function to read TriangleMesh from file", "filename"_a);
78     m.def("write_triangle_mesh", [](const std::string &filename,
79             const TriangleMesh &mesh, bool write_ascii, bool compressed) {
80         return WriteTriangleMesh(filename, mesh, write_ascii, compressed);
81     }, "Function to write TriangleMesh to file", "filename"_a, "mesh"_a,
82             "write_ascii"_a = false, "compressed"_a = false);
83     m.def("select_down_sample", &SelectDownSample,
84             "Function to select mesh from input triangle mesh into output triangle mesh",
85             "input"_a, "indices"_a);
86     m.def("crop_triangle_mesh", &CropTriangleMesh,
87             "Function to crop input triangle mesh into output triangle mesh",
88             "input"_a, "min_bound"_a, "max_bound"_a);
89     m.def("create_mesh_sphere", &CreateMeshSphere,
90             "Factory function to create a sphere mesh",
91             "radius"_a = 1.0, "resolution"_a = 20);
92     m.def("create_mesh_cylinder", &CreateMeshCylinder,
93             "Factory function to create a cylinder mesh",
94             "radius"_a = 1.0, "height"_a = 2.0, "resolution"_a = 20,
95             "split"_a = 4);
96     m.def("create_mesh_cone", &CreateMeshCone,
97             "Factory function to create a cone mesh",
98             "radius"_a = 1.0, "height"_a = 2.0, "resolution"_a = 20,
99             "split"_a = 1);
100     m.def("create_mesh_arrow", &CreateMeshArrow,
101             "Factory function to create an arrow mesh",
102             "cylinder_radius"_a = 1.0, "cone_radius"_a = 1.5,
103             "cylinder_height"_a = 5.0, "cone_height"_a = 4.0,
104             "resolution"_a = 20, "cylinder_split"_a = 4, "cone_split"_a = 1);
105     m.def("create_mesh_coordinate_frame", &CreateMeshCoordinateFrame,
106             "Factory function to create a coordinate frame mesh",
107             "size"_a = 1.0, "origin"_a = Eigen::Vector3d(0.0, 0.0, 0.0));
108 }
109