ConjugateFFSolverData.h 12 KB

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