removeUnreferenced.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // removeUnreferenced.h
  3. // Preview3D
  4. //
  5. // Created by Daniele Panozzo on 17/11/11.
  6. #ifndef RemoveUnreferenced_h
  7. #define 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. void removeUnreferenced(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Eigen::MatrixXd &NV, Eigen::MatrixXi &NF, Eigen::VectorXi &I);
  20. }
  21. // Implementation
  22. inline void igl::removeUnreferenced(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Eigen::MatrixXd &NV, Eigen::MatrixXi &NF, Eigen::VectorXi &I)
  23. {
  24. // Mark referenced vertices
  25. Eigen::MatrixXi mark = Eigen::MatrixXi::Zero(V.rows(),1);
  26. for(int i=0; i<F.rows(); ++i)
  27. {
  28. for(int j=0; j<F.cols(); ++j)
  29. {
  30. if (F(i,j) != -1)
  31. mark(F(i,j),0) = 1;
  32. }
  33. }
  34. // Sum the occupied cells
  35. int newsize = mark.sum();
  36. NV = Eigen::MatrixXd(newsize,V.cols());
  37. NF = Eigen::MatrixXi(F.rows(),F.cols());
  38. I = Eigen::MatrixXi(V.rows(),1);
  39. // Do a pass on the marked vector and remove the unreferenced vertices
  40. int count = 0;
  41. for(int i=0;i<mark.rows();++i)
  42. {
  43. if (mark(i) == 1)
  44. {
  45. NV.row(count) = V.row(i);
  46. I(i) = count;
  47. count++;
  48. }
  49. else
  50. {
  51. I(i) = -1;
  52. }
  53. }
  54. // Apply I on F
  55. count = 0;
  56. for (int i =0; i<F.rows(); ++i)
  57. {
  58. int v0 = I[F(i,0)];
  59. int v1 = I[F(i,1)];
  60. int v2 = I[F(i,2)];
  61. if ( (v0 != v1) && (v2 != v1) && (v0 != v2) )
  62. NF.row(count++) << v0, v1, v2;
  63. }
  64. }
  65. #endif