remove_unreferenced.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(),[&I](int & a){a=I(a);});
  47. slice(V,J,1,NV);
  48. }
  49. template <
  50. typename DerivedF,
  51. typename DerivedI,
  52. typename DerivedJ>
  53. IGL_INLINE void igl::remove_unreferenced(
  54. const size_t n,
  55. const Eigen::PlainObjectBase<DerivedF> &F,
  56. Eigen::PlainObjectBase<DerivedI> &I,
  57. Eigen::PlainObjectBase<DerivedJ> &J)
  58. {
  59. // Mark referenced vertices
  60. typedef Eigen::Matrix<bool,Eigen::Dynamic,1> MatrixXb;
  61. MatrixXb mark = MatrixXb::Zero(n,1);
  62. for(int i=0; i<F.rows(); ++i)
  63. {
  64. for(int j=0; j<F.cols(); ++j)
  65. {
  66. if (F(i,j) != -1)
  67. {
  68. mark(F(i,j)) = 1;
  69. }
  70. }
  71. }
  72. // Sum the occupied cells
  73. int newsize = mark.count();
  74. I.resize(n,1);
  75. J.resize(newsize,1);
  76. // Do a pass on the marked vector and remove the unreferenced vertices
  77. int count = 0;
  78. for(int i=0;i<mark.rows();++i)
  79. {
  80. if (mark(i) == 1)
  81. {
  82. I(i) = count;
  83. J(count) = i;
  84. count++;
  85. }
  86. else
  87. {
  88. I(i) = -1;
  89. }
  90. }
  91. }
  92. #ifdef IGL_STATIC_LIBRARY
  93. // Explicit template specialization
  94. 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> >&);
  95. 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> >&);
  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, -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> >&);
  97. #endif