peel_outer_hull_layers.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "outer_hull.h"
  10. #include "../../LinSpaced.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 DerivedI,
  23. typename Derivedflip>
  24. IGL_INLINE size_t igl::copyleft::cgal::peel_outer_hull_layers(
  25. const Eigen::PlainObjectBase<DerivedV > & V,
  26. const Eigen::PlainObjectBase<DerivedF > & F,
  27. Eigen::PlainObjectBase<DerivedI> & I,
  28. Eigen::PlainObjectBase<Derivedflip > & flip)
  29. {
  30. using namespace Eigen;
  31. using namespace std;
  32. typedef typename DerivedF::Index Index;
  33. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  34. typedef Matrix<Index,Dynamic,1> MatrixXI;
  35. typedef Matrix<typename Derivedflip::Scalar,Dynamic,Derivedflip::ColsAtCompileTime> MatrixXflip;
  36. const Index m = F.rows();
  37. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  38. cout<<"peel outer hull layers..."<<endl;
  39. #endif
  40. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  41. cout<<"calling outer hull..."<<endl;
  42. writePLY(STR("peel-outer-hull-input.ply"),V,F);
  43. #endif
  44. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  45. cout<<"resize output ..."<<endl;
  46. #endif
  47. // keep track of iteration parity and whether flipped in hull
  48. MatrixXF Fr = F;
  49. I.resize(m,1);
  50. flip.resize(m,1);
  51. // Keep track of index map
  52. MatrixXI IM = igl::LinSpaced<MatrixXI >(m,0,m-1);
  53. // This is O(n * layers)
  54. MatrixXI P(m,1);
  55. Index iter = 0;
  56. while(Fr.size() > 0)
  57. {
  58. assert(Fr.rows() == IM.rows());
  59. // Compute outer hull of current Fr
  60. MatrixXF Fo;
  61. MatrixXI Jo;
  62. MatrixXflip flipr;
  63. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  64. {
  65. cout<<"calling outer hull..." << iter <<endl;
  66. std::stringstream ss;
  67. ss << "outer_hull_" << iter << ".ply";
  68. Eigen::MatrixXd vertices(V.rows(), V.cols());
  69. std::transform(V.data(), V.data() + V.rows()*V.cols(),
  70. vertices.data(),
  71. [](typename DerivedV::Scalar val)
  72. {return CGAL::to_double(val); });
  73. writePLY(ss.str(), vertices, Fr);
  74. }
  75. #endif
  76. outer_hull_legacy(V,Fr,Fo,Jo,flipr);
  77. #ifdef IGL_PEEL_OUTER_HULL_LAYERS_DEBUG
  78. writePLY(STR("outer-hull-output-"<<iter<<".ply"),V,Fo);
  79. cout<<"reindex, flip..."<<endl;
  80. #endif
  81. assert(Fo.rows() != 0);
  82. assert(Fo.rows() == Jo.rows());
  83. // all faces in Fo of Fr
  84. vector<bool> in_outer(Fr.rows(),false);
  85. for(Index g = 0;g<Jo.rows();g++)
  86. {
  87. I(IM(Jo(g))) = iter;
  88. P(IM(Jo(g))) = iter;
  89. in_outer[Jo(g)] = true;
  90. flip(IM(Jo(g))) = flipr(Jo(g));
  91. }
  92. // Fr = Fr - Fo
  93. // update IM
  94. MatrixXF prev_Fr = Fr;
  95. MatrixXI prev_IM = IM;
  96. Fr.resize(prev_Fr.rows() - Fo.rows(),F.cols());
  97. IM.resize(Fr.rows());
  98. {
  99. Index g = 0;
  100. for(Index f = 0;f<prev_Fr.rows();f++)
  101. {
  102. if(!in_outer[f])
  103. {
  104. Fr.row(g) = prev_Fr.row(f);
  105. IM(g) = prev_IM(f);
  106. g++;
  107. }
  108. }
  109. }
  110. iter++;
  111. }
  112. return iter;
  113. }
  114. #ifdef IGL_STATIC_LIBRARY
  115. // Explicit template instantiation
  116. // generated by autoexplicit.sh
  117. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  118. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  119. template unsigned long igl::copyleft::cgal::peel_outer_hull_layers<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  120. template size_t igl::copyleft::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> >&);
  121. #ifdef WIN32
  122. template unsigned __int64 igl::copyleft::cgal::peel_outer_hull_layers<class Eigen::Matrix<class CGAL::Lazy_exact_nt<class CGAL::Gmpq>,-1,-1,0,-1,-1>,class Eigen::Matrix<int,-1,-1,0,-1,-1>,class Eigen::Matrix<int,-1,1,0,-1,1>,class Eigen::Matrix<int,-1,1,0,-1,1> >(class Eigen::PlainObjectBase<class Eigen::Matrix<class CGAL::Lazy_exact_nt<class CGAL::Gmpq>,-1,-1,0,-1,-1> > const &,class Eigen::PlainObjectBase<class Eigen::Matrix<int,-1,-1,0,-1,-1> > const &,class Eigen::PlainObjectBase<class Eigen::Matrix<int,-1,1,0,-1,1> > &,class Eigen::PlainObjectBase<class Eigen::Matrix<int,-1,1,0,-1,1> > &);
  123. #endif
  124. #endif