removeUnreferenced.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "removeUnreferenced.h"
  9. template <typename Scalar, typename Index>
  10. IGL_INLINE void igl::removeUnreferenced(
  11. const Eigen::PlainObjectBase<Scalar> &V,
  12. const Eigen::PlainObjectBase<Index> &F,
  13. Eigen::PlainObjectBase<Scalar> &NV,
  14. Eigen::PlainObjectBase<Index> &NF,
  15. Eigen::PlainObjectBase<Index> &I)
  16. {
  17. // Mark referenced vertices
  18. Eigen::MatrixXi mark = Eigen::MatrixXi::Zero(V.rows(),1);
  19. for(int i=0; i<F.rows(); ++i)
  20. {
  21. for(int j=0; j<F.cols(); ++j)
  22. {
  23. if (F(i,j) != -1)
  24. mark(F(i,j)) = 1;
  25. }
  26. }
  27. // Sum the occupied cells
  28. int newsize = mark.sum();
  29. NV.resize(newsize,V.cols());
  30. I.resize(V.rows(),1);
  31. // Do a pass on the marked vector and remove the unreferenced vertices
  32. int count = 0;
  33. for(int i=0;i<mark.rows();++i)
  34. {
  35. if (mark(i) == 1)
  36. {
  37. NV.row(count) = V.row(i);
  38. I(i) = count;
  39. count++;
  40. }
  41. else
  42. {
  43. I(i) = -1;
  44. }
  45. }
  46. NF.resize(F.rows(),F.cols());
  47. // Apply I on F
  48. for (int i=0; i<F.rows(); ++i)
  49. {
  50. Eigen::RowVectorXi t(F.cols());
  51. for (int j=0; j<F.cols(); ++j)
  52. t(j) = I(F(i,j));
  53. NF.row(i) = t;
  54. }
  55. }
  56. #ifndef IGL_HEADER_ONLY
  57. // Explicit template specialization
  58. #endif