slice_into.cpp 2.7 KB

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