Kaynağa Gözat

Update vector binding to work with new pybind11

Former-commit-id: 9bfa4df10758d90d6ec12d49cad2ee9082e360fc
skoch 8 yıl önce
ebeveyn
işleme
3567e2d9b0

+ 29 - 31
python/modules/py_vector.cpp

@@ -7,14 +7,13 @@
 
 /// Creates Python bindings for a dynamic Eigen matrix
 template <typename Type>
-py::class_<Type> bind_eigen_2(py::module &m, const char *name,
-                                py::object parent = py::object()) {
+py::class_<Type> bind_eigen_2(py::module &m, const char *name) {
     typedef typename Type::Scalar Scalar;
 
     /* Many Eigen functions are templated and can't easily be referenced using
        a function pointer, thus a big portion of the binding code below
        instantiates Eigen code using small anonymous wrapper functions */
-    py::class_<Type> matrix(m, name, parent);
+    py::class_<Type> matrix(m, name, py::buffer_protocol());
 
     matrix
         /* Constructors */
@@ -24,6 +23,26 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
             new (&m) Type(1, 1);
             m(0, 0) = f;
         })
+        .def("__init__", [](Type &m, py::buffer b) {
+            py::buffer_info info = b.request();
+            if (info.format != py::format_descriptor<Scalar>::format())
+                throw std::runtime_error("Incompatible buffer format!");
+            if (info.ndim == 1) {
+                new (&m) Type(info.shape[0], 1);
+                memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
+            } else if (info.ndim == 2) {
+                if (info.strides[0] == sizeof(Scalar)) {
+                    new (&m) Type(info.shape[0], info.shape[1]);
+                    memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
+                } else {
+                    new (&m) Type(info.shape[1], info.shape[0]);
+                    memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
+                    m.transposeInPlace();
+                }
+            } else {
+                throw std::runtime_error("Incompatible buffer dimension!");
+            }
+        })
         .def("__init__", [](Type &m, std::vector<std::vector< Scalar> >& b) {
           if (b.size() == 0)
           {
@@ -66,26 +85,7 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
 
           return;
         })
-        .def("__init__", [](Type &m, py::buffer b) {
-            py::buffer_info info = b.request();
-            if (info.format != py::format_descriptor<Scalar>::value)
-                throw std::runtime_error("Incompatible buffer format!");
-            if (info.ndim == 1) {
-                new (&m) Type(info.shape[0], 1);
-                memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
-            } else if (info.ndim == 2) {
-                if (info.strides[0] == sizeof(Scalar)) {
-                    new (&m) Type(info.shape[0], info.shape[1]);
-                    memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
-                } else {
-                    new (&m) Type(info.shape[1], info.shape[0]);
-                    memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
-                    m.transposeInPlace();
-                }
-            } else {
-                throw std::runtime_error("Incompatible buffer dimension!");
-            }
-        })
+
 
         /* Size query functions */
         .def("size", [](const Type &m) { return m.size(); })
@@ -319,7 +319,7 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
                 m.data(),                /* Pointer to buffer */
                 sizeof(Scalar),          /* Size of one scalar */
                 /* Python struct-style format descriptor */
-                py::format_descriptor<Scalar>::value,
+                py::format_descriptor<Scalar>::format(),
                 2,                       /* Number of dimensions */
                 { (size_t) m.rows(),     /* Buffer dimensions */
                   (size_t) m.cols() },
@@ -347,14 +347,13 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
 
 /// Creates Python bindings for a dynamic Eigen sparse order-2 tensor (i.e. a matrix)
 template <typename Type>
-py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name,
-                                py::object parent = py::object()) {
+py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name) {
     typedef typename Type::Scalar Scalar;
 
     /* Many Eigen functions are templated and can't easily be referenced using
        a function pointer, thus a big portion of the binding code below
        instantiates Eigen code using small anonymous wrapper functions */
-    py::class_<Type> matrix(m, name, parent);
+    py::class_<Type> matrix(m, name, py::buffer_protocol());
 
     matrix
         /* Constructors */
@@ -532,14 +531,13 @@ py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name,
 
 /// Creates Python bindings for a diagonal Eigen sparse order-2 tensor (i.e. a matrix)
 template <typename Type>
-py::class_<Type> bind_eigen_diagonal_2(py::module &m, const char *name,
-                                py::object parent = py::object()) {
+py::class_<Type> bind_eigen_diagonal_2(py::module &m, const char *name) {
     typedef typename Type::Scalar Scalar;
 
     /* Many Eigen functions are templated and can't easily be referenced using
        a function pointer, thus a big portion of the binding code below
        instantiates Eigen code using small anonymous wrapper functions */
-    py::class_<Type> matrix(m, name, parent);
+    py::class_<Type> matrix(m, name, py::buffer_protocol());
 
     matrix
         /* Constructors */
@@ -632,7 +630,7 @@ void python_export_vector(py::module &m) {
 
     /* Bindings for MatrixXi */
     bind_eigen_2<Eigen::MatrixXi> (me, "MatrixXi");
-//    py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
+    //py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
     //py::implicitly_convertible<double, Eigen::MatrixXi>();
 
     /* Bindings for MatrixXb */

+ 10 - 10
python/py_igl/py_boundary_facets.cpp

@@ -1,13 +1,3 @@
-m.def("boundary_facets", []
-(
-  const std::vector<std::vector<int> > & T,
-  std::vector<std::vector<int> > & F
-)
-{
-  return igl::boundary_facets(T,F);
-}, __doc_igl_boundary_facets,
-py::arg("T"), py::arg("F"));
-
 m.def("boundary_facets", []
 (
   const Eigen::MatrixXi& T,
@@ -28,3 +18,13 @@ m.def("boundary_facets", []
   return F;
 }, __doc_igl_boundary_facets,
 py::arg("T"));
+
+m.def("boundary_facets", []
+(
+  const std::vector<std::vector<int> > & T,
+  std::vector<std::vector<int> > & F
+)
+{
+  return igl::boundary_facets(T,F);
+}, __doc_igl_boundary_facets,
+py::arg("T"), py::arg("F"));

+ 9 - 7
python/py_igl/py_boundary_loop.cpp

@@ -1,17 +1,19 @@
 m.def("boundary_loop", []
 (
   const Eigen::MatrixXi& F,
-  std::vector<std::vector<int> >& L
+  Eigen::MatrixXi& L
 )
 {
-  return igl::boundary_loop(F,L);
+  Eigen::VectorXi T;
+  igl::boundary_loop(F,T);
+  L = T;
 }, __doc_igl_boundary_loop,
 py::arg("F"), py::arg("L"));
 
 m.def("boundary_loop", []
 (
   const Eigen::MatrixXi& F,
-  std::vector<int>& L
+  std::vector<std::vector<int> >& L
 )
 {
   return igl::boundary_loop(F,L);
@@ -21,11 +23,11 @@ py::arg("F"), py::arg("L"));
 m.def("boundary_loop", []
 (
   const Eigen::MatrixXi& F,
-  Eigen::MatrixXi& L
+  std::vector<int>& L
 )
 {
-  Eigen::VectorXi T;
-  igl::boundary_loop(F,T);
-  L = T;
+  return igl::boundary_loop(F,L);
 }, __doc_igl_boundary_loop,
 py::arg("F"), py::arg("L"));
+
+

+ 10 - 9
python/py_igl/py_internal_angles.cpp

@@ -21,13 +21,14 @@ m.def("internal_angles_using_squared_edge_lengths", []
 }, __doc_igl_internal_angles,
 py::arg("L_sq"), py::arg("K"));
 
-m.def("internal_angles_using_edge_lengths", []
-(
-  const Eigen::MatrixXd& L,
-  Eigen::MatrixXd& K
-)
-{
-  return igl::internal_angles_using_edge_lengths(L, K);
-}, __doc_igl_internal_angles,
-py::arg("L"), py::arg("K"));
+//m.def("internal_angles_using_edge_lengths", []
+//(
+//  const Eigen::MatrixXd& L,
+//  Eigen::MatrixXd& K
+//)
+//{
+//  return igl::internal_angles_using_edge_lengths(L, K);
+//}, __doc_igl_internal_angles,
+//py::arg("L"), py::arg("K"));
+
 

+ 9 - 6
python/py_igl/py_readMESH.cpp

@@ -1,24 +1,27 @@
 m.def("readMESH", []
 (
   const std::string mesh_file_name,
-  std::vector<std::vector<double> > & V,
-  std::vector<std::vector<int> > & T,
-  std::vector<std::vector<int> > & F
+  Eigen::MatrixXd& V,
+  Eigen::MatrixXi& T,
+  Eigen::MatrixXi& F
 )
 {
   return igl::readMESH(mesh_file_name, V, T, F);
 }, __doc_igl_readMESH,
 py::arg("mesh_file_name"), py::arg("V"), py::arg("T"), py::arg("F"));
 
+
 m.def("readMESH", []
 (
   const std::string mesh_file_name,
-  Eigen::MatrixXd& V,
-  Eigen::MatrixXi& T,
-  Eigen::MatrixXi& F
+  std::vector<std::vector<double> > & V,
+  std::vector<std::vector<int> > & T,
+  std::vector<std::vector<int> > & F
 )
 {
   return igl::readMESH(mesh_file_name, V, T, F);
 }, __doc_igl_readMESH,
 py::arg("mesh_file_name"), py::arg("V"), py::arg("T"), py::arg("F"));
 
+
+

+ 11 - 11
python/py_igl/py_read_triangle_mesh.cpp

@@ -1,14 +1,3 @@
-m.def("read_triangle_mesh", []
-(
-  const std::string str,
-  std::vector<std::vector<double> >& V,
-  std::vector<std::vector<int> >& F
-)
-{
-  return igl::read_triangle_mesh(str,V,F);
-}, __doc_igl_read_triangle_mesh,
-py::arg("str"), py::arg("V"), py::arg("F"));
-
 m.def("read_triangle_mesh", []
 (
   const std::string str,
@@ -34,3 +23,14 @@ m.def("read_triangle_mesh", []
   return igl::read_triangle_mesh(str,V,F,dir,base,ext,name);
 }, __doc_igl_read_triangle_mesh,
 py::arg("str"), py::arg("V"), py::arg("F"), py::arg("dir"), py::arg("base"), py::arg("ext"), py::arg("name"));
+
+m.def("read_triangle_mesh", []
+(
+  const std::string str,
+  std::vector<std::vector<double> >& V,
+  std::vector<std::vector<int> >& F
+)
+{
+  return igl::read_triangle_mesh(str,V,F);
+}, __doc_igl_read_triangle_mesh,
+py::arg("str"), py::arg("V"), py::arg("F"));

+ 30 - 31
python/py_igl/py_unique.cpp

@@ -1,25 +1,3 @@
-m.def("unique", []
-(
-  const std::vector<double> & A,
-  std::vector<double> & C,
-  std::vector<size_t> & IA,
-  std::vector<size_t> & IC
-)
-{
-  return igl::unique(A,C,IA,IC);
-}, __doc_igl_unique,
-py::arg("A"), py::arg("C"), py::arg("IA"), py::arg("IC"));
-
-m.def("unique", []
-(
-  const std::vector<double> & A,
-  std::vector<double> & C
-)
-{
-  return igl::unique(A,C);
-}, __doc_igl_unique,
-py::arg("A"), py::arg("C"));
-
 m.def("unique", []
 (
   const Eigen::MatrixXd& A,
@@ -54,15 +32,10 @@ m.def("unique_rows", []
 }, __doc_igl_unique,
 py::arg("A"), py::arg("C"), py::arg("IA"), py::arg("IC"));
 
-
-
-// int
-
-
 m.def("unique", []
 (
-  const std::vector<int> & A,
-  std::vector<int> & C,
+  const std::vector<double> & A,
+  std::vector<double> & C,
   std::vector<size_t> & IA,
   std::vector<size_t> & IC
 )
@@ -73,14 +46,18 @@ py::arg("A"), py::arg("C"), py::arg("IA"), py::arg("IC"));
 
 m.def("unique", []
 (
-  const std::vector<int> & A,
-  std::vector<int> & C
+  const std::vector<double> & A,
+  std::vector<double> & C
 )
 {
   return igl::unique(A,C);
 }, __doc_igl_unique,
 py::arg("A"), py::arg("C"));
 
+
+// int
+
+
 m.def("unique", []
 (
   const Eigen::MatrixXi& A,
@@ -114,3 +91,25 @@ m.def("unique_rows", []
   return igl::unique_rows(A,C,IA,IC);
 }, __doc_igl_unique,
 py::arg("A"), py::arg("C"), py::arg("IA"), py::arg("IC"));
+
+m.def("unique", []
+(
+  const std::vector<int> & A,
+  std::vector<int> & C,
+  std::vector<size_t> & IA,
+  std::vector<size_t> & IC
+)
+{
+  return igl::unique(A,C,IA,IC);
+}, __doc_igl_unique,
+py::arg("A"), py::arg("C"), py::arg("IA"), py::arg("IC"));
+
+m.def("unique", []
+(
+  const std::vector<int> & A,
+  std::vector<int> & C
+)
+{
+  return igl::unique(A,C);
+}, __doc_igl_unique,
+py::arg("A"), py::arg("C"));