straighten_seams.cpp 12 KB

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