Browse Source

added 306
finished chapter 3 of the tutorial
warning: there is a memory leak in 305 that happens rarely, it requires debugging

Former-commit-id: b25ed574a235495f08775c257508574c7cfacfd3

Daniele Panozzo 9 years ago
parent
commit
b57ee21b85
4 changed files with 95 additions and 0 deletions
  1. 56 0
      python/306_EigenDecomposition.py
  2. 2 0
      python/py_igl.cpp
  3. 22 0
      python/py_igl/py_eigs.cpp
  4. 15 0
      python/py_vector.cpp

+ 56 - 0
python/306_EigenDecomposition.py

@@ -0,0 +1,56 @@
+import igl
+
+V = igl.eigen.MatrixXd()
+U = igl.eigen.MatrixXd()
+F = igl.eigen.MatrixXi()
+
+c = 0
+bbd = 1.0
+twod = False
+
+if not igl.read_triangle_mesh("../tutorial/shared/beetle.off",V,F):
+    print("failed to load mesh")
+
+twod = V.col(2).minCoeff() == V.col(2).maxCoeff()
+bbd = (V.colwiseMaxCoeff() - V.colwiseMinCoeff()).norm()
+
+L = igl.eigen.SparseMatrixd()
+M = igl.eigen.SparseMatrixd()
+
+igl.cotmatrix(V,F,L)
+L = -L
+igl.massmatrix(V,F,igl.MASSMATRIX_TYPE_DEFAULT,M)
+k = 5
+
+D = igl.eigen.MatrixXd()
+if not igl.eigs(L,M,k+1,igl.EIGS_TYPE_SM,U,D):
+    print("Eigs failed.")
+
+U = (U-U.minCoeff())/(U.maxCoeff()-U.minCoeff());
+
+viewer = igl.viewer.Viewer()
+
+def key_down(viewer,key,mod):
+    global U, c
+    
+    if key == ord(' '):
+        U = U.rightCols(k)
+
+        # Rescale eigen vectors for visualization
+        Z = bbd*0.5*U.col(c)
+        C = igl.eigen.MatrixXd()
+        igl.parula(U.col(c),False,C)
+        c = (c+1)%U.cols()
+
+        if twod:
+            V.setcol(2,Z)
+
+        viewer.data.set_mesh(V,F)
+        viewer.data.compute_normals()
+        viewer.data.set_colors(C)
+        return True
+
+viewer.callback_key_down = key_down
+viewer.callback_key_down(viewer,ord(' '),0);
+viewer.core.show_lines = False
+viewer.launch()

+ 2 - 0
python/py_igl.cpp

@@ -31,6 +31,7 @@
 #include <igl/min_quad_with_fixed.h>
 #include <igl/SolverStatus.h>
 #include <igl/active_set.h>
+#include <igl/eigs.h>
 
 void python_export_igl(py::module &m)
 {
@@ -63,5 +64,6 @@ void python_export_igl(py::module &m)
 #include "py_igl/py_min_quad_with_fixed.cpp"
 #include "py_igl/py_SolverStatus.cpp"
 #include "py_igl/py_active_set.cpp"
+#include "py_igl/py_eigs.cpp"
 
 }

+ 22 - 0
python/py_igl/py_eigs.cpp

@@ -0,0 +1,22 @@
+py::enum_<igl::EigsType>(m, "EigsType")
+    .value("EIGS_TYPE_SM", igl::EIGS_TYPE_SM)
+    .value("EIGS_TYPE_LM", igl::EIGS_TYPE_LM)
+    .value("NUM_EIGS_TYPES", igl::NUM_EIGS_TYPES)
+    .export_values();
+
+m.def("eigs", []
+(
+  const Eigen::SparseMatrix<double>& A,
+  const Eigen::SparseMatrix<double>& B,
+  const size_t k,
+  const igl::EigsType type,
+  Eigen::MatrixXd& sU,
+  Eigen::MatrixXd& sS
+)
+{
+  Eigen::VectorXd sSt;
+  bool ret = igl::eigs(A,B,k,type,sU,sSt);
+  sS = sSt;
+  return ret;
+}, __doc_igl_eigs,
+py::arg("A"), py::arg("B"), py::arg("k"), py::arg("type"), py::arg("sU"), py::arg("sS"));

+ 15 - 0
python/py_vector.cpp

@@ -95,6 +95,21 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
         .def("setConstant", [](Type &m, const int& r, const int& c, Scalar value) { m.setConstant(r,c,value); })
         .def("setRandom", [](Type &m, const int& r, const int& c) { m.setRandom(r,c); })
 
+        .def("setCol", [](Type &m, int i, const Type& v) { m.col(i) = v; })
+        .def("setRow", [](Type &m, int i, const Type& v) { m.row(i) = v; })
+
+
+        .def("rightCols", [](Type &m, const int& k) { return Type(m.rightCols(k)); })
+        .def("leftCols", [](Type &m, const int& k) { return Type(m.leftCols(k)); })
+
+        .def("topRows", [](Type &m, const int& k) { return Type(m.topRows(k)); })
+        .def("bottomRows", [](Type &m, const int& k) { return Type(m.bottomRows(k)); })
+
+        .def("topLeftCorner", [](Type &m, const int& p, const int&q) { return Type(m.topLeftCorner(p,q)); })
+        .def("bottomLeftCorner", [](Type &m, const int& p, const int&q) { return Type(m.bottomLeftCorner(p,q)); })
+        .def("topRightCorner", [](Type &m, const int& p, const int&q) { return Type(m.topRightCorner(p,q)); })
+        .def("bottomRightCorner", [](Type &m, const int& p, const int&q) { return Type(m.bottomRightCorner(p,q)); })
+
         /* Resizing */
         .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
         .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })