ismember.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "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. #include <iostream>
  16. template <
  17. typename DerivedA,
  18. typename DerivedB,
  19. typename DerivedIA,
  20. typename DerivedLOCB>
  21. IGL_INLINE void igl::ismember(
  22. const Eigen::MatrixBase<DerivedA> & A,
  23. const Eigen::MatrixBase<DerivedB> & B,
  24. Eigen::PlainObjectBase<DerivedIA> & IA,
  25. Eigen::PlainObjectBase<DerivedLOCB> & LOCB)
  26. {
  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 Eigen::Matrix<typename DerivedA::Scalar ,Eigen::Dynamic,1> VectorA;
  42. typedef Eigen::Matrix<typename DerivedB::Scalar ,Eigen::Dynamic,1> VectorB;
  43. const VectorA vA(Eigen::Map<const VectorA>(DerivedA(A).data(), A.cols()*A.rows(),1));
  44. const VectorB vB(Eigen::Map<const VectorB>(DerivedB(B).data(), B.cols()*B.rows(),1));
  45. VectorA uA;
  46. VectorB uB;
  47. Eigen::Matrix<typename DerivedA::Index ,Eigen::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 ,Eigen::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. Eigen::Map< Eigen::Matrix<typename DerivedIA::Scalar ,Eigen::Dynamic,1> >
  80. vIA(IA.data(),IA.cols()*IA.rows(),1);
  81. Eigen::Map< Eigen::Matrix<typename DerivedLOCB::Scalar ,Eigen::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. #ifdef IGL_STATIC_LIBRARY
  90. // Explicit template instantiation
  91. 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> >&);
  92. #endif