straighten_seams.cpp 13 KB

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