slice.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef IGL_SLICE_H
  2. #define IGL_SLICE_H
  3. namespace igl
  4. {
  5. // Act like the matlab X(row_indices,col_indices) operator
  6. //
  7. // Inputs:
  8. // X m by n matrix
  9. // R list of row indices
  10. // C list of column indices
  11. // Output:
  12. // Y #R by #C matrix
  13. template <typename T>
  14. inline void slice(
  15. const Eigen::SparseMatrix<T>& X,
  16. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  17. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  18. Eigen::SparseMatrix<T>& Y);
  19. template <typename T, const int W, const int H>
  20. inline void slice(
  21. const Eigen::Matrix<T,W,H> & X,
  22. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  23. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  24. Eigen::Matrix<T,W,H> & Y);
  25. template <typename T>
  26. inline void slice(
  27. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  28. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  29. Eigen::Matrix<T,Eigen::Dynamic,1> & Y);
  30. }
  31. // Implementation
  32. template <typename T>
  33. inline void igl::slice(
  34. const Eigen::SparseMatrix<T>& X,
  35. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  36. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  37. Eigen::SparseMatrix<T>& Y)
  38. {
  39. int xm = X.rows();
  40. int xn = X.cols();
  41. int ym = R.size();
  42. int yn = C.size();
  43. assert(R.minCoeff() >= 0);
  44. assert(R.maxCoeff() < xm);
  45. assert(C.minCoeff() >= 0);
  46. assert(C.maxCoeff() < xn);
  47. // Build reindexing maps for columns and rows, -1 means not in map
  48. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  49. RI.resize(xm);
  50. // initialize to -1
  51. for(int i = 0;i<xm;i++)
  52. {
  53. RI(i) = -1;
  54. }
  55. for(int i = 0;i<ym;i++)
  56. {
  57. RI(R(i)) = i;
  58. }
  59. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  60. CI.resize(xn);
  61. // initialize to -1
  62. for(int i = 0;i<xn;i++)
  63. {
  64. CI(i) = -1;
  65. }
  66. for(int i = 0;i<yn;i++)
  67. {
  68. CI(C(i)) = i;
  69. }
  70. // Resize output
  71. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor>
  72. dyn_Y(ym,yn);
  73. // Iterate over outside
  74. for(int k=0; k<X.outerSize(); ++k)
  75. {
  76. // Iterate over inside
  77. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  78. {
  79. if(RI(it.row()) >= 0 && CI(it.col()) >= 0)
  80. {
  81. dyn_Y.coeffRef(RI(it.row()),CI(it.col())) = it.value();
  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. // Build reindexing maps for columns and rows, -1 means not in map
  103. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  104. RI.resize(xm);
  105. // initialize to -1
  106. for(int i = 0;i<xm;i++)
  107. {
  108. RI(i) = -1;
  109. }
  110. for(int i = 0;i<ym;i++)
  111. {
  112. RI(R(i)) = i;
  113. }
  114. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  115. CI.resize(xn);
  116. // initialize to -1
  117. for(int i = 0;i<xn;i++)
  118. {
  119. CI(i) = -1;
  120. }
  121. for(int i = 0;i<yn;i++)
  122. {
  123. CI(C(i)) = i;
  124. }
  125. // Resize output
  126. Y.resize(ym,yn);
  127. for(int i = 0;i<xm;i++)
  128. {
  129. for(int j = 0;j<xn;j++)
  130. {
  131. if(RI(i) >= 0 && CI(j) >= 0)
  132. {
  133. Y(RI(i),CI(j)) = X(i,j);
  134. }
  135. }
  136. }
  137. }
  138. template <typename T>
  139. inline void igl::slice(
  140. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  141. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  142. Eigen::Matrix<T,Eigen::Dynamic,1> & Y)
  143. {
  144. // phony column indices
  145. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  146. C.resize(1);
  147. C(0) = 0;
  148. return igl::slice(X,R,C,Y);
  149. }
  150. #endif