Browse Source

added 301

Former-commit-id: b1ea24a66b8de868773d0dd06621a896e736ea25
Daniele Panozzo 9 years ago
parent
commit
50e499e0e9

+ 39 - 0
python/301_Slice.py

@@ -0,0 +1,39 @@
+from __future__ import print_function
+import igl
+from iglhelpers import *
+
+V = igl.eigen.MatrixXd()
+F = igl.eigen.MatrixXi()
+
+igl.readOFF("../tutorial/shared/decimated-knight.off",V,F)
+
+temp  = igl.eigen.MatrixXd()
+
+# 100 random indicies into rows of F
+temp.setRandom(100,1)
+
+I = igl.eigen.MatrixXi()
+igl.floor((0.5*(temp+1.)*F.rows()),I);
+
+# 50 random indicies into rows of I
+temp.setRandom(50,1)
+J = igl.eigen.MatrixXi()
+igl.floor((0.5*(temp+1.)*I.rows()),J)
+
+# K = I(J);
+K = igl.eigen.MatrixXi()
+igl.slice(I,J,K)
+
+# default green for all faces
+C = p2e(np.array([[0.4,0.8,0.3]])).replicate(F.rows(),1)
+
+# Red for each in K
+R = p2e(np.array([[1.0,0.3,0.3]])).replicate(K.rows(),1)
+# C(K,:) = R
+igl.slice_into(R,K,1,C)
+
+# Plot the mesh with pseudocolors
+viewer = igl.viewer.Viewer()
+viewer.data.set_mesh(V, F)
+viewer.data.set_colors(C)
+viewer.launch()

+ 6 - 0
python/py_igl.cpp

@@ -20,6 +20,9 @@
 #include <igl/avg_edge_length.h>
 #include <igl/barycenter.h>
 #include <igl/doublearea.h>
+#include <igl/floor.h>
+#include <igl/slice.h>
+#include <igl/slice_into.h>
 
 void python_export_igl(py::module &m)
 {
@@ -41,5 +44,8 @@ void python_export_igl(py::module &m)
 #include "py_igl/py_avg_edge_length.cpp"
 #include "py_igl/py_barycenter.cpp"
 #include "py_igl/py_doublearea.cpp"
+#include "py_igl/py_floor.cpp"
+#include "py_igl/py_slice.cpp"
+#include "py_igl/py_slice_into.cpp"
 
 }

+ 9 - 0
python/py_igl/py_floor.cpp

@@ -0,0 +1,9 @@
+m.def("floor", []
+(
+  const Eigen::MatrixXd& X,
+  Eigen::MatrixXi& Y
+)
+{
+  return igl::floor(X,Y);
+}, __doc_igl_floor,
+py::arg("X"), py::arg("Y"));

+ 1 - 1
python/py_igl/py_jet.cpp

@@ -5,7 +5,7 @@ m.def("jet", []
   Eigen::MatrixXd& C
 )
 {
-  assert_is_VectorXd("Z",Z);
+  assert_is_VectorX("Z",Z);
   return igl::jet(Z,normalize,C);
 }, __doc_igl_jet,
 py::arg("Z"), py::arg("normalize"), py::arg("C"));

+ 2 - 2
python/py_igl/py_parula.cpp

@@ -5,7 +5,7 @@ m.def("parula", []
   Eigen::MatrixXd& C
 )
 {
-  assert_is_VectorXd("Z",Z);
+  assert_is_VectorX("Z",Z);
   return igl::parula(Z,normalize,C);
 }, __doc_igl_parula,
 py::arg("Z"), py::arg("normalize"), py::arg("C"));
@@ -18,7 +18,7 @@ m.def("parula", []
   Eigen::MatrixXd& C
 )
 {
-  assert_is_VectorXd("Z",Z);
+  assert_is_VectorX("Z",Z);
   return igl::parula(Z,min_Z,max_Z,C);
 }, __doc_igl_parula,
 py::arg("Z"), py::arg("min_Z"), py::arg("max_Z"), py::arg("C"));

+ 1 - 1
python/py_igl/py_per_face_normals.cpp

@@ -6,7 +6,7 @@ m.def("per_face_normals", []
   Eigen::MatrixXd& N
 )
 {
-  assert_is_VectorXd("Z",Z);
+  assert_is_VectorX("Z",Z);
   return igl::per_face_normals(V,F,Z,N);
 }, __doc_igl_per_face_normals,
 py::arg("V"), py::arg("F"), py::arg("Z"), py::arg("N"));

+ 154 - 0
python/py_igl/py_slice.cpp

@@ -0,0 +1,154 @@
+// Double
+
+m.def("slice", []
+(
+  const Eigen::SparseMatrix<double>& X,
+  const Eigen::MatrixXi& R,
+  const Eigen::MatrixXi& C,
+  Eigen::SparseMatrix<double>& Y
+)
+{
+  assert_is_VectorX("R",R);
+  assert_is_VectorX("C",C);
+  return igl::slice(X,R,C,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("C"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::SparseMatrix<double>& X,
+  const Eigen::MatrixXi& R,
+  const int& dim,
+  Eigen::SparseMatrix<double>& Y
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R,dim,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("dim"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R,
+  const Eigen::MatrixXi& C,
+  Eigen::MatrixXd& Y
+)
+{
+  assert_is_VectorX("R",R);
+  assert_is_VectorX("C",C);
+  return igl::slice(X,R,C,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("C"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R,
+  Eigen::MatrixXd& Y
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R,
+  const int& dim
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R,dim);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("dim"));
+
+// int
+m.def("slice", []
+(
+  const Eigen::SparseMatrix<int>& X,
+  const Eigen::MatrixXi& R,
+  const Eigen::MatrixXi& C,
+  Eigen::SparseMatrix<int>& Y
+)
+{
+  assert_is_VectorX("R",R);
+  assert_is_VectorX("C",C);
+  return igl::slice(X,R,C,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("C"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::SparseMatrix<int>& X,
+  const Eigen::MatrixXi& R,
+  const int& dim,
+  Eigen::SparseMatrix<int>& Y
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R,dim,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("dim"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXi& X,
+  const Eigen::MatrixXi& R,
+  const Eigen::MatrixXi& C,
+  Eigen::MatrixXi& Y
+)
+{
+  assert_is_VectorX("R",R);
+  assert_is_VectorX("C",C);
+  return igl::slice(X,R,C,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("C"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXi& X,
+  const Eigen::MatrixXi& R,
+  Eigen::MatrixXi& Y
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R,Y);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("Y"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXi& X,
+  const Eigen::MatrixXi& R
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"));
+
+m.def("slice", []
+(
+  const Eigen::MatrixXi& X,
+  const Eigen::MatrixXi& R,
+  const int& dim
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice(X,R,dim);
+}, __doc_igl_slice,
+py::arg("X"), py::arg("R"), py::arg("dim"));

+ 52 - 0
python/py_igl/py_slice_into.cpp

@@ -0,0 +1,52 @@
+m.def("slice_into", []
+(
+  const Eigen::SparseMatrix<double>& X,
+  const Eigen::MatrixXi& R,
+  const Eigen::MatrixXi& C,
+  Eigen::SparseMatrix<double>& Y
+)
+{
+  assert_is_VectorX("R",R);
+  assert_is_VectorX("C",C);
+  return igl::slice_into(X,R,C,Y);
+}, __doc_igl_slice_into,
+py::arg("X"), py::arg("R"), py::arg("C"), py::arg("Y"));
+
+m.def("slice_into", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R,
+  const Eigen::MatrixXi& C,
+  Eigen::MatrixXd& Y
+)
+{
+  assert_is_VectorX("R",R);
+  assert_is_VectorX("C",C);
+  return igl::slice_into(X,R,C,Y);
+}, __doc_igl_slice_into,
+py::arg("X"), py::arg("R"), py::arg("C"), py::arg("Y"));
+
+m.def("slice_into", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R,
+  const int& dim,
+  Eigen::MatrixXd& Y
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice_into(X,R,dim,Y);
+}, __doc_igl_slice_into,
+py::arg("X"), py::arg("R"), py::arg("dim"), py::arg("Y"));
+
+m.def("slice_into", []
+(
+  const Eigen::MatrixXd& X,
+  const Eigen::MatrixXi& R,
+  Eigen::MatrixXd& Y
+)
+{
+  assert_is_VectorX("R",R);
+  return igl::slice_into(X,R,Y);
+}, __doc_igl_slice_into,
+py::arg("X"), py::arg("R"), py::arg("Y"));

+ 16 - 16
python/py_igl_viewer.cpp

@@ -59,7 +59,7 @@ py::enum_<igl::viewer::ViewerData::DirtyFlags>(viewerdata_class, "DirtyFlags")
 
     .def("add_label", [] (igl::viewer::ViewerData& data, const Eigen::MatrixXd& P,  const std::string& str)
     {
-      assert_is_VectorXd("P",P);
+      assert_is_VectorX("P",P);
       data.add_label(P,str);
     })
 
@@ -67,9 +67,9 @@ py::enum_<igl::viewer::ViewerData::DirtyFlags>(viewerdata_class, "DirtyFlags")
 
     .def("uniform_colors", [] (igl::viewer::ViewerData& data, const Eigen::MatrixXd& ambient, const Eigen::MatrixXd& diffuse, const Eigen::MatrixXd& specular)
     {
-      assert_is_Vector3d("ambient",ambient);
-      assert_is_Vector3d("diffuse",diffuse);
-      assert_is_Vector3d("specular",specular);
+      assert_is_Vector3("ambient",ambient);
+      assert_is_Vector3("diffuse",diffuse);
+      assert_is_Vector3("specular",specular);
       data.uniform_colors(ambient,diffuse, specular);
     })
 
@@ -139,7 +139,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.background_color.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("background_color",v);
+      assert_is_Vector3("background_color",v);
       core.background_color = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -147,7 +147,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.line_color.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("line_color",v);
+      assert_is_Vector3("line_color",v);
       core.line_color = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -155,7 +155,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.light_position.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("light_position",v);
+      assert_is_Vector3("light_position",v);
       core.light_position = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -167,7 +167,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.model_translation.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("model_translation",v);
+      assert_is_Vector3("model_translation",v);
       core.model_translation = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -177,7 +177,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.model_translation_uv.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("model_translation_uv",v);
+      assert_is_Vector3("model_translation_uv",v);
       core.model_translation_uv = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -188,7 +188,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.camera_eye.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("camera_eye",v);
+      assert_is_Vector3("camera_eye",v);
       core.camera_eye = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -196,7 +196,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.camera_up.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("camera_up",v);
+      assert_is_Vector3("camera_up",v);
       core.camera_up = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -204,7 +204,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.camera_center.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector3d("camera_center",v);
+      assert_is_Vector3("camera_center",v);
       core.camera_center = Eigen::Vector3f(v.cast<float>());
     })
 
@@ -236,7 +236,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.viewport.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Vector4d("viewport",v);
+      assert_is_Vector4("viewport",v);
       core.viewport = Eigen::Vector4f(v.cast<float>());
     })
 
@@ -244,7 +244,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.view.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Matrix4d("view",v);
+      assert_is_Matrix4("view",v);
       core.view = Eigen::Matrix4f(v.cast<float>());
     })
 
@@ -252,7 +252,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.model.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Matrix4d("model",v);
+      assert_is_Matrix4("model",v);
       core.model = Eigen::Matrix4f(v.cast<float>());
     })
 
@@ -260,7 +260,7 @@ py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
     [](const igl::viewer::ViewerCore& core) {return Eigen::MatrixXd(core.proj.cast<double>());},
     [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& v)
     {
-      assert_is_Matrix4d("proj",v);
+      assert_is_Matrix4("proj",v);
       core.proj = Eigen::Matrix4f(v.cast<float>());
     })
 

+ 6 - 0
python/py_vector.cpp

@@ -65,6 +65,12 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
         .def("setZero", [](Type &m) { m.setZero(); })
         .def("setIdentity", [](Type &m) { m.setIdentity(); })
         .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
+        .def("setRandom", [](Type &m) { m.setRandom(); })
+
+        .def("setZero", [](Type &m, const int& r, const int& c) { m.setZero(r,c); })
+        .def("setIdentity", [](Type &m, const int& r, const int& c) { m.setIdentity(r,c); })
+        .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); })
 
         /* Resizing */
         .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })

+ 0 - 42
python/python.cpp

@@ -3,48 +3,6 @@
 #include <string>
 #include <fstream>
 
-void assert_is_VectorXd(const std::string name, const Eigen::MatrixXd& v)
-{
-  if (v.cols() != 1)
-    throw std::runtime_error(name + " must be a column vector.");
-}
-
-void assert_is_RowVectorXd(const std::string name, const Eigen::MatrixXd& v)
-{
-  if (v.rows() != 1)
-    throw std::runtime_error(name + " must be a row vector.");
-}
-
-void assert_is_Vector3d(const std::string name, const Eigen::MatrixXd& v)
-{
-  if ((v.cols() != 1) || (v.rows() != 3))
-    throw std::runtime_error(name + " must be a column vector with 3 entries.");
-}
-
-void assert_is_RowVector3d(const std::string name, const Eigen::MatrixXd& v)
-{
-  if ((v.cols() != 3) || (v.rows() != 1))
-    throw std::runtime_error(name + " must be a row vector with 3 entries.");
-}
-
-void assert_is_Vector4d(const std::string name, const Eigen::MatrixXd& v)
-{
-  if ((v.cols() != 1) || (v.rows() != 4))
-    throw std::runtime_error(name + " must be a column vector with 4 entries.");
-}
-
-void assert_is_RowVector4d(const std::string name, const Eigen::MatrixXd& v)
-{
-  if ((v.cols() != 4) || (v.rows() != 1))
-    throw std::runtime_error(name + " must be a row vector with 4 entries.");
-}
-
-void assert_is_Matrix4d(const std::string name, const Eigen::MatrixXd& v)
-{
-  if ((v.cols() != 4) || (v.rows() != 4))
-    throw std::runtime_error(name + " must be a 4x4 matrix.");
-}
-
 extern void python_export_vector(py::module &);
 extern void python_export_igl(py::module &);
 extern void python_export_igl_viewer(py::module &);

+ 49 - 7
python/python.h

@@ -11,13 +11,55 @@
 
 #include <Eigen/Dense>
 
-void assert_is_VectorXd(const std::string name, const Eigen::MatrixXd& v);
-void assert_is_RowVectorXd(const std::string name, const Eigen::MatrixXd& v);
-void assert_is_Vector3d(const std::string name, const Eigen::MatrixXd& v);
-void assert_is_RowVector3d(const std::string name, const Eigen::MatrixXd& v);
-void assert_is_Vector4d(const std::string name, const Eigen::MatrixXd& v);
-void assert_is_RowVector4d(const std::string name, const Eigen::MatrixXd& v);
-void assert_is_Matrix4d(const std::string name, const Eigen::MatrixXd& v);
+template<typename Scalar>
+void assert_is_VectorX(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if (v.cols() != 1)
+    throw std::runtime_error(name + " must be a column vector.");
+}
+
+template<typename Scalar>
+void assert_is_RowVectorX(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if (v.rows() != 1)
+    throw std::runtime_error(name + " must be a row vector.");
+}
+
+template<typename Scalar>
+void assert_is_Vector3(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if ((v.cols() != 1) || (v.rows() != 3))
+    throw std::runtime_error(name + " must be a column vector with 3 entries.");
+}
+
+template<typename Scalar>
+void assert_is_RowVector3(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if ((v.cols() != 3) || (v.rows() != 1))
+    throw std::runtime_error(name + " must be a row vector with 3 entries.");
+}
+
+template<typename Scalar>
+void assert_is_Vector4(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if ((v.cols() != 1) || (v.rows() != 4))
+    throw std::runtime_error(name + " must be a column vector with 4 entries.");
+}
+
+template<typename Scalar>
+void assert_is_RowVector4(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if ((v.cols() != 4) || (v.rows() != 1))
+    throw std::runtime_error(name + " must be a row vector with 4 entries.");
+}
+
+template<typename Scalar>
+void assert_is_Matrix4(const std::string name, const Eigen::PlainObjectBase<Scalar>& v)
+{
+  if ((v.cols() != 4) || (v.rows() != 4))
+    throw std::runtime_error(name + " must be a 4x4 matrix.");
+}
+
 
 
 namespace py = pybind;