straighten_seams.cpp 11 KB

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