peel_outer_hull_layers.cpp 4.6 KB

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