cut_mesh.cpp 14 KB

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