slice.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. template <typename T>
  35. inline void igl::slice(
  36. const Eigen::SparseMatrix<T>& X,
  37. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  38. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  39. Eigen::SparseMatrix<T>& Y)
  40. {
  41. int xm = X.rows();
  42. int xn = X.cols();
  43. int ym = R.size();
  44. int yn = C.size();
  45. assert(R.minCoeff() >= 0);
  46. assert(R.maxCoeff() < xm);
  47. assert(C.minCoeff() >= 0);
  48. assert(C.maxCoeff() < xn);
  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. // initialize to -1
  53. for(int i = 0;i<xm;i++)
  54. {
  55. RI(i) = -1;
  56. }
  57. for(int i = 0;i<ym;i++)
  58. {
  59. RI(R(i)) = i;
  60. }
  61. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  62. CI.resize(xn);
  63. // initialize to -1
  64. for(int i = 0;i<xn;i++)
  65. {
  66. CI(i) = -1;
  67. }
  68. for(int i = 0;i<yn;i++)
  69. {
  70. CI(C(i)) = i;
  71. }
  72. // Resize output
  73. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor>
  74. dyn_Y(ym,yn);
  75. // Take a guess at the number of nonzeros (this assumes uniform distribution
  76. // not banded or heavily diagonal)
  77. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  78. // Iterate over outside
  79. for(int k=0; k<X.outerSize(); ++k)
  80. {
  81. // Iterate over inside
  82. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  83. {
  84. if(RI(it.row()) >= 0 && CI(it.col()) >= 0)
  85. {
  86. dyn_Y.coeffRef(RI(it.row()),CI(it.col())) = it.value();
  87. }
  88. }
  89. }
  90. Y = Eigen::SparseMatrix<T>(dyn_Y);
  91. }
  92. template <typename T, const int W, const int H>
  93. inline void igl::slice(
  94. const Eigen::Matrix<T,W,H> & X,
  95. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  96. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  97. Eigen::Matrix<T,W,H> & Y)
  98. {
  99. int xm = X.rows();
  100. int xn = X.cols();
  101. int ym = R.size();
  102. int yn = C.size();
  103. assert(R.minCoeff() >= 0);
  104. assert(R.maxCoeff() < xm);
  105. assert(C.minCoeff() >= 0);
  106. assert(C.maxCoeff() < xn);
  107. // Build reindexing maps for columns and rows, -1 means not in map
  108. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  109. RI.resize(xm);
  110. // initialize to -1
  111. for(int i = 0;i<xm;i++)
  112. {
  113. RI(i) = -1;
  114. }
  115. for(int i = 0;i<ym;i++)
  116. {
  117. RI(R(i)) = i;
  118. }
  119. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  120. CI.resize(xn);
  121. // initialize to -1
  122. for(int i = 0;i<xn;i++)
  123. {
  124. CI(i) = -1;
  125. }
  126. for(int i = 0;i<yn;i++)
  127. {
  128. CI(C(i)) = i;
  129. }
  130. // Resize output
  131. Y.resize(ym,yn);
  132. for(int i = 0;i<xm;i++)
  133. {
  134. for(int j = 0;j<xn;j++)
  135. {
  136. if(RI(i) >= 0 && CI(j) >= 0)
  137. {
  138. Y(RI(i),CI(j)) = X(i,j);
  139. }
  140. }
  141. }
  142. }
  143. template <typename T>
  144. inline void igl::slice(
  145. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  146. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  147. Eigen::Matrix<T,Eigen::Dynamic,1> & Y)
  148. {
  149. // phony column indices
  150. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  151. C.resize(1);
  152. C(0) = 0;
  153. return igl::slice(X,R,C,Y);
  154. }
  155. #endif