ismember.cpp 5.4 KB

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