remove_unreferenced.cpp 3.3 KB

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