#ifndef IGL_SLICE_H #define IGL_SLICE_H #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET #include namespace igl { // Act like the matlab X(row_indices,col_indices) operator // // Inputs: // X m by n matrix // R list of row indices // C list of column indices // Output: // Y #R by #C matrix template inline void slice( const Eigen::SparseMatrix& X, const Eigen::Matrix & R, const Eigen::Matrix & C, Eigen::SparseMatrix& Y); template inline void slice( const Eigen::Matrix & X, const Eigen::Matrix & R, const Eigen::Matrix & C, Eigen::Matrix & Y); template inline void slice( const Eigen::Matrix & X, const Eigen::Matrix & R, Eigen::Matrix & Y); } // Implementation template inline void igl::slice( const Eigen::SparseMatrix& X, const Eigen::Matrix & R, const Eigen::Matrix & C, Eigen::SparseMatrix& Y) { int xm = X.rows(); int xn = X.cols(); int ym = R.size(); int yn = C.size(); assert(R.minCoeff() >= 0); assert(R.maxCoeff() < xm); assert(C.minCoeff() >= 0); assert(C.maxCoeff() < xn); // Build reindexing maps for columns and rows, -1 means not in map Eigen::Matrix RI; RI.resize(xm); // initialize to -1 for(int i = 0;i CI; CI.resize(xn); // initialize to -1 for(int i = 0;i dyn_Y(ym,yn); // Iterate over outside for(int k=0; k::InnerIterator it (X,k); it; ++it) { if(RI(it.row()) >= 0 && CI(it.col()) >= 0) { dyn_Y.coeffRef(RI(it.row()),CI(it.col())) = it.value(); } } } Y = Eigen::SparseMatrix(dyn_Y); } template inline void igl::slice( const Eigen::Matrix & X, const Eigen::Matrix & R, const Eigen::Matrix & C, Eigen::Matrix & Y) { int xm = X.rows(); int xn = X.cols(); int ym = R.size(); int yn = C.size(); assert(R.minCoeff() >= 0); assert(R.maxCoeff() < xm); assert(C.minCoeff() >= 0); assert(C.maxCoeff() < xn); // Build reindexing maps for columns and rows, -1 means not in map Eigen::Matrix RI; RI.resize(xm); // initialize to -1 for(int i = 0;i CI; CI.resize(xn); // initialize to -1 for(int i = 0;i= 0 && CI(j) >= 0) { Y(RI(i),CI(j)) = X(i,j); } } } } template inline void igl::slice( const Eigen::Matrix & X, const Eigen::Matrix & R, Eigen::Matrix & Y) { // phony column indices Eigen::Matrix C; C.resize(1); C(0) = 0; return igl::slice(X,R,C,Y); } #endif