conjugate_frame_fields.cpp 29 KB

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