intersect.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. M dyn_C(A.size() > B.size() ? A.size() : B.size(),1);
  14. // count of intersects
  15. int c = 0;
  16. // Loop over A
  17. for(int i = 0;i<A.size();i++)
  18. {
  19. // Loop over B
  20. for(int j = 0;j<B.size();j++)
  21. {
  22. if(A(i) == B(j))
  23. {
  24. dyn_C(c) = A(i);
  25. c++;
  26. }
  27. }
  28. }
  29. // resize output
  30. C.resize(c,1);
  31. // Loop over intersects
  32. for(int i = 0;i<c;i++)
  33. {
  34. C(i) = dyn_C(i);
  35. }
  36. }
  37. template <class M>
  38. IGL_INLINE M igl::intersect(const M & A, const M & B)
  39. {
  40. M C;
  41. intersect(A,B,C);
  42. return C;
  43. }
  44. #ifndef IGL_HEADER_ONLY
  45. // Explicit template specialization
  46. 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&);
  47. #endif