nrosy.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@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 "nrosy.h"
  9. #include <igl/comiso/nrosy.h>
  10. #include <igl/triangle_triangle_adjacency.h>
  11. #include <igl/edge_topology.h>
  12. #include <igl/per_face_normals.h>
  13. #include <iostream>
  14. #include <fstream>
  15. #include <Eigen/Geometry>
  16. #include <Eigen/Sparse>
  17. #include <queue>
  18. #include <gmm/gmm.h>
  19. #include <CoMISo/Solver/ConstrainedSolver.hh>
  20. #include <CoMISo/Solver/MISolver.hh>
  21. #include <CoMISo/Solver/GMM_Tools.hh>
  22. namespace igl
  23. {
  24. class NRosyField
  25. {
  26. public:
  27. // Init
  28. IGL_INLINE NRosyField(const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F);
  29. // Generate the N-rosy field
  30. // N degree of the rosy field
  31. // roundseparately: round the integer variables one at a time, slower but higher quality
  32. IGL_INLINE void solve(const int N = 4);
  33. // Set a hard constraint on fid
  34. // fid: face id
  35. // v: direction to fix (in 3d)
  36. IGL_INLINE void setConstraintHard(const int fid, const Eigen::Vector3d& v);
  37. // Set a soft constraint on fid
  38. // fid: face id
  39. // w: weight of the soft constraint, clipped between 0 and 1
  40. // v: direction to fix (in 3d)
  41. IGL_INLINE void setConstraintSoft(const int fid, const double w, const Eigen::Vector3d& v);
  42. // Set the ratio between smoothness and soft constraints (0 -> smoothness only, 1 -> soft constr only)
  43. IGL_INLINE void setSoftAlpha(double alpha);
  44. // Reset constraints (at least one constraint must be present or solve will fail)
  45. IGL_INLINE void resetConstraints();
  46. // Return the current field
  47. IGL_INLINE Eigen::MatrixXd getFieldPerFace();
  48. // Return the current field (in Ahish's ffield format)
  49. IGL_INLINE Eigen::MatrixXd getFFieldPerFace();
  50. // Compute singularity indexes
  51. IGL_INLINE void findCones(int N);
  52. // Return the singularities
  53. IGL_INLINE Eigen::VectorXd getSingularityIndexPerVertex();
  54. private:
  55. // Compute angle differences between reference frames
  56. IGL_INLINE void computek();
  57. // Remove useless matchings
  58. IGL_INLINE void reduceSpace();
  59. // Prepare the system matrix
  60. IGL_INLINE void prepareSystemMatrix(const int N);
  61. // Solve without roundings
  62. IGL_INLINE void solveNoRoundings();
  63. // Solve with roundings using CoMIso
  64. IGL_INLINE void solveRoundings();
  65. // Round all p to 0 and fix
  66. IGL_INLINE void roundAndFixToZero();
  67. // Round all p and fix
  68. IGL_INLINE void roundAndFix();
  69. // Convert a vector in 3d to an angle wrt the local reference system
  70. IGL_INLINE double convert3DtoLocal(unsigned fid, const Eigen::Vector3d& v);
  71. // Convert an angle wrt the local reference system to a 3d vector
  72. IGL_INLINE Eigen::Vector3d convertLocalto3D(unsigned fid, double a);
  73. // Compute the per vertex angle defect
  74. IGL_INLINE Eigen::VectorXd angleDefect();
  75. // Temporary variable for the field
  76. Eigen::VectorXd angles;
  77. // Hard constraints
  78. Eigen::VectorXd hard;
  79. std::vector<bool> isHard;
  80. // Soft constraints
  81. Eigen::VectorXd soft;
  82. Eigen::VectorXd wSoft;
  83. double softAlpha;
  84. // Face Topology
  85. Eigen::MatrixXi TT, TTi;
  86. // Edge Topology
  87. Eigen::MatrixXi EV, FE, EF;
  88. std::vector<bool> isBorderEdge;
  89. // Per Edge information
  90. // Angle between two reference frames
  91. Eigen::VectorXd k;
  92. // Jumps
  93. Eigen::VectorXi p;
  94. std::vector<bool> pFixed;
  95. // Mesh
  96. Eigen::MatrixXd V;
  97. Eigen::MatrixXi F;
  98. // Normals per face
  99. Eigen::MatrixXd N;
  100. // Singularity index
  101. Eigen::VectorXd singularityIndex;
  102. // Reference frame per triangle
  103. std::vector<Eigen::MatrixXd> TPs;
  104. // System stuff
  105. Eigen::SparseMatrix<double> A;
  106. Eigen::VectorXd b;
  107. Eigen::VectorXi tag_t;
  108. Eigen::VectorXi tag_p;
  109. };
  110. } // NAMESPACE IGL
  111. igl::NRosyField::NRosyField(const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F)
  112. {
  113. using namespace std;
  114. using namespace Eigen;
  115. V = _V;
  116. F = _F;
  117. assert(V.rows() > 0);
  118. assert(F.rows() > 0);
  119. // Generate topological relations
  120. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  121. igl::edge_topology(V,F, EV, FE, EF);
  122. // Flag border edges
  123. isBorderEdge.resize(EV.rows());
  124. for(unsigned i=0; i<EV.rows(); ++i)
  125. isBorderEdge[i] = (EF(i,0) == -1) || ((EF(i,1) == -1));
  126. // Generate normals per face
  127. igl::per_face_normals(V, F, N);
  128. // Generate reference frames
  129. for(unsigned fid=0; fid<F.rows(); ++fid)
  130. {
  131. // First edge
  132. Vector3d e1 = V.row(F(fid,1)) - V.row(F(fid,0));
  133. e1.normalize();
  134. Vector3d e2 = N.row(fid);
  135. e2 = e2.cross(e1);
  136. e2.normalize();
  137. MatrixXd TP(2,3);
  138. TP << e1.transpose(), e2.transpose();
  139. TPs.push_back(TP);
  140. }
  141. // Alloc internal variables
  142. angles = VectorXd::Zero(F.rows());
  143. p = VectorXi::Zero(EV.rows());
  144. pFixed.resize(EV.rows());
  145. k = VectorXd::Zero(EV.rows());
  146. singularityIndex = VectorXd::Zero(V.rows());
  147. // Reset the constraints
  148. resetConstraints();
  149. // Compute k, differences between reference frames
  150. computek();
  151. softAlpha = 0.5;
  152. }
  153. void igl::NRosyField::setSoftAlpha(double alpha)
  154. {
  155. assert(alpha >= 0 && alpha < 1);
  156. softAlpha = alpha;
  157. }
  158. void igl::NRosyField::prepareSystemMatrix(const int N)
  159. {
  160. using namespace std;
  161. using namespace Eigen;
  162. double Nd = N;
  163. // Minimize the MIQ energy
  164. // Energy on edge ij is
  165. // (t_i - t_j + kij + pij*(2*pi/N))^2
  166. // Partial derivatives:
  167. // t_i: 2 ( t_i - t_j + kij + pij*(2*pi/N)) = 0
  168. // t_j: 2 (-t_i + t_j - kij - pij*(2*pi/N)) = 0
  169. // pij: 4pi/N ( t_i - t_j + kij + pij*(2*pi/N)) = 0
  170. //
  171. // t_i t_j pij kij
  172. // t_i [ 2 -2 4pi/N 2 ]
  173. // t_j [ -2 2 -4pi/N -2 ]
  174. // pij [ 4pi/N -4pi/N 2*(2pi/N)^2 4pi/N ]
  175. // Count and tag the variables
  176. tag_t = VectorXi::Constant(F.rows(),-1);
  177. vector<int> id_t;
  178. int count = 0;
  179. for(unsigned i=0; i<F.rows(); ++i)
  180. if (!isHard[i])
  181. {
  182. tag_t(i) = count++;
  183. id_t.push_back(i);
  184. }
  185. unsigned count_t = id_t.size();
  186. tag_p = VectorXi::Constant(EF.rows(),-1);
  187. vector<int> id_p;
  188. for(unsigned i=0; i<EF.rows(); ++i)
  189. {
  190. if (!pFixed[i])
  191. {
  192. // if it is not fixed then it is a variable
  193. tag_p(i) = count++;
  194. }
  195. // if it is not a border edge,
  196. if (!isBorderEdge[i])
  197. {
  198. // and it is not between two fixed faces
  199. if (!(isHard[EF(i,0)] && isHard[EF(i,1)]))
  200. {
  201. // then it participates in the energy!
  202. id_p.push_back(i);
  203. }
  204. }
  205. }
  206. unsigned count_p = count - count_t;
  207. // System sizes: A (count_t + count_p) x (count_t + count_p)
  208. // b (count_t + count_p)
  209. b = VectorXd::Zero(count_t + count_p);
  210. std::vector<Eigen::Triplet<double> > T;
  211. T.reserve(3 * 4 * count_p);
  212. for(unsigned r=0; r<id_p.size(); ++r)
  213. {
  214. int eid = id_p[r];
  215. int i = EF(eid,0);
  216. int j = EF(eid,1);
  217. bool isFixed_i = isHard[i];
  218. bool isFixed_j = isHard[j];
  219. bool isFixed_p = pFixed[eid];
  220. int row;
  221. // (i)-th row: t_i [ 2 -2 4pi/N 2 ]
  222. if (!isFixed_i)
  223. {
  224. row = tag_t[i];
  225. if (isFixed_i) b(row) += -2 * hard[i]; else T.push_back(Eigen::Triplet<double>(row,tag_t[i] , 2 ));
  226. if (isFixed_j) b(row) += 2 * hard[j]; else T.push_back(Eigen::Triplet<double>(row,tag_t[j] ,-2 ));
  227. if (isFixed_p) b(row) += -((4 * M_PI)/Nd) * p[eid] ; else T.push_back(Eigen::Triplet<double>(row,tag_p[eid],((4 * M_PI)/Nd)));
  228. b(row) += -2 * k[eid];
  229. assert(hard[i] == hard[i]);
  230. assert(hard[j] == hard[j]);
  231. assert(p[eid] == p[eid]);
  232. assert(k[eid] == k[eid]);
  233. assert(b(row) == b(row));
  234. }
  235. // (j)+1 -th row: t_j [ -2 2 -4pi/N -2 ]
  236. if (!isFixed_j)
  237. {
  238. row = tag_t[j];
  239. if (isFixed_i) b(row) += 2 * hard[i]; else T.push_back(Eigen::Triplet<double>(row,tag_t[i] , -2 ));
  240. if (isFixed_j) b(row) += -2 * hard[j]; else T.push_back(Eigen::Triplet<double>(row,tag_t[j] , 2 ));
  241. if (isFixed_p) b(row) += ((4 * M_PI)/Nd) * p[eid] ; else T.push_back(Eigen::Triplet<double>(row,tag_p[eid],-((4 * M_PI)/Nd)));
  242. b(row) += 2 * k[eid];
  243. assert(k[eid] == k[eid]);
  244. assert(b(row) == b(row));
  245. }
  246. // (r*3)+2 -th row: pij [ 4pi/N -4pi/N 2*(2pi/N)^2 4pi/N ]
  247. if (!isFixed_p)
  248. {
  249. row = tag_p[eid];
  250. if (isFixed_i) b(row) += -(4 * M_PI)/Nd * hard[i]; else T.push_back(Eigen::Triplet<double>(row,tag_t[i] , (4 * M_PI)/Nd ));
  251. if (isFixed_j) b(row) += (4 * M_PI)/Nd * hard[j]; else T.push_back(Eigen::Triplet<double>(row,tag_t[j] , -(4 * M_PI)/Nd ));
  252. if (isFixed_p) b(row) += -(2 * pow(((2*M_PI)/Nd),2)) * p[eid] ; else T.push_back(Eigen::Triplet<double>(row,tag_p[eid], (2 * pow(((2*M_PI)/Nd),2))));
  253. b(row) += - (4 * M_PI)/Nd * k[eid];
  254. assert(k[eid] == k[eid]);
  255. assert(b(row) == b(row));
  256. }
  257. }
  258. A = SparseMatrix<double>(count_t + count_p, count_t + count_p);
  259. A.setFromTriplets(T.begin(), T.end());
  260. // Soft constraints
  261. bool addSoft = false;
  262. for(unsigned i=0; i<wSoft.size();++i)
  263. if (wSoft[i] != 0)
  264. addSoft = true;
  265. if (addSoft)
  266. {
  267. cerr << " Adding soft here: " << endl;
  268. cerr << " softAplha: " << softAlpha << endl;
  269. VectorXd bSoft = VectorXd::Zero(count_t + count_p);
  270. std::vector<Eigen::Triplet<double> > TSoft;
  271. TSoft.reserve(2 * count_p);
  272. for(unsigned i=0; i<F.rows(); ++i)
  273. {
  274. int varid = tag_t[i];
  275. if (varid != -1) // if it is a variable in the system
  276. {
  277. TSoft.push_back(Eigen::Triplet<double>(varid,varid,wSoft[i]));
  278. bSoft[varid] += wSoft[i] * soft[i];
  279. }
  280. }
  281. SparseMatrix<double> ASoft(count_t + count_p, count_t + count_p);
  282. ASoft.setFromTriplets(TSoft.begin(), TSoft.end());
  283. // ofstream s("/Users/daniele/As.txt");
  284. // for(unsigned i=0; i<TSoft.size(); ++i)
  285. // s << TSoft[i].row() << " " << TSoft[i].col() << " " << TSoft[i].value() << endl;
  286. // s.close();
  287. // ofstream s2("/Users/daniele/bs.txt");
  288. // for(unsigned i=0; i<bSoft.rows(); ++i)
  289. // s2 << bSoft(i) << endl;
  290. // s2.close();
  291. // Stupid Eigen bug
  292. SparseMatrix<double> Atmp (count_t + count_p, count_t + count_p);
  293. SparseMatrix<double> Atmp2(count_t + count_p, count_t + count_p);
  294. SparseMatrix<double> Atmp3(count_t + count_p, count_t + count_p);
  295. // Merge the two part of the energy
  296. Atmp = (1.0 - softAlpha)*A;
  297. Atmp2 = softAlpha * ASoft;
  298. Atmp3 = Atmp+Atmp2;
  299. A = Atmp3;
  300. b = b*(1.0 - softAlpha) + bSoft * softAlpha;
  301. }
  302. // ofstream s("/Users/daniele/A.txt");
  303. // for (int k=0; k<A.outerSize(); ++k)
  304. // for (SparseMatrix<double>::InnerIterator it(A,k); it; ++it)
  305. // {
  306. // s << it.row() << " " << it.col() << " " << it.value() << endl;
  307. // }
  308. // s.close();
  309. //
  310. // ofstream s2("/Users/daniele/b.txt");
  311. // for(unsigned i=0; i<b.rows(); ++i)
  312. // s2 << b(i) << endl;
  313. // s2.close();
  314. }
  315. void igl::NRosyField::solveNoRoundings()
  316. {
  317. using namespace std;
  318. using namespace Eigen;
  319. // Solve the linear system
  320. SimplicialLDLT<SparseMatrix<double> > solver;
  321. solver.compute(A);
  322. VectorXd x = solver.solve(b);
  323. // Copy the result back
  324. for(unsigned i=0; i<F.rows(); ++i)
  325. if (tag_t[i] != -1)
  326. angles[i] = x(tag_t[i]);
  327. else
  328. angles[i] = hard[i];
  329. for(unsigned i=0; i<EF.rows(); ++i)
  330. if(tag_p[i] != -1)
  331. p[i] = roundl(x[tag_p[i]]);
  332. }
  333. void igl::NRosyField::solveRoundings()
  334. {
  335. using namespace std;
  336. using namespace Eigen;
  337. unsigned n = A.rows();
  338. gmm::col_matrix< gmm::wsvector< double > > gmm_A;
  339. std::vector<double> gmm_b;
  340. std::vector<int> ids_to_round;
  341. std::vector<double> x;
  342. gmm_A.resize(n,n);
  343. gmm_b.resize(n);
  344. x.resize(n);
  345. // Copy A
  346. for (int k=0; k<A.outerSize(); ++k)
  347. for (SparseMatrix<double>::InnerIterator it(A,k); it; ++it)
  348. {
  349. gmm_A(it.row(),it.col()) += it.value();
  350. }
  351. // Copy b
  352. for(unsigned i=0; i<n;++i)
  353. gmm_b[i] = b[i];
  354. // Set variables to round
  355. ids_to_round.clear();
  356. for(unsigned i=0; i<tag_p.size();++i)
  357. if(tag_p[i] != -1)
  358. ids_to_round.push_back(tag_p[i]);
  359. // Empty constraints
  360. gmm::row_matrix< gmm::wsvector< double > > gmm_C(0, n);
  361. COMISO::ConstrainedSolver cs;
  362. //print_miso_settings(cs.misolver());
  363. cs.solve(gmm_C, gmm_A, x, gmm_b, ids_to_round, 0.0, false, true);
  364. // Copy the result back
  365. for(unsigned i=0; i<F.rows(); ++i)
  366. if (tag_t[i] != -1)
  367. angles[i] = x[tag_t[i]];
  368. else
  369. angles[i] = hard[i];
  370. for(unsigned i=0; i<EF.rows(); ++i)
  371. if(tag_p[i] != -1)
  372. p[i] = roundl(x[tag_p[i]]);
  373. }
  374. void igl::NRosyField::roundAndFix()
  375. {
  376. for(unsigned i=0; i<p.rows(); ++i)
  377. pFixed[i] = true;
  378. }
  379. void igl::NRosyField::roundAndFixToZero()
  380. {
  381. for(unsigned i=0; i<p.rows(); ++i)
  382. {
  383. pFixed[i] = true;
  384. p[i] = 0;
  385. }
  386. }
  387. void igl::NRosyField::solve(const int N)
  388. {
  389. // Reduce the search space by fixing matchings
  390. reduceSpace();
  391. // Build the system
  392. prepareSystemMatrix(N);
  393. // Solve with integer roundings
  394. solveRoundings();
  395. // This is a very greedy solving strategy
  396. // // Solve with no roundings
  397. // solveNoRoundings();
  398. //
  399. // // Round all p and fix them
  400. // roundAndFix();
  401. //
  402. // // Build the system
  403. // prepareSystemMatrix(N);
  404. //
  405. // // Solve with no roundings (they are all fixed)
  406. // solveNoRoundings();
  407. // Find the cones
  408. findCones(N);
  409. }
  410. void igl::NRosyField::setConstraintHard(const int fid, const Eigen::Vector3d& v)
  411. {
  412. isHard[fid] = true;
  413. hard(fid) = convert3DtoLocal(fid, v);
  414. }
  415. void igl::NRosyField::setConstraintSoft(const int fid, const double w, const Eigen::Vector3d& v)
  416. {
  417. wSoft(fid) = w;
  418. soft(fid) = convert3DtoLocal(fid, v);
  419. }
  420. void igl::NRosyField::resetConstraints()
  421. {
  422. using namespace std;
  423. using namespace Eigen;
  424. isHard.resize(F.rows());
  425. for(unsigned i=0; i<F.rows(); ++i)
  426. isHard[i] = false;
  427. hard = VectorXd::Zero(F.rows());
  428. wSoft = VectorXd::Zero(F.rows());
  429. soft = VectorXd::Zero(F.rows());
  430. }
  431. Eigen::MatrixXd igl::NRosyField::getFieldPerFace()
  432. {
  433. using namespace std;
  434. using namespace Eigen;
  435. MatrixXd result(F.rows(),3);
  436. for(unsigned i=0; i<F.rows(); ++i)
  437. result.row(i) = convertLocalto3D(i, angles(i));
  438. return result;
  439. }
  440. Eigen::MatrixXd igl::NRosyField::getFFieldPerFace()
  441. {
  442. using namespace std;
  443. using namespace Eigen;
  444. MatrixXd result(F.rows(),6);
  445. for(unsigned i=0; i<F.rows(); ++i)
  446. {
  447. Vector3d v1 = convertLocalto3D(i, angles(i));
  448. Vector3d n = N.row(i);
  449. Vector3d v2 = n.cross(v1);
  450. v1.normalize();
  451. v2.normalize();
  452. result.block(i,0,1,3) = v1.transpose();
  453. result.block(i,3,1,3) = v2.transpose();
  454. }
  455. return result;
  456. }
  457. void igl::NRosyField::computek()
  458. {
  459. using namespace std;
  460. using namespace Eigen;
  461. // For every non-border edge
  462. for (unsigned eid=0; eid<EF.rows(); ++eid)
  463. {
  464. if (!isBorderEdge[eid])
  465. {
  466. int fid0 = EF(eid,0);
  467. int fid1 = EF(eid,1);
  468. Vector3d N0 = N.row(fid0);
  469. Vector3d N1 = N.row(fid1);
  470. // find common edge on triangle 0 and 1
  471. int fid0_vc = -1;
  472. int fid1_vc = -1;
  473. for (unsigned i=0;i<3;++i)
  474. {
  475. if (EV(eid,0) == F(fid0,i))
  476. fid0_vc = i;
  477. if (EV(eid,1) == F(fid1,i))
  478. fid1_vc = i;
  479. }
  480. assert(fid0_vc != -1);
  481. assert(fid1_vc != -1);
  482. Vector3d common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  483. common_edge.normalize();
  484. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  485. MatrixXd P(3,3);
  486. VectorXd o = V.row(F(fid0,fid0_vc));
  487. VectorXd tmp = -N0.cross(common_edge);
  488. P << common_edge, tmp, N0;
  489. P.transposeInPlace();
  490. MatrixXd V0(3,3);
  491. V0.row(0) = V.row(F(fid0,0)).transpose() -o;
  492. V0.row(1) = V.row(F(fid0,1)).transpose() -o;
  493. V0.row(2) = V.row(F(fid0,2)).transpose() -o;
  494. V0 = (P*V0.transpose()).transpose();
  495. assert(V0(0,2) < 10e-10);
  496. assert(V0(1,2) < 10e-10);
  497. assert(V0(2,2) < 10e-10);
  498. MatrixXd V1(3,3);
  499. V1.row(0) = V.row(F(fid1,0)).transpose() -o;
  500. V1.row(1) = V.row(F(fid1,1)).transpose() -o;
  501. V1.row(2) = V.row(F(fid1,2)).transpose() -o;
  502. V1 = (P*V1.transpose()).transpose();
  503. assert(V1(fid1_vc,2) < 10e-10);
  504. assert(V1((fid1_vc+1)%3,2) < 10e-10);
  505. // compute rotation R such that R * N1 = N0
  506. // i.e. map both triangles to the same plane
  507. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  508. MatrixXd R(3,3);
  509. R << 1, 0, 0,
  510. 0, cos(alpha), -sin(alpha) ,
  511. 0, sin(alpha), cos(alpha);
  512. V1 = (R*V1.transpose()).transpose();
  513. assert(V1(0,2) < 10e-10);
  514. assert(V1(1,2) < 10e-10);
  515. assert(V1(2,2) < 10e-10);
  516. // measure the angle between the reference frames
  517. // k_ij is the angle between the triangle on the left and the one on the right
  518. VectorXd ref0 = V0.row(1) - V0.row(0);
  519. VectorXd ref1 = V1.row(1) - V1.row(0);
  520. ref0.normalize();
  521. ref1.normalize();
  522. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  523. // just to be sure, rotate ref0 using angle ktemp...
  524. MatrixXd R2(2,2);
  525. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  526. tmp = R2*ref0.head<2>();
  527. assert(tmp(0) - ref1(0) < 10^10);
  528. assert(tmp(1) - ref1(1) < 10^10);
  529. k[eid] = ktemp;
  530. }
  531. }
  532. }
  533. void igl::NRosyField::reduceSpace()
  534. {
  535. using namespace std;
  536. using namespace Eigen;
  537. // All variables are free in the beginning
  538. for(unsigned i=0; i<EV.rows(); ++i)
  539. pFixed[i] = false;
  540. vector<VectorXd> debug;
  541. // debug
  542. // MatrixXd B(F.rows(),3);
  543. // for(unsigned i=0; i<F.rows(); ++i)
  544. // B.row(i) = 1./3. * (V.row(F(i,0)) + V.row(F(i,1)) + V.row(F(i,2)));
  545. vector<bool> visited(EV.rows());
  546. for(unsigned i=0; i<EV.rows(); ++i)
  547. visited[i] = false;
  548. vector<bool> starting(EV.rows());
  549. for(unsigned i=0; i<EV.rows(); ++i)
  550. starting[i] = false;
  551. queue<int> q;
  552. for(unsigned i=0; i<F.rows(); ++i)
  553. if (isHard[i] || wSoft[i] != 0)
  554. {
  555. q.push(i);
  556. starting[i] = true;
  557. }
  558. // Reduce the search space (see MI paper)
  559. while (!q.empty())
  560. {
  561. int c = q.front();
  562. q.pop();
  563. visited[c] = true;
  564. for(int i=0; i<3; ++i)
  565. {
  566. int eid = FE(c,i);
  567. int fid = TT(c,i);
  568. // skip borders
  569. if (fid != -1)
  570. {
  571. assert((EF(eid,0) == c && EF(eid,1) == fid) || (EF(eid,1) == c && EF(eid,0) == fid));
  572. // for every neighbouring face
  573. if (!visited[fid] && !starting[fid])
  574. {
  575. pFixed[eid] = true;
  576. p[eid] = 0;
  577. visited[fid] = true;
  578. q.push(fid);
  579. }
  580. }
  581. else
  582. {
  583. // fix borders
  584. pFixed[eid] = true;
  585. p[eid] = 0;
  586. }
  587. }
  588. }
  589. // Force matchings between fixed faces
  590. for(unsigned i=0; i<F.rows();++i)
  591. {
  592. if (isHard[i])
  593. {
  594. for(unsigned int j=0; j<3; ++j)
  595. {
  596. int fid = TT(i,j);
  597. if ((fid!=-1) && (isHard[fid]))
  598. {
  599. // i and fid are adjacent and fixed
  600. int eid = FE(i,j);
  601. int fid0 = EF(eid,0);
  602. int fid1 = EF(eid,1);
  603. pFixed[eid] = true;
  604. p[eid] = roundl(2.0/M_PI*(hard(fid1) - hard(fid0) - k(eid)));
  605. }
  606. }
  607. }
  608. }
  609. // std::ofstream s("/Users/daniele/debug.txt");
  610. // for(unsigned i=0; i<debug.size(); i += 2)
  611. // s << debug[i].transpose() << " " << debug[i+1].transpose() << endl;
  612. // s.close();
  613. }
  614. double igl::NRosyField::convert3DtoLocal(unsigned fid, const Eigen::Vector3d& v)
  615. {
  616. using namespace std;
  617. using namespace Eigen;
  618. // Project onto the tangent plane
  619. Vector2d vp = TPs[fid] * v;
  620. // Convert to angle
  621. return atan2(vp(1),vp(0));
  622. }
  623. Eigen::Vector3d igl::NRosyField::convertLocalto3D(unsigned fid, double a)
  624. {
  625. using namespace std;
  626. using namespace Eigen;
  627. Vector2d vp(cos(a),sin(a));
  628. return vp.transpose() * TPs[fid];
  629. }
  630. Eigen::VectorXd igl::NRosyField::angleDefect()
  631. {
  632. Eigen::VectorXd A = Eigen::VectorXd::Constant(V.rows(),-2*M_PI);
  633. for (unsigned i=0; i < F.rows(); ++i)
  634. {
  635. for (int j = 0; j < 3; ++j)
  636. {
  637. Eigen::VectorXd a = V.row(F(i,(j+1)%3)) - V.row(F(i,j));
  638. Eigen::VectorXd b = V.row(F(i,(j+2)%3)) - V.row(F(i,j));
  639. double t = a.transpose()*b;
  640. t /= (a.norm() * b.norm());
  641. A(F(i,j)) += acos(t);
  642. }
  643. }
  644. return A;
  645. }
  646. void igl::NRosyField::findCones(int N)
  647. {
  648. // Compute I0, see http://www.graphics.rwth-aachen.de/media/papers/bommes_zimmer_2009_siggraph_011.pdf for details
  649. Eigen::VectorXd I0 = Eigen::VectorXd::Zero(V.rows());
  650. // first the k
  651. for (unsigned i=0; i < EV.rows(); ++i)
  652. {
  653. if (!isBorderEdge[i])
  654. {
  655. I0(EV(i,0)) -= k(i);
  656. I0(EV(i,1)) += k(i);
  657. }
  658. }
  659. // then the A
  660. Eigen::VectorXd A = angleDefect();
  661. I0 = I0 + A;
  662. // normalize
  663. I0 = I0 / (2*M_PI);
  664. // round to integer (remove numerical noise)
  665. for (unsigned i=0; i < I0.size(); ++i)
  666. I0(i) = round(I0(i));
  667. // compute I
  668. Eigen::VectorXd I = I0;
  669. for (unsigned i=0; i < EV.rows(); ++i)
  670. {
  671. if (!isBorderEdge[i])
  672. {
  673. I(EV(i,0)) -= double(p(i))/double(N);
  674. I(EV(i,1)) += double(p(i))/double(N);
  675. }
  676. }
  677. // Clear the vertices on the edges
  678. for (unsigned i=0; i < EV.rows(); ++i)
  679. {
  680. if (isBorderEdge[i])
  681. {
  682. I0(EV(i,0)) = 0;
  683. I0(EV(i,1)) = 0;
  684. I(EV(i,0)) = 0;
  685. I(EV(i,1)) = 0;
  686. A(EV(i,0)) = 0;
  687. A(EV(i,1)) = 0;
  688. }
  689. }
  690. singularityIndex = I;
  691. }
  692. Eigen::VectorXd igl::NRosyField::getSingularityIndexPerVertex()
  693. {
  694. return singularityIndex;
  695. }
  696. IGL_INLINE void igl::nrosy(
  697. const Eigen::MatrixXd& V,
  698. const Eigen::MatrixXi& F,
  699. const Eigen::VectorXi& b,
  700. const Eigen::MatrixXd& bc,
  701. const Eigen::VectorXi& b_soft,
  702. const Eigen::VectorXd& w_soft,
  703. const Eigen::MatrixXd& bc_soft,
  704. const int N,
  705. const double soft,
  706. Eigen::MatrixXd& R,
  707. Eigen::VectorXd& S
  708. )
  709. {
  710. // Init solver
  711. igl::NRosyField solver(V,F);
  712. // Add hard constraints
  713. for (unsigned i=0; i<b.size();++i)
  714. solver.setConstraintHard(b(i),bc.row(i));
  715. // Add soft constraints
  716. for (unsigned i=0; i<b_soft.size();++i)
  717. solver.setConstraintSoft(b_soft(i),w_soft(i),bc_soft.row(i));
  718. // Set the soft constraints global weight
  719. solver.setSoftAlpha(soft);
  720. // Interpolate
  721. solver.solve(N);
  722. // Copy the result back
  723. R = solver.getFieldPerFace();
  724. // Extract singularity indices
  725. S = solver.getSingularityIndexPerVertex();
  726. }
  727. IGL_INLINE void igl::nrosy(
  728. const Eigen::MatrixXd& V,
  729. const Eigen::MatrixXi& F,
  730. const Eigen::VectorXi& b,
  731. const Eigen::MatrixXd& bc,
  732. const int N,
  733. Eigen::MatrixXd& R,
  734. Eigen::VectorXd& S
  735. )
  736. {
  737. // Init solver
  738. igl::NRosyField solver(V,F);
  739. // Add hard constraints
  740. for (unsigned i=0; i<b.size();++i)
  741. solver.setConstraintHard(b(i),bc.row(i));
  742. // Interpolate
  743. solver.solve(N);
  744. // Copy the result back
  745. R = solver.getFieldPerFace();
  746. // Extract singularity indices
  747. S = solver.getSingularityIndexPerVertex();
  748. }