cut_mesh_from_singularities.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <igl/triangle_triangle_adjacency.h>
  10. #include <vector>
  11. #include <deque>
  12. namespace igl {
  13. template <
  14. typename DerivedV,
  15. typename DerivedF,
  16. typename DerivedM,
  17. typename DerivedS,
  18. typename DerivedO
  19. >
  20. class MeshCutter
  21. {
  22. protected:
  23. const Eigen::PlainObjectBase<DerivedV> &V;
  24. const Eigen::PlainObjectBase<DerivedF> &F;
  25. const Eigen::PlainObjectBase<DerivedS> &Handle_Singular;
  26. const Eigen::PlainObjectBase<DerivedS> &Handle_SingularDegree;
  27. const Eigen::PlainObjectBase<DerivedM> &Handle_MMatch;
  28. Eigen::VectorXi F_visited;
  29. Eigen::PlainObjectBase<DerivedF> TT;
  30. Eigen::PlainObjectBase<DerivedF> TTi;
  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. bool over=true;
  81. int guard = 0;
  82. do
  83. {
  84. over = true;
  85. for (int f = 0; f<F.rows(); f++) //if (!f->IsD())
  86. {
  87. for (int s = 0; s<3; s++)
  88. {
  89. if (Handle_Seams(f,s))
  90. if (!(IsRotSeam(f,s))) // never retract rot seams
  91. {
  92. if (e[ F(f,s) ] == 1) {
  93. // dissolve seam
  94. Handle_Seams(f,s)=false;
  95. if (TT(f,s) != -1)
  96. Handle_Seams(TT(f,s),TTi(f,s))=false;
  97. e[ F(f,s)] --;
  98. e[ F(f,(s+1)%3) ] --;
  99. over = false;
  100. }
  101. }
  102. }
  103. }
  104. if (guard++>10000)
  105. over = true;
  106. } while (!over);
  107. }
  108. public:
  109. inline MeshCutter(const Eigen::PlainObjectBase<DerivedV> &V_,
  110. const Eigen::PlainObjectBase<DerivedF> &F_,
  111. const Eigen::PlainObjectBase<DerivedM> &Handle_MMatch_,
  112. const Eigen::PlainObjectBase<DerivedS> &Handle_Singular_,
  113. const Eigen::PlainObjectBase<DerivedS> &Handle_SingularDegree_):
  114. V(V_),
  115. F(F_),
  116. Handle_MMatch(Handle_MMatch_),
  117. Handle_Singular(Handle_Singular_),
  118. Handle_SingularDegree(Handle_SingularDegree_)
  119. {
  120. triangle_triangle_adjacency(V,F,TT,TTi);
  121. };
  122. inline void cut(Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  123. {
  124. F_visited.setConstant(F.rows(),0);
  125. Handle_Seams.setConstant(F.rows(),3,1);
  126. int index=0;
  127. for (unsigned f = 0; f<F.rows(); f++)
  128. {
  129. if (!F_visited(f))
  130. {
  131. index++;
  132. FloodFill(f, Handle_Seams);
  133. }
  134. }
  135. Retract(Handle_Seams);
  136. for (unsigned int f=0;f<F.rows();f++)
  137. for (int j=0;j<3;j++)
  138. if (IsRotSeam(f,j))
  139. Handle_Seams(f,j)=true;
  140. }
  141. };
  142. }
  143. template <typename DerivedV,
  144. typename DerivedF,
  145. typename DerivedM,
  146. typename DerivedS,
  147. typename DerivedO>
  148. IGL_INLINE void igl::cut_mesh_from_singularities(const Eigen::PlainObjectBase<DerivedV> &V,
  149. const Eigen::PlainObjectBase<DerivedF> &F,
  150. const Eigen::PlainObjectBase<DerivedM> &Handle_MMatch,
  151. const Eigen::PlainObjectBase<DerivedS> &isSingularity,
  152. const Eigen::PlainObjectBase<DerivedS> &singularityIndex,
  153. Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  154. {
  155. igl::MeshCutter< DerivedV, DerivedF, DerivedM, DerivedS, DerivedO> mc(V, F, Handle_MMatch, isSingularity, singularityIndex);
  156. mc.cut(Handle_Seams);
  157. }
  158. #ifdef IGL_STATIC_LIBRARY
  159. // Explicit template specialization
  160. 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, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(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, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  161. #endif