slice_into.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 T, const int W, const int H>
  33. IGL_INLINE void igl::slice_into(
  34. const Eigen::Matrix<T,W,H> & X,
  35. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  36. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  37. Eigen::Matrix<T,W,H> & 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 T>
  61. IGL_INLINE void igl::slice_into(
  62. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  63. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  64. Eigen::Matrix<T,Eigen::Dynamic,1> & 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. #endif