peel_outer_hull_layers.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "peel_outer_hull_layers.h"
  9. #include "../per_face_normals.h"
  10. #include "outer_hull.h"
  11. #include <vector>
  12. #include <iostream>
  13. //#define IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  14. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  15. #include "../writePLY.h"
  16. #include "../writeDMAT.h"
  17. #include "../STR.h"
  18. #endif
  19. template <
  20. typename DerivedV,
  21. typename DerivedF,
  22. typename DerivedN,
  23. typename DerivedI,
  24. typename Derivedflip>
  25. IGL_INLINE size_t igl::cgal::peel_outer_hull_layers(
  26. const Eigen::PlainObjectBase<DerivedV > & V,
  27. const Eigen::PlainObjectBase<DerivedF > & F,
  28. const Eigen::PlainObjectBase<DerivedN > & N,
  29. Eigen::PlainObjectBase<DerivedI> & I,
  30. Eigen::PlainObjectBase<Derivedflip > & flip)
  31. {
  32. using namespace Eigen;
  33. using namespace std;
  34. typedef typename DerivedF::Index Index;
  35. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  36. typedef Matrix<typename DerivedN::Scalar,Dynamic,DerivedN::ColsAtCompileTime> MatrixXN;
  37. typedef Matrix<Index,Dynamic,1> MatrixXI;
  38. typedef Matrix<typename Derivedflip::Scalar,Dynamic,Derivedflip::ColsAtCompileTime> MatrixXflip;
  39. const Index m = F.rows();
  40. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  41. cout<<"peel outer hull layers..."<<endl;
  42. #endif
  43. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  44. cout<<"calling outer hull..."<<endl;
  45. writePLY(STR("peel-outer-hull-input.ply"),V,F);
  46. #endif
  47. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  48. cout<<"resize output ..."<<endl;
  49. #endif
  50. // keep track of iteration parity and whether flipped in hull
  51. MatrixXF Fr = F;
  52. MatrixXN Nr = N;
  53. I.resize(m,1);
  54. flip.resize(m,1);
  55. // Keep track of index map
  56. MatrixXI IM = MatrixXI::LinSpaced(m,0,m-1);
  57. // This is O(n * layers)
  58. MatrixXI P(m,1);
  59. Index iter = 0;
  60. while(Fr.size() > 0)
  61. {
  62. assert(Fr.rows() == IM.rows());
  63. // Compute outer hull of current Fr
  64. MatrixXF Fo;
  65. MatrixXI Jo;
  66. MatrixXflip flipr;
  67. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  68. cout<<"calling outer hull..."<<endl;
  69. writePLY(STR("outer-hull-input-"<<iter<<".ply"),V,Fr);
  70. writeDMAT(STR("outer-hull-input-"<<iter<<".dmat"),Nr);
  71. #endif
  72. outer_hull(V,Fr,Nr,Fo,Jo,flipr);
  73. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  74. writePLY(STR("outer-hull-output-"<<iter<<".ply"),V,Fo);
  75. cout<<"reindex, flip..."<<endl;
  76. #endif
  77. assert(Fo.rows() == Jo.rows());
  78. // all faces in Fo of Fr
  79. vector<bool> in_outer(Fr.rows(),false);
  80. for(Index g = 0;g<Jo.rows();g++)
  81. {
  82. I(IM(Jo(g))) = iter;
  83. P(IM(Jo(g))) = iter;
  84. in_outer[Jo(g)] = true;
  85. flip(IM(Jo(g))) = flipr(Jo(g));
  86. }
  87. // Fr = Fr - Fo
  88. // update IM
  89. MatrixXF prev_Fr = Fr;
  90. MatrixXN prev_Nr = Nr;
  91. MatrixXI prev_IM = IM;
  92. Fr.resize(prev_Fr.rows() - Fo.rows(),F.cols());
  93. Nr.resize(Fr.rows(),3);
  94. IM.resize(Fr.rows());
  95. {
  96. Index g = 0;
  97. for(Index f = 0;f<prev_Fr.rows();f++)
  98. {
  99. if(!in_outer[f])
  100. {
  101. Fr.row(g) = prev_Fr.row(f);
  102. Nr.row(g) = prev_Nr.row(f);
  103. IM(g) = prev_IM(f);
  104. g++;
  105. }
  106. }
  107. }
  108. iter++;
  109. }
  110. return iter;
  111. }
  112. template <
  113. typename DerivedV,
  114. typename DerivedF,
  115. typename DerivedI,
  116. typename Derivedflip>
  117. IGL_INLINE size_t igl::cgal::peel_outer_hull_layers(
  118. const Eigen::PlainObjectBase<DerivedV > & V,
  119. const Eigen::PlainObjectBase<DerivedF > & F,
  120. Eigen::PlainObjectBase<DerivedI > & I,
  121. Eigen::PlainObjectBase<Derivedflip > & flip)
  122. {
  123. using namespace std;
  124. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  125. per_face_normals(V,F,N);
  126. return peel_outer_hull_layers(V,F,N,I,flip);
  127. }
  128. #ifdef IGL_STATIC_LIBRARY
  129. // Explicit template specialization
  130. template size_t igl::cgal::peel_outer_hull_layers<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -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<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  131. template size_t igl::cgal::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<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -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<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  132. #endif