ConjugateFFSolverData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Olga Diamanti, 2015 Alec Jacobson
  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. #ifndef IGL_CONJUGATE_FF_SOLVER_DATA_H
  9. #define IGL_CONJUGATE_FF_SOLVER_DATA_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <Eigen/Sparse>
  13. namespace igl
  14. {
  15. // Data class for the Conjugate Frame Field Solver
  16. template <typename DerivedV, typename DerivedF>
  17. class ConjugateFFSolverData
  18. {
  19. public:
  20. const Eigen::PlainObjectBase<DerivedV> &V; int numV;
  21. const Eigen::PlainObjectBase<DerivedF> &F; int numF;
  22. Eigen::MatrixXi EV; int numE;
  23. Eigen::MatrixXi F2E;
  24. Eigen::MatrixXi E2F;
  25. Eigen::VectorXd K;
  26. Eigen::VectorXi isBorderEdge;
  27. int numInteriorEdges;
  28. Eigen::Matrix<int,Eigen::Dynamic,2> E2F_int;
  29. Eigen::VectorXi indInteriorToFull;
  30. Eigen::VectorXi indFullToInterior;
  31. Eigen::PlainObjectBase<DerivedV> B1, B2, FN;
  32. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic,1> kmin, kmax;
  33. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic,2> dmin, dmax;
  34. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic,3> dmin3, dmax3;
  35. Eigen::VectorXd nonPlanarityMeasure;
  36. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > planarityWeight;
  37. //conjugacy matrix
  38. std::vector<Eigen::Matrix<typename DerivedV::Scalar, 4,4> > H;
  39. //conjugacy matrix eigenvectors and (scaled) eigenvalues
  40. std::vector<Eigen::Matrix<typename DerivedV::Scalar, 4,4> > UH;
  41. std::vector<Eigen::Matrix<typename DerivedV::Scalar, 4,1> > s;
  42. //laplacians
  43. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar>> DDA, DDB;
  44. private:
  45. IGL_INLINE void computeCurvatureAndPrincipals();
  46. IGL_INLINE void precomputeConjugacyStuff();
  47. IGL_INLINE void computeLaplacians();
  48. IGL_INLINE void computek();
  49. IGL_INLINE void computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D);
  50. IGL_INLINE void precomputeInteriorEdges();
  51. public:
  52. IGL_INLINE ConjugateFFSolverData(const Eigen::PlainObjectBase<DerivedV> &_V,
  53. const Eigen::PlainObjectBase<DerivedF> &_F);
  54. IGL_INLINE void evaluateConjugacy(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> &pvU,
  55. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> &pvV,
  56. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &conjValues) const ;
  57. };
  58. }
  59. #include <igl/colon.h>
  60. #include <igl/edge_topology.h>
  61. #include <igl/false_barycentric_subdivision.h>
  62. #include <igl/local_basis.h>
  63. #include <igl/principal_curvature.h>
  64. #include <igl/sparse.h>
  65. template <typename DerivedV, typename DerivedF>
  66. IGL_INLINE igl::ConjugateFFSolverData<DerivedV, DerivedF>::
  67. ConjugateFFSolverData(const Eigen::PlainObjectBase<DerivedV> &_V,
  68. const Eigen::PlainObjectBase<DerivedF> &_F):
  69. V(_V),
  70. numV(_V.rows()),
  71. F(_F),
  72. numF(_F.rows())
  73. {
  74. igl::edge_topology(V,F,EV,F2E,E2F);
  75. numE = EV.rows();
  76. precomputeInteriorEdges();
  77. igl::local_basis(V,F,B1,B2,FN);
  78. computek();
  79. computeLaplacians();
  80. computeCurvatureAndPrincipals();
  81. precomputeConjugacyStuff();
  82. };
  83. template <typename DerivedV, typename DerivedF>
  84. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::computeCurvatureAndPrincipals()
  85. {
  86. Eigen::MatrixXd VCBary;
  87. Eigen::MatrixXi FCBary;
  88. VCBary.setZero(numV+numF,3);
  89. FCBary.setZero(3*numF,3);
  90. igl::false_barycentric_subdivision(V, F, VCBary, FCBary);
  91. Eigen::MatrixXd dmax3_,dmin3_;
  92. igl::principal_curvature(VCBary, FCBary, dmax3_, dmin3_, kmax, kmin, 5,true);
  93. dmax3 = dmax3_.bottomRows(numF);
  94. dmin3 = dmin3_.bottomRows(numF);
  95. kmax = kmax.bottomRows(numF);
  96. kmin = kmin.bottomRows(numF);
  97. // kmax = dmax3.rowwise().norm();
  98. // kmin = dmin3.rowwise().norm();
  99. dmin3.rowwise().normalize();
  100. dmax3.rowwise().normalize();
  101. dmax.setZero(numF,2);
  102. dmin.setZero(numF,2);
  103. for (int i= 0; i <numF; ++i)
  104. {
  105. 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())
  106. {
  107. kmin[i] = 0;
  108. kmax[i] = 0;
  109. dmin3.row(i) = B1.row(i);
  110. dmax3.row(i) = B2.row(i);
  111. }
  112. else
  113. {
  114. dmax3.row(i) = (dmax3.row(i) - (dmax3.row(i).dot(FN.row(i)))*FN.row(i)).normalized();
  115. dmin3.row(i) = dmin3.row(i) - (dmin3.row(i).dot(FN.row(i)))*FN.row(i);
  116. dmin3.row(i) = (dmin3.row(i) - (dmin3.row(i).dot(dmax3.row(i)))*dmax3.row(i)).normalized();
  117. if ((dmin3.row(i).cross(dmax3.row(i))).dot(FN.row(i))<0)
  118. dmin3.row(i) = -dmin3.row(i);
  119. }
  120. dmax.row(i) << dmax3.row(i).dot(B1.row(i)), dmax3.row(i).dot(B2.row(i));
  121. dmax.row(i).normalize();
  122. dmin.row(i) << dmin3.row(i).dot(B1.row(i)), dmin3.row(i).dot(B2.row(i));
  123. dmin.row(i).normalize();
  124. }
  125. nonPlanarityMeasure = kmax.cwiseAbs().array()*kmin.cwiseAbs().array();
  126. typename DerivedV::Scalar minP = nonPlanarityMeasure.minCoeff();
  127. typename DerivedV::Scalar maxP = nonPlanarityMeasure.maxCoeff();
  128. nonPlanarityMeasure = (nonPlanarityMeasure.array()-minP)/(maxP-minP);
  129. Eigen::VectorXi I = igl::colon<typename DerivedF::Scalar>(0, numF-1);
  130. igl::sparse(I, I, nonPlanarityMeasure, numF, numF, planarityWeight);
  131. }
  132. template <typename DerivedV, typename DerivedF>
  133. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::precomputeConjugacyStuff()
  134. {
  135. H.resize(numF);
  136. UH.resize(numF);
  137. s.resize(numF);
  138. for (int i = 0; i<numF; ++i)
  139. {
  140. //compute conjugacy matrix
  141. 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];
  142. H[i]<<
  143. 0, 0, k1*e1x*e1x, k1*e1x*e1y,
  144. 0, 0, k1*e1x*e1y, k1*e1y*e1y,
  145. k2*e2x*e2x, k2*e2x*e2y, 0, 0,
  146. k2*e2x*e2y, k2*e2y*e2y, 0, 0;
  147. Eigen::Matrix<typename DerivedV::Scalar, 4, 4> Ht = H[i].transpose();
  148. H[i] = .5*(H[i]+Ht);
  149. Eigen::EigenSolver<Eigen::Matrix<typename DerivedV::Scalar, 4, 4> > es(H[i]);
  150. s[i] = es.eigenvalues().real();//ok to do this because H symmetric
  151. //scale
  152. s[i] = s[i]/(s[i].cwiseAbs().minCoeff());
  153. UH[i] = es.eigenvectors().real();
  154. }
  155. }
  156. template <typename DerivedV, typename DerivedF>
  157. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::computeLaplacians()
  158. {
  159. computeCoefficientLaplacian(2, DDA);
  160. computeCoefficientLaplacian(4, DDB);
  161. }
  162. template<typename DerivedV, typename DerivedF>
  163. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::
  164. precomputeInteriorEdges()
  165. {
  166. // Flag border edges
  167. numInteriorEdges = 0;
  168. isBorderEdge.setZero(numE,1);
  169. indFullToInterior = -1*Eigen::VectorXi::Ones(numE,1);
  170. for(unsigned i=0; i<numE; ++i)
  171. {
  172. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  173. isBorderEdge[i] = 1;
  174. else
  175. {
  176. indFullToInterior[i] = numInteriorEdges;
  177. numInteriorEdges++;
  178. }
  179. }
  180. E2F_int.resize(numInteriorEdges, 2);
  181. indInteriorToFull.setZero(numInteriorEdges,1);
  182. int ii = 0;
  183. for (int k=0; k<numE; ++k)
  184. {
  185. if (isBorderEdge[k])
  186. continue;
  187. E2F_int.row(ii) = E2F.row(k);
  188. indInteriorToFull[ii] = k;
  189. ii++;
  190. }
  191. }
  192. template<typename DerivedV, typename DerivedF>
  193. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::
  194. computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D)
  195. {
  196. std::vector<Eigen::Triplet<std::complex<typename DerivedV::Scalar> >> tripletList;
  197. // For every non-border edge
  198. for (unsigned eid=0; eid<numE; ++eid)
  199. {
  200. if (!isBorderEdge[eid])
  201. {
  202. int fid0 = E2F(eid,0);
  203. int fid1 = E2F(eid,1);
  204. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  205. fid0,
  206. std::complex<typename DerivedV::Scalar>(1.)));
  207. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  208. fid1,
  209. std::complex<typename DerivedV::Scalar>(1.)));
  210. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  211. fid1,
  212. -1.*std::polar(1.,-1.*n*K[eid])));
  213. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  214. fid0,
  215. -1.*std::polar(1.,1.*n*K[eid])));
  216. }
  217. }
  218. D.resize(numF,numF);
  219. D.setFromTriplets(tripletList.begin(), tripletList.end());
  220. }
  221. template<typename DerivedV, typename DerivedF>
  222. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::
  223. computek()
  224. {
  225. K.setZero(numE);
  226. // For every non-border edge
  227. for (unsigned eid=0; eid<numE; ++eid)
  228. {
  229. if (!isBorderEdge[eid])
  230. {
  231. int fid0 = E2F(eid,0);
  232. int fid1 = E2F(eid,1);
  233. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N0 = FN.row(fid0);
  234. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N1 = FN.row(fid1);
  235. // find common edge on triangle 0 and 1
  236. int fid0_vc = -1;
  237. int fid1_vc = -1;
  238. for (unsigned i=0;i<3;++i)
  239. {
  240. if (F2E(fid0,i) == eid)
  241. fid0_vc = i;
  242. if (F2E(fid1,i) == eid)
  243. fid1_vc = i;
  244. }
  245. assert(fid0_vc != -1);
  246. assert(fid1_vc != -1);
  247. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  248. common_edge.normalize();
  249. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  250. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> P;
  251. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> o = V.row(F(fid0,fid0_vc));
  252. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> tmp = -N0.cross(common_edge);
  253. P << common_edge, tmp, N0;
  254. // P.transposeInPlace();
  255. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V0;
  256. V0.row(0) = V.row(F(fid0,0)) -o;
  257. V0.row(1) = V.row(F(fid0,1)) -o;
  258. V0.row(2) = V.row(F(fid0,2)) -o;
  259. V0 = (P*V0.transpose()).transpose();
  260. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V1;
  261. V1.row(0) = V.row(F(fid1,0)) -o;
  262. V1.row(1) = V.row(F(fid1,1)) -o;
  263. V1.row(2) = V.row(F(fid1,2)) -o;
  264. V1 = (P*V1.transpose()).transpose();
  265. // compute rotation R such that R * N1 = N0
  266. // i.e. map both triangles to the same plane
  267. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  268. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> R;
  269. R << 1, 0, 0,
  270. 0, cos(alpha), -sin(alpha) ,
  271. 0, sin(alpha), cos(alpha);
  272. V1 = (R*V1.transpose()).transpose();
  273. // measure the angle between the reference frames
  274. // k_ij is the angle between the triangle on the left and the one on the right
  275. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref0 = V0.row(1) - V0.row(0);
  276. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref1 = V1.row(1) - V1.row(0);
  277. ref0.normalize();
  278. ref1.normalize();
  279. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  280. // just to be sure, rotate ref0 using angle ktemp...
  281. Eigen::Matrix<typename DerivedV::Scalar, 2, 2> R2;
  282. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  283. Eigen::Matrix<typename DerivedV::Scalar, 1, 2> tmp1 = R2*(ref0.head(2)).transpose();
  284. K[eid] = ktemp;
  285. }
  286. }
  287. }
  288. template<typename DerivedV, typename DerivedF>
  289. IGL_INLINE void igl::ConjugateFFSolverData<DerivedV, DerivedF>::
  290. evaluateConjugacy(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> &pvU,
  291. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> &pvV,
  292. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &conjValues) const
  293. {
  294. conjValues.resize(numF,1);
  295. for (int j =0; j<numF; ++j)
  296. {
  297. Eigen::Matrix<typename DerivedV::Scalar, 4, 1> x; x<<pvU.row(j).transpose(), pvV.row(j).transpose();
  298. conjValues[j] = x.transpose()*H[j]*x;
  299. }
  300. }
  301. #endif