cut_mesh_from_singularities.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>, Olga Diamanti <[email protected]>
  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 "cut_mesh_from_singularities.h"
  9. #include "triangle_triangle_adjacency.h"
  10. #include "edge_topology.h"
  11. #include "PlainMatrix.h"
  12. #include <vector>
  13. #include <deque>
  14. namespace igl {
  15. template <
  16. typename DerivedV,
  17. typename DerivedF,
  18. typename DerivedM,
  19. typename DerivedO
  20. >
  21. class MeshCutter
  22. {
  23. protected:
  24. const Eigen::MatrixBase<DerivedV> &V;
  25. const Eigen::MatrixBase<DerivedF> &F;
  26. const Eigen::MatrixBase<DerivedM> &Handle_MMatch;
  27. Eigen::VectorXi F_visited;
  28. PlainMatrix<DerivedF> TT;
  29. PlainMatrix<DerivedF> TTi;
  30. Eigen::MatrixXi E, F2E, E2F;
  31. protected:
  32. inline bool IsRotSeam(const int f0,const int edge)
  33. {
  34. unsigned char MM = Handle_MMatch(f0,edge);
  35. return (MM!=0);
  36. }
  37. inline void FloodFill(const int start, Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  38. {
  39. std::deque<int> d;
  40. ///clean the visited flag
  41. F_visited(start) = true;
  42. d.push_back(start);
  43. while (!d.empty())
  44. {
  45. int f = d.at(0); d.pop_front();
  46. for (int s = 0; s<3; s++)
  47. {
  48. int g = TT(f,s); // f->FFp(s);
  49. int j = TTi(f,s); // f->FFi(s);
  50. if (j == -1)
  51. {
  52. g = f;
  53. j = s;
  54. }
  55. if ((!(IsRotSeam(f,s))) && (!(IsRotSeam(g,j))) && (!F_visited(g)) )
  56. {
  57. Handle_Seams(f,s)=false;
  58. Handle_Seams(g,j)=false;
  59. F_visited(g) = true;
  60. d.push_back(g);
  61. }
  62. }
  63. }
  64. }
  65. inline void Retract(Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  66. {
  67. std::vector<int> e(V.rows(),0); // number of edges per vert
  68. // for (unsigned f=0; f<F.rows(); f++)
  69. // {
  70. // for (int s = 0; s<3; s++)
  71. // {
  72. // if (Handle_Seams(f,s))
  73. // if (TT(f,s)<=f)
  74. // {
  75. // e[ F(f,s) ] ++;
  76. // e[ F(f,(s+1)%3) ] ++;
  77. // }
  78. // }
  79. // }
  80. for (int ei=0; ei<E.rows(); ++ei)
  81. {
  82. //only need one face
  83. int f0 = E2F(ei,0);
  84. if (f0==-1)
  85. f0 = E2F(ei,1);
  86. int k=0;
  87. for (k=0; k<3; ++k)
  88. if (F2E(f0,k)==ei)
  89. break;
  90. if (Handle_Seams(f0,k))
  91. {
  92. e[ F(f0,k) ] ++;
  93. e[ F(f0,(k+1)%3) ] ++;
  94. }
  95. }
  96. bool over=true;
  97. int guard = 0;
  98. do
  99. {
  100. over = true;
  101. for (int f = 0; f<F.rows(); f++) //if (!f->IsD())
  102. {
  103. for (int s = 0; s<3; s++)
  104. {
  105. if (Handle_Seams(f,s))
  106. if (!(IsRotSeam(f,s))) // never retract rot seams
  107. {
  108. if (e[ F(f,s) ] == 1) {
  109. // dissolve seam
  110. Handle_Seams(f,s)=false;
  111. if (TT(f,s) != -1)
  112. Handle_Seams(TT(f,s),TTi(f,s))=false;
  113. e[ F(f,s)] --;
  114. e[ F(f,(s+1)%3) ] --;
  115. over = false;
  116. }
  117. }
  118. }
  119. }
  120. if (guard++>10000)
  121. over = true;
  122. } while (!over);
  123. }
  124. public:
  125. inline MeshCutter(const Eigen::MatrixBase<DerivedV> &V_,
  126. const Eigen::MatrixBase<DerivedF> &F_,
  127. const Eigen::MatrixBase<DerivedM> &Handle_MMatch_):
  128. V(V_),
  129. F(F_),
  130. Handle_MMatch(Handle_MMatch_)
  131. {
  132. triangle_triangle_adjacency(F,TT,TTi);
  133. edge_topology(V,F,E,F2E,E2F);
  134. };
  135. inline void cut(Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  136. {
  137. F_visited.setConstant(F.rows(),0);
  138. Handle_Seams.setConstant(F.rows(),3,1);
  139. for (unsigned f = 0; f<F.rows(); f++)
  140. {
  141. if (!F_visited(f))
  142. {
  143. FloodFill(f, Handle_Seams);
  144. }
  145. }
  146. Retract(Handle_Seams);
  147. for (unsigned int f=0;f<F.rows();f++)
  148. for (int j=0;j<3;j++)
  149. if (IsRotSeam(f,j))
  150. Handle_Seams(f,j)=true;
  151. }
  152. };
  153. }
  154. template <typename DerivedV,
  155. typename DerivedF,
  156. typename DerivedM,
  157. typename DerivedO>
  158. IGL_INLINE void igl::cut_mesh_from_singularities(const Eigen::MatrixBase<DerivedV> &V,
  159. const Eigen::MatrixBase<DerivedF> &F,
  160. const Eigen::MatrixBase<DerivedM> &Handle_MMatch,
  161. Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  162. {
  163. igl::MeshCutter< DerivedV, DerivedF, DerivedM, DerivedO> mc(V, F, Handle_MMatch);
  164. mc.cut(Handle_Seams);
  165. }
  166. #ifdef IGL_STATIC_LIBRARY
  167. // Explicit template instantiation
  168. template void igl::cut_mesh_from_singularities<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::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  169. template void igl::cut_mesh_from_singularities<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::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  170. template void igl::cut_mesh_from_singularities<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  171. template void igl::cut_mesh_from_singularities<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  172. template void igl::cut_mesh_from_singularities<Eigen::Matrix<double, -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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  173. template void igl::cut_mesh_from_singularities<Eigen::Matrix<double, -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, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  174. template void igl::cut_mesh_from_singularities<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  175. #endif