Browse Source

use Eigen::Map to copy, handle RowMajor

Former-commit-id: 2c3b4f86817a6addd167f4bac468e564472cd291
Alec Jacobson 8 years ago
parent
commit
11512fbe33

+ 12 - 9
include/igl/matlab/MatlabWorkspace.h

@@ -108,7 +108,7 @@ namespace igl
         // matrices like lists of faces or elements
         // matrices like lists of faces or elements
         template <typename DerivedM>
         template <typename DerivedM>
         inline MatlabWorkspace& save_index(
         inline MatlabWorkspace& save_index(
-          const Eigen::PlainObjectBase<DerivedM>& M,
+          const Eigen::DenseBase<DerivedM>& M,
           const std::string & name);
           const std::string & name);
         template <typename ScalarM>
         template <typename ScalarM>
         inline MatlabWorkspace& save_index(
         inline MatlabWorkspace& save_index(
@@ -298,8 +298,10 @@ inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
   data.push_back(mx_data);
   data.push_back(mx_data);
   names.push_back(name);
   names.push_back(name);
   // Copy data immediately
   // Copy data immediately
-  // Q: Won't this be incorrect for integers?
-  copy(M.data(),M.data()+M.size(),mxGetPr(mx_data));
+  // Use Eigen's map and cast to copy
+  Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> > 
+    map(mxGetPr(mx_data),m,n);
+  map = M.template cast<double>();
   return *this;
   return *this;
 }
 }
 
 
@@ -386,7 +388,7 @@ inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
 template <typename DerivedM>
 template <typename DerivedM>
 inline igl::matlab::MatlabWorkspace& 
 inline igl::matlab::MatlabWorkspace& 
   igl::matlab::MatlabWorkspace::save_index(
   igl::matlab::MatlabWorkspace::save_index(
-    const Eigen::PlainObjectBase<DerivedM>& M,
+    const Eigen::DenseBase<DerivedM>& M,
     const std::string & name)
     const std::string & name)
 {
 {
   DerivedM Mp1 = M;
   DerivedM Mp1 = M;
@@ -432,7 +434,8 @@ inline bool igl::matlab::MatlabWorkspace::find(
   //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
   //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
   const int m = mxGetM(mx_data);
   const int m = mxGetM(mx_data);
   const int n = mxGetN(mx_data);
   const int n = mxGetN(mx_data);
-  // Handle vectors
+  // Handle vectors: in the sense that anything found becomes a column vector,
+  // whether it was column vector, row vector or matrix
   if(DerivedM::IsVectorAtCompileTime)
   if(DerivedM::IsVectorAtCompileTime)
   {
   {
     assert(m==1 || n==1 || (m==0 && n==0));
     assert(m==1 || n==1 || (m==0 && n==0));
@@ -441,10 +444,10 @@ inline bool igl::matlab::MatlabWorkspace::find(
   {
   {
     M.resize(m,n);
     M.resize(m,n);
   }
   }
-  copy(
-    mxGetPr(mx_data),
-    mxGetPr(mx_data)+mxGetNumberOfElements(mx_data),
-    M.data());
+  assert(mxGetNumberOfElements(mx_data) == M.size());
+  // Use Eigen's map and cast to copy
+  M = Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> > 
+    (mxGetPr(mx_data),M.rows(),M.cols()).cast<typename DerivedM::Scalar>();
   return true;
   return true;
 }
 }
 
 

+ 4 - 24
include/igl/matlab/parse_rhs.cpp

@@ -13,31 +13,11 @@ IGL_INLINE void igl::matlab::parse_rhs_double(
     const mxArray *prhs[], 
     const mxArray *prhs[], 
     Eigen::PlainObjectBase<DerivedV> & V)
     Eigen::PlainObjectBase<DerivedV> & V)
 {
 {
-  using namespace std;
   using namespace Eigen;
   using namespace Eigen;
-  // set number of mesh vertices
-  const int n = mxGetM(prhs[0]);
-  // set vertex position pointers
-  double * Vp = mxGetPr(prhs[0]);
-  const int dim = mxGetN(prhs[0]);
-
-  typedef typename DerivedV::Scalar Scalar;
-  Matrix<Scalar, DerivedV::ColsAtCompileTime, DerivedV::RowsAtCompileTime, RowMajor> VT;
-  Scalar * V_data;
-  if(DerivedV::IsRowMajor)
-  {
-    VT.resize(dim,n);
-    V_data = VT.data();
-  }else
-  {
-    V.resize(n,dim);
-    V_data = V.data();
-  }
-  copy(Vp,Vp+n*dim,V_data);
-  if(DerivedV::IsRowMajor)
-  {
-    V = VT.transpose();
-  }
+  // Use Eigen's map and cast to copy
+  V = Map< Matrix<double,Dynamic,Dynamic> >
+    (mxGetPr(prhs[0]),mxGetM(prhs[0]),mxGetN(prhs[0]))
+    .cast<typename DerivedV::Scalar>();
 }
 }
 
 
 template <typename DerivedV>
 template <typename DerivedV>

+ 6 - 15
include/igl/matlab/prepare_lhs.cpp

@@ -17,14 +17,9 @@ IGL_INLINE void igl::matlab::prepare_lhs_double(
   const int m = V.rows();
   const int m = V.rows();
   const int n = V.cols();
   const int n = V.cols();
   plhs[0] = mxCreateDoubleMatrix(m,n, mxREAL);
   plhs[0] = mxCreateDoubleMatrix(m,n, mxREAL);
-  double * Vp = mxGetPr(plhs[0]);
-  for(int i = 0;i<m;i++)
-  {
-    for(int j = 0;j<n;j++)
-    {
-      Vp[i+m*j] = V(i,j);
-    }
-  }
+  Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> > 
+    map(mxGetPr(plhs[0]),m,n);
+  map = V.template cast<double>();
 }
 }
 
 
 template <typename DerivedV>
 template <typename DerivedV>
@@ -38,13 +33,9 @@ IGL_INLINE void igl::matlab::prepare_lhs_logical(
   const int n = V.cols();
   const int n = V.cols();
   plhs[0] = mxCreateLogicalMatrix(m,n);
   plhs[0] = mxCreateLogicalMatrix(m,n);
   mxLogical * Vp = static_cast<mxLogical*>(mxGetData(plhs[0]));
   mxLogical * Vp = static_cast<mxLogical*>(mxGetData(plhs[0]));
-  for(int i = 0;i<m;i++)
-  {
-    for(int j = 0;j<n;j++)
-    {
-      Vp[i+m*j] = V(i,j);
-    }
-  }
+  Eigen::Map< Eigen::Matrix<mxLogical,Eigen::Dynamic,Eigen::Dynamic> > 
+    map(static_cast<mxLogical*>(mxGetData(plhs[0])),m,n);
+  map = V.template cast<mxLogical>();
 }
 }
 
 
 template <typename DerivedV>
 template <typename DerivedV>