remove_unreferenced.cpp 4.7 KB

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