removeUnreferenced.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. {
  18. // Mark referenced vertices
  19. Eigen::MatrixXi mark = Eigen::MatrixXi::Zero(V.rows(),1);
  20. for(int i=0; i<F.rows(); ++i)
  21. {
  22. for(int j=0; j<F.cols(); ++j)
  23. {
  24. if (F(i,j) != -1)
  25. mark(F(i,j)) = 1;
  26. }
  27. }
  28. // Sum the occupied cells
  29. int newsize = mark.sum();
  30. NV.resize(newsize,V.cols());
  31. I.resize(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. NF.resize(F.rows(),F.cols());
  48. // Apply I on F
  49. for (int i=0; i<F.rows(); ++i)
  50. {
  51. Eigen::RowVectorXi t(F.cols());
  52. for (int j=0; j<F.cols(); ++j)
  53. t(j) = I(F(i,j));
  54. NF.row(i) = t;
  55. }
  56. }
  57. // template <typename T, typename S>
  58. // IGL_INLINE void removeUnreferenced(
  59. // const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V,
  60. // const vector<vector<S> > &F,
  61. // Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &NV,
  62. // const vector<vector<S> > &NF,
  63. // Eigen::Matrix<S, Eigen::Dynamic, 1> &I)
  64. //{
  65. // // Mark referenced vertices
  66. // Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> mark = Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>::Zero(V.rows(),1);
  67. //
  68. // for(int i=0; i<F.size(); ++i)
  69. // {
  70. // for(int j=0; j<F[i].size(); ++j)
  71. // {
  72. // if (F[i][j] != -1)
  73. // mark(F[i][j],0) = 1;
  74. // }
  75. // }
  76. //
  77. // // Sum the occupied cells
  78. // int newsize = mark.sum();
  79. //
  80. // NV = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>(newsize,V.cols());
  81. // NF.clear();
  82. // I = Eigen::Matrix<S, Eigen::Dynamic, 1>(V.rows(),1);
  83. //
  84. // // Do a pass on the marked vector and remove the unreferenced vertices
  85. // int count = 0;
  86. // for(int i=0;i<mark.rows();++i)
  87. // {
  88. // if (mark(i) == 1)
  89. // {
  90. // NV.row(count) = V.row(i);
  91. // I(i) = count;
  92. // count++;
  93. // }
  94. // else
  95. // {
  96. // I(i) = -1;
  97. // }
  98. // }
  99. //
  100. // // Apply I on F
  101. // for (int i=0; i<F.size(); ++i)
  102. // {
  103. // vector<S> t;
  104. // for (int j=0; j<F[i].size(); ++j)
  105. // t.push_back(I[F[i][j]]);
  106. //
  107. // vector<S> t_copy = t;
  108. // typename std::vector<S>::iterator it;
  109. // it = std::unique (t_copy.begin(), t_copy.end());
  110. // t_copy.resize( std::distance(t_copy.begin(),it) );
  111. //
  112. // if (t_copy.size() > 2)
  113. // NF.push_back(t);
  114. // }
  115. //}
  116. #ifndef IGL_HEADER_ONLY
  117. // Explicit template specialization
  118. #endif