slice_mask.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "slice_mask.h"
  2. #include <cassert>
  3. template <typename DerivedX>
  4. IGL_INLINE void igl::slice_mask(
  5. const Eigen::PlainObjectBase<DerivedX> & X,
  6. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  7. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  8. Eigen::PlainObjectBase<DerivedX> & Y)
  9. {
  10. int xm = X.rows();
  11. int xn = X.cols();
  12. int ym = R.count();
  13. int yn = C.count();
  14. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  15. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  16. Y.resize(ym,yn);
  17. {
  18. int yi = 0;
  19. for(int i = 0;i<xm;i++)
  20. {
  21. if(R(i))
  22. {
  23. int yj = 0;
  24. for(int j = 0;j<xn;j++)
  25. {
  26. if(C(j))
  27. {
  28. Y(yi,yj) = X(i,j);
  29. yj++;
  30. }
  31. }
  32. yi++;
  33. }
  34. }
  35. }
  36. }
  37. template <typename DerivedX>
  38. IGL_INLINE void igl::slice_mask(
  39. const Eigen::PlainObjectBase<DerivedX> & X,
  40. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  41. const int dim,
  42. Eigen::PlainObjectBase<DerivedX> & Y)
  43. {
  44. switch(dim)
  45. {
  46. case 1:
  47. {
  48. const int ym = R.count();
  49. Y.resize(ym,X.cols());
  50. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  51. {
  52. int yi = 0;
  53. for(int i = 0;i<X.rows();i++)
  54. {
  55. if(R(i))
  56. {
  57. Y.row(yi++) = X.row(i);
  58. }
  59. }
  60. }
  61. return;
  62. }
  63. case 2:
  64. {
  65. const auto & C = R;
  66. const int yn = C.count();
  67. Y.resize(X.rows(),yn);
  68. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  69. {
  70. int yj = 0;
  71. for(int j = 0;j<X.cols();j++)
  72. {
  73. if(C(j))
  74. {
  75. Y.col(yj++) = X.col(j);
  76. }
  77. }
  78. }
  79. return;
  80. }
  81. default:
  82. assert(false && "Unsupported dimension");
  83. return;
  84. }
  85. }
  86. template <typename DerivedX>
  87. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  88. const Eigen::PlainObjectBase<DerivedX> & X,
  89. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  90. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  91. {
  92. Eigen::PlainObjectBase<DerivedX> Y;
  93. igl::slice_mask(X,R,C,Y);
  94. return Y;
  95. }
  96. template <typename DerivedX>
  97. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  98. const Eigen::PlainObjectBase<DerivedX>& X,
  99. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  100. const int dim)
  101. {
  102. Eigen::PlainObjectBase<DerivedX> Y;
  103. igl::slice_mask(X,R,dim,Y);
  104. return Y;
  105. }
  106. #ifdef IGL_STATIC_LIBRARY
  107. template void igl::slice_mask<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  108. #endif