cut_mesh_from_singularities.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <igl/edge_topology.h>
  11. #include <vector>
  12. #include <deque>
  13. namespace igl {
  14. template <
  15. typename DerivedV,
  16. typename DerivedF,
  17. typename DerivedM,
  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<DerivedM> &Handle_MMatch;
  26. Eigen::VectorXi F_visited;
  27. #warning "Constructing Eigen::PlainObjectBase directly is deprecated"
  28. Eigen::PlainObjectBase<DerivedF> TT;
  29. Eigen::PlainObjectBase<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::PlainObjectBase<DerivedV> &V_,
  126. const Eigen::PlainObjectBase<DerivedF> &F_,
  127. const Eigen::PlainObjectBase<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. int index=0;
  140. for (unsigned f = 0; f<F.rows(); f++)
  141. {
  142. if (!F_visited(f))
  143. {
  144. index++;
  145. FloodFill(f, Handle_Seams);
  146. }
  147. }
  148. Retract(Handle_Seams);
  149. for (unsigned int f=0;f<F.rows();f++)
  150. for (int j=0;j<3;j++)
  151. if (IsRotSeam(f,j))
  152. Handle_Seams(f,j)=true;
  153. }
  154. };
  155. }
  156. template <typename DerivedV,
  157. typename DerivedF,
  158. typename DerivedM,
  159. typename DerivedO>
  160. IGL_INLINE void igl::cut_mesh_from_singularities(const Eigen::PlainObjectBase<DerivedV> &V,
  161. const Eigen::PlainObjectBase<DerivedF> &F,
  162. const Eigen::PlainObjectBase<DerivedM> &Handle_MMatch,
  163. Eigen::PlainObjectBase<DerivedO> &Handle_Seams)
  164. {
  165. igl::MeshCutter< DerivedV, DerivedF, DerivedM, DerivedO> mc(V, F, Handle_MMatch);
  166. mc.cut(Handle_Seams);
  167. }
  168. #ifdef IGL_STATIC_LIBRARY
  169. // Explicit template specialization
  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, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  171. 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::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<int, -1, -1, 0, -1, -1> >&);
  172. 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::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, 3, 0, -1, 3> >&);
  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<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  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, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  175. 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::PlainObjectBase<Eigen::Matrix<double, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  176. #endif