10 #include <pybind11/pybind11.h>
11 #include <pybind11/numpy.h>
12 #include <boost/shared_ptr.hpp>
20 namespace py = pybind11;
23 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::shared_ptr<T>);
25 void initTrTin(py::module &m) {
28 py::class_<xms::TrTin, boost::shared_ptr<xms::TrTin>> iTrTin(m,
"TrTin");
30 .def(
"set_points", [](
xms::TrTin &
self, py::iterable pts) {
32 self.SetPoints(vec_pts);
33 },
"Sets the tin points.", py::arg(
"vec_pts")
35 .def(
"set_triangles", [](
xms::TrTin &
self, py::iterable tris) {
37 self.SetTriangles(vec_tris);
38 },
"Sets the tin triangles.",py::arg(
"vec_tris_adj")
40 .def(
"set_triangles_adjacent_to_points", [](
xms::TrTin &
self, py::iterable tris_adj) {
42 self.SetTrianglesAdjacentToPoints(vec_tris_adj);
43 },
"Sets the adjacency info of triangles adjacent to points.",py::arg(
"vec_tris_adj")
45 .def(
"set_geometry", [](
xms::TrTin &
self, py::iterable pts, py::iterable tris, py::iterable tris_adj) {
49 self.SetGeometry(vec_pts, vec_tris, vec_tris_adj);
50 },
"Set all the tin geometry at once (points, triangles, adjacency).",
51 py::arg(
"vec_pts"), py::arg(
"vec_tris"), py::arg(
"vec_tris_adj")
53 .def_property_readonly(
"pts", [](
xms::TrTin &
self) -> py::iterable {
56 },
"Return the tin points."
58 .def_property_readonly(
"tris", [](
xms::TrTin &
self) -> py::iterable {
61 },
"Return 0-based indices of triangle points (grouped by 3s)."
63 .def_property_readonly(
"tris_adj", [](
xms::TrTin &
self) -> py::iterable {
66 },
"Returns triangles adjacent to points (0-based)."
68 .def_property_readonly(
"num_points", &xms::TrTin::NumPoints,
"Return the number of points.")
69 .def_property_readonly(
"num_triangles", &xms::TrTin::NumTriangles,
"Return the number of triangles.")
70 .def(
"triangle_from_edge", [](
xms::TrTin &
self,
int pt1,
int pt2) -> py::tuple {
71 int tri, localPt1, localPt2;
72 bool ret =
self.TriangleFromEdge(pt1, pt2, tri, localPt1, localPt2);
73 return py::make_tuple(ret, tri, localPt1, localPt2);
74 },
"Finds the triangle with the edge defined by a_pt1 and a_pt2 and the local index of those"
75 " points. Compare to trTinTriFromEdge.",
76 py::arg(
"pt1"), py::arg(
"pt2")
78 .def(
"triangle_adjacent_to_edge", [](
xms::TrTin &
self,
int pt1,
int pt2) ->
int {
79 return self.TriangleAdjacentToEdge(pt1, pt2);
80 },
"Returns the triangle adjacent to the edge defined by a_pt1 and a_pt2. Compare to"
81 " trTriangleAdjacentToEdge.", py::arg(
"pt1"), py::arg(
"pt2")
83 .def(
"local_index", &xms::TrTin::LocalIndex,
"Returns index (0-2) of point within triangle given"
84 " global index. Compare to trIndex.",py::arg(
"tri"), py::arg(
"pt"))
85 .def(
"global_index", &xms::TrTin::GlobalIndex,
"Returns index into m_pts of a_localPt which is 0-2.",
86 py::arg(
"tri_idx"), py::arg(
"local_pt")
88 .def(
"vertices_are_adjacent", &xms::TrTin::VerticesAreAdjacent,
"Return true if vertices form"
89 " the edge of a triangle. Compare to vrVerticesAreAdjacent.", py::arg(
"pt1"),py::arg(
"pt2")
91 .def(
"common_edge_index", &xms::TrTin::CommonEdgeIndex,
"Return index of common edge between"
92 " triangle and neighbor. Edge index is 0-2 based on a_tri. Compare to trCommonEdgeIndex.",
93 py::arg(
"tri"), py::arg(
"adj_tri")
95 .def(
"adjacent_triangle", &xms::TrTin::AdjacentTriangle,
"Returns the triangle adjacent"
96 "to a_triIdx across a_edgeIdx (0-2). Compare to trAdjacentTriangle.",
97 py::arg(
"tri_idx"), py::arg(
"edge_idx")
99 .def(
"triangle_centroid", [](
xms::TrTin &
self,
int tri) -> py::tuple {
100 xms::Pt3d pt =
self.TriangleCentroid(tri);
102 },
"Calculate and return the centroid of a triangle.",py::arg(
"tri")
104 .def(
"triangle_area", &xms::TrTin::TriangleArea,
"Calculate and return the area of a triangle.",
107 .def(
"next_boundary_point", &xms::TrTin::NextBoundaryPoint,
"Returns the next point CW from"
108 " point on the boundary. CCW if in an inside hole. Compare to trNextBoundaryVertex (or"
109 " trPreviousBoundaryVertex since order here is CW, not CCW).",
112 .def(
"previous_boundary_point", &xms::TrTin::PreviousBoundaryPoint,
"Returns the next point CCW"
113 " from point on the boundary. CW if in an inside hole. Compare to trPreviousBoundaryVertex"
114 " (or trNextBoundaryVertex since order here is CW, not CCW).",py::arg(
"point")
116 .def(
"get_boundary_points", [](
xms::TrTin &
self) -> py::iterable {
118 self.GetBoundaryPoints(bp);
120 },
"Gives the 0-based indices of all points on any boundary, in no particular order.")
121 .def(
"get_boundary_polys", [](
xms::TrTin &
self) -> py::tuple {
123 self.GetBoundaryPolys(bp);
125 },
"Gets exterior boundary and all interior voids as polygons of 0-based point indices."
126 " First point is not repeated as the last point."
128 .def(
"get_extents", [](
xms::TrTin &
self) -> py::tuple {
130 self.GetExtents(pt_min, pt_max);
133 return py::make_tuple(min_pt, max_pt);
134 },
"Computes the extents (min, max) of the tin.")
135 .def(
"export_tin_file", [](
xms::TrTin &
self, std::string fname) {
138 fb.open(fname, std::ios::out);
139 std::ostream os(&fb);
140 self.ExportTinFile(os);
142 },
"Export in the .tin file format. Useful for debugging.",py::arg(
"fname")
144 .def(
"swap_edge", &xms::TrTin::SwapEdge,
"Swap edges if triangles combine to form convex quad."
145 " Compare to trSwapEdge.",py::arg(
"tri_A"), py::arg(
"tri_B"), py::arg(
"check_angle")
147 .def(
"delete_triangles", [](
xms::TrTin &
self, py::iterable tris) {
149 for (
auto item : tris) {
150 to_delete.insert(item.cast<
int>());
152 self.DeleteTriangles(to_delete);
153 },
"Deletes the triangles specified in \a a_trisToDelete and updates and renumbers triangle"
154 " adjacency info.",py::arg(
"tris")
156 .def(
"delete_points", [](
xms::TrTin &
self, py::iterable pts) {
158 for (
auto item : pts) {
159 to_delete.insert(item.cast<
int>());
161 self.DeletePoints(to_delete);
162 },
"Deletes the points and any attached triangles, updates adjacency and renumbers things.",
165 .def(
"optimize_triangulation", &xms::TrTin::OptimizeTriangulation,
166 "Swaps triangle edges until they are a Delauney triangulation."
168 .def(
"build_tris_adj_to_pts", &xms::TrTin::BuildTrisAdjToPts,
169 "Build array of triangles adjacent to points."
171 .def(
"clear", &xms::TrTin::Clear,
"Delete the memory.")
172 .def(
"from_string", &xms::TrTin::FromString,
"Use boost archive to turn the text into a TrTin.")
173 .def(
"__str__", &xms::TrTin::ToString,
"Use boost archive to get the TrTin as text.")
py::iterable PyIterFromVecInt2d(const VecInt2d &int2d)
py::iterable PyIterFromVecPt3d(const VecPt3d &pts)
std::vector< int > VecInt
py::iterable PyIterFromVecInt(const VecInt &ints, bool numpy)
static BSHP< TrTin > New()
Create a TrTinImpl object.
std::vector< Pt3d > VecPt3d
boost::shared_ptr< VecInt2d > VecInt2dFromPyIter(const py::iterable &int2d)
boost::shared_ptr< VecInt > VecIntFromPyIter(const py::iterable &ints)
boost::shared_ptr< VecPt3d > VecPt3dFromPyIter(const py::iterable &pts)
py::tuple PyIterFromPt3d(const Pt3d &pt)
std::vector< VecInt > VecInt2d