ConjugateFFSolverData.h 12 KB

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