slice_mask.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "slice_mask.h"
  9. #include <cassert>
  10. template <typename DerivedX>
  11. IGL_INLINE void igl::slice_mask(
  12. const Eigen::PlainObjectBase<DerivedX> & X,
  13. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  14. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  15. Eigen::PlainObjectBase<DerivedX> & Y)
  16. {
  17. int xm = X.rows();
  18. int xn = X.cols();
  19. int ym = R.count();
  20. int yn = C.count();
  21. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  22. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  23. Y.resize(ym,yn);
  24. {
  25. int yi = 0;
  26. for(int i = 0;i<xm;i++)
  27. {
  28. if(R(i))
  29. {
  30. int yj = 0;
  31. for(int j = 0;j<xn;j++)
  32. {
  33. if(C(j))
  34. {
  35. Y(yi,yj) = X(i,j);
  36. yj++;
  37. }
  38. }
  39. yi++;
  40. }
  41. }
  42. }
  43. }
  44. template <typename DerivedX>
  45. IGL_INLINE void igl::slice_mask(
  46. const Eigen::PlainObjectBase<DerivedX> & X,
  47. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  48. const int dim,
  49. Eigen::PlainObjectBase<DerivedX> & Y)
  50. {
  51. switch(dim)
  52. {
  53. case 1:
  54. {
  55. const int ym = R.count();
  56. Y.resize(ym,X.cols());
  57. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  58. {
  59. int yi = 0;
  60. for(int i = 0;i<X.rows();i++)
  61. {
  62. if(R(i))
  63. {
  64. Y.row(yi++) = X.row(i);
  65. }
  66. }
  67. }
  68. return;
  69. }
  70. case 2:
  71. {
  72. const auto & C = R;
  73. const int yn = C.count();
  74. Y.resize(X.rows(),yn);
  75. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  76. {
  77. int yj = 0;
  78. for(int j = 0;j<X.cols();j++)
  79. {
  80. if(C(j))
  81. {
  82. Y.col(yj++) = X.col(j);
  83. }
  84. }
  85. }
  86. return;
  87. }
  88. default:
  89. assert(false && "Unsupported dimension");
  90. return;
  91. }
  92. }
  93. template <typename DerivedX>
  94. IGL_INLINE DerivedX igl::slice_mask(
  95. const Eigen::PlainObjectBase<DerivedX> & X,
  96. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  97. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  98. {
  99. DerivedX Y;
  100. igl::slice_mask(X,R,C,Y);
  101. return Y;
  102. }
  103. template <typename DerivedX>
  104. IGL_INLINE DerivedX igl::slice_mask(
  105. const Eigen::PlainObjectBase<DerivedX>& X,
  106. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  107. const int dim)
  108. {
  109. DerivedX Y;
  110. igl::slice_mask(X,R,dim,Y);
  111. return Y;
  112. }
  113. #ifdef IGL_STATIC_LIBRARY
  114. 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> >&);
  115. #endif