peel_outer_hull_layers.cpp 3.0 KB

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