slice_into.cpp 2.6 KB

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