peel_outer_hull_layers.cpp 3.1 KB

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