peel_outer_hull_layers.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. template <
  124. typename Kernel,
  125. typename DerivedV,
  126. typename DerivedF,
  127. typename DerivedN,
  128. typename Derivedodd,
  129. typename Derivedflip>
  130. IGL_INLINE size_t igl::peel_outer_hull_layers_exact(
  131. const Eigen::PlainObjectBase<DerivedV > & V,
  132. const Eigen::PlainObjectBase<DerivedF > & F,
  133. const Eigen::PlainObjectBase<DerivedN > & N,
  134. Eigen::PlainObjectBase<Derivedodd > & odd,
  135. Eigen::PlainObjectBase<Derivedflip > & flip)
  136. {
  137. using namespace Eigen;
  138. using namespace std;
  139. typedef typename DerivedF::Index Index;
  140. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  141. typedef Matrix<typename DerivedN::Scalar,Dynamic,DerivedN::ColsAtCompileTime> MatrixXN;
  142. typedef Matrix<Index,Dynamic,1> MatrixXI;
  143. typedef Matrix<typename Derivedflip::Scalar,Dynamic,Derivedflip::ColsAtCompileTime> MatrixXflip;
  144. const Index m = F.rows();
  145. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  146. cout<<"peel outer hull layers..."<<endl;
  147. #endif
  148. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  149. cout<<"calling outer hull..."<<endl;
  150. writePLY(STR("peel-outer-hull-input.ply"),V,F);
  151. #endif
  152. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  153. cout<<"resize output ..."<<endl;
  154. #endif
  155. // keep track of iteration parity and whether flipped in hull
  156. MatrixXF Fr = F;
  157. MatrixXN Nr = N;
  158. odd.resize(m,1);
  159. flip.resize(m,1);
  160. // Keep track of index map
  161. MatrixXI IM = MatrixXI::LinSpaced(m,0,m-1);
  162. // This is O(n * layers)
  163. bool odd_iter = true;
  164. MatrixXI P(m,1);
  165. Index iter = 0;
  166. while(Fr.size() > 0)
  167. {
  168. assert(Fr.rows() == IM.rows());
  169. // Compute outer hull of current Fr
  170. MatrixXF Fo;
  171. MatrixXI Jo;
  172. MatrixXflip flipr;
  173. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  174. cout<<"calling outer hull..."<<endl;
  175. writePLY(STR("outer-hull-input-"<<iter<<".ply"),V,Fr);
  176. writeDMAT(STR("outer-hull-input-"<<iter<<".dmat"),Nr);
  177. #endif
  178. outer_hull_exact<Kernel>(V,Fr,Nr,Fo,Jo,flipr);
  179. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  180. writePLY(STR("outer-hull-output-"<<iter<<".ply"),V,Fo);
  181. cout<<"reindex, flip..."<<endl;
  182. #endif
  183. assert(Fo.rows() == Jo.rows());
  184. // all faces in Fo of Fr
  185. vector<bool> in_outer(Fr.rows(),false);
  186. for(Index g = 0;g<Jo.rows();g++)
  187. {
  188. odd(IM(Jo(g))) = odd_iter;
  189. P(IM(Jo(g))) = iter;
  190. in_outer[Jo(g)] = true;
  191. flip(IM(Jo(g))) = flipr(Jo(g));
  192. }
  193. // Fr = Fr - Fo
  194. // update IM
  195. MatrixXF prev_Fr = Fr;
  196. MatrixXN prev_Nr = Nr;
  197. MatrixXI prev_IM = IM;
  198. Fr.resize(prev_Fr.rows() - Fo.rows(),F.cols());
  199. Nr.resize(Fr.rows(),3);
  200. IM.resize(Fr.rows());
  201. {
  202. Index g = 0;
  203. for(Index f = 0;f<prev_Fr.rows();f++)
  204. {
  205. if(!in_outer[f])
  206. {
  207. Fr.row(g) = prev_Fr.row(f);
  208. Nr.row(g) = prev_Nr.row(f);
  209. IM(g) = prev_IM(f);
  210. g++;
  211. }
  212. }
  213. }
  214. odd_iter = !odd_iter;
  215. iter++;
  216. }
  217. return iter;
  218. }
  219. #ifdef IGL_STATIC_LIBRARY
  220. // Explicit template specialization
  221. 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> >&);
  222. 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> >&);
  223. #endif