slice_into.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "slice_into.h"
  2. template <typename T>
  3. IGL_INLINE void igl::slice_into(
  4. const Eigen::SparseMatrix<T>& X,
  5. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  6. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  7. Eigen::SparseMatrix<T>& Y)
  8. {
  9. int xm = X.rows();
  10. int xn = X.cols();
  11. assert(R.size() == xm);
  12. assert(C.size() == xn);
  13. int ym = Y.size();
  14. int yn = Y.size();
  15. assert(R.minCoeff() >= 0);
  16. assert(R.maxCoeff() < ym);
  17. assert(C.minCoeff() >= 0);
  18. assert(C.maxCoeff() < yn);
  19. // create temporary dynamic sparse matrix
  20. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(Y);
  21. // Iterate over outside
  22. for(int k=0; k<X.outerSize(); ++k)
  23. {
  24. // Iterate over inside
  25. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  26. {
  27. dyn_Y.coeffRef(R(it.row()),C(it.col())) = it.value();
  28. }
  29. }
  30. Y = Eigen::SparseMatrix<T>(dyn_Y);
  31. }
  32. template <typename DerivedX>
  33. IGL_INLINE void igl::slice_into(
  34. const Eigen::PlainObjectBase<DerivedX> & X,
  35. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  36. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  37. Eigen::PlainObjectBase<DerivedX> & Y)
  38. {
  39. int xm = X.rows();
  40. int xn = X.cols();
  41. assert(R.size() == xm);
  42. assert(C.size() == xn);
  43. int ym = Y.size();
  44. int yn = Y.size();
  45. assert(R.minCoeff() >= 0);
  46. assert(R.maxCoeff() < ym);
  47. assert(C.minCoeff() >= 0);
  48. assert(C.maxCoeff() < yn);
  49. // Build reindexing maps for columns and rows, -1 means not in map
  50. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  51. RI.resize(xm);
  52. for(int i = 0;i<xm;i++)
  53. {
  54. for(int j = 0;j<xn;j++)
  55. {
  56. Y(R(i),C(j)) = X(i,j);
  57. }
  58. }
  59. }
  60. template <typename DerivedX>
  61. IGL_INLINE void igl::slice_into(
  62. const Eigen::PlainObjectBase<DerivedX> & X,
  63. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  64. Eigen::PlainObjectBase<DerivedX> & Y)
  65. {
  66. // phony column indices
  67. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  68. C.resize(1);
  69. C(0) = 0;
  70. return igl::slice_into(X,R,C,Y);
  71. }
  72. #ifndef IGL_HEADER_ONLY
  73. // Explicit template specialization
  74. // generated by autoexplicit.sh
  75. 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> >&);
  76. #endif