slice_mask.cpp 9.4 KB

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