remove_unreferenced.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Eigen::Matrix<typename DerivedI::Scalar,Eigen::Dynamic,1> J;
  23. remove_unreferenced(V,F,NV,NF,I,J);
  24. }
  25. template <
  26. typename DerivedV,
  27. typename DerivedF,
  28. typename DerivedNV,
  29. typename DerivedNF,
  30. typename DerivedI,
  31. typename DerivedJ>
  32. IGL_INLINE void igl::remove_unreferenced(
  33. const Eigen::PlainObjectBase<DerivedV> &V,
  34. const Eigen::PlainObjectBase<DerivedF> &F,
  35. Eigen::PlainObjectBase<DerivedNV> &NV,
  36. Eigen::PlainObjectBase<DerivedNF> &NF,
  37. Eigen::PlainObjectBase<DerivedI> &I,
  38. Eigen::PlainObjectBase<DerivedJ> &J)
  39. {
  40. const size_t n = V.rows();
  41. remove_unreferenced(n,F,I,J);
  42. NF = F;
  43. for_each(NF.data(),NF.data()+NF.size(),[&I](int & a){a=I(a);});
  44. slice(V,J,1,NV);
  45. }
  46. template <
  47. typename DerivedF,
  48. typename DerivedI,
  49. typename DerivedJ>
  50. IGL_INLINE void igl::remove_unreferenced(
  51. const size_t n,
  52. const Eigen::PlainObjectBase<DerivedF> &F,
  53. Eigen::PlainObjectBase<DerivedI> &I,
  54. Eigen::PlainObjectBase<DerivedJ> &J)
  55. {
  56. // Mark referenced vertices
  57. typedef Eigen::Matrix<bool,Eigen::Dynamic,1> MatrixXb;
  58. MatrixXb mark = MatrixXb::Zero(n,1);
  59. for(int i=0; i<F.rows(); ++i)
  60. {
  61. for(int j=0; j<F.cols(); ++j)
  62. {
  63. if (F(i,j) != -1)
  64. {
  65. mark(F(i,j)) = 1;
  66. }
  67. }
  68. }
  69. // Sum the occupied cells
  70. int newsize = mark.count();
  71. I.resize(n,1);
  72. J.resize(newsize,1);
  73. // Do a pass on the marked vector and remove the unreferenced vertices
  74. int count = 0;
  75. for(int i=0;i<mark.rows();++i)
  76. {
  77. if (mark(i) == 1)
  78. {
  79. I(i) = count;
  80. J(count) = i;
  81. count++;
  82. }
  83. else
  84. {
  85. I(i) = -1;
  86. }
  87. }
  88. }
  89. #ifdef IGL_STATIC_LIBRARY
  90. // Explicit template specialization
  91. 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> >&);
  92. 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> >&);
  93. 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> >&);
  94. #endif