ismember.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifdef IGL_STATIC_LIBRARY
  89. // Explicit template specialization
  90. 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> >&);
  91. #endif