nrosy.cpp 23 KB

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