ismember_rows.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_rows.h"
  9. #include "sortrows.h"
  10. #include "unique.h"
  11. #include "unique_rows.h"
  12. #include <iostream>
  13. template <
  14. typename DerivedA,
  15. typename DerivedB,
  16. typename DerivedIA,
  17. typename DerivedLOCB>
  18. IGL_INLINE void igl::ismember_rows(
  19. const Eigen::MatrixBase<DerivedA> & A,
  20. const Eigen::MatrixBase<DerivedB> & B,
  21. Eigen::PlainObjectBase<DerivedIA> & IA,
  22. Eigen::PlainObjectBase<DerivedLOCB> & LOCB)
  23. {
  24. assert(A.cols() == B.cols() && "number of columns must match");
  25. IA.resize(A.rows(),1);
  26. IA.setConstant(false);
  27. LOCB.resize(A.rows(),1);
  28. LOCB.setConstant(-1);
  29. // boring base cases
  30. if(A.size() == 0)
  31. {
  32. return;
  33. }
  34. if(B.size() == 0)
  35. {
  36. return;
  37. }
  38. // Get rid of any duplicates
  39. typedef Eigen::Matrix<typename DerivedA::Scalar,DerivedA::RowsAtCompileTime,DerivedA::RowsAtCompileTime> MatrixA;
  40. typedef Eigen::Matrix<typename DerivedB::Scalar,DerivedB::RowsAtCompileTime,DerivedB::RowsAtCompileTime> MatrixB;
  41. MatrixA uA;
  42. MatrixB uB;
  43. // Using Eigen::Index or typename DerivedA::Index for the _Scalar_ of the
  44. // types below is a big mistake. It triggers all sorts of type confusion in
  45. // the templates on windows. Until we systematically template to support int64
  46. // etc. we should assume the inputs don't exceed 2147483647, which is
  47. // reasonable.
  48. typedef int Index;
  49. Eigen::Matrix<Index ,Eigen::Dynamic,1> uIA,uIuA,uIB,uIuB;
  50. unique_rows(A,uA,uIA,uIuA);
  51. unique_rows(B,uB,uIB,uIuB);
  52. // Sort both
  53. MatrixA sA;
  54. MatrixB sB;
  55. Eigen::Matrix<Index ,Eigen::Dynamic,1> sIA,sIB;
  56. sortrows(uA,true,sA,sIA);
  57. sortrows(uB,true,sB,sIB);
  58. Eigen::Matrix<bool,Eigen::Dynamic,1> uF =
  59. Eigen::Matrix<bool,Eigen::Dynamic,1>::Zero(sA.size(),1);
  60. Eigen::Matrix<typename DerivedLOCB::Scalar, Eigen::Dynamic,1> uLOCB =
  61. Eigen::Matrix<typename DerivedLOCB::Scalar,Eigen::Dynamic,1>::
  62. Constant(sA.size(),1,-1);
  63. const auto & row_greater_than = [&sA,&sB](const int a, const int b)
  64. {
  65. for(int c = 0;c<sA.cols();c++)
  66. {
  67. if(sA(a,c) > sB(b,c)) return true;
  68. if(sA(a,c) < sB(b,c)) return false;
  69. }
  70. return false;
  71. };
  72. {
  73. int bi = 0;
  74. // loop over sA
  75. bool past = false;
  76. for(int a = 0;a<sA.rows();a++)
  77. {
  78. assert(past || (bi < sB.rows()));
  79. while(!past && row_greater_than(a,bi))
  80. {
  81. bi++;
  82. past = bi>=sB.rows();
  83. }
  84. if(!past && (sA.row(a).array()==sB.row(bi).array()).all() )
  85. {
  86. uF(sIA(a)) = true;
  87. uLOCB(sIA(a)) = uIB(sIB(bi));
  88. }
  89. }
  90. }
  91. for(int a = 0;a<A.rows();a++)
  92. {
  93. IA(a) = uF(uIuA(a));
  94. LOCB(a) = uLOCB(uIuA(a));
  95. }
  96. }
  97. #ifdef IGL_STATIC_LIBRARY
  98. // Explicit template instantiation
  99. // generated by autoexplicit.sh
  100. template void igl::ismember_rows<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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, -1, 0, -1, -1>> const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>>&);
  101. // generated by autoexplicit.sh
  102. template void igl::ismember_rows<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Array<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::Array<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  103. 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> >&);
  104. 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> >&);
  105. 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> >&);
  106. template void igl::ismember_rows<Eigen::Matrix<int, -1, 4, 0, -1, 4>, Eigen::Matrix<int, 12, 4, 0, 12, 4>, Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 4, 0, -1, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<int, 12, 4, 0, 12, 4> > const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  107. #endif