peel_outer_hull_layers.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "peel_outer_hull_layers.h"
  2. #include "per_face_normals.h"
  3. #include "outer_hull.h"
  4. #include <vector>
  5. #include <iostream>
  6. //#define IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  7. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  8. #include "writePLY.h"
  9. #include "STR.h"
  10. #endif
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedN,
  15. typename Derivedodd,
  16. typename Derivedflip>
  17. IGL_INLINE size_t igl::peel_outer_hull_layers(
  18. const Eigen::PlainObjectBase<DerivedV > & V,
  19. const Eigen::PlainObjectBase<DerivedF > & F,
  20. const Eigen::PlainObjectBase<DerivedN > & N,
  21. Eigen::PlainObjectBase<Derivedodd > & odd,
  22. Eigen::PlainObjectBase<Derivedflip > & flip)
  23. {
  24. using namespace Eigen;
  25. using namespace std;
  26. typedef typename DerivedF::Index Index;
  27. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  28. typedef Matrix<typename DerivedN::Scalar,Dynamic,DerivedN::ColsAtCompileTime> MatrixXN;
  29. typedef Matrix<Index,Dynamic,1> MatrixXI;
  30. typedef Matrix<typename Derivedflip::Scalar,Dynamic,Derivedflip::ColsAtCompileTime> MatrixXflip;
  31. const Index m = F.rows();
  32. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  33. cout<<"peel outer hull layers..."<<endl;
  34. #endif
  35. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  36. cout<<"resize output ..."<<endl;
  37. #endif
  38. // keep track of iteration parity and whether flipped in hull
  39. MatrixXF Fr = F;
  40. MatrixXN Nr = N;
  41. odd.resize(m,1);
  42. flip.resize(m,1);
  43. // Keep track of index map
  44. MatrixXI IM = MatrixXI::LinSpaced(m,0,m-1);
  45. // This is O(n * layers)
  46. bool odd_iter = true;
  47. MatrixXI P(m,1);
  48. Index iter = 0;
  49. while(Fr.size() > 0)
  50. {
  51. assert(Fr.rows() == IM.rows());
  52. // Compute outer hull of current Fr
  53. MatrixXF Fo;
  54. MatrixXI Jo;
  55. MatrixXflip flipr;
  56. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  57. cout<<"calling outer hull..."<<endl;
  58. writePLY(STR("outer-hull-input-"<<iter<<".ply"),V,Fr);
  59. #endif
  60. outer_hull(V,Fr,Nr,Fo,Jo,flipr);
  61. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  62. writePLY(STR("outer-hull-output-"<<iter<<".ply"),V,Fo);
  63. cout<<"reindex, flip..."<<endl;
  64. #endif
  65. assert(Fo.rows() == Jo.rows());
  66. // all faces in Fo of Fr
  67. vector<bool> in_outer(Fr.rows(),false);
  68. for(Index g = 0;g<Jo.rows();g++)
  69. {
  70. odd(IM(Jo(g))) = odd_iter;
  71. P(IM(Jo(g))) = iter;
  72. in_outer[Jo(g)] = true;
  73. flip(IM(Jo(g))) = flipr(Jo(g));
  74. }
  75. // Fr = Fr - Fo
  76. // update IM
  77. MatrixXF prev_Fr = Fr;
  78. MatrixXN prev_Nr = Nr;
  79. MatrixXI prev_IM = IM;
  80. Fr.resize(prev_Fr.rows() - Fo.rows(),F.cols());
  81. Nr.resize(Fr.rows(),3);
  82. IM.resize(Fr.rows());
  83. {
  84. Index g = 0;
  85. for(Index f = 0;f<prev_Fr.rows();f++)
  86. {
  87. if(!in_outer[f])
  88. {
  89. Fr.row(g) = prev_Fr.row(f);
  90. Nr.row(g) = prev_Nr.row(f);
  91. IM(g) = prev_IM(f);
  92. g++;
  93. }
  94. }
  95. }
  96. odd_iter = !odd_iter;
  97. iter++;
  98. }
  99. return iter;
  100. }
  101. template <
  102. typename DerivedV,
  103. typename DerivedF,
  104. typename Derivedodd,
  105. typename Derivedflip>
  106. IGL_INLINE size_t igl::peel_outer_hull_layers(
  107. const Eigen::PlainObjectBase<DerivedV > & V,
  108. const Eigen::PlainObjectBase<DerivedF > & F,
  109. Eigen::PlainObjectBase<Derivedodd > & odd,
  110. Eigen::PlainObjectBase<Derivedflip > & flip)
  111. {
  112. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  113. per_face_normals(V,F,N);
  114. return peel_outer_hull_layers(V,F,N,odd,flip);
  115. }
  116. #ifdef IGL_STATIC_LIBRARY
  117. // Explicit template specialization
  118. template size_t igl::peel_outer_hull_layers<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -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<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  119. template size_t igl::peel_outer_hull_layers<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<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  120. #endif