slice_mask.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "slice.h"
  10. #include "find.h"
  11. #include <cassert>
  12. template <typename DerivedX,typename DerivedY>
  13. IGL_INLINE void igl::slice_mask(
  14. const Eigen::DenseBase<DerivedX> & X,
  15. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  16. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  17. Eigen::PlainObjectBase<DerivedY> & Y)
  18. {
  19. int xm = X.rows();
  20. int xn = X.cols();
  21. int ym = R.count();
  22. int yn = C.count();
  23. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  24. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  25. Y.resize(ym,yn);
  26. {
  27. int yi = 0;
  28. for(int i = 0;i<xm;i++)
  29. {
  30. if(R(i))
  31. {
  32. int yj = 0;
  33. for(int j = 0;j<xn;j++)
  34. {
  35. if(C(j))
  36. {
  37. Y(yi,yj) = X(i,j);
  38. yj++;
  39. }
  40. }
  41. yi++;
  42. }
  43. }
  44. }
  45. }
  46. template <typename DerivedX, typename DerivedY>
  47. IGL_INLINE void igl::slice_mask(
  48. const Eigen::DenseBase<DerivedX> & X,
  49. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  50. const int dim,
  51. Eigen::PlainObjectBase<DerivedY> & Y)
  52. {
  53. switch(dim)
  54. {
  55. case 1:
  56. {
  57. const int ym = R.count();
  58. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  59. Y.resize(ym,X.cols());
  60. {
  61. int yi = 0;
  62. for(int i = 0;i<X.rows();i++)
  63. {
  64. if(R(i))
  65. {
  66. Y.row(yi++) = X.row(i);
  67. }
  68. }
  69. }
  70. return;
  71. }
  72. case 2:
  73. {
  74. const auto & C = R;
  75. const int yn = C.count();
  76. Y.resize(X.rows(),yn);
  77. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  78. {
  79. int yj = 0;
  80. for(int j = 0;j<X.cols();j++)
  81. {
  82. if(C(j))
  83. {
  84. Y.col(yj++) = X.col(j);
  85. }
  86. }
  87. }
  88. return;
  89. }
  90. default:
  91. assert(false && "Unsupported dimension");
  92. return;
  93. }
  94. }
  95. template <typename DerivedX>
  96. IGL_INLINE DerivedX igl::slice_mask(
  97. const Eigen::DenseBase<DerivedX> & X,
  98. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  99. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  100. {
  101. DerivedX Y;
  102. igl::slice_mask(X,R,C,Y);
  103. return Y;
  104. }
  105. template <typename DerivedX>
  106. IGL_INLINE DerivedX igl::slice_mask(
  107. const Eigen::DenseBase<DerivedX>& X,
  108. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  109. const int dim)
  110. {
  111. DerivedX Y;
  112. igl::slice_mask(X,R,dim,Y);
  113. return Y;
  114. }
  115. template <typename XType, typename YType>
  116. IGL_INLINE void igl::slice_mask(
  117. const Eigen::SparseMatrix<XType> & X,
  118. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  119. const int dim,
  120. Eigen::SparseMatrix<YType> & Y)
  121. {
  122. // Cheapskate solution
  123. Eigen::VectorXi Ri;
  124. find(R,Ri);
  125. return slice(X,Ri,dim,Y);
  126. }
  127. template <typename XType, typename YType>
  128. IGL_INLINE void igl::slice_mask(
  129. const Eigen::SparseMatrix<XType> & X,
  130. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  131. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  132. Eigen::SparseMatrix<YType> & Y)
  133. {
  134. // Cheapskate solution
  135. Eigen::VectorXi Ri;
  136. find(R,Ri);
  137. Eigen::VectorXi Ci;
  138. find(C,Ci);
  139. return slice(X,Ri,Ci,Y);
  140. }
  141. #ifdef IGL_STATIC_LIBRARY
  142. // Explicit template instantiation
  143. // generated by autoexplicit.sh
  144. template Eigen::Matrix<int, -1, 3, 1, -1, 3> igl::slice_mask<Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int);
  145. // generated by autoexplicit.sh
  146. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::slice_mask<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int);
  147. // generated by autoexplicit.sh
  148. template Eigen::Array<int, -1, 3, 1, -1, 3> igl::slice_mask<Eigen::Array<int, -1, 3, 1, -1, 3> >(Eigen::DenseBase<Eigen::Array<int, -1, 3, 1, -1, 3> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int);
  149. // generated by autoexplicit.sh
  150. template void igl::slice_mask<bool, bool>(Eigen::SparseMatrix<bool, 0, int> const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::SparseMatrix<bool, 0, int>&);
  151. template void igl::slice_mask<Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<double, -1, 2, 0, -1, 2> >(Eigen::DenseBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&);
  152. template void igl::slice_mask<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
  153. template void igl::slice_mask<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  154. template void igl::slice_mask<Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Array<bool, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Array<bool, -1, 1, 0, -1, 1> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&);
  155. template void igl::slice_mask<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::DenseBase<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> >&);
  156. template void igl::slice_mask<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::DenseBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  157. template void igl::slice_mask<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::DenseBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  158. template void igl::slice_mask<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, Eigen::Array<bool, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  159. template void igl::slice_mask<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<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> >&);
  160. #endif