straighten_seams.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 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 "straighten_seams.h"
  9. #include "on_boundary.h"
  10. #include "sparse.h"
  11. #include "max.h"
  12. #include "count.h"
  13. #include "any.h"
  14. #include "slice_mask.h"
  15. #include "slice_into.h"
  16. #include "unique_simplices.h"
  17. #include "adjacency_matrix.h"
  18. #include "setxor.h"
  19. #include "edges_to_path.h"
  20. #include "ramer_douglas_peucker.h"
  21. #include "components.h"
  22. #include "ears.h"
  23. #include "slice.h"
  24. #include "sum.h"
  25. #include "find.h"
  26. template <
  27. typename DerivedV,
  28. typename DerivedF,
  29. typename DerivedVT,
  30. typename DerivedFT,
  31. typename Scalar,
  32. typename DerivedUE,
  33. typename DerivedUT,
  34. typename DerivedOT>
  35. IGL_INLINE void igl::straighten_seams(
  36. const Eigen::MatrixBase<DerivedV> & V,
  37. const Eigen::MatrixBase<DerivedF> & F,
  38. const Eigen::MatrixBase<DerivedVT> & VT,
  39. const Eigen::MatrixBase<DerivedFT> & FT,
  40. const Scalar tol,
  41. Eigen::PlainObjectBase<DerivedUE> & UE,
  42. Eigen::PlainObjectBase<DerivedUT> & UT,
  43. Eigen::PlainObjectBase<DerivedOT> & OT)
  44. {
  45. using namespace Eigen;
  46. // number of faces
  47. assert(FT.rows() == F.rows() && "#FT must == #F");
  48. assert(F.cols() == 3 && "F should contain triangles");
  49. assert(FT.cols() == 3 && "FT should contain triangles");
  50. const int m = F.rows();
  51. // Boundary edges of the texture map and 3d meshes
  52. Array<bool,Dynamic,1> _;
  53. Array<bool,Dynamic,3> BT,BF;
  54. on_boundary(FT,_,BT);
  55. on_boundary(F,_,BF);
  56. assert((!((BF && (BT!=true)).any())) &&
  57. "Not dealing with boundaries of mesh that get 'stitched' in texture mesh");
  58. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  59. const MatrixX2I ET = (MatrixX2I(FT.rows()*3,2)
  60. <<FT.col(1),FT.col(2),FT.col(2),FT.col(0),FT.col(0),FT.col(1)).finished();
  61. // "half"-edges with indices into 3D-mesh
  62. const MatrixX2I EF = (MatrixX2I(F.rows()*3,2)
  63. <<F.col(1),F.col(2),F.col(2),F.col(0),F.col(0),F.col(1)).finished();
  64. // Find unique (undirected) edges in F
  65. VectorXi EFMAP;
  66. {
  67. MatrixX2I _1;
  68. VectorXi _2;
  69. unique_simplices(EF,_1,_2,EFMAP);
  70. }
  71. Array<bool,Dynamic,1>vBT = Map<Array<bool,Dynamic,1> >(BT.data(),BT.size(),1);
  72. Array<bool,Dynamic,1>vBF = Map<Array<bool,Dynamic,1> >(BF.data(),BF.size(),1);
  73. MatrixX2I OF;
  74. slice_mask(ET,vBT,1,OT);
  75. slice_mask(EF,vBT,1,OF);
  76. VectorXi OFMAP;
  77. slice_mask(EFMAP,vBT,1,OFMAP);
  78. // Two boundary edges on the texture-mapping are "equivalent" to each other on
  79. // the 3D-mesh if their 3D-mesh vertex indices match
  80. SparseMatrix<bool> OEQ;
  81. {
  82. SparseMatrix<bool> OEQR;
  83. sparse(
  84. VectorXi::LinSpaced(OT.rows(),0,OT.rows()-1),
  85. OFMAP,
  86. Array<bool,Dynamic,1>::Ones(OT.rows(),1),
  87. OT.rows(),
  88. m*3,
  89. OEQR);
  90. OEQ = OEQR * OEQR.transpose();
  91. // Remove diagonal
  92. OEQ.prune([](const int r, const int c, const bool)->bool{return r!=c;});
  93. }
  94. // For each edge in OT, for each endpoint, how many _other_ texture-vertices
  95. // are images of all the 3d-mesh vertices in F who map from "corners" in F/FT
  96. // mapping to this endpoint.
  97. //
  98. // Adjacency matrix between 3d-vertices and texture-vertices
  99. SparseMatrix<bool> V2VT;
  100. sparse(
  101. F,
  102. FT,
  103. Array<bool,Dynamic,3>::Ones(F.rows(),F.cols()),
  104. V.rows(),
  105. VT.rows(),
  106. V2VT);
  107. // For each 3d-vertex count how many different texture-coordinates its getting
  108. // from different incident corners
  109. VectorXi DV;
  110. count(V2VT,2,DV);
  111. VectorXi M,I;
  112. max(V2VT,1,M,I);
  113. assert( (M.array() == 1).all() );
  114. VectorXi DT;
  115. // Map counts onto texture-vertices
  116. slice(DV,I,1,DT);
  117. // Boundary in 3D && UV
  118. Array<bool,Dynamic,1> BTF;
  119. slice_mask(vBF, vBT, 1, BTF);
  120. // Texture-vertex is "sharp" if incident on "half-"edge that is not a
  121. // boundary in the 3D mesh but is a boundary in the texture-mesh AND is not
  122. // "cut cleanly" (the vertex is mapped to exactly 2 locations)
  123. Array<bool,Dynamic,1> SV = Array<bool,Dynamic,1>::Zero(VT.rows(),1);
  124. assert(BTF.size() == OT.rows());
  125. for(int h = 0;h<BTF.size();h++)
  126. {
  127. if(!BTF(h))
  128. {
  129. SV(OT(h,0)) = true;
  130. SV(OT(h,1)) = true;
  131. }
  132. }
  133. Array<bool,Dynamic,1> CL = DT.array()==2;
  134. SparseMatrix<bool> VTOT;
  135. {
  136. Eigen::MatrixXi I =
  137. VectorXi::LinSpaced(OT.rows(),0,OT.rows()-1).replicate(1,2);
  138. sparse(
  139. OT,
  140. I,
  141. Array<bool,Dynamic,2>::Ones(OT.rows(),OT.cols()),
  142. VT.rows(),
  143. OT.rows(),
  144. VTOT);
  145. Array<int,Dynamic,1> cuts;
  146. count( (VTOT*OEQ).eval(), 2, cuts);
  147. CL = (CL && (cuts.array() == 2)).eval();
  148. }
  149. assert(CL.size() == SV.size());
  150. for(int c = 0;c<CL.size();c++) if(CL(c)) SV(c) = false;
  151. // vertices at the corner of ears are declared to be sharp. This is
  152. // conservative: for example, if the ear is strictly convex and stays strictly
  153. // convex then the ear won't be flipped.
  154. VectorXi ear,ear_opp;
  155. ears(FT,ear,ear_opp);
  156. // There might be an ear on one copy, so mark vertices on other copies, too
  157. // ears as they live on the 3D mesh
  158. VectorXi earTi(ear.size());
  159. for(int e = 0;e<ear.size();e++) earTi(e) = FT(ear(e),ear_opp(e));
  160. SparseMatrix<bool> V2VTearTi,V2VTearFi;
  161. slice(V2VT,earTi,2,V2VTearTi);
  162. VectorXi earFi;
  163. Array<bool,Dynamic,1> earFb;
  164. any(V2VTearTi,2,earFb);
  165. find(earFb,earFi);
  166. slice(V2VT,earFi,1,V2VTearFi);
  167. Array<bool,Dynamic,1> earT;
  168. any(V2VTearFi,1,earT);
  169. // Even if ear-vertices are marked as sharp if it changes, e.g., from convex
  170. // to concave then it will _force_ a flip of the ear triangle. So, declare
  171. // that neighbors of ears are also sharp.
  172. SparseMatrix<bool> A;
  173. adjacency_matrix(FT,A);
  174. earT = (earT || (A*earT.matrix()).array()).eval();
  175. assert(earT.size() == SV.size());
  176. for(int e = 0;e<earT.size();e++) if(earT(e)) SV(e) = true;
  177. SparseMatrix<bool> OTVT = VTOT.transpose();
  178. int nc;
  179. ArrayXi C;
  180. {
  181. // Doesn't Compile on older Eigen:
  182. //SparseMatrix<bool> A = OTVT * (!SV).matrix().asDiagonal() * VTOT;
  183. SparseMatrix<bool> A = OTVT * (SV!=true).matrix().asDiagonal() * VTOT;
  184. components(A,C);
  185. nc = C.maxCoeff()+1;
  186. }
  187. //std::cout<<"nc: "<<nc<<std::endl;
  188. // New texture-vertex locations
  189. UT = VT;
  190. // Indices into UT of coarse output polygon edges
  191. std::vector<std::vector<typename DerivedUE::Scalar> > vUE;
  192. // loop over each component
  193. std::vector<bool> done(nc,false);
  194. for(int c = 0;c<nc;c++)
  195. {
  196. if(done[c])
  197. {
  198. continue;
  199. }
  200. done[c] = true;
  201. // edges of this component
  202. Eigen::VectorXi Ic;
  203. find(C==c,Ic);
  204. if(Ic.size() == 0)
  205. {
  206. continue;
  207. }
  208. SparseMatrix<bool> OEQIc;
  209. slice(OEQ,Ic,1,OEQIc);
  210. Eigen::VectorXi N;
  211. sum(OEQIc,2,N);
  212. const int ncopies = N(0)+1;
  213. assert((N.array() == ncopies-1).all());
  214. assert((ncopies == 1 || ncopies == 2) &&
  215. "Not dealing with non-manifold meshes");
  216. Eigen::VectorXi vpath,epath,eend;
  217. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,2> MatrixX2S;
  218. switch(ncopies)
  219. {
  220. case 1:
  221. {
  222. MatrixX2I OTIc;
  223. slice(OT,Ic,1,OTIc);
  224. edges_to_path(OTIc,vpath,epath,eend);
  225. Array<bool,Dynamic,1> SVvpath;
  226. slice(SV,vpath,1,SVvpath);
  227. assert(
  228. (vpath(0) != vpath(vpath.size()-1) || !SVvpath.any()) &&
  229. "Not dealing with 1-loops touching 'sharp' corners");
  230. // simple open boundary
  231. MatrixX2S PI;
  232. slice(VT,vpath,1,PI);
  233. const Scalar bbd =
  234. (PI.colwise().maxCoeff() - PI.colwise().minCoeff()).norm();
  235. // Do not collapse boundaries to fewer than 3 vertices
  236. const bool allow_boundary_collapse = false;
  237. Scalar eff_tol = std::min(tol,2.);
  238. VectorXi UIc;
  239. while(true)
  240. {
  241. MatrixX2S UPI,UTvpath;
  242. ramer_douglas_peucker(PI,eff_tol*bbd,UPI,UIc,UTvpath);
  243. slice_into(UTvpath,vpath,1,UT);
  244. if(allow_boundary_collapse)
  245. {
  246. break;
  247. }
  248. if(UPI.rows()>=4)
  249. {
  250. break;
  251. }
  252. eff_tol = eff_tol*0.5;
  253. }
  254. for(int i = 0;i<UIc.size()-1;i++)
  255. {
  256. vUE.push_back({vpath(UIc(i)),vpath(UIc(i+1))});
  257. }
  258. }
  259. break;
  260. case 2:
  261. {
  262. // Find copies
  263. VectorXi Icc;
  264. {
  265. VectorXi II;
  266. Array<bool,Dynamic,1> IV;
  267. SparseMatrix<bool> OEQIcT = OEQIc.transpose().eval();
  268. find(OEQIcT,Icc,II,IV);
  269. assert(II.size() == Ic.size() &&
  270. (II.array() ==
  271. VectorXi::LinSpaced(Ic.size(),0,Ic.size()-1).array()).all());
  272. assert(Icc.size() == Ic.size());
  273. const int cc = C(Icc(0));
  274. Eigen::VectorXi CIcc;
  275. slice(C,Icc,1,CIcc);
  276. assert((CIcc.array() == cc).all());
  277. assert(!done[cc]);
  278. done[cc] = true;
  279. }
  280. Array<bool,Dynamic,1> flipped;
  281. {
  282. MatrixX2I OFIc,OFIcc;
  283. slice(OF,Ic,1,OFIc);
  284. slice(OF,Icc,1,OFIcc);
  285. Eigen::VectorXi XOR,IA,IB;
  286. setxor(OFIc,OFIcc,XOR,IA,IB);
  287. assert(XOR.size() == 0);
  288. flipped = OFIc.array().col(0) != OFIcc.array().col(0);
  289. }
  290. if(Ic.size() == 1)
  291. {
  292. // No change to UT
  293. vUE.push_back({OT(Ic(0),0),OT(Ic(0),1)});
  294. assert(Icc.size() == 1);
  295. vUE.push_back({OT(Icc(0),flipped(0)?1:0),OT(Icc(0),flipped(0)?0:1)});
  296. }else
  297. {
  298. MatrixX2I OTIc;
  299. slice(OT,Ic,1,OTIc);
  300. edges_to_path(OTIc,vpath,epath,eend);
  301. // Flip endpoints if needed
  302. for(int e = 0;e<eend.size();e++)if(flipped(e))eend(e)=1-eend(e);
  303. VectorXi vpathc(epath.size()+1);
  304. for(int e = 0;e<epath.size();e++)
  305. {
  306. vpathc(e) = OT(Icc(epath(e)),eend(e));
  307. }
  308. vpathc(epath.size()) =
  309. OT(Icc(epath(epath.size()-1)),1-eend(eend.size()-1));
  310. assert(vpath.size() == vpathc.size());
  311. Matrix<Scalar,Dynamic,Dynamic> PI(vpath.size(),VT.cols()*2);
  312. for(int p = 0;p<PI.rows();p++)
  313. {
  314. for(int d = 0;d<VT.cols();d++)
  315. {
  316. PI(p, d) = VT( vpath(p),d);
  317. PI(p,VT.cols()+d) = VT(vpathc(p),d);
  318. }
  319. }
  320. const Scalar bbd =
  321. (PI.colwise().maxCoeff() - PI.colwise().minCoeff()).norm();
  322. Matrix<Scalar,Dynamic,Dynamic> UPI,SI;
  323. VectorXi UIc;
  324. ramer_douglas_peucker(PI,tol*bbd,UPI,UIc,SI);
  325. slice_into(SI.leftCols (VT.cols()), vpath,1,UT);
  326. slice_into(SI.rightCols(VT.cols()),vpathc,1,UT);
  327. for(int i = 0;i<UIc.size()-1;i++)
  328. {
  329. vUE.push_back({vpath(UIc(i)),vpath(UIc(i+1))});
  330. }
  331. for(int i = 0;i<UIc.size()-1;i++)
  332. {
  333. vUE.push_back({vpathc(UIc(i)),vpathc(UIc(i+1))});
  334. }
  335. }
  336. }
  337. break;
  338. default:
  339. assert(false && "Should never reach here");
  340. }
  341. }
  342. list_to_matrix(vUE,UE);
  343. }
  344. #ifdef IGL_STATIC_LIBRARY
  345. // Explicit template specialization
  346. #endif