ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
halfedgemesh.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <sstream>
9 
10 #include "HalfEdgeTriangleMesh.h"
11 #include "pybind/docstring.h"
14 
15 namespace cloudViewer {
16 namespace geometry {
17 
18 void pybind_half_edge(py::module &m) {
19  py::class_<HalfEdgeTriangleMesh::HalfEdge> half_edge(
20  m, "HalfEdge",
21  "HalfEdge class contains vertex, triangle info about a half edge, "
22  "as well as relations of next and twin half edge.");
23  py::detail::bind_default_constructor<HalfEdgeTriangleMesh::HalfEdge>(
24  half_edge);
25  py::detail::bind_copy_functions<HalfEdgeTriangleMesh::HalfEdge>(half_edge);
26  half_edge
27  .def("__repr__",
28  [](const HalfEdgeTriangleMesh::HalfEdge &he) {
29  std::ostringstream repr;
30  repr << "HalfEdge(vertex_indices {"
31  << he.vertex_indices_(0) << ", "
32  << he.vertex_indices_(1) << "}, triangle_index "
33  << he.triangle_index_ << ", next " << he.next_
34  << ", twin " << he.twin_ << ")";
35  return repr.str();
36  })
37  .def("is_boundary", &HalfEdgeTriangleMesh::HalfEdge::IsBoundary,
38  "Returns ``True`` iff the half edge is the boundary (has not "
39  "twin, i.e. twin index == -1).")
40  .def_readwrite(
42  "int: Index of the next HalfEdge in the same triangle.")
43  .def_readwrite("twin", &HalfEdgeTriangleMesh::HalfEdge::twin_,
44  "int: Index of the twin HalfEdge")
45  .def_readwrite(
46  "vertex_indices",
48  "List(int) of length 2: Index of the ordered vertices "
49  "forming this half edge")
50  .def_readwrite(
51  "triangle_index",
53  "int: Index of the triangle containing this half edge");
54 }
55 
56 void pybind_halfedgetrianglemesh(py::module &m) {
58 
59  // cloudViewer.geometry.HalfEdgeTriangleMesh
60  py::class_<HalfEdgeTriangleMesh, PyGeometry<HalfEdgeTriangleMesh>,
61  std::shared_ptr<HalfEdgeTriangleMesh>, ecvMeshBase>
62  half_edge_triangle_mesh(
63  m, "HalfEdgeTriangleMesh",
64  "HalfEdgeTriangleMesh inherits TriangleMesh class with the "
65  "addition of HalfEdge data structure for each half edge in "
66  "the mesh as well as related functions.");
67  py::detail::bind_default_constructor<HalfEdgeTriangleMesh>(
68  half_edge_triangle_mesh);
69  py::detail::bind_copy_functions<HalfEdgeTriangleMesh>(
70  half_edge_triangle_mesh);
71  half_edge_triangle_mesh
72  .def("__repr__",
73  [](const HalfEdgeTriangleMesh &mesh) {
74  return std::string("HalfEdgeTriangleMesh with ") +
75  std::to_string(mesh.vertices_.size()) +
76  " points and " +
77  std::to_string(mesh.half_edges_.size()) +
78  " half edges.";
79  })
80  .def_readwrite("triangles", &HalfEdgeTriangleMesh::triangles_,
81  "``int`` array of shape ``(num_triangles, 3)``, use "
82  "``numpy.asarray()`` to access data: List of "
83  "triangles denoted by the index of points forming "
84  "the triangle.")
85  .def_readwrite("triangle_normals",
87  "``float64`` array of shape ``(num_triangles, 3)``, "
88  "use ``numpy.asarray()`` to access data: Triangle "
89  "normals.")
90  .def("has_half_edges", &HalfEdgeTriangleMesh::hasHalfEdges,
91  "Returns ``True`` if half-edges have already been computed.")
92  .def("boundary_half_edges_from_vertex",
94  "vertex_index"_a,
95  "Query manifold boundary half edges from a starting vertex. "
96  "If query vertex is not on boundary, empty vector will be "
97  "returned.")
98  .def("boundary_vertices_from_vertex",
100  "vertex_index"_a
101  "Query manifold boundary vertices from a starting vertex. If "
102  "query vertex is not on boundary, empty vector will be "
103  "returned.")
104  .def("get_boundaries", &HalfEdgeTriangleMesh::getBoundaries,
105  "Returns a vector of boundaries. A boundary is a vector of "
106  "vertices.")
107  .def_static("create_from_triangle_mesh",
109  "Convert HalfEdgeTriangleMesh from TriangleMesh. "
110  "Throws exception if "
111  "the input mesh is not manifolds")
112  .def_readwrite("half_edges", &HalfEdgeTriangleMesh::half_edges_,
113  "List of HalfEdge in the mesh")
114  .def_readwrite(
115  "ordered_half_edge_from_vertex",
117  "Counter-clockwise ordered half-edges started from "
118  "each vertex");
119  docstring::ClassMethodDocInject(m, "HalfEdgeTriangleMesh",
120  "boundary_half_edges_from_vertex");
121  docstring::ClassMethodDocInject(m, "HalfEdgeTriangleMesh",
122  "boundary_vertices_from_vertex");
123  docstring::ClassMethodDocInject(m, "HalfEdgeTriangleMesh",
124  "get_boundaries");
125  docstring::ClassMethodDocInject(m, "HalfEdgeTriangleMesh",
126  "has_half_edges");
127  docstring::ClassMethodDocInject(m, "HalfEdgeTriangleMesh",
128  "create_from_triangle_mesh",
129  {{"mesh", "The input TriangleMesh"}});
130 }
131 
132 } // namespace geometry
133 } // namespace cloudViewer
HalfEdge class contains vertex, triangle info about a half edge, as well as relations of next and twi...
int next_
Index of the next HalfEdge in the same triangle.
Eigen::Vector2i vertex_indices_
Index of the ordered vertices forming this half edge.
int triangle_index_
Index of the triangle containing this half edge.
HalfEdgeTriangleMesh inherits TriangleMesh class with the addition of HalfEdge data structure for eac...
std::vector< int > boundaryVerticesFromVertex(int vertex_index) const
std::vector< int > boundaryHalfEdgesFromVertex(int vertex_index) const
std::vector< std::vector< int > > ordered_half_edge_from_vertex_
bool hasHalfEdges() const
Returns true if half-edges have already been computed.
static std::shared_ptr< HalfEdgeTriangleMesh > CreateFromTriangleMesh(const ccMesh &mesh)
std::vector< Eigen::Vector3i > triangles_
List of triangles in the mesh.
std::vector< HalfEdge > half_edges_
List of HalfEdge in the mesh.
std::vector< std::vector< int > > getBoundaries() const
Returns a vector of boundaries. A boundary is a vector of vertices.
std::vector< Eigen::Vector3d > triangle_normals_
List of triangle normals in the mesh.
std::vector< Eigen::Vector3d > vertices_
Vertex coordinates.
Definition: ecvMeshBase.h:132
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:27
void pybind_half_edge(py::module &m)
void pybind_halfedgetrianglemesh(py::module &m)
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20