123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include "slice_into.h"
- // Bug in unsupported/Eigen/SparseExtra needs iostream first
- #include <iostream>
- #include <unsupported/Eigen/SparseExtra>
- template <typename T>
- IGL_INLINE void igl::slice_into(
- const Eigen::SparseMatrix<T>& X,
- const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
- const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
- Eigen::SparseMatrix<T>& Y)
- {
- int xm = X.rows();
- int xn = X.cols();
- assert(R.size() == xm);
- assert(C.size() == xn);
- #ifndef NDEBUG
- int ym = Y.size();
- int yn = Y.size();
- assert(R.minCoeff() >= 0);
- assert(R.maxCoeff() < ym);
- assert(C.minCoeff() >= 0);
- assert(C.maxCoeff() < yn);
- #endif
- // create temporary dynamic sparse matrix
- Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(Y);
- // Iterate over outside
- for(int k=0; k<X.outerSize(); ++k)
- {
- // Iterate over inside
- for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
- {
- dyn_Y.coeffRef(R(it.row()),C(it.col())) = it.value();
- }
- }
- Y = Eigen::SparseMatrix<T>(dyn_Y);
- }
- template <typename DerivedX>
- IGL_INLINE void igl::slice_into(
- const Eigen::PlainObjectBase<DerivedX> & X,
- const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
- const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
- Eigen::PlainObjectBase<DerivedX> & Y)
- {
- int xm = X.rows();
- int xn = X.cols();
- assert(R.size() == xm);
- assert(C.size() == xn);
- #ifndef NDEBUG
- int ym = Y.size();
- int yn = Y.size();
- assert(R.minCoeff() >= 0);
- assert(R.maxCoeff() < ym);
- assert(C.minCoeff() >= 0);
- assert(C.maxCoeff() < yn);
- #endif
- // Build reindexing maps for columns and rows, -1 means not in map
- Eigen::Matrix<int,Eigen::Dynamic,1> RI;
- RI.resize(xm);
- for(int i = 0;i<xm;i++)
- {
- for(int j = 0;j<xn;j++)
- {
- Y(R(i),C(j)) = X(i,j);
- }
- }
- }
- template <typename DerivedX>
- IGL_INLINE void igl::slice_into(
- const Eigen::PlainObjectBase<DerivedX> & X,
- const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
- Eigen::PlainObjectBase<DerivedX> & Y)
- {
- // phony column indices
- Eigen::Matrix<int,Eigen::Dynamic,1> C;
- C.resize(1);
- C(0) = 0;
- return igl::slice_into(X,R,C,Y);
- }
- #ifndef IGL_HEADER_ONLY
- // Explicit template specialization
- // generated by autoexplicit.sh
- template void igl::slice_into<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
- template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
- #endif
|