peal_outer_hull_layers.cpp 2.9 KB

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