slice.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // 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. if(RI(it.row()) >= 0 && CI(it.col()) >= 0)
  82. {
  83. dyn_Y.coeffRef(RI(it.row()),CI(it.col())) = it.value();
  84. }
  85. }
  86. }
  87. Y = Eigen::SparseMatrix<T>(dyn_Y);
  88. }
  89. template <typename T, const int W, const int H>
  90. inline void igl::slice(
  91. const Eigen::Matrix<T,W,H> & X,
  92. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  93. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  94. Eigen::Matrix<T,W,H> & Y)
  95. {
  96. int xm = X.rows();
  97. int xn = X.cols();
  98. int ym = R.size();
  99. int yn = C.size();
  100. assert(R.minCoeff() >= 0);
  101. assert(R.maxCoeff() < xm);
  102. assert(C.minCoeff() >= 0);
  103. assert(C.maxCoeff() < xn);
  104. // Build reindexing maps for columns and rows, -1 means not in map
  105. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  106. RI.resize(xm);
  107. // initialize to -1
  108. for(int i = 0;i<xm;i++)
  109. {
  110. RI(i) = -1;
  111. }
  112. for(int i = 0;i<ym;i++)
  113. {
  114. RI(R(i)) = i;
  115. }
  116. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  117. CI.resize(xn);
  118. // initialize to -1
  119. for(int i = 0;i<xn;i++)
  120. {
  121. CI(i) = -1;
  122. }
  123. for(int i = 0;i<yn;i++)
  124. {
  125. CI(C(i)) = i;
  126. }
  127. // Resize output
  128. Y.resize(ym,yn);
  129. for(int i = 0;i<xm;i++)
  130. {
  131. for(int j = 0;j<xn;j++)
  132. {
  133. if(RI(i) >= 0 && CI(j) >= 0)
  134. {
  135. Y(RI(i),CI(j)) = X(i,j);
  136. }
  137. }
  138. }
  139. }
  140. template <typename T>
  141. inline void igl::slice(
  142. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  143. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  144. Eigen::Matrix<T,Eigen::Dynamic,1> & Y)
  145. {
  146. // phony column indices
  147. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  148. C.resize(1);
  149. C(0) = 0;
  150. return igl::slice(X,R,C,Y);
  151. }
  152. #endif