slice.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // special case when R or C is empty
  47. if(ym == 0 || yn == 0)
  48. {
  49. Y.resize(ym,yn);
  50. return;
  51. }
  52. assert(R.minCoeff() >= 0);
  53. assert(R.maxCoeff() < xm);
  54. assert(C.minCoeff() >= 0);
  55. assert(C.maxCoeff() < xn);
  56. // Build reindexing maps for columns and rows, -1 means not in map
  57. std::vector<std::vector<int> > RI;
  58. RI.resize(xm);
  59. for(int i = 0;i<ym;i++)
  60. {
  61. RI[R(i)].push_back(i);
  62. }
  63. std::vector<std::vector<int> > CI;
  64. CI.resize(xn);
  65. // initialize to -1
  66. for(int i = 0;i<yn;i++)
  67. {
  68. CI[C(i)].push_back(i);
  69. }
  70. // Resize output
  71. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(ym,yn);
  72. // Take a guess at the number of nonzeros (this assumes uniform distribution
  73. // not banded or heavily diagonal)
  74. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  75. // Iterate over outside
  76. for(int k=0; k<X.outerSize(); ++k)
  77. {
  78. // Iterate over inside
  79. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  80. {
  81. std::vector<int>::iterator rit, cit;
  82. for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
  83. {
  84. for(cit = CI[it.col()].begin();cit != CI[it.col()].end(); cit++)
  85. {
  86. dyn_Y.coeffRef(*rit,*cit) = it.value();
  87. }
  88. }
  89. }
  90. }
  91. Y = Eigen::SparseMatrix<T>(dyn_Y);
  92. }
  93. template <typename T, const int W, const int H>
  94. inline void igl::slice(
  95. const Eigen::Matrix<T,W,H> & X,
  96. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  97. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  98. Eigen::Matrix<T,W,H> & Y)
  99. {
  100. int xm = X.rows();
  101. int xn = X.cols();
  102. int ym = R.size();
  103. int yn = C.size();
  104. // special case when R or C is empty
  105. if(ym == 0 || yn == 0)
  106. {
  107. Y.resize(ym,yn);
  108. return;
  109. }
  110. assert(R.minCoeff() >= 0);
  111. assert(R.maxCoeff() < xm);
  112. assert(C.minCoeff() >= 0);
  113. assert(C.maxCoeff() < xn);
  114. // Resize output
  115. Y.resize(ym,yn);
  116. // loop over output rows, then columns
  117. for(int i = 0;i<ym;i++)
  118. {
  119. for(int j = 0;j<yn;j++)
  120. {
  121. Y(i,j) = X(R(i),C(j));
  122. }
  123. }
  124. }
  125. template <typename T>
  126. inline void igl::slice(
  127. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  128. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  129. Eigen::Matrix<T,Eigen::Dynamic,1> & Y)
  130. {
  131. // phony column indices
  132. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  133. C.resize(1);
  134. C(0) = 0;
  135. return igl::slice(X,R,C,Y);
  136. }
  137. #endif