slice.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. RI.array() = RI.array()*0-1;
  51. for(int i = 0;i<ym;i++)
  52. {
  53. RI(R(i)) = i;
  54. }
  55. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  56. CI.resize(xn);
  57. CI.array() = CI.array()*0-1;
  58. for(int i = 0;i<yn;i++)
  59. {
  60. CI(C(i)) = i;
  61. }
  62. // Resize output
  63. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor>
  64. dyn_Y(ym,yn);
  65. // Iterate over outside
  66. for(int k=0; k<X.outerSize(); ++k)
  67. {
  68. // Iterate over inside
  69. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  70. {
  71. if(RI(it.row()) >= 0 && CI(it.col()) >= 0)
  72. {
  73. dyn_Y.coeffRef(RI(it.row()),CI(it.col())) = it.value();
  74. }
  75. }
  76. }
  77. Y = Eigen::SparseMatrix<T>(dyn_Y);
  78. }
  79. template <typename T, const int W, const int H>
  80. inline void igl::slice(
  81. const Eigen::Matrix<T,W,H> & X,
  82. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  83. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  84. Eigen::Matrix<T,W,H> & Y)
  85. {
  86. int xm = X.rows();
  87. int xn = X.cols();
  88. int ym = R.size();
  89. int yn = C.size();
  90. assert(R.minCoeff() >= 0);
  91. assert(R.maxCoeff() < xm);
  92. assert(C.minCoeff() >= 0);
  93. assert(C.maxCoeff() < xn);
  94. // Build reindexing maps for columns and rows, -1 means not in map
  95. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  96. RI.resize(xm);
  97. RI.array() = RI.array()*0-1;
  98. for(int i = 0;i<ym;i++)
  99. {
  100. RI(R(i)) = i;
  101. }
  102. Eigen::Matrix<int,Eigen::Dynamic,1> CI;
  103. CI.resize(xn);
  104. CI.array() = CI.array()*0-1;
  105. for(int i = 0;i<yn;i++)
  106. {
  107. CI(C(i)) = i;
  108. }
  109. // Resize output
  110. Y.resize(ym,yn);
  111. for(int i = 0;i<xm;i++)
  112. {
  113. for(int j = 0;j<xn;j++)
  114. {
  115. if(RI(i) >= 0 && CI(j) >= 0)
  116. {
  117. Y(RI(i),CI(j)) = X(i,j);
  118. }
  119. }
  120. }
  121. }
  122. template <typename T>
  123. inline void igl::slice(
  124. const Eigen::Matrix<T,Eigen::Dynamic,1> & X,
  125. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  126. Eigen::Matrix<T,Eigen::Dynamic,1> & Y)
  127. {
  128. // phony column indices
  129. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  130. C.resize(1);
  131. C(0) = 0;
  132. return igl::slice(X,R,C,Y);
  133. }
  134. #endif