ismember.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "ismember.h"
  9. #include "colon.h"
  10. #include "list_to_matrix.h"
  11. #include "sort.h"
  12. #include "sortrows.h"
  13. #include "unique.h"
  14. #include "unique_rows.h"
  15. template <
  16. typename DerivedA,
  17. typename DerivedB,
  18. typename DerivedIA,
  19. typename DerivedLOCB>
  20. IGL_INLINE void igl::ismember(
  21. const Eigen::MatrixBase<DerivedA> & A,
  22. const Eigen::MatrixBase<DerivedB> & B,
  23. Eigen::PlainObjectBase<DerivedIA> & IA,
  24. Eigen::PlainObjectBase<DerivedLOCB> & LOCB)
  25. {
  26. using namespace Eigen;
  27. using namespace std;
  28. IA.resizeLike(A);
  29. IA.setConstant(false);
  30. LOCB.resizeLike(A);
  31. LOCB.setConstant(-1);
  32. // boring base cases
  33. if(A.size() == 0)
  34. {
  35. return;
  36. }
  37. if(B.size() == 0)
  38. {
  39. return;
  40. }
  41. // Get rid of any duplicates
  42. typedef Matrix<typename DerivedA::Scalar,Dynamic,1> VectorA;
  43. typedef Matrix<typename DerivedB::Scalar,Dynamic,1> VectorB;
  44. const VectorA vA(Eigen::Map<const VectorA>(DerivedA(A).data(), A.cols()*A.rows(),1));
  45. const VectorB vB(Eigen::Map<const VectorB>(DerivedB(B).data(), B.cols()*B.rows(),1));
  46. VectorA uA;
  47. VectorB uB;
  48. Eigen::Matrix<typename DerivedA::Index,Dynamic,1> uIA,uIuA,uIB,uIuB;
  49. unique(vA,uA,uIA,uIuA);
  50. unique(vB,uB,uIB,uIuB);
  51. // Sort both
  52. VectorA sA;
  53. VectorB sB;
  54. Eigen::Matrix<typename DerivedA::Index,Dynamic,1> sIA,sIB;
  55. sort(uA,1,true,sA,sIA);
  56. sort(uB,1,true,sB,sIB);
  57. Eigen::Matrix<bool,Eigen::Dynamic,1> uF =
  58. Eigen::Matrix<bool,Eigen::Dynamic,1>::Zero(sA.size(),1);
  59. Eigen::Matrix<typename DerivedLOCB::Scalar, Eigen::Dynamic,1> uLOCB =
  60. Eigen::Matrix<typename DerivedLOCB::Scalar,Eigen::Dynamic,1>::
  61. Constant(sA.size(),1,-1);
  62. {
  63. int bi = 0;
  64. // loop over sA
  65. bool past = false;
  66. for(int a = 0;a<sA.size();a++)
  67. {
  68. while(!past && sA(a)>sB(bi))
  69. {
  70. bi++;
  71. past = bi>=sB.size();
  72. }
  73. if(!past && sA(a)==sB(bi))
  74. {
  75. uF(sIA(a)) = true;
  76. uLOCB(sIA(a)) = uIB(sIB(bi));
  77. }
  78. }
  79. }
  80. Map< Matrix<typename DerivedIA::Scalar,Dynamic,1> >
  81. vIA(IA.data(),IA.cols()*IA.rows(),1);
  82. Map< Matrix<typename DerivedLOCB::Scalar,Dynamic,1> >
  83. vLOCB(LOCB.data(),LOCB.cols()*LOCB.rows(),1);
  84. for(int a = 0;a<A.size();a++)
  85. {
  86. vIA(a) = uF(uIuA(a));
  87. vLOCB(a) = uLOCB(uIuA(a));
  88. }
  89. }
  90. template <
  91. typename DerivedA,
  92. typename DerivedB,
  93. typename DerivedIA,
  94. typename DerivedLOCB>
  95. IGL_INLINE void igl::ismember_rows(
  96. const Eigen::MatrixBase<DerivedA> & A,
  97. const Eigen::MatrixBase<DerivedB> & B,
  98. Eigen::PlainObjectBase<DerivedIA> & IA,
  99. Eigen::PlainObjectBase<DerivedLOCB> & LOCB)
  100. {
  101. using namespace Eigen;
  102. using namespace std;
  103. assert(A.cols() == B.cols() && "number of columns must match");
  104. IA.resize(A.rows(),1);
  105. IA.setConstant(false);
  106. LOCB.resize(A.rows(),1);
  107. LOCB.setConstant(-1);
  108. // boring base cases
  109. if(A.size() == 0)
  110. {
  111. return;
  112. }
  113. if(B.size() == 0)
  114. {
  115. return;
  116. }
  117. // Get rid of any duplicates
  118. DerivedA uA;
  119. DerivedB uB;
  120. Eigen::Matrix<typename DerivedA::Index,Dynamic,1> uIA,uIuA,uIB,uIuB;
  121. unique_rows(A,uA,uIA,uIuA);
  122. unique_rows(B,uB,uIB,uIuB);
  123. // Sort both
  124. DerivedA sA;
  125. DerivedB sB;
  126. Eigen::Matrix<typename DerivedA::Index,Dynamic,1> sIA,sIB;
  127. sortrows(uA,true,sA,sIA);
  128. sortrows(uB,true,sB,sIB);
  129. Eigen::Matrix<bool,Eigen::Dynamic,1> uF =
  130. Eigen::Matrix<bool,Eigen::Dynamic,1>::Zero(sA.size(),1);
  131. Eigen::Matrix<typename DerivedLOCB::Scalar, Eigen::Dynamic,1> uLOCB =
  132. Eigen::Matrix<typename DerivedLOCB::Scalar,Eigen::Dynamic,1>::
  133. Constant(sA.size(),1,-1);
  134. const auto & row_greater_than = [&sA,&sB](const int a, const int b)
  135. {
  136. for(int c = 0;c<sA.cols();c++)
  137. {
  138. if(sA(a,c) > sB(b,c)) return true;
  139. if(sA(a,c) < sB(b,c)) return false;
  140. }
  141. return false;
  142. };
  143. {
  144. int bi = 0;
  145. // loop over sA
  146. bool past = false;
  147. for(int a = 0;a<sA.rows();a++)
  148. {
  149. while(!past && row_greater_than(a,bi))
  150. {
  151. bi++;
  152. past = bi>=sB.size();
  153. }
  154. if(!past && (sA.row(a).array()==sB.row(bi).array()).all() )
  155. {
  156. uF(sIA(a)) = true;
  157. uLOCB(sIA(a)) = uIB(sIB(bi));
  158. }
  159. }
  160. }
  161. for(int a = 0;a<A.rows();a++)
  162. {
  163. IA(a) = uF(uIuA(a));
  164. LOCB(a) = uLOCB(uIuA(a));
  165. }
  166. }
  167. #ifdef IGL_STATIC_LIBRARY
  168. // Explicit template instantiation
  169. template void igl::ismember<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  170. template void igl::ismember_rows<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  171. template void igl::ismember_rows<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  172. template void igl::ismember_rows<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  173. #endif