straighten_seams.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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).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. // New texture-vertex locations
  188. UT = VT;
  189. // Indices into UT of coarse output polygon edges
  190. std::vector<std::vector<typename DerivedUE::Scalar> > vUE;
  191. // loop over each component
  192. std::vector<bool> done(nc,false);
  193. for(int c = 0;c<nc;c++)
  194. {
  195. if(done[c])
  196. {
  197. continue;
  198. }
  199. done[c] = true;
  200. // edges of this component
  201. Eigen::VectorXi Ic;
  202. find(C==c,Ic);
  203. if(Ic.size() == 0)
  204. {
  205. continue;
  206. }
  207. SparseMatrix<bool> OEQIc;
  208. slice(OEQ,Ic,1,OEQIc);
  209. Eigen::VectorXi N;
  210. sum(OEQIc,2,N);
  211. const int ncopies = N(0)+1;
  212. assert((N.array() == ncopies-1).all());
  213. assert((ncopies == 1 || ncopies == 2) &&
  214. "Not dealing with non-manifold meshes");
  215. Eigen::VectorXi vpath,epath,eend;
  216. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,2> MatrixX2S;
  217. switch(ncopies)
  218. {
  219. case 1:
  220. {
  221. MatrixX2I OTIc;
  222. slice(OT,Ic,1,OTIc);
  223. edges_to_path(OTIc,vpath,epath,eend);
  224. Array<bool,Dynamic,1> SVvpath;
  225. slice(SV,vpath,1,SVvpath);
  226. assert(
  227. (vpath(0) != vpath(vpath.size()-1) || !SVvpath.any()) &&
  228. "Not dealing with 1-loops touching 'sharp' corners");
  229. // simple open boundary
  230. MatrixX2S PI;
  231. slice(VT,vpath,1,PI);
  232. const Scalar bbd =
  233. (PI.colwise().maxCoeff() - PI.colwise().minCoeff()).norm();
  234. // Do not collapse boundaries to fewer than 3 vertices
  235. const bool allow_boundary_collapse = false;
  236. Scalar eff_tol = std::min(tol,2.);
  237. VectorXi UIc;
  238. while(true)
  239. {
  240. MatrixX2S UPI,UTvpath;
  241. ramer_douglas_peucker(PI,eff_tol*bbd,UPI,UIc,UTvpath);
  242. slice_into(UTvpath,vpath,1,UT);
  243. if(allow_boundary_collapse)
  244. {
  245. break;
  246. }
  247. if(UPI.rows()>=4)
  248. {
  249. break;
  250. }
  251. eff_tol = eff_tol*0.5;
  252. }
  253. for(int i = 0;i<UIc.size()-1;i++)
  254. {
  255. vUE.push_back({vpath(UIc(i)),vpath(UIc(i+1))});
  256. }
  257. }
  258. break;
  259. case 2:
  260. {
  261. // Find copies
  262. VectorXi Icc;
  263. {
  264. VectorXi II;
  265. Array<bool,Dynamic,1> IV;
  266. SparseMatrix<bool> OEQIcT = OEQIc.transpose().eval();
  267. find(OEQIcT,Icc,II,IV);
  268. assert(II.size() == Ic.size() &&
  269. (II.array() ==
  270. VectorXi::LinSpaced(Ic.size(),0,Ic.size()-1).array()).all());
  271. assert(Icc.size() == Ic.size());
  272. const int cc = C(Icc(0));
  273. Eigen::VectorXi CIcc;
  274. slice(C,Icc,1,CIcc);
  275. assert((CIcc.array() == cc).all());
  276. assert(!done[cc]);
  277. done[cc] = true;
  278. }
  279. Array<bool,Dynamic,1> flipped;
  280. {
  281. MatrixX2I OFIc,OFIcc;
  282. slice(OF,Ic,1,OFIc);
  283. slice(OF,Icc,1,OFIcc);
  284. Eigen::VectorXi XOR,IA,IB;
  285. setxor(OFIc,OFIcc,XOR,IA,IB);
  286. assert(XOR.size() == 0);
  287. flipped = OFIc.array().col(0) != OFIcc.array().col(0);
  288. }
  289. if(Ic.size() == 1)
  290. {
  291. // No change to UT
  292. vUE.push_back({OT(Ic(0),0),OT(Ic(0),1)});
  293. assert(Icc.size() == 1);
  294. vUE.push_back({OT(Icc(0),flipped(0)?1:0),OT(Icc(0),flipped(0)?0:1)});
  295. }else
  296. {
  297. MatrixX2I OTIc;
  298. slice(OT,Ic,1,OTIc);
  299. edges_to_path(OTIc,vpath,epath,eend);
  300. // Flip endpoints if needed
  301. for(int e = 0;e<eend.size();e++)if(flipped(e))eend(e)=1-eend(e);
  302. VectorXi vpathc(epath.size()+1);
  303. for(int e = 0;e<epath.size();e++)
  304. {
  305. vpathc(e) = OT(Icc(epath(e)),eend(e));
  306. }
  307. vpathc(epath.size()) =
  308. OT(Icc(epath(epath.size()-1)),1-eend(eend.size()-1));
  309. assert(vpath.size() == vpathc.size());
  310. Matrix<Scalar,Dynamic,Dynamic> PI(vpath.size(),VT.cols()*2);
  311. for(int p = 0;p<PI.rows();p++)
  312. {
  313. for(int d = 0;d<VT.cols();d++)
  314. {
  315. PI(p, d) = VT( vpath(p),d);
  316. PI(p,VT.cols()+d) = VT(vpathc(p),d);
  317. }
  318. }
  319. const Scalar bbd =
  320. (PI.colwise().maxCoeff() - PI.colwise().minCoeff()).norm();
  321. Matrix<Scalar,Dynamic,Dynamic> UPI,SI;
  322. VectorXi UIc;
  323. ramer_douglas_peucker(PI,tol*bbd,UPI,UIc,SI);
  324. slice_into(SI.leftCols (VT.cols()), vpath,1,UT);
  325. slice_into(SI.rightCols(VT.cols()),vpathc,1,UT);
  326. for(int i = 0;i<UIc.size()-1;i++)
  327. {
  328. vUE.push_back({vpath(UIc(i)),vpath(UIc(i+1))});
  329. }
  330. for(int i = 0;i<UIc.size()-1;i++)
  331. {
  332. vUE.push_back({vpathc(UIc(i)),vpathc(UIc(i+1))});
  333. }
  334. }
  335. }
  336. break;
  337. default:
  338. assert(false && "Should never reach here");
  339. }
  340. }
  341. list_to_matrix(vUE,UE);
  342. }
  343. #ifdef IGL_STATIC_LIBRARY
  344. // Explicit template specialization
  345. #endif