removeUnreferenced.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // removeUnreferenced.h
  3. // Preview3D
  4. //
  5. // Created by Daniele Panozzo on 17/11/11.
  6. #ifndef IGL_REMOVEUNREFERENCED_H
  7. #define IGL_REMOVEUNREFERENCED_H
  8. #include <Eigen/Core>
  9. namespace igl
  10. {
  11. // [ NV, NF ] = removeUnreferenced( V,F,epsilon )
  12. // Remove unreferenced vertices from V, updating F accordingly
  13. //
  14. // Input:
  15. // V,F: mesh description
  16. //
  17. // Output:
  18. // NV, NF: new mesh without unreferenced vertices
  19. template <typename T>
  20. inline void removeUnreferenced(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V, const Eigen::MatrixXi &F, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &NV, Eigen::MatrixXi &NF, Eigen::VectorXi &I);
  21. }
  22. // Implementation
  23. template <typename T>
  24. inline void igl::removeUnreferenced(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V, const Eigen::MatrixXi &F, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &NV, Eigen::MatrixXi &NF, Eigen::VectorXi &I)
  25. {
  26. // Mark referenced vertices
  27. Eigen::MatrixXi mark = Eigen::MatrixXi::Zero(V.rows(),1);
  28. for(int i=0; i<F.rows(); ++i)
  29. {
  30. for(int j=0; j<F.cols(); ++j)
  31. {
  32. if (F(i,j) != -1)
  33. mark(F(i,j),0) = 1;
  34. }
  35. }
  36. // Sum the occupied cells
  37. int newsize = mark.sum();
  38. NV = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>(newsize,V.cols());
  39. NF = Eigen::MatrixXi(F.rows(),F.cols());
  40. I = Eigen::MatrixXi(V.rows(),1);
  41. // Do a pass on the marked vector and remove the unreferenced vertices
  42. int count = 0;
  43. for(int i=0;i<mark.rows();++i)
  44. {
  45. if (mark(i) == 1)
  46. {
  47. NV.row(count) = V.row(i);
  48. I(i) = count;
  49. count++;
  50. }
  51. else
  52. {
  53. I(i) = -1;
  54. }
  55. }
  56. // Apply I on F
  57. count = 0;
  58. for (int i =0; i<F.rows(); ++i)
  59. {
  60. int v0 = I[F(i,0)];
  61. int v1 = I[F(i,1)];
  62. int v2 = I[F(i,2)];
  63. if ( (v0 != v1) && (v2 != v1) && (v0 != v2) )
  64. NF.row(count++) << v0, v1, v2;
  65. }
  66. }
  67. #endif