intersect.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "intersect.h"
  9. template <class M>
  10. IGL_INLINE void igl::intersect(const M & A, const M & B, M & C)
  11. {
  12. // Stupid O(size(A) * size(B)) to do it
  13. // Alec: This should be implemented by using unique and sort like `setdiff`
  14. M dyn_C(A.size() > B.size() ? A.size() : B.size(),1);
  15. // count of intersects
  16. int c = 0;
  17. // Loop over A
  18. for(int i = 0;i<A.size();i++)
  19. {
  20. // Loop over B
  21. for(int j = 0;j<B.size();j++)
  22. {
  23. if(A(i) == B(j))
  24. {
  25. dyn_C(c) = A(i);
  26. c++;
  27. }
  28. }
  29. }
  30. // resize output
  31. C.resize(c,1);
  32. // Loop over intersects
  33. for(int i = 0;i<c;i++)
  34. {
  35. C(i) = dyn_C(i);
  36. }
  37. }
  38. template <class M>
  39. IGL_INLINE M igl::intersect(const M & A, const M & B)
  40. {
  41. M C;
  42. intersect(A,B,C);
  43. return C;
  44. }
  45. #ifdef IGL_STATIC_LIBRARY
  46. // Explicit template specialization
  47. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::intersect<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&);
  48. #endif