slice.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef IGL_SLICE_H
  2. #define IGL_SLICE_H
  3. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  4. #include <Eigen/Sparse>
  5. namespace igl
  6. {
  7. // Act like the matlab X(row_indices,col_indices) operator
  8. //
  9. // Inputs:
  10. // X m by n matrix
  11. // R list of row indices
  12. // C list of column indices
  13. // Output:
  14. // Y #R by #C matrix
  15. template <typename T>
  16. inline void slice(
  17. const Eigen::SparseMatrix<T>& X,
  18. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  19. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  20. Eigen::SparseMatrix<T>& Y);
  21. template <typename T, const int W, const int H>
  22. inline void slice(
  23. const Eigen::Matrix<T,W,H> & X,
  24. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  25. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  26. Eigen::Matrix<T,W,H> & Y);
  27. template <typename T>
  28. inline void slice(
  29. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  30. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  31. Eigen::Matrix<T,Eigen::Dynamic,1> & Y);
  32. }
  33. // Implementation
  34. #include <vector>
  35. template <typename T>
  36. inline void igl::slice(
  37. const Eigen::SparseMatrix<T>& X,
  38. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  39. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  40. Eigen::SparseMatrix<T>& Y)
  41. {
  42. int xm = X.rows();
  43. int xn = X.cols();
  44. int ym = R.size();
  45. int yn = C.size();
  46. assert(R.minCoeff() >= 0);
  47. assert(R.maxCoeff() < xm);
  48. assert(C.minCoeff() >= 0);
  49. assert(C.maxCoeff() < xn);
  50. // Build reindexing maps for columns and rows, -1 means not in map
  51. std::vector<std::vector<int> > RI;
  52. RI.resize(xm);
  53. for(int i = 0;i<ym;i++)
  54. {
  55. RI[R(i)].push_back(i);
  56. }
  57. std::vector<std::vector<int> > CI;
  58. CI.resize(xn);
  59. // initialize to -1
  60. for(int i = 0;i<yn;i++)
  61. {
  62. CI[C(i)].push_back(i);
  63. }
  64. // Resize output
  65. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(ym,yn);
  66. // Take a guess at the number of nonzeros (this assumes uniform distribution
  67. // not banded or heavily diagonal)
  68. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  69. // Iterate over outside
  70. for(int k=0; k<X.outerSize(); ++k)
  71. {
  72. // Iterate over inside
  73. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  74. {
  75. std::vector<int>::iterator rit, cit;
  76. for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
  77. {
  78. for(cit = CI[it.row()].begin();cit != CI[it.row()].end(); cit++)
  79. {
  80. dyn_Y.coeffRef(*rit,*cit) = it.value();
  81. }
  82. }
  83. }
  84. }
  85. Y = Eigen::SparseMatrix<T>(dyn_Y);
  86. }
  87. template <typename T, const int W, const int H>
  88. inline void igl::slice(
  89. const Eigen::Matrix<T,W,H> & X,
  90. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  91. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  92. Eigen::Matrix<T,W,H> & Y)
  93. {
  94. int xm = X.rows();
  95. int xn = X.cols();
  96. int ym = R.size();
  97. int yn = C.size();
  98. assert(R.minCoeff() >= 0);
  99. assert(R.maxCoeff() < xm);
  100. assert(C.minCoeff() >= 0);
  101. assert(C.maxCoeff() < xn);
  102. // Resize output
  103. Y.resize(ym,yn);
  104. // loop over output rows, then columns
  105. for(int i = 0;i<ym;i++)
  106. {
  107. for(int j = 0;j<yn;j++)
  108. {
  109. Y(i,j) = X(R(i),C(j));
  110. }
  111. }
  112. }
  113. template <typename T>
  114. inline void igl::slice(
  115. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  116. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  117. Eigen::Matrix<T,Eigen::Dynamic,1> & Y)
  118. {
  119. // phony column indices
  120. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  121. C.resize(1);
  122. C(0) = 0;
  123. return igl::slice(X,R,C,Y);
  124. }
  125. #endif