setunion.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "setunion.h"
  9. #include "unique.h"
  10. template <
  11. typename DerivedA,
  12. typename DerivedB,
  13. typename DerivedC,
  14. typename DerivedIA,
  15. typename DerivedIB>
  16. IGL_INLINE void igl::setunion(
  17. const Eigen::DenseBase<DerivedA> & A,
  18. const Eigen::DenseBase<DerivedB> & B,
  19. Eigen::PlainObjectBase<DerivedC> & C,
  20. Eigen::PlainObjectBase<DerivedIA> & IA,
  21. Eigen::PlainObjectBase<DerivedIB> & IB)
  22. {
  23. DerivedC CS(A.size()+B.size(),1);
  24. {
  25. int k = 0;
  26. for(int j = 0;j<A.cols();j++)
  27. {
  28. for(int i = 0;i<A.rows();i++)
  29. {
  30. CS(k++) = A(i,j);
  31. }
  32. }
  33. for(int j = 0;j<B.cols();j++)
  34. {
  35. for(int i = 0;i<B.rows();i++)
  36. {
  37. CS(k++) = B(i,j);
  38. }
  39. }
  40. assert(k==CS.size());
  41. }
  42. DerivedIA IAC;
  43. {
  44. DerivedIA IC;
  45. unique(CS,C,IAC,IC);
  46. }
  47. const int nia = (IAC.array()<A.size()).count();
  48. IA.resize(nia);
  49. IB.resize(IAC.size() - nia);
  50. {
  51. int ka = 0;
  52. int kb = 0;
  53. for(int i = 0;i<IAC.size();i++)
  54. {
  55. if(IAC(i)<A.size())
  56. {
  57. IA(ka++) = IAC(i);
  58. }else
  59. {
  60. IB(kb++) = IAC(i)-A.size();
  61. }
  62. }
  63. assert(ka == IA.size());
  64. assert(kb == IB.size());
  65. }
  66. }
  67. #ifdef IGL_STATIC_LIBRARY
  68. // Explicit template instantiation
  69. // generated by autoexplicit.sh
  70. template void igl::setunion<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  71. #endif