angle_bound_frame_fields.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Olga Diamanti <olga.diam@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 <igl/angle_bound_frame_fields.h>
  9. #include <igl/edge_topology.h>
  10. #include <igl/local_basis.h>
  11. #include <igl/sparse.h>
  12. #include <igl/speye.h>
  13. #include <igl/slice.h>
  14. #include <igl/polyroots.h>
  15. #include <igl/colon.h>
  16. #include <Eigen/Sparse>
  17. #include <iostream>
  18. namespace igl {
  19. template <typename DerivedV, typename DerivedF>
  20. class AngleBoundFFSolverData
  21. {
  22. public:
  23. const Eigen::PlainObjectBase<DerivedV> &V; int numV;
  24. const Eigen::PlainObjectBase<DerivedF> &F; int numF;
  25. Eigen::MatrixXi EV; int numE;
  26. Eigen::MatrixXi F2E;
  27. Eigen::MatrixXi E2F;
  28. Eigen::VectorXd K;
  29. Eigen::VectorXi isBorderEdge;
  30. int numInteriorEdges;
  31. Eigen::Matrix<int,Eigen::Dynamic,2> E2F_int;
  32. Eigen::VectorXi indInteriorToFull;
  33. Eigen::VectorXi indFullToInterior;
  34. Eigen::PlainObjectBase<DerivedV> B1, B2, FN;
  35. //laplacians
  36. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar>> DDA, DDB;
  37. private:
  38. IGL_INLINE void computeLaplacians();
  39. IGL_INLINE void computek();
  40. IGL_INLINE void computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D);
  41. IGL_INLINE void precomputeInteriorEdges();
  42. public:
  43. IGL_INLINE AngleBoundFFSolverData(const Eigen::PlainObjectBase<DerivedV> &_V,
  44. const Eigen::PlainObjectBase<DerivedF> &_F);
  45. };
  46. template <typename DerivedV, typename DerivedF, typename DerivedO>
  47. class AngleBoundFFSolver
  48. {
  49. public:
  50. IGL_INLINE AngleBoundFFSolver(const AngleBoundFFSolverData<DerivedV, DerivedF> &_data,
  51. const typename DerivedV::Scalar &_thetaMin = 30,
  52. int _maxIter = 50,
  53. const typename DerivedV::Scalar &_lambdaInit = 100,
  54. const typename DerivedV::Scalar &_lambdaMultFactor = 1.01,
  55. const bool _doHardConstraints = false);
  56. IGL_INLINE bool solve(const Eigen::VectorXi &isConstrained,
  57. const Eigen::PlainObjectBase<DerivedO> &initialSolution,
  58. Eigen::PlainObjectBase<DerivedO> &output,
  59. typename DerivedV::Scalar *lambdaOut = NULL);
  60. private:
  61. const AngleBoundFFSolverData<DerivedV, DerivedF> &data;
  62. //polyVF data
  63. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> Acoeff, Bcoeff;
  64. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> pvU, pvV;
  65. typename DerivedV::Scalar lambda;
  66. //parameters
  67. typename DerivedV::Scalar lambdaInit,lambdaMultFactor;
  68. int maxIter;
  69. typename DerivedV::Scalar thetaMin;
  70. bool doHardConstraints;
  71. typename DerivedV::Scalar computeAngle(const std::complex<typename DerivedV::Scalar> &u,
  72. const std::complex<typename DerivedV::Scalar> &v);
  73. // IGL_INLINE void computeAngles(Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &angles);
  74. IGL_INLINE int getNumOutOfBounds();
  75. IGL_INLINE void rotateAroundBisector(const std::complex<typename DerivedV::Scalar> &uin,
  76. const std::complex<typename DerivedV::Scalar> &vin,
  77. const typename DerivedV::Scalar theta,
  78. std::complex<typename DerivedV::Scalar> &uout,
  79. std::complex<typename DerivedV::Scalar> &vout);
  80. IGL_INLINE void localStep();
  81. IGL_INLINE void globalStep(const Eigen::Matrix<int, Eigen::Dynamic, 1> &isConstrained,
  82. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &Ak,
  83. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &Bk);
  84. IGL_INLINE void minQuadWithKnownMini(const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &Q,
  85. const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &f,
  86. const Eigen::VectorXi isConstrained,
  87. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &xknown,
  88. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &x);
  89. IGL_INLINE void setFieldFromCoefficients();
  90. IGL_INLINE void setCoefficientsFromField();
  91. };
  92. }
  93. //Implementation
  94. /***************************** Data ***********************************/
  95. template <typename DerivedV, typename DerivedF>
  96. IGL_INLINE igl::AngleBoundFFSolverData<DerivedV, DerivedF>::
  97. AngleBoundFFSolverData(const Eigen::PlainObjectBase<DerivedV> &_V,
  98. const Eigen::PlainObjectBase<DerivedF> &_F):
  99. V(_V),
  100. numV(_V.rows()),
  101. F(_F),
  102. numF(_F.rows())
  103. {
  104. igl::edge_topology(V,F,EV,F2E,E2F);
  105. numE = EV.rows();
  106. precomputeInteriorEdges();
  107. igl::local_basis(V,F,B1,B2,FN);
  108. computek();
  109. computeLaplacians();
  110. };
  111. template <typename DerivedV, typename DerivedF>
  112. IGL_INLINE void igl::AngleBoundFFSolverData<DerivedV, DerivedF>::computeLaplacians()
  113. {
  114. computeCoefficientLaplacian(2, DDA);
  115. computeCoefficientLaplacian(4, DDB);
  116. }
  117. template<typename DerivedV, typename DerivedF>
  118. IGL_INLINE void igl::AngleBoundFFSolverData<DerivedV, DerivedF>::
  119. precomputeInteriorEdges()
  120. {
  121. // Flag border edges
  122. numInteriorEdges = 0;
  123. isBorderEdge.setZero(numE,1);
  124. indFullToInterior = -1.*Eigen::VectorXi::Ones(numE,1);
  125. for(unsigned i=0; i<numE; ++i)
  126. {
  127. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  128. isBorderEdge[i] = 1;
  129. else
  130. {
  131. indFullToInterior[i] = numInteriorEdges;
  132. numInteriorEdges++;
  133. }
  134. }
  135. E2F_int.resize(numInteriorEdges, 2);
  136. indInteriorToFull.setZero(numInteriorEdges,1);
  137. int ii = 0;
  138. for (int k=0; k<numE; ++k)
  139. {
  140. if (isBorderEdge[k])
  141. continue;
  142. E2F_int.row(ii) = E2F.row(k);
  143. indInteriorToFull[ii] = k;
  144. ii++;
  145. }
  146. }
  147. template<typename DerivedV, typename DerivedF>
  148. IGL_INLINE void igl::AngleBoundFFSolverData<DerivedV, DerivedF>::
  149. computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D)
  150. {
  151. std::vector<Eigen::Triplet<std::complex<typename DerivedV::Scalar> >> tripletList;
  152. // For every non-border edge
  153. for (unsigned eid=0; eid<numE; ++eid)
  154. {
  155. if (!isBorderEdge[eid])
  156. {
  157. int fid0 = E2F(eid,0);
  158. int fid1 = E2F(eid,1);
  159. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  160. fid0,
  161. std::complex<typename DerivedV::Scalar>(1.)));
  162. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  163. fid1,
  164. std::complex<typename DerivedV::Scalar>(1.)));
  165. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  166. fid1,
  167. -1.*std::polar(1.,-1.*n*K[eid])));
  168. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  169. fid0,
  170. -1.*std::polar(1.,1.*n*K[eid])));
  171. }
  172. }
  173. D.resize(numF,numF);
  174. D.setFromTriplets(tripletList.begin(), tripletList.end());
  175. }
  176. template<typename DerivedV, typename DerivedF>
  177. IGL_INLINE void igl::AngleBoundFFSolverData<DerivedV, DerivedF>::
  178. computek()
  179. {
  180. K.setZero(numE);
  181. // For every non-border edge
  182. for (unsigned eid=0; eid<numE; ++eid)
  183. {
  184. if (!isBorderEdge[eid])
  185. {
  186. int fid0 = E2F(eid,0);
  187. int fid1 = E2F(eid,1);
  188. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N0 = FN.row(fid0);
  189. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N1 = FN.row(fid1);
  190. // find common edge on triangle 0 and 1
  191. int fid0_vc = -1;
  192. int fid1_vc = -1;
  193. for (unsigned i=0;i<3;++i)
  194. {
  195. if (F2E(fid0,i) == eid)
  196. fid0_vc = i;
  197. if (F2E(fid1,i) == eid)
  198. fid1_vc = i;
  199. }
  200. assert(fid0_vc != -1);
  201. assert(fid1_vc != -1);
  202. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  203. common_edge.normalize();
  204. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  205. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> P;
  206. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> o = V.row(F(fid0,fid0_vc));
  207. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> tmp = -N0.cross(common_edge);
  208. P << common_edge, tmp, N0;
  209. // P.transposeInPlace();
  210. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V0;
  211. V0.row(0) = V.row(F(fid0,0)) -o;
  212. V0.row(1) = V.row(F(fid0,1)) -o;
  213. V0.row(2) = V.row(F(fid0,2)) -o;
  214. V0 = (P*V0.transpose()).transpose();
  215. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V1;
  216. V1.row(0) = V.row(F(fid1,0)) -o;
  217. V1.row(1) = V.row(F(fid1,1)) -o;
  218. V1.row(2) = V.row(F(fid1,2)) -o;
  219. V1 = (P*V1.transpose()).transpose();
  220. // compute rotation R such that R * N1 = N0
  221. // i.e. map both triangles to the same plane
  222. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  223. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> R;
  224. R << 1, 0, 0,
  225. 0, cos(alpha), -sin(alpha) ,
  226. 0, sin(alpha), cos(alpha);
  227. V1 = (R*V1.transpose()).transpose();
  228. // measure the angle between the reference frames
  229. // k_ij is the angle between the triangle on the left and the one on the right
  230. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref0 = V0.row(1) - V0.row(0);
  231. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref1 = V1.row(1) - V1.row(0);
  232. ref0.normalize();
  233. ref1.normalize();
  234. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  235. // just to be sure, rotate ref0 using angle ktemp...
  236. Eigen::Matrix<typename DerivedV::Scalar, 2, 2> R2;
  237. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  238. Eigen::Matrix<typename DerivedV::Scalar, 1, 2> tmp1 = R2*(ref0.head(2)).transpose();
  239. K[eid] = ktemp;
  240. }
  241. }
  242. }
  243. /***************************** Solver ***********************************/
  244. template <typename DerivedV, typename DerivedF, typename DerivedO>
  245. IGL_INLINE igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  246. AngleBoundFFSolver(const AngleBoundFFSolverData<DerivedV, DerivedF> &_data,
  247. const typename DerivedV::Scalar &_thetaMin,
  248. int _maxIter,
  249. const typename DerivedV::Scalar &_lambdaInit,
  250. const typename DerivedV::Scalar &_lambdaMultFactor,
  251. const bool _doHardConstraints):
  252. data(_data),
  253. lambdaInit(_lambdaInit),
  254. maxIter(_maxIter),
  255. lambdaMultFactor(_lambdaMultFactor),
  256. doHardConstraints(_doHardConstraints),
  257. thetaMin(_thetaMin)
  258. {
  259. Acoeff.resize(data.numF,1);
  260. Bcoeff.resize(data.numF,1);
  261. pvU.setZero(data.numF, 2);
  262. pvV.setZero(data.numF, 2);
  263. };
  264. template<typename DerivedV, typename DerivedF, typename DerivedO>
  265. IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  266. rotateAroundBisector(const std::complex<typename DerivedV::Scalar> &uin,
  267. const std::complex<typename DerivedV::Scalar> &vin,
  268. const typename DerivedV::Scalar diff,
  269. std::complex<typename DerivedV::Scalar> &uout,
  270. std::complex<typename DerivedV::Scalar> &vout)
  271. {
  272. //rotate 2D complex vectors u and v around their bisector so that their
  273. //angle is at least theta
  274. uout = uin;
  275. vout = vin;
  276. typename DerivedV::Scalar au = arg(uin);
  277. typename DerivedV::Scalar av = arg(vin);
  278. if (au<av)
  279. {
  280. uout = std::polar (1.0,-.5*diff)*uin;
  281. vout = std::polar (1.0, .5*diff)*vin;
  282. }
  283. else
  284. {
  285. uout = std::polar (1.0, .5*diff)*uin;
  286. vout = std::polar (1.0,-.5*diff)*vin;
  287. }
  288. }
  289. template<typename DerivedV, typename DerivedF, typename DerivedO>
  290. IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  291. localStep()
  292. {
  293. for (int j =0; j<data.numF; ++j)
  294. {
  295. std::complex<typename DerivedV::Scalar> u(pvU(j,0),pvU(j,1));
  296. std::complex<typename DerivedV::Scalar> v(pvV(j,0),pvV(j,1));
  297. typename DerivedV::Scalar current_angle = computeAngle(u, v);
  298. if (current_angle<thetaMin*M_PI/180)
  299. {
  300. // bring all to 1st or 4th quarter plane
  301. if ((arg(u)>=0.5*M_PI || arg(u)<-0.5*M_PI ))
  302. u = -u;
  303. if ((arg(v)>=0.5*M_PI || arg(v)<-0.5*M_PI ))
  304. v = -v;
  305. assert(fabs(computeAngle(u, v) - current_angle)<1e-5);
  306. if ( fabs(arg(u) - arg(v)) >0.5*M_PI )
  307. v = -v;
  308. assert(fabs(computeAngle(u, v) - current_angle)<1e-5);
  309. std::complex<typename DerivedV::Scalar> u1, v1;
  310. typename DerivedV::Scalar diff = thetaMin*M_PI/180 - current_angle + 1e-6;
  311. rotateAroundBisector(u, v, diff, u1, v1);
  312. // if (computeAngle(u1, v1)<thetaMin*M_PI/180)
  313. // {
  314. // std::cerr<<"u = ["<<real(u)<<","<<imag(u)<< "]; v= ["<<real(v)<<","<<imag(v)<<"];"<<std::endl;
  315. // std::cerr<<"u1 = ["<<real(u1)<<","<<imag(u1)<< "]; v1= ["<<real(v1)<<","<<imag(v1)<<"];"<<std::endl;
  316. // std::cerr<<"current_angle = "<<current_angle<<std::endl;
  317. // std::cerr<<"aout = "<<computeAngle(u1, v1)<< "; theta= "<<thetaMin*M_PI/180<<";"<<std::endl;
  318. // }
  319. // assert(computeAngle(u1, v1)>=thetaMin*M_PI/180);
  320. pvU.row(j) << real(u1),imag(u1);
  321. pvV.row(j) << real(v1),imag(v1);
  322. }
  323. }
  324. }
  325. //
  326. //template<typename DerivedV, typename DerivedF, typename DerivedO>
  327. //IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  328. //computeAngles(Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &angles)
  329. //{
  330. // angles.resize(data.numF,1);
  331. // for (int i =0; i<data.numF; ++i)
  332. // {
  333. // std::complex<typename DerivedV::Scalar> u(pvU(i,0),pvU(i,1));
  334. // std::complex<typename DerivedV::Scalar> v(pvV(i,0),pvV(i,1));
  335. // angles[i] = fabs(arg(u) - arg(v));
  336. // if (angles[i]>M_PI)
  337. // angles[i] = 2*M_PI-angles[i];
  338. // if (angles[i]>.5*M_PI)
  339. // angles[i] = M_PI-angles[i];
  340. // }
  341. //}
  342. template<typename DerivedV, typename DerivedF, typename DerivedO>
  343. IGL_INLINE typename DerivedV::Scalar igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  344. computeAngle(const std::complex<typename DerivedV::Scalar> &u,
  345. const std::complex<typename DerivedV::Scalar> &v)
  346. {
  347. typename DerivedV::Scalar angle = std::min(fabs(arg(u*conj(v))), fabs(arg(u*conj(-v))));
  348. // typename DerivedV::Scalar angle;
  349. // typename DerivedV::Scalar a1 = fabs(arg(u*conj(v)));
  350. // typename DerivedV::Scalar a2 = fabs(arg(u*conj(-v)));
  351. // if (a1 < a2)
  352. // angle = a1;
  353. // else
  354. // {
  355. // angle = a2; v = -v;
  356. // }
  357. // typename DerivedV::Scalar angle = fabs(arg(u) - arg(v));
  358. // if (angle>M_PI)
  359. // {
  360. // u = -u;
  361. // angle = fabs(arg(u) - arg(v));
  362. // };
  363. //
  364. // if (angle>.5*M_PI)
  365. // {
  366. // v = -v;
  367. // angle = fabs(arg(u) - arg(v));
  368. // };
  369. //
  370. // assert(fabs(angle-angle1)<1e-6);
  371. // if (angle>M_PI)
  372. // angle = 2*M_PI-angle;
  373. // if (angle>.5*M_PI)
  374. // angle = M_PI-angle;
  375. // typename DerivedV::Scalar angle = fabs(arg(u) - arg(v));
  376. // if (angle>M_PI)
  377. // angle = 2*M_PI-angle;
  378. // if (angle>.5*M_PI)
  379. // angle = M_PI-angle;
  380. assert(angle <= .5*M_PI && angle >0);
  381. return angle;
  382. }
  383. template<typename DerivedV, typename DerivedF, typename DerivedO>
  384. IGL_INLINE int igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  385. getNumOutOfBounds()
  386. {
  387. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> angles;
  388. // computeAngles(angles);
  389. int numOoB = 0;
  390. for (int i =0; i<data.numF; ++i)
  391. {
  392. std::complex<typename DerivedV::Scalar> u(pvU(i,0),pvU(i,1));
  393. std::complex<typename DerivedV::Scalar> v(pvV(i,0),pvV(i,1));
  394. typename DerivedV::Scalar angle = computeAngle(u,v);
  395. // if (angles[i] <thetaMin*M_PI/180)
  396. if (angle <thetaMin*M_PI/180)
  397. numOoB ++;
  398. }
  399. return numOoB;
  400. }
  401. template<typename DerivedV, typename DerivedF, typename DerivedO>
  402. IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  403. setCoefficientsFromField()
  404. {
  405. for (int i = 0; i <data.numF; ++i)
  406. {
  407. std::complex<typename DerivedV::Scalar> u(pvU(i,0),pvU(i,1));
  408. std::complex<typename DerivedV::Scalar> v(pvV(i,0),pvV(i,1));
  409. Acoeff(i) = u*u+v*v;
  410. Bcoeff(i) = u*u*v*v;
  411. }
  412. }
  413. template<typename DerivedV, typename DerivedF, typename DerivedO>
  414. IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  415. globalStep(const Eigen::Matrix<int, Eigen::Dynamic, 1> &isConstrained,
  416. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &Ak,
  417. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &Bk)
  418. {
  419. setCoefficientsFromField();
  420. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > I;
  421. igl::speye(data.numF, data.numF, I);
  422. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > QA = data.DDA+lambda*I;
  423. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > fA = (-2*lambda*Acoeff).sparseView();
  424. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > QB = data.DDB+lambda*I;
  425. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > fB = (-2*lambda*I*Bcoeff).sparseView();
  426. if(doHardConstraints)
  427. {
  428. minQuadWithKnownMini(QA, fA, isConstrained, Ak, Acoeff);
  429. minQuadWithKnownMini(QB, fB, isConstrained, Bk, Bcoeff);
  430. }
  431. else
  432. {
  433. Eigen::Matrix<int, Eigen::Dynamic, 1>isknown_; isknown_.setZero(data.numF,1);
  434. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> xknown_; xknown_.setZero(0,1);
  435. minQuadWithKnownMini(QA, fA, isknown_, xknown_, Acoeff);
  436. minQuadWithKnownMini(QB, fB, isknown_, xknown_, Bcoeff);
  437. }
  438. setFieldFromCoefficients();
  439. }
  440. template<typename DerivedV, typename DerivedF, typename DerivedO>
  441. IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  442. setFieldFromCoefficients()
  443. {
  444. for (int i = 0; i <data.numF; ++i)
  445. {
  446. // poly coefficients: 1, 0, -Acoeff, 0, Bcoeff
  447. // matlab code from roots (given there are no trailing zeros in the polynomial coefficients)
  448. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> polyCoeff(5,1);
  449. polyCoeff<<1., 0., -Acoeff(i), 0., Bcoeff(i);
  450. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> roots;
  451. polyRoots<std::complex<typename DerivedV::Scalar>>(polyCoeff,roots);
  452. std::complex<typename DerivedV::Scalar> u = roots[0];
  453. int maxi = -1;
  454. float maxd = -1;
  455. for (int k =1; k<4; ++k)
  456. {
  457. float dist = abs(roots[k]+u);
  458. if (dist>maxd)
  459. {
  460. maxd = dist;
  461. maxi = k;
  462. }
  463. }
  464. std::complex<typename DerivedV::Scalar> v = roots[maxi];
  465. pvU(i,0) = real(u); pvU(i,1) = imag(u);
  466. pvV(i,0) = real(v); pvV(i,1) = imag(v);
  467. }
  468. }
  469. template<typename DerivedV, typename DerivedF, typename DerivedO>
  470. IGL_INLINE void igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  471. minQuadWithKnownMini(const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &Q,
  472. const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &f,
  473. const Eigen::VectorXi isConstrained,
  474. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &xknown,
  475. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &x)
  476. {
  477. int N = Q.rows();
  478. int nc = xknown.rows();
  479. Eigen::VectorXi known; known.setZero(nc,1);
  480. Eigen::VectorXi unknown; unknown.setZero(N-nc,1);
  481. int indk = 0, indu = 0;
  482. for (int i = 0; i<N; ++i)
  483. if (isConstrained[i])
  484. {
  485. known[indk] = i;
  486. indk++;
  487. }
  488. else
  489. {
  490. unknown[indu] = i;
  491. indu++;
  492. }
  493. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar>> Quu, Quk;
  494. igl::slice(Q,unknown, unknown, Quu);
  495. igl::slice(Q,unknown, known, Quk);
  496. std::vector<typename Eigen::Triplet<std::complex<typename DerivedV::Scalar> > > tripletList;
  497. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > fu(N-nc,1);
  498. igl::slice(f,unknown, Eigen::VectorXi::Zero(1,1), fu);
  499. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > rhs = (Quk*xknown).sparseView()+.5*fu;
  500. Eigen::SparseLU< Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar>>> solver;
  501. solver.compute(-Quu);
  502. if(solver.info()!=Eigen::Success)
  503. {
  504. std::cerr<<"Decomposition failed!"<<std::endl;
  505. return;
  506. }
  507. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar>> b = solver.solve(rhs);
  508. if(solver.info()!=Eigen::Success)
  509. {
  510. std::cerr<<"Solving failed!"<<std::endl;
  511. return;
  512. }
  513. indk = 0, indu = 0;
  514. x.setZero(N,1);
  515. for (int i = 0; i<N; ++i)
  516. if (isConstrained[i])
  517. x[i] = xknown[indk++];
  518. else
  519. x[i] = b.coeff(indu++,0);
  520. }
  521. template<typename DerivedV, typename DerivedF, typename DerivedO>
  522. IGL_INLINE bool igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO>::
  523. solve(const Eigen::VectorXi &isConstrained,
  524. const Eigen::PlainObjectBase<DerivedO> &initialSolution,
  525. Eigen::PlainObjectBase<DerivedO> &output,
  526. typename DerivedV::Scalar *lambdaOut)
  527. {
  528. int numConstrained = isConstrained.sum();
  529. // coefficient values
  530. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> Ak, Bk;
  531. pvU.resize(data.numF,2);
  532. pvV.resize(data.numF,2);
  533. for (int fi = 0; fi <data.numF; ++fi)
  534. {
  535. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b1 = data.B1.row(fi);
  536. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b2 = data.B2.row(fi);
  537. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &u3 = initialSolution.block(fi,0,1,3);
  538. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &v3 = initialSolution.block(fi,3,1,3);
  539. pvU.row(fi)<< u3.dot(b1), u3.dot(b2);
  540. pvV.row(fi)<< v3.dot(b1), v3.dot(b2);
  541. }
  542. setCoefficientsFromField();
  543. Ak.resize(numConstrained,1);
  544. Bk.resize(numConstrained,1);
  545. int ind = 0;
  546. for (int i = 0; i <data.numF; ++i)
  547. {
  548. if(isConstrained[i])
  549. {
  550. Ak(ind) = Acoeff[i];
  551. Bk(ind) = Bcoeff[i];
  552. ind ++;
  553. }
  554. }
  555. typename DerivedV::Scalar smoothnessValue;
  556. int oob;
  557. smoothnessValue = (Acoeff.adjoint()*data.DDA*Acoeff + Bcoeff.adjoint()*data.DDB*Bcoeff).real()[0];
  558. printf("\n\nInitial smoothness: %.5g\n",smoothnessValue);
  559. oob = getNumOutOfBounds();
  560. printf("\n\nInitial out-of-bounds: %d\n",oob);
  561. printf(" %d %.5g %d\n",-1, smoothnessValue, oob);
  562. lambda = lambdaInit;
  563. for (int iter = 0; iter<maxIter; ++iter)
  564. {
  565. printf("\n\n--- Iteration %d ---\n",iter);
  566. localStep();
  567. globalStep(isConstrained, Ak, Bk);
  568. smoothnessValue = (Acoeff.adjoint()*data.DDA*Acoeff + Bcoeff.adjoint()*data.DDB*Bcoeff).real()[0];
  569. printf("Smoothness: %.5g\n",smoothnessValue);
  570. oob = getNumOutOfBounds();
  571. bool stoppingCriterion = (oob == 0) ;
  572. if (stoppingCriterion)
  573. break;
  574. lambda = lambda*lambdaMultFactor;
  575. // printf(" %d %.5g %d\n",iter, smoothnessValue, oob);
  576. }
  577. output.setZero(data.numF,6);
  578. for (int fi=0; fi<data.numF; ++fi)
  579. {
  580. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b1 = data.B1.row(fi);
  581. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b2 = data.B2.row(fi);
  582. output.block(fi,0, 1, 3) = pvU(fi,0)*b1 + pvU(fi,1)*b2;
  583. output.block(fi,3, 1, 3) = pvV(fi,0)*b1 + pvV(fi,1)*b2;
  584. }
  585. if (lambdaOut)
  586. *lambdaOut = lambda;
  587. return (oob==0);
  588. }
  589. template <typename DerivedV, typename DerivedF, typename DerivedO>
  590. IGL_INLINE bool igl::angle_bound_frame_fields(const Eigen::PlainObjectBase<DerivedV> &V,
  591. const Eigen::PlainObjectBase<DerivedF> &F,
  592. const typename DerivedV::Scalar &thetaMin,
  593. const Eigen::VectorXi &isConstrained,
  594. const Eigen::PlainObjectBase<DerivedO> &initialSolution,
  595. Eigen::PlainObjectBase<DerivedO> &output,
  596. int maxIter,
  597. const typename DerivedV::Scalar &lambdaInit,
  598. const typename DerivedV::Scalar &lambdaMultFactor,
  599. const bool doHardConstraints)
  600. {
  601. igl::AngleBoundFFSolverData<DerivedV, DerivedF> csdata(V, F);
  602. igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO> cs(csdata, thetaMin, maxIter, lambdaInit, lambdaMultFactor, doHardConstraints);
  603. return (cs.solve(isConstrained, initialSolution, output));
  604. }
  605. template <typename DerivedV, typename DerivedF, typename DerivedO>
  606. IGL_INLINE bool igl::angle_bound_frame_fields(const igl::AngleBoundFFSolverData<DerivedV, DerivedF> &csdata,
  607. const typename DerivedV::Scalar &thetaMin,
  608. const Eigen::VectorXi &isConstrained,
  609. const Eigen::PlainObjectBase<DerivedO> &initialSolution,
  610. Eigen::PlainObjectBase<DerivedO> &output,
  611. int maxIter,
  612. const typename DerivedV::Scalar &lambdaInit,
  613. const typename DerivedV::Scalar &lambdaMultFactor,
  614. const bool doHardConstraints,
  615. typename DerivedV::Scalar *lambdaOut)
  616. {
  617. igl::AngleBoundFFSolver<DerivedV, DerivedF, DerivedO> cs(csdata, thetaMin, maxIter, lambdaInit, lambdaMultFactor, doHardConstraints);
  618. return (cs.solve(isConstrained, initialSolution, output, lambdaOut));
  619. }
  620. #ifdef IGL_STATIC_LIBRARY
  621. // Explicit template specialization
  622. #endif