cut_mesh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include <igl/cut_mesh.h>
  2. #include <igl/vertex_triangle_adjacency.h>
  3. #include <igl/triangle_triangle_adjacency.h>
  4. #include <igl/is_border_vertex.h>
  5. #include <igl/HalfEdgeIterator.h>
  6. #include <set>
  7. namespace igl {
  8. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  9. class MeshCutterMini
  10. {
  11. public:
  12. // Input
  13. //mesh
  14. const Eigen::PlainObjectBase<DerivedV> &V;
  15. const Eigen::PlainObjectBase<DerivedF> &F;
  16. const Eigen::PlainObjectBase<DerivedTT> &TT;
  17. const Eigen::PlainObjectBase<DerivedTT> &TTi;
  18. const std::vector<std::vector<VFType> >& VF;
  19. const std::vector<std::vector<VFType> >& VFi;
  20. const std::vector<bool> &V_border; // bool
  21. //edges to cut
  22. const Eigen::PlainObjectBase<DerivedC> &Handle_Seams; // 3 bool
  23. // total number of scalar variables
  24. int num_scalar_variables;
  25. // per face indexes of vertex in the solver
  26. Eigen::PlainObjectBase<DerivedF> HandleS_Index;
  27. // per vertex variable indexes
  28. std::vector<std::vector<int> > HandleV_Integer;
  29. IGL_INLINE MeshCutterMini(const Eigen::PlainObjectBase<DerivedV> &_V,
  30. const Eigen::PlainObjectBase<DerivedF> &_F,
  31. const Eigen::PlainObjectBase<DerivedTT> &_TT,
  32. const Eigen::PlainObjectBase<DerivedTT> &_TTi,
  33. const std::vector<std::vector<VFType> > &_VF,
  34. const std::vector<std::vector<VFType> > &_VFi,
  35. const std::vector<bool> &_V_border,
  36. const Eigen::PlainObjectBase<DerivedC> &_Handle_Seams);
  37. // vertex to variable mapping
  38. // initialize the mapping for a given sampled mesh
  39. IGL_INLINE void InitMappingSeam();
  40. private:
  41. IGL_INLINE void FirstPos(const int v, int &f, int &edge);
  42. IGL_INLINE int AddNewIndex(const int v0);
  43. IGL_INLINE bool IsSeam(const int f0, const int f1);
  44. // find initial position of the pos to
  45. // assing face to vert inxex correctly
  46. IGL_INLINE void FindInitialPos(const int vert, int &edge, int &face);
  47. // intialize the mapping given an initial pos
  48. // whih must be initialized with FindInitialPos
  49. IGL_INLINE void MapIndexes(const int vert, const int edge_init, const int f_init);
  50. // intialize the mapping for a given vertex
  51. IGL_INLINE void InitMappingSeam(const int vert);
  52. };
  53. }
  54. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  55. IGL_INLINE igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  56. MeshCutterMini(const Eigen::PlainObjectBase<DerivedV> &_V,
  57. const Eigen::PlainObjectBase<DerivedF> &_F,
  58. const Eigen::PlainObjectBase<DerivedTT> &_TT,
  59. const Eigen::PlainObjectBase<DerivedTT> &_TTi,
  60. const std::vector<std::vector<VFType> > &_VF,
  61. const std::vector<std::vector<VFType> > &_VFi,
  62. const std::vector<bool> &_V_border,
  63. const Eigen::PlainObjectBase<DerivedC> &_Handle_Seams):
  64. V(_V),
  65. F(_F),
  66. TT(_TT),
  67. TTi(_TTi),
  68. VF(_VF),
  69. VFi(_VFi),
  70. V_border(_V_border),
  71. Handle_Seams(_Handle_Seams)
  72. {
  73. num_scalar_variables=0;
  74. HandleS_Index.setConstant(F.rows(),3,-1);
  75. HandleV_Integer.resize(V.rows());
  76. }
  77. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  78. IGL_INLINE void igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  79. FirstPos(const int v, int &f, int &edge)
  80. {
  81. f = VF[v][0]; // f=v->cVFp();
  82. edge = VFi[v][0]; // edge=v->cVFi();
  83. }
  84. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  85. IGL_INLINE int igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  86. AddNewIndex(const int v0)
  87. {
  88. num_scalar_variables++;
  89. HandleV_Integer[v0].push_back(num_scalar_variables);
  90. return num_scalar_variables;
  91. }
  92. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  93. IGL_INLINE bool igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  94. IsSeam(const int f0, const int f1)
  95. {
  96. for (int i=0;i<3;i++)
  97. {
  98. int f_clos = TT(f0,i);
  99. if (f_clos == -1)
  100. continue; ///border
  101. if (f_clos == f1)
  102. return(Handle_Seams(f0,i));
  103. }
  104. assert(0);
  105. return false;
  106. }
  107. ///find initial position of the pos to
  108. // assing face to vert inxex correctly
  109. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  110. IGL_INLINE void igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  111. FindInitialPos(const int vert,
  112. int &edge,
  113. int &face)
  114. {
  115. int f_init;
  116. int edge_init;
  117. FirstPos(vert,f_init,edge_init); // todo manually the function
  118. igl::HalfEdgeIterator<DerivedF> VFI(F,TT,TTi,f_init,edge_init);
  119. bool vertexB = V_border[vert];
  120. bool possible_split=false;
  121. bool complete_turn=false;
  122. do
  123. {
  124. int curr_f = VFI.Fi();
  125. int curr_edge=VFI.Ei();
  126. VFI.NextFE();
  127. int next_f=VFI.Fi();
  128. ///test if I've just crossed a border
  129. bool on_border=(TT(curr_f,curr_edge)==-1);
  130. //bool mismatch=false;
  131. bool seam=false;
  132. ///or if I've just crossed a seam
  133. ///if I'm on a border I MUST start from the one next t othe border
  134. if (!vertexB)
  135. //seam=curr_f->IsSeam(next_f);
  136. seam=IsSeam(curr_f,next_f);
  137. // if (vertexB)
  138. // assert(!Handle_Singular(vert));
  139. // ;
  140. //assert(!vert->IsSingular());
  141. possible_split=((on_border)||(seam));
  142. complete_turn = next_f == f_init;
  143. } while ((!possible_split)&&(!complete_turn));
  144. face=VFI.Fi();
  145. edge=VFI.Ei();
  146. }
  147. ///intialize the mapping given an initial pos
  148. ///whih must be initialized with FindInitialPos
  149. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  150. IGL_INLINE void igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  151. MapIndexes(const int vert,
  152. const int edge_init,
  153. const int f_init)
  154. {
  155. ///check that is not on border..
  156. ///in such case maybe it's non manyfold
  157. ///insert an initial index
  158. int curr_index=AddNewIndex(vert);
  159. ///and initialize the jumping pos
  160. igl::HalfEdgeIterator<DerivedF> VFI(F,TT,TTi,f_init,edge_init);
  161. bool complete_turn=false;
  162. do
  163. {
  164. int curr_f = VFI.Fi();
  165. int curr_edge = VFI.Ei();
  166. ///assing the current index
  167. HandleS_Index(curr_f,curr_edge) = curr_index;
  168. VFI.NextFE();
  169. int next_f = VFI.Fi();
  170. ///test if I've finiseh with the face exploration
  171. complete_turn = (next_f==f_init);
  172. ///or if I've just crossed a mismatch
  173. if (!complete_turn)
  174. {
  175. bool seam=false;
  176. //seam=curr_f->IsSeam(next_f);
  177. seam=IsSeam(curr_f,next_f);
  178. if (seam)
  179. {
  180. ///then add a new index
  181. curr_index=AddNewIndex(vert);
  182. }
  183. }
  184. } while (!complete_turn);
  185. }
  186. ///initialize the mapping for a given vertex
  187. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  188. IGL_INLINE void igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  189. InitMappingSeam(const int vert)
  190. {
  191. ///first rotate until find the first pos after a mismatch
  192. ///or a border or return to the first position...
  193. int f_init = VF[vert][0];
  194. int indexE = VFi[vert][0];
  195. igl::HalfEdgeIterator<DerivedF> VFI(F,TT,TTi,f_init,indexE);
  196. int edge_init;
  197. int face_init;
  198. FindInitialPos(vert,edge_init,face_init);
  199. MapIndexes(vert,edge_init,face_init);
  200. }
  201. ///vertex to variable mapping
  202. ///initialize the mapping for a given sampled mesh
  203. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  204. IGL_INLINE void igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC>::
  205. InitMappingSeam()
  206. {
  207. num_scalar_variables=-1;
  208. for (unsigned int i=0;i<V.rows();i++)
  209. InitMappingSeam(i);
  210. for (unsigned int j=0;j<V.rows();j++)
  211. assert(HandleV_Integer[j].size()>0);
  212. }
  213. template <typename DerivedV, typename DerivedF, typename VFType, typename DerivedTT, typename DerivedC>
  214. IGL_INLINE void igl::cut_mesh(
  215. const Eigen::PlainObjectBase<DerivedV> &V,
  216. const Eigen::PlainObjectBase<DerivedF> &F,
  217. const std::vector<std::vector<VFType> >& VF,
  218. const std::vector<std::vector<VFType> >& VFi,
  219. const Eigen::PlainObjectBase<DerivedTT>& TT,
  220. const Eigen::PlainObjectBase<DerivedTT>& TTi,
  221. const std::vector<bool> &V_border,
  222. const Eigen::PlainObjectBase<DerivedC> &cuts,
  223. Eigen::PlainObjectBase<DerivedV> &Vcut,
  224. Eigen::PlainObjectBase<DerivedF> &Fcut)
  225. {
  226. //finding the cuts is done, now we need to actually generate a cut mesh
  227. igl::MeshCutterMini<DerivedV, DerivedF, VFType, DerivedTT, DerivedC> mc(V, F, TT, TTi, VF, VFi, V_border, cuts);
  228. mc.InitMappingSeam();
  229. Fcut = mc.HandleS_Index;
  230. //we have the faces, we need the vertices;
  231. int newNumV = Fcut.maxCoeff()+1;
  232. Vcut.setZero(newNumV,3);
  233. for (int vi=0; vi<V.rows(); ++vi)
  234. for (int i=0; i<mc.HandleV_Integer[vi].size();++i)
  235. Vcut.row(mc.HandleV_Integer[vi][i]) = V.row(vi);
  236. //ugly hack to fix some problematic cases (border vertex that is also on the boundary of the hole
  237. for (int fi =0; fi<Fcut.rows(); ++fi)
  238. for (int k=0; k<3; ++k)
  239. if (Fcut(fi,k)==-1)
  240. {
  241. //we need to add a vertex
  242. Fcut(fi,k) = newNumV;
  243. newNumV ++;
  244. Vcut.conservativeResize(newNumV, Eigen::NoChange);
  245. Vcut.row(newNumV-1) = V.row(F(fi,k));
  246. }
  247. }
  248. //Wrapper of the above with only vertices and faces as mesh input
  249. template <typename DerivedV, typename DerivedF, typename DerivedC>
  250. IGL_INLINE void igl::cut_mesh(
  251. const Eigen::PlainObjectBase<DerivedV> &V,
  252. const Eigen::PlainObjectBase<DerivedF> &F,
  253. const Eigen::PlainObjectBase<DerivedC> &cuts,
  254. Eigen::PlainObjectBase<DerivedV> &Vcut,
  255. Eigen::PlainObjectBase<DerivedF> &Fcut)
  256. {
  257. std::vector<std::vector<int> > VF, VFi;
  258. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  259. Eigen::MatrixXi TT, TTi;
  260. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  261. std::vector<bool> V_border = igl::is_border_vertex(V,F);
  262. igl::cut_mesh(V, F, VF, VFi, TT, TTi, V_border, cuts, Vcut, Fcut);
  263. }
  264. #ifdef IGL_STATIC_LIBRARY
  265. // Explicit template specialization
  266. template void igl::cut_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, int, 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&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<bool, std::allocator<bool> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  267. template void igl::cut_mesh<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::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<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  268. #endif