peel_outer_hull_layers.cpp 5.0 KB

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