frame_field.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #include "frame_field.h"
  2. #include <igl/triangle_triangle_adjacency.h>
  3. #include <igl/edge_topology.h>
  4. #include <igl/per_face_normals.h>
  5. #include <igl/comiso/nrosy.h>
  6. #include <iostream>
  7. namespace igl
  8. {
  9. class FrameInterpolator
  10. {
  11. public:
  12. // Init
  13. IGL_INLINE FrameInterpolator(const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F);
  14. IGL_INLINE ~FrameInterpolator();
  15. // Reset constraints (at least one constraint must be present or solve will fail)
  16. IGL_INLINE void resetConstraints();
  17. IGL_INLINE void setConstraint(const int fid, const Eigen::VectorXd& v);
  18. IGL_INLINE void interpolateSymmetric();
  19. // Generate the frame field
  20. IGL_INLINE void solve();
  21. // Convert the frame field in the canonical representation
  22. IGL_INLINE void frame2canonical(const Eigen::MatrixXd& TP, const Eigen::RowVectorXd& v, double& theta, Eigen::VectorXd& S);
  23. // Convert the canonical representation in a frame field
  24. IGL_INLINE void canonical2frame(const Eigen::MatrixXd& TP, const double theta, const Eigen::VectorXd& S, Eigen::RowVectorXd& v);
  25. IGL_INLINE Eigen::MatrixXd getFieldPerFace();
  26. IGL_INLINE void PolarDecomposition(Eigen::MatrixXd V, Eigen::MatrixXd& U, Eigen::MatrixXd& P);
  27. // Symmetric
  28. Eigen::MatrixXd S;
  29. std::vector<bool> S_c;
  30. // -------------------------------------------------
  31. // Face Topology
  32. Eigen::MatrixXi TT, TTi;
  33. // Two faces are consistent if their representative vector are taken modulo PI
  34. std::vector<bool> edge_consistency;
  35. Eigen::MatrixXi edge_consistency_TT;
  36. private:
  37. IGL_INLINE double mod2pi(double d);
  38. IGL_INLINE double modpi2(double d);
  39. IGL_INLINE double modpi(double d);
  40. // Convert a direction on the tangent space into an angle
  41. IGL_INLINE double vector2theta(const Eigen::MatrixXd& TP, const Eigen::RowVectorXd& v);
  42. // Convert an angle in a vector in the tangent space
  43. IGL_INLINE Eigen::RowVectorXd theta2vector(const Eigen::MatrixXd& TP, const double theta);
  44. // Interpolate the cross field (theta)
  45. IGL_INLINE void interpolateCross();
  46. // Compute difference between reference frames
  47. IGL_INLINE void computek();
  48. // Compute edge consistency
  49. IGL_INLINE void compute_edge_consistency();
  50. // Cross field direction
  51. Eigen::VectorXd thetas;
  52. std::vector<bool> thetas_c;
  53. // Edge Topology
  54. Eigen::MatrixXi EV, FE, EF;
  55. std::vector<bool> isBorderEdge;
  56. // Angle between two reference frames
  57. // R(k) * t0 = t1
  58. Eigen::VectorXd k;
  59. // Mesh
  60. Eigen::MatrixXd V;
  61. Eigen::MatrixXi F;
  62. // Normals per face
  63. Eigen::MatrixXd N;
  64. // Reference frame per triangle
  65. std::vector<Eigen::MatrixXd> TPs;
  66. };
  67. FrameInterpolator::FrameInterpolator(const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F)
  68. {
  69. using namespace std;
  70. using namespace Eigen;
  71. V = _V;
  72. F = _F;
  73. assert(V.rows() > 0);
  74. assert(F.rows() > 0);
  75. // Generate topological relations
  76. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  77. igl::edge_topology(V,F, EV, FE, EF);
  78. // Flag border edges
  79. isBorderEdge.resize(EV.rows());
  80. for(unsigned i=0; i<EV.rows(); ++i)
  81. isBorderEdge[i] = (EF(i,0) == -1) || ((EF(i,1) == -1));
  82. // Generate normals per face
  83. igl::per_face_normals(V, F, N);
  84. // Generate reference frames
  85. for(unsigned fid=0; fid<F.rows(); ++fid)
  86. {
  87. // First edge
  88. Vector3d e1 = V.row(F(fid,1)) - V.row(F(fid,0));
  89. e1.normalize();
  90. Vector3d e2 = N.row(fid);
  91. e2 = e2.cross(e1);
  92. e2.normalize();
  93. MatrixXd TP(2,3);
  94. TP << e1.transpose(), e2.transpose();
  95. TPs.push_back(TP);
  96. }
  97. // Reset the constraints
  98. resetConstraints();
  99. // Compute k, differences between reference frames
  100. computek();
  101. // Alloc internal variables
  102. thetas = VectorXd::Zero(F.rows());
  103. S = MatrixXd::Zero(F.rows(),3);
  104. compute_edge_consistency();
  105. }
  106. FrameInterpolator::~FrameInterpolator()
  107. {
  108. }
  109. double FrameInterpolator::mod2pi(double d)
  110. {
  111. while(d<0)
  112. d = d + (2.0*M_PI);
  113. return fmod(d, (2.0*M_PI));
  114. }
  115. double FrameInterpolator::modpi2(double d)
  116. {
  117. while(d<0)
  118. d = d + (M_PI/2.0);
  119. return fmod(d, (M_PI/2.0));
  120. }
  121. double FrameInterpolator::modpi(double d)
  122. {
  123. while(d<0)
  124. d = d + (M_PI);
  125. return fmod(d, (M_PI));
  126. }
  127. double FrameInterpolator::vector2theta(const Eigen::MatrixXd& TP, const Eigen::RowVectorXd& v)
  128. {
  129. // Project onto the tangent plane
  130. Eigen::Vector2d vp = TP * v.transpose();
  131. // Convert to angle
  132. double theta = atan2(vp(1),vp(0));
  133. return theta;
  134. }
  135. Eigen::RowVectorXd FrameInterpolator::theta2vector(const Eigen::MatrixXd& TP, const double theta)
  136. {
  137. Eigen::Vector2d vp(cos(theta),sin(theta));
  138. return vp.transpose() * TP;
  139. }
  140. void FrameInterpolator::interpolateCross()
  141. {
  142. using namespace std;
  143. using namespace Eigen;
  144. //olga: was
  145. // NRosyField nrosy(V,F);
  146. // for (unsigned i=0; i<F.rows(); ++i)
  147. // if(thetas_c[i])
  148. // nrosy.setConstraintHard(i,theta2vector(TPs[i],thetas(i)));
  149. // nrosy.solve(4);
  150. // MatrixXd R = nrosy.getFieldPerFace();
  151. //olga: is
  152. Eigen::MatrixXd R;
  153. Eigen::VectorXd S;
  154. Eigen::VectorXi b; b.resize(F.rows(),1);
  155. Eigen::MatrixXd bc; bc.resize(F.rows(),3);
  156. int num = 0;
  157. for (unsigned i=0; i<F.rows(); ++i)
  158. if(thetas_c[i])
  159. {
  160. b[num] = i;
  161. bc.row(num) = theta2vector(TPs[i],thetas(i));
  162. num++;
  163. }
  164. b.conservativeResize(num,Eigen::NoChange);
  165. bc.conservativeResize(num,Eigen::NoChange);
  166. igl::nrosy(V, F, b, bc, 4, R, S);
  167. //olga:end
  168. assert(R.rows() == F.rows());
  169. for (unsigned i=0; i<F.rows(); ++i)
  170. thetas(i) = vector2theta(TPs[i],R.row(i));
  171. }
  172. void FrameInterpolator::resetConstraints()
  173. {
  174. thetas_c.resize(F.rows());
  175. S_c.resize(F.rows());
  176. for(unsigned i=0; i<F.rows(); ++i)
  177. {
  178. thetas_c[i] = false;
  179. S_c[i] = false;
  180. }
  181. }
  182. void FrameInterpolator::compute_edge_consistency()
  183. {
  184. using namespace std;
  185. using namespace Eigen;
  186. // Compute per-edge consistency
  187. edge_consistency.resize(EF.rows());
  188. edge_consistency_TT = MatrixXi::Constant(TT.rows(),3,-1);
  189. // For every non-border edge
  190. for (unsigned eid=0; eid<EF.rows(); ++eid)
  191. {
  192. if (!isBorderEdge[eid])
  193. {
  194. int fid0 = EF(eid,0);
  195. int fid1 = EF(eid,1);
  196. double theta0 = thetas(fid0);
  197. double theta1 = thetas(fid1);
  198. theta0 = theta0 + k(eid);
  199. double r = modpi(theta0-theta1);
  200. edge_consistency[eid] = r < M_PI/4.0 || r > 3*(M_PI/4.0);
  201. // Copy it into edge_consistency_TT
  202. int i1 = -1;
  203. int i2 = -1;
  204. for (unsigned i=0; i<3; ++i)
  205. {
  206. if (TT(fid0,i) == fid1)
  207. i1 = i;
  208. if (TT(fid1,i) == fid0)
  209. i2 = i;
  210. }
  211. assert(i1 != -1);
  212. assert(i2 != -1);
  213. edge_consistency_TT(fid0,i1) = edge_consistency[eid];
  214. edge_consistency_TT(fid1,i2) = edge_consistency[eid];
  215. }
  216. }
  217. }
  218. void FrameInterpolator::computek()
  219. {
  220. using namespace std;
  221. using namespace Eigen;
  222. k.resize(EF.rows());
  223. // For every non-border edge
  224. for (unsigned eid=0; eid<EF.rows(); ++eid)
  225. {
  226. if (!isBorderEdge[eid])
  227. {
  228. int fid0 = EF(eid,0);
  229. int fid1 = EF(eid,1);
  230. Vector3d N0 = N.row(fid0);
  231. //Vector3d N1 = N.row(fid1);
  232. // find common edge on triangle 0 and 1
  233. int fid0_vc = -1;
  234. int fid1_vc = -1;
  235. for (unsigned i=0;i<3;++i)
  236. {
  237. if (EV(eid,0) == F(fid0,i))
  238. fid0_vc = i;
  239. if (EV(eid,1) == F(fid1,i))
  240. fid1_vc = i;
  241. }
  242. assert(fid0_vc != -1);
  243. assert(fid1_vc != -1);
  244. Vector3d common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  245. common_edge.normalize();
  246. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  247. MatrixXd P(3,3);
  248. VectorXd o = V.row(F(fid0,fid0_vc));
  249. VectorXd tmp = -N0.cross(common_edge);
  250. P << common_edge, tmp, N0;
  251. P.transposeInPlace();
  252. MatrixXd V0(3,3);
  253. V0.row(0) = V.row(F(fid0,0)).transpose() -o;
  254. V0.row(1) = V.row(F(fid0,1)).transpose() -o;
  255. V0.row(2) = V.row(F(fid0,2)).transpose() -o;
  256. V0 = (P*V0.transpose()).transpose();
  257. assert(V0(0,2) < 10e-10);
  258. assert(V0(1,2) < 10e-10);
  259. assert(V0(2,2) < 10e-10);
  260. MatrixXd V1(3,3);
  261. V1.row(0) = V.row(F(fid1,0)).transpose() -o;
  262. V1.row(1) = V.row(F(fid1,1)).transpose() -o;
  263. V1.row(2) = V.row(F(fid1,2)).transpose() -o;
  264. V1 = (P*V1.transpose()).transpose();
  265. assert(V1(fid1_vc,2) < 10e-10);
  266. assert(V1((fid1_vc+1)%3,2) < 10e-10);
  267. // compute rotation R such that R * N1 = N0
  268. // i.e. map both triangles to the same plane
  269. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  270. MatrixXd R(3,3);
  271. R << 1, 0, 0,
  272. 0, cos(alpha), -sin(alpha) ,
  273. 0, sin(alpha), cos(alpha);
  274. V1 = (R*V1.transpose()).transpose();
  275. assert(V1(0,2) < 10e-10);
  276. assert(V1(1,2) < 10e-10);
  277. assert(V1(2,2) < 10e-10);
  278. // measure the angle between the reference frames
  279. // k_ij is the angle between the triangle on the left and the one on the right
  280. VectorXd ref0 = V0.row(1) - V0.row(0);
  281. VectorXd ref1 = V1.row(1) - V1.row(0);
  282. ref0.normalize();
  283. ref1.normalize();
  284. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  285. // just to be sure, rotate ref0 using angle ktemp...
  286. MatrixXd R2(2,2);
  287. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  288. tmp = R2*ref0.head<2>();
  289. assert(tmp(0) - ref1(0) < (0.000001));
  290. assert(tmp(1) - ref1(1) < (0.000001));
  291. k[eid] = ktemp;
  292. }
  293. }
  294. }
  295. void FrameInterpolator::frame2canonical(const Eigen::MatrixXd& TP, const Eigen::RowVectorXd& v, double& theta, Eigen::VectorXd& S_v)
  296. {
  297. using namespace std;
  298. using namespace Eigen;
  299. RowVectorXd v0 = v.segment<3>(0);
  300. RowVectorXd v1 = v.segment<3>(3);
  301. // Project onto the tangent plane
  302. Vector2d vp0 = TP * v0.transpose();
  303. Vector2d vp1 = TP * v1.transpose();
  304. // Assemble matrix
  305. MatrixXd M(2,2);
  306. M << vp0, vp1;
  307. if (M.determinant() < 0)
  308. M.col(1) = -M.col(1);
  309. assert(M.determinant() > 0);
  310. // cerr << "M: " << M << endl;
  311. MatrixXd R,S;
  312. PolarDecomposition(M,R,S);
  313. // Finally, express the cross field as an angle
  314. theta = atan2(R(1,0),R(0,0));
  315. MatrixXd R2(2,2);
  316. R2 << cos(theta), -sin(theta), sin(theta), cos(theta);
  317. assert((R2-R).norm() < 10e-8);
  318. // Convert into rotation invariant form
  319. S = R * S * R.inverse();
  320. // Copy in vector form
  321. S_v = VectorXd(3);
  322. S_v << S(0,0), S(0,1), S(1,1);
  323. }
  324. void FrameInterpolator::canonical2frame(const Eigen::MatrixXd& TP, const double theta, const Eigen::VectorXd& S_v, Eigen::RowVectorXd& v)
  325. {
  326. using namespace std;
  327. using namespace Eigen;
  328. assert(S_v.size() == 3);
  329. MatrixXd S_temp(2,2);
  330. S_temp << S_v(0), S_v(1), S_v(1), S_v(2);
  331. // Convert angle in vector in the tangent plane
  332. // Vector2d vp(cos(theta),sin(theta));
  333. // First reconstruct R
  334. MatrixXd R(2,2);
  335. R << cos(theta), -sin(theta), sin(theta), cos(theta);
  336. // Rotation invariant reconstruction
  337. MatrixXd M = S_temp * R;
  338. Vector2d vp0(M(0,0),M(1,0));
  339. Vector2d vp1(M(0,1),M(1,1));
  340. // Unproject the vectors
  341. RowVectorXd v0 = vp0.transpose() * TP;
  342. RowVectorXd v1 = vp1.transpose() * TP;
  343. v.resize(6);
  344. v << v0, v1;
  345. }
  346. void FrameInterpolator::solve()
  347. {
  348. interpolateCross();
  349. interpolateSymmetric();
  350. }
  351. void FrameInterpolator::interpolateSymmetric()
  352. {
  353. using namespace std;
  354. using namespace Eigen;
  355. // Generate uniform Laplacian matrix
  356. typedef Eigen::Triplet<double> triplet;
  357. std::vector<triplet> triplets;
  358. // Variables are stacked as x1,y1,z1,x2,y2,z2
  359. triplets.reserve(3*4*F.rows());
  360. MatrixXd b = MatrixXd::Zero(3*F.rows(),1);
  361. // Build L and b
  362. for (unsigned eid=0; eid<EF.rows(); ++eid)
  363. {
  364. if (!isBorderEdge[eid])
  365. {
  366. for (int z=0;z<2;++z)
  367. {
  368. // W = [w_a, w_b
  369. // w_b, w_c]
  370. //
  371. // It is not symmetric
  372. int i = EF(eid,z==0?0:1);
  373. int j = EF(eid,z==0?1:0);
  374. int w_a_0 = (i*3)+0;
  375. int w_b_0 = (i*3)+1;
  376. int w_c_0 = (i*3)+2;
  377. int w_a_1 = (j*3)+0;
  378. int w_b_1 = (j*3)+1;
  379. int w_c_1 = (j*3)+2;
  380. // Rotation to change frame
  381. double r_a = cos(z==1?k(eid):-k(eid));
  382. double r_b = -sin(z==1?k(eid):-k(eid));
  383. double r_c = sin(z==1?k(eid):-k(eid));
  384. double r_d = cos(z==1?k(eid):-k(eid));
  385. // First term
  386. // w_a_0 = r_a^2 w_a_1 + 2 r_a r_b w_b_1 + r_b^2 w_c_1 = 0
  387. triplets.push_back(triplet(w_a_0,w_a_0, -1 ));
  388. triplets.push_back(triplet(w_a_0,w_a_1, r_a*r_a ));
  389. triplets.push_back(triplet(w_a_0,w_b_1, 2 * r_a*r_b ));
  390. triplets.push_back(triplet(w_a_0,w_c_1, r_b*r_b ));
  391. // Second term
  392. // w_b_0 = r_a r_c w_a + (r_b r_c + r_a r_d) w_b + r_b r_d w_c
  393. triplets.push_back(triplet(w_b_0,w_b_0, -1 ));
  394. triplets.push_back(triplet(w_b_0,w_a_1, r_a*r_c ));
  395. triplets.push_back(triplet(w_b_0,w_b_1, r_b*r_c + r_a*r_d ));
  396. triplets.push_back(triplet(w_b_0,w_c_1, r_b*r_d ));
  397. // Third term
  398. // w_c_0 = r_c^2 w_a + 2 r_c r_d w_b + r_d^2 w_c
  399. triplets.push_back(triplet(w_c_0,w_c_0, -1 ));
  400. triplets.push_back(triplet(w_c_0,w_a_1, r_c*r_c ));
  401. triplets.push_back(triplet(w_c_0,w_b_1, 2 * r_c*r_d ));
  402. triplets.push_back(triplet(w_c_0,w_c_1, r_d*r_d ));
  403. }
  404. }
  405. }
  406. SparseMatrix<double> L(3*F.rows(),3*F.rows());
  407. L.setFromTriplets(triplets.begin(), triplets.end());
  408. triplets.clear();
  409. // Add soft constraints
  410. double w = 100000;
  411. for (unsigned fid=0; fid < F.rows(); ++fid)
  412. {
  413. if (S_c[fid])
  414. {
  415. for (unsigned i=0;i<3;++i)
  416. {
  417. triplets.push_back(triplet(3*fid + i,3*fid + i,w));
  418. b(3*fid + i) += w*S(fid,i);
  419. }
  420. }
  421. }
  422. SparseMatrix<double> soft(3*F.rows(),3*F.rows());
  423. soft.setFromTriplets(triplets.begin(), triplets.end());
  424. SparseMatrix<double> M;
  425. M = L + soft;
  426. // Solve Lx = b;
  427. SparseLU<SparseMatrix<double> > solver;
  428. solver.compute(M);
  429. if(solver.info()!=Success)
  430. {
  431. std::cerr << "LU failed - frame_interpolator.cpp" << std::endl;
  432. assert(0);
  433. }
  434. MatrixXd x;
  435. x = solver.solve(b);
  436. if(solver.info()!=Success)
  437. {
  438. std::cerr << "Linear solve failed - frame_interpolator.cpp" << std::endl;
  439. assert(0);
  440. }
  441. S = MatrixXd::Zero(F.rows(),3);
  442. // Copy back the result
  443. for (unsigned i=0;i<F.rows();++i)
  444. S.row(i) << x(i*3+0), x(i*3+1), x(i*3+2);
  445. }
  446. void FrameInterpolator::setConstraint(const int fid, const Eigen::VectorXd& v)
  447. {
  448. using namespace std;
  449. using namespace Eigen;
  450. double t_;
  451. VectorXd S_;
  452. frame2canonical(TPs[fid],v,t_,S_);
  453. Eigen::RowVectorXd v2;
  454. canonical2frame(TPs[fid], t_, S_, v2);
  455. thetas(fid) = t_;
  456. thetas_c[fid] = true;
  457. S.row(fid) = S_;
  458. S_c[fid] = true;
  459. }
  460. Eigen::MatrixXd FrameInterpolator::getFieldPerFace()
  461. {
  462. using namespace std;
  463. using namespace Eigen;
  464. MatrixXd R(F.rows(),6);
  465. for (unsigned i=0; i<F.rows(); ++i)
  466. {
  467. RowVectorXd v;
  468. canonical2frame(TPs[i],thetas(i),S.row(i),v);
  469. R.row(i) = v;
  470. }
  471. return R;
  472. }
  473. void FrameInterpolator::PolarDecomposition(Eigen::MatrixXd V, Eigen::MatrixXd& U, Eigen::MatrixXd& P)
  474. {
  475. using namespace std;
  476. using namespace Eigen;
  477. // Polar Decomposition
  478. JacobiSVD<MatrixXd> svd(V,Eigen::ComputeFullU | Eigen::ComputeFullV);
  479. U = svd.matrixU() * svd.matrixV().transpose();
  480. P = svd.matrixV() * svd.singularValues().asDiagonal() * svd.matrixV().transpose();
  481. }
  482. }
  483. IGL_INLINE void igl::frame_field(
  484. const Eigen::MatrixXd& V,
  485. const Eigen::MatrixXi& F,
  486. const Eigen::VectorXi& b,
  487. const Eigen::MatrixXd& bc1,
  488. const Eigen::MatrixXd& bc2,
  489. Eigen::MatrixXd& FF1,
  490. Eigen::MatrixXd& FF2
  491. )
  492. {
  493. using namespace std;
  494. using namespace Eigen;
  495. assert(b.size() > 0);
  496. // Init Solver
  497. FrameInterpolator field(V,F);
  498. for (unsigned i=0; i<b.size(); ++i)
  499. {
  500. VectorXd t(6); t << bc1.row(i).transpose(), bc2.row(i).transpose();
  501. field.setConstraint(b(i), t);
  502. }
  503. // Solve
  504. field.solve();
  505. // Copy back
  506. MatrixXd R = field.getFieldPerFace();
  507. FF1 = R.block(0, 0, R.rows(), 3);
  508. FF2 = R.block(0, 3, R.rows(), 3);
  509. }