cut_mesh_from_singularities.cpp 5.0 KB

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