xmsgeom  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
TrTin_py.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 
9 //----- Included files ---------------------------------------------------------
10 #include <pybind11/pybind11.h>
11 #include <pybind11/numpy.h>
12 #include <boost/shared_ptr.hpp>
15 #include <iostream>
16 #include <fstream>
17 
18 
19 //----- Namespace declaration --------------------------------------------------
20 namespace py = pybind11;
21 
22 //----- Python Interface -------------------------------------------------------
23 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::shared_ptr<T>);
24 
25 void initTrTin(py::module &m) {
26 
27  // Class
28  py::class_<xms::TrTin, boost::shared_ptr<xms::TrTin>> iTrTin(m, "TrTin");
29  iTrTin.def(py::init(&xms::TrTin::New))
30  .def("set_points", [](xms::TrTin &self, py::iterable pts) {
31  BSHP<xms::VecPt3d> vec_pts = xms::VecPt3dFromPyIter(pts);
32  self.SetPoints(vec_pts);
33  },"Sets the tin points.", py::arg("vec_pts")
34  )
35  .def("set_triangles", [](xms::TrTin &self, py::iterable tris) {
36  BSHP<xms::VecInt> vec_tris = xms::VecIntFromPyIter(tris);
37  self.SetTriangles(vec_tris);
38  },"Sets the tin triangles.",py::arg("vec_tris_adj")
39  )
40  .def("set_triangles_adjacent_to_points", [](xms::TrTin &self, py::iterable tris_adj) {
41  BSHP<xms::VecInt2d> vec_tris_adj = xms::VecInt2dFromPyIter(tris_adj);
42  self.SetTrianglesAdjacentToPoints(vec_tris_adj);
43  },"Sets the adjacency info of triangles adjacent to points.",py::arg("vec_tris_adj")
44  )
45  .def("set_geometry", [](xms::TrTin &self, py::iterable pts, py::iterable tris, py::iterable tris_adj) {
46  boost::shared_ptr<xms::VecPt3d> vec_pts = xms::VecPt3dFromPyIter(pts);
47  boost::shared_ptr<xms::VecInt> vec_tris = xms::VecIntFromPyIter(tris);
48  boost::shared_ptr<xms::VecInt2d> vec_tris_adj = xms::VecInt2dFromPyIter(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")
52  )
53  .def_property_readonly("pts", [](xms::TrTin &self) -> py::iterable {
54  xms::VecPt3d pts = self.Points();
55  return xms::PyIterFromVecPt3d(pts);
56  },"Return the tin points."
57  )
58  .def_property_readonly("tris", [](xms::TrTin &self) -> py::iterable {
59  xms::VecInt tris = self.Triangles();
60  return xms::PyIterFromVecInt(tris);
61  },"Return 0-based indices of triangle points (grouped by 3s)."
62  )
63  .def_property_readonly("tris_adj", [](xms::TrTin &self) -> py::iterable {
64  xms::VecInt2d tris_adj = self.TrisAdjToPts();
65  return xms::PyIterFromVecInt2d(tris_adj);
66  },"Returns triangles adjacent to points (0-based)."
67  )
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")
77  )
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")
82  )
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")
87  )
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")
90  )
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")
94  )
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")
98  )
99  .def("triangle_centroid", [](xms::TrTin &self, int tri) -> py::tuple {
100  xms::Pt3d pt = self.TriangleCentroid(tri);
101  return xms::PyIterFromPt3d(pt);
102  },"Calculate and return the centroid of a triangle.",py::arg("tri")
103  )
104  .def("triangle_area", &xms::TrTin::TriangleArea,"Calculate and return the area of a triangle.",
105  py::arg("tri")
106  )
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).",
110  py::arg("point")
111  )
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")
115  )
116  .def("get_boundary_points", [](xms::TrTin &self) -> py::iterable {
117  xms::VecInt bp;
118  self.GetBoundaryPoints(bp);
119  return xms::PyIterFromVecInt(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 {
122  xms::VecInt2d bp;
123  self.GetBoundaryPolys(bp);
124  return xms::PyIterFromVecInt2d(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."
127  )
128  .def("get_extents", [](xms::TrTin &self) -> py::tuple {
129  xms::Pt3d pt_min, pt_max;
130  self.GetExtents(pt_min, pt_max);
131  auto min_pt = xms::PyIterFromPt3d(pt_min);
132  auto max_pt = xms::PyIterFromPt3d(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) {
136  // TODO: This needs some error checking
137  std::filebuf fb;
138  fb.open(fname, std::ios::out);
139  std::ostream os(&fb);
140  self.ExportTinFile(os);
141  fb.close();
142  },"Export in the .tin file format. Useful for debugging.",py::arg("fname")
143  )
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")
146  )
147  .def("delete_triangles", [](xms::TrTin &self, py::iterable tris) {
148  xms::SetInt to_delete;
149  for (auto item : tris) {
150  to_delete.insert(item.cast<int>());
151  }
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")
155  )
156  .def("delete_points", [](xms::TrTin &self, py::iterable pts) {
157  xms::SetInt to_delete;
158  for (auto item : pts) {
159  to_delete.insert(item.cast<int>());
160  }
161  self.DeletePoints(to_delete);
162  },"Deletes the points and any attached triangles, updates adjacency and renumbers things.",
163  py::arg("pts")
164  )
165  .def("optimize_triangulation", &xms::TrTin::OptimizeTriangulation,
166  "Swaps triangle edges until they are a Delauney triangulation."
167  )
168  .def("build_tris_adj_to_pts", &xms::TrTin::BuildTrisAdjToPts,
169  "Build array of triangles adjacent to points."
170  )
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.")
174  ;
175 }
py::iterable PyIterFromVecInt2d(const VecInt2d &int2d)
py::iterable PyIterFromVecPt3d(const VecPt3d &pts)
std::vector< int > VecInt
std::set< int > SetInt
py::iterable PyIterFromVecInt(const VecInt &ints, bool numpy)
static BSHP< TrTin > New()
Create a TrTinImpl object.
Definition: TrTin.cpp:1247
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