peel_outer_hull_layers.cpp 4.5 KB

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