ismember.cpp 4.9 KB

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