frame_field.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. NRosyField nrosy(V,F);
  145. for (unsigned i=0; i<F.rows(); ++i)
  146. if(thetas_c[i])
  147. nrosy.setConstraintHard(i,theta2vector(TPs[i],thetas(i)));
  148. nrosy.solve(4);
  149. MatrixXd R = nrosy.getFieldPerFace();
  150. assert(R.rows() == F.rows());
  151. for (unsigned i=0; i<F.rows(); ++i)
  152. thetas(i) = vector2theta(TPs[i],R.row(i));
  153. }
  154. void FrameInterpolator::resetConstraints()
  155. {
  156. thetas_c.resize(F.rows());
  157. S_c.resize(F.rows());
  158. for(unsigned i=0; i<F.rows(); ++i)
  159. {
  160. thetas_c[i] = false;
  161. S_c[i] = false;
  162. }
  163. }
  164. void FrameInterpolator::compute_edge_consistency()
  165. {
  166. using namespace std;
  167. using namespace Eigen;
  168. // Compute per-edge consistency
  169. edge_consistency.resize(EF.rows());
  170. edge_consistency_TT = MatrixXi::Constant(TT.rows(),3,-1);
  171. // For every non-border edge
  172. for (unsigned eid=0; eid<EF.rows(); ++eid)
  173. {
  174. if (!isBorderEdge[eid])
  175. {
  176. int fid0 = EF(eid,0);
  177. int fid1 = EF(eid,1);
  178. double theta0 = thetas(fid0);
  179. double theta1 = thetas(fid1);
  180. theta0 = theta0 + k(eid);
  181. double r = modpi(theta0-theta1);
  182. edge_consistency[eid] = r < M_PI/4.0 || r > 3*(M_PI/4.0);
  183. // Copy it into edge_consistency_TT
  184. int i1 = -1;
  185. int i2 = -1;
  186. for (unsigned i=0; i<3; ++i)
  187. {
  188. if (TT(fid0,i) == fid1)
  189. i1 = i;
  190. if (TT(fid1,i) == fid0)
  191. i2 = i;
  192. }
  193. assert(i1 != -1);
  194. assert(i2 != -1);
  195. edge_consistency_TT(fid0,i1) = edge_consistency[eid];
  196. edge_consistency_TT(fid1,i2) = edge_consistency[eid];
  197. }
  198. }
  199. }
  200. void FrameInterpolator::computek()
  201. {
  202. using namespace std;
  203. using namespace Eigen;
  204. k.resize(EF.rows());
  205. // For every non-border edge
  206. for (unsigned eid=0; eid<EF.rows(); ++eid)
  207. {
  208. if (!isBorderEdge[eid])
  209. {
  210. int fid0 = EF(eid,0);
  211. int fid1 = EF(eid,1);
  212. Vector3d N0 = N.row(fid0);
  213. //Vector3d N1 = N.row(fid1);
  214. // find common edge on triangle 0 and 1
  215. int fid0_vc = -1;
  216. int fid1_vc = -1;
  217. for (unsigned i=0;i<3;++i)
  218. {
  219. if (EV(eid,0) == F(fid0,i))
  220. fid0_vc = i;
  221. if (EV(eid,1) == F(fid1,i))
  222. fid1_vc = i;
  223. }
  224. assert(fid0_vc != -1);
  225. assert(fid1_vc != -1);
  226. Vector3d common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  227. common_edge.normalize();
  228. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  229. MatrixXd P(3,3);
  230. VectorXd o = V.row(F(fid0,fid0_vc));
  231. VectorXd tmp = -N0.cross(common_edge);
  232. P << common_edge, tmp, N0;
  233. P.transposeInPlace();
  234. MatrixXd V0(3,3);
  235. V0.row(0) = V.row(F(fid0,0)).transpose() -o;
  236. V0.row(1) = V.row(F(fid0,1)).transpose() -o;
  237. V0.row(2) = V.row(F(fid0,2)).transpose() -o;
  238. V0 = (P*V0.transpose()).transpose();
  239. assert(V0(0,2) < 10e-10);
  240. assert(V0(1,2) < 10e-10);
  241. assert(V0(2,2) < 10e-10);
  242. MatrixXd V1(3,3);
  243. V1.row(0) = V.row(F(fid1,0)).transpose() -o;
  244. V1.row(1) = V.row(F(fid1,1)).transpose() -o;
  245. V1.row(2) = V.row(F(fid1,2)).transpose() -o;
  246. V1 = (P*V1.transpose()).transpose();
  247. assert(V1(fid1_vc,2) < 10e-10);
  248. assert(V1((fid1_vc+1)%3,2) < 10e-10);
  249. // compute rotation R such that R * N1 = N0
  250. // i.e. map both triangles to the same plane
  251. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  252. MatrixXd R(3,3);
  253. R << 1, 0, 0,
  254. 0, cos(alpha), -sin(alpha) ,
  255. 0, sin(alpha), cos(alpha);
  256. V1 = (R*V1.transpose()).transpose();
  257. assert(V1(0,2) < 10e-10);
  258. assert(V1(1,2) < 10e-10);
  259. assert(V1(2,2) < 10e-10);
  260. // measure the angle between the reference frames
  261. // k_ij is the angle between the triangle on the left and the one on the right
  262. VectorXd ref0 = V0.row(1) - V0.row(0);
  263. VectorXd ref1 = V1.row(1) - V1.row(0);
  264. ref0.normalize();
  265. ref1.normalize();
  266. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  267. // just to be sure, rotate ref0 using angle ktemp...
  268. MatrixXd R2(2,2);
  269. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  270. tmp = R2*ref0.head<2>();
  271. assert(tmp(0) - ref1(0) < (0.000001));
  272. assert(tmp(1) - ref1(1) < (0.000001));
  273. k[eid] = ktemp;
  274. }
  275. }
  276. }
  277. void FrameInterpolator::frame2canonical(const Eigen::MatrixXd& TP, const Eigen::RowVectorXd& v, double& theta, Eigen::VectorXd& S_v)
  278. {
  279. using namespace std;
  280. using namespace Eigen;
  281. RowVectorXd v0 = v.segment<3>(0);
  282. RowVectorXd v1 = v.segment<3>(3);
  283. // Project onto the tangent plane
  284. Vector2d vp0 = TP * v0.transpose();
  285. Vector2d vp1 = TP * v1.transpose();
  286. // Assemble matrix
  287. MatrixXd M(2,2);
  288. M << vp0, vp1;
  289. if (M.determinant() < 0)
  290. M.col(1) = -M.col(1);
  291. assert(M.determinant() > 0);
  292. // cerr << "M: " << M << endl;
  293. MatrixXd R,S;
  294. PolarDecomposition(M,R,S);
  295. // Finally, express the cross field as an angle
  296. theta = atan2(R(1,0),R(0,0));
  297. MatrixXd R2(2,2);
  298. R2 << cos(theta), -sin(theta), sin(theta), cos(theta);
  299. assert((R2-R).norm() < 10e-8);
  300. // Convert into rotation invariant form
  301. S = R * S * R.inverse();
  302. // Copy in vector form
  303. S_v = VectorXd(3);
  304. S_v << S(0,0), S(0,1), S(1,1);
  305. }
  306. void FrameInterpolator::canonical2frame(const Eigen::MatrixXd& TP, const double theta, const Eigen::VectorXd& S_v, Eigen::RowVectorXd& v)
  307. {
  308. using namespace std;
  309. using namespace Eigen;
  310. assert(S_v.size() == 3);
  311. MatrixXd S_temp(2,2);
  312. S_temp << S_v(0), S_v(1), S_v(1), S_v(2);
  313. // Convert angle in vector in the tangent plane
  314. // Vector2d vp(cos(theta),sin(theta));
  315. // First reconstruct R
  316. MatrixXd R(2,2);
  317. R << cos(theta), -sin(theta), sin(theta), cos(theta);
  318. // Rotation invariant reconstruction
  319. MatrixXd M = S_temp * R;
  320. Vector2d vp0(M(0,0),M(1,0));
  321. Vector2d vp1(M(0,1),M(1,1));
  322. // Unproject the vectors
  323. RowVectorXd v0 = vp0.transpose() * TP;
  324. RowVectorXd v1 = vp1.transpose() * TP;
  325. v.resize(6);
  326. v << v0, v1;
  327. }
  328. void FrameInterpolator::solve()
  329. {
  330. interpolateCross();
  331. interpolateSymmetric();
  332. }
  333. void FrameInterpolator::interpolateSymmetric()
  334. {
  335. using namespace std;
  336. using namespace Eigen;
  337. // Generate uniform Laplacian matrix
  338. typedef Eigen::Triplet<double> triplet;
  339. std::vector<triplet> triplets;
  340. // Variables are stacked as x1,y1,z1,x2,y2,z2
  341. triplets.reserve(3*4*F.rows());
  342. MatrixXd b = MatrixXd::Zero(3*F.rows(),1);
  343. // Build L and b
  344. for (unsigned eid=0; eid<EF.rows(); ++eid)
  345. {
  346. if (!isBorderEdge[eid])
  347. {
  348. for (int z=0;z<2;++z)
  349. {
  350. // W = [w_a, w_b
  351. // w_b, w_c]
  352. //
  353. // It is not symmetric
  354. int i = EF(eid,z==0?0:1);
  355. int j = EF(eid,z==0?1:0);
  356. int w_a_0 = (i*3)+0;
  357. int w_b_0 = (i*3)+1;
  358. int w_c_0 = (i*3)+2;
  359. int w_a_1 = (j*3)+0;
  360. int w_b_1 = (j*3)+1;
  361. int w_c_1 = (j*3)+2;
  362. // Rotation to change frame
  363. double r_a = cos(z==1?k(eid):-k(eid));
  364. double r_b = -sin(z==1?k(eid):-k(eid));
  365. double r_c = sin(z==1?k(eid):-k(eid));
  366. double r_d = cos(z==1?k(eid):-k(eid));
  367. // First term
  368. // w_a_0 = r_a^2 w_a_1 + 2 r_a r_b w_b_1 + r_b^2 w_c_1 = 0
  369. triplets.push_back(triplet(w_a_0,w_a_0, -1 ));
  370. triplets.push_back(triplet(w_a_0,w_a_1, r_a*r_a ));
  371. triplets.push_back(triplet(w_a_0,w_b_1, 2 * r_a*r_b ));
  372. triplets.push_back(triplet(w_a_0,w_c_1, r_b*r_b ));
  373. // Second term
  374. // 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
  375. triplets.push_back(triplet(w_b_0,w_b_0, -1 ));
  376. triplets.push_back(triplet(w_b_0,w_a_1, r_a*r_c ));
  377. triplets.push_back(triplet(w_b_0,w_b_1, r_b*r_c + r_a*r_d ));
  378. triplets.push_back(triplet(w_b_0,w_c_1, r_b*r_d ));
  379. // Third term
  380. // w_c_0 = r_c^2 w_a + 2 r_c r_d w_b + r_d^2 w_c
  381. triplets.push_back(triplet(w_c_0,w_c_0, -1 ));
  382. triplets.push_back(triplet(w_c_0,w_a_1, r_c*r_c ));
  383. triplets.push_back(triplet(w_c_0,w_b_1, 2 * r_c*r_d ));
  384. triplets.push_back(triplet(w_c_0,w_c_1, r_d*r_d ));
  385. }
  386. }
  387. }
  388. SparseMatrix<double> L(3*F.rows(),3*F.rows());
  389. L.setFromTriplets(triplets.begin(), triplets.end());
  390. triplets.clear();
  391. // Add soft constraints
  392. double w = 100000;
  393. for (unsigned fid=0; fid < F.rows(); ++fid)
  394. {
  395. if (S_c[fid])
  396. {
  397. for (unsigned i=0;i<3;++i)
  398. {
  399. triplets.push_back(triplet(3*fid + i,3*fid + i,w));
  400. b(3*fid + i) += w*S(fid,i);
  401. }
  402. }
  403. }
  404. SparseMatrix<double> soft(3*F.rows(),3*F.rows());
  405. soft.setFromTriplets(triplets.begin(), triplets.end());
  406. SparseMatrix<double> M;
  407. M = L + soft;
  408. // Solve Lx = b;
  409. SparseLU<SparseMatrix<double> > solver;
  410. solver.compute(M);
  411. if(solver.info()!=Success)
  412. {
  413. std::cerr << "LU failed - frame_interpolator.cpp" << std::endl;
  414. assert(0);
  415. }
  416. MatrixXd x;
  417. x = solver.solve(b);
  418. if(solver.info()!=Success)
  419. {
  420. std::cerr << "Linear solve failed - frame_interpolator.cpp" << std::endl;
  421. assert(0);
  422. }
  423. S = MatrixXd::Zero(F.rows(),3);
  424. // Copy back the result
  425. for (unsigned i=0;i<F.rows();++i)
  426. S.row(i) << x(i*3+0), x(i*3+1), x(i*3+2);
  427. }
  428. void FrameInterpolator::setConstraint(const int fid, const Eigen::VectorXd& v)
  429. {
  430. using namespace std;
  431. using namespace Eigen;
  432. double t_;
  433. VectorXd S_;
  434. frame2canonical(TPs[fid],v,t_,S_);
  435. Eigen::RowVectorXd v2;
  436. canonical2frame(TPs[fid], t_, S_, v2);
  437. thetas(fid) = t_;
  438. thetas_c[fid] = true;
  439. S.row(fid) = S_;
  440. S_c[fid] = true;
  441. }
  442. Eigen::MatrixXd FrameInterpolator::getFieldPerFace()
  443. {
  444. using namespace std;
  445. using namespace Eigen;
  446. MatrixXd R(F.rows(),6);
  447. for (unsigned i=0; i<F.rows(); ++i)
  448. {
  449. RowVectorXd v;
  450. canonical2frame(TPs[i],thetas(i),S.row(i),v);
  451. R.row(i) = v;
  452. }
  453. return R;
  454. }
  455. void FrameInterpolator::PolarDecomposition(Eigen::MatrixXd V, Eigen::MatrixXd& U, Eigen::MatrixXd& P)
  456. {
  457. using namespace std;
  458. using namespace Eigen;
  459. // Polar Decomposition
  460. JacobiSVD<MatrixXd> svd(V,Eigen::ComputeFullU | Eigen::ComputeFullV);
  461. U = svd.matrixU() * svd.matrixV().transpose();
  462. P = svd.matrixV() * svd.singularValues().asDiagonal() * svd.matrixV().transpose();
  463. }
  464. }
  465. IGL_INLINE void igl::frame_field(
  466. const Eigen::MatrixXd& V,
  467. const Eigen::MatrixXi& F,
  468. const Eigen::VectorXi& b,
  469. const Eigen::MatrixXd& bc1,
  470. const Eigen::MatrixXd& bc2,
  471. Eigen::MatrixXd& FF1,
  472. Eigen::MatrixXd& FF2
  473. )
  474. {
  475. using namespace std;
  476. using namespace Eigen;
  477. assert(b.size() > 0);
  478. // Init Solver
  479. FrameInterpolator field(V,F);
  480. for (unsigned i=0; i<b.size(); ++i)
  481. {
  482. VectorXd t(6); t << bc1.row(i).transpose(), bc2.row(i).transpose();
  483. field.setConstraint(b(i), t);
  484. }
  485. // Solve
  486. field.solve();
  487. // Copy back
  488. MatrixXd R = field.getFieldPerFace();
  489. FF1 = R.block(0, 0, R.rows(), 3);
  490. FF2 = R.block(0, 3, R.rows(), 3);
  491. }