removeUnreferenced.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 T, typename S>
  10. IGL_INLINE void igl::removeUnreferenced(
  11. const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V,
  12. const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> &F,
  13. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &NV,
  14. Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> &NF,
  15. Eigen::Matrix<S, Eigen::Dynamic, 1> &I)
  16. {
  17. // Mark referenced vertices
  18. Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> mark = Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>::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),0) = 1;
  25. }
  26. }
  27. // Sum the occupied cells
  28. int newsize = mark.sum();
  29. NV = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>(newsize,V.cols());
  30. NF = Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>(F.rows(),F.cols());
  31. I = Eigen::Matrix<S, Eigen::Dynamic, 1>(V.rows(),1);
  32. // Do a pass on the marked vector and remove the unreferenced vertices
  33. int count = 0;
  34. for(int i=0;i<mark.rows();++i)
  35. {
  36. if (mark(i) == 1)
  37. {
  38. NV.row(count) = V.row(i);
  39. I(i) = count;
  40. count++;
  41. }
  42. else
  43. {
  44. I(i) = -1;
  45. }
  46. }
  47. // Apply I on F
  48. // Why is this also removing combinatorially degenerate faces?
  49. count = 0;
  50. for (int i =0; i<F.rows(); ++i)
  51. {
  52. int v0 = I[F(i,0)];
  53. int v1 = I[F(i,1)];
  54. int v2 = I[F(i,2)];
  55. if ( (v0 != v1) && (v2 != v1) && (v0 != v2) )
  56. NF.row(count++) << v0, v1, v2;
  57. }
  58. }
  59. #ifndef IGL_HEADER_ONLY
  60. // Explicit template specialization
  61. #endif