n_polyvector.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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/n_polyvector.h>
  9. #include <igl/edge_topology.h>
  10. #include <igl/local_basis.h>
  11. #include <igl/nchoosek.h>
  12. #include <igl/slice.h>
  13. #include <igl/polyroots.h>
  14. #include <igl/igl_inline.h>
  15. #include <Eigen/Sparse>
  16. #include <Eigen/Geometry>
  17. #include <iostream>
  18. namespace igl {
  19. template <typename DerivedV, typename DerivedF>
  20. class PolyVectorFieldFinder
  21. {
  22. private:
  23. const Eigen::PlainObjectBase<DerivedV> &V;
  24. const Eigen::PlainObjectBase<DerivedF> &F; int numF;
  25. const int n;
  26. Eigen::MatrixXi EV; int numE;
  27. Eigen::MatrixXi F2E;
  28. Eigen::MatrixXi E2F;
  29. Eigen::VectorXd K;
  30. Eigen::VectorXi isBorderEdge;
  31. int numInteriorEdges;
  32. Eigen::Matrix<int,Eigen::Dynamic,2> E2F_int;
  33. Eigen::VectorXi indInteriorToFull;
  34. Eigen::VectorXi indFullToInterior;
  35. Eigen::PlainObjectBase<DerivedV> B1, B2, FN;
  36. IGL_INLINE void computek();
  37. IGL_INLINE void setFieldFromGeneralCoefficients(const std::vector<Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> > &coeffs,
  38. std::vector<Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> > &pv);
  39. IGL_INLINE void computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D);
  40. IGL_INLINE void getGeneralCoeffConstraints(const Eigen::VectorXi &isConstrained,
  41. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  42. int k,
  43. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> &Ck);
  44. IGL_INLINE void precomputeInteriorEdges();
  45. IGL_INLINE void minQuadWithKnownMini(const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &Q,
  46. const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &f,
  47. const Eigen::VectorXi isConstrained,
  48. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &xknown,
  49. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &x);
  50. public:
  51. IGL_INLINE PolyVectorFieldFinder(const Eigen::PlainObjectBase<DerivedV> &_V,
  52. const Eigen::PlainObjectBase<DerivedF> &_F,
  53. const int &_n);
  54. IGL_INLINE bool solve(const Eigen::VectorXi &isConstrained,
  55. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  56. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &output);
  57. };
  58. }
  59. template<typename DerivedV, typename DerivedF>
  60. IGL_INLINE igl::PolyVectorFieldFinder<DerivedV, DerivedF>::
  61. PolyVectorFieldFinder(const Eigen::PlainObjectBase<DerivedV> &_V,
  62. const Eigen::PlainObjectBase<DerivedF> &_F,
  63. const int &_n):
  64. V(_V),
  65. F(_F),
  66. numF(_F.rows()),
  67. n(_n)
  68. {
  69. igl::edge_topology(V,F,EV,F2E,E2F);
  70. numE = EV.rows();
  71. precomputeInteriorEdges();
  72. igl::local_basis(V,F,B1,B2,FN);
  73. computek();
  74. };
  75. template<typename DerivedV, typename DerivedF>
  76. IGL_INLINE void igl::PolyVectorFieldFinder<DerivedV, DerivedF>::
  77. precomputeInteriorEdges()
  78. {
  79. // Flag border edges
  80. numInteriorEdges = 0;
  81. isBorderEdge.setZero(numE,1);
  82. indFullToInterior = -1.*Eigen::VectorXi::Ones(numE,1);
  83. for(unsigned i=0; i<numE; ++i)
  84. {
  85. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  86. isBorderEdge[i] = 1;
  87. else
  88. {
  89. indFullToInterior[i] = numInteriorEdges;
  90. numInteriorEdges++;
  91. }
  92. }
  93. E2F_int.resize(numInteriorEdges, 2);
  94. indInteriorToFull.setZero(numInteriorEdges,1);
  95. int ii = 0;
  96. for (int k=0; k<numE; ++k)
  97. {
  98. if (isBorderEdge[k])
  99. continue;
  100. E2F_int.row(ii) = E2F.row(k);
  101. indInteriorToFull[ii] = k;
  102. ii++;
  103. }
  104. }
  105. template<typename DerivedV, typename DerivedF>
  106. IGL_INLINE void igl::PolyVectorFieldFinder<DerivedV, DerivedF>::
  107. minQuadWithKnownMini(const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &Q,
  108. const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &f,
  109. const Eigen::VectorXi isConstrained,
  110. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &xknown,
  111. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &x)
  112. {
  113. int N = Q.rows();
  114. int nc = xknown.rows();
  115. Eigen::VectorXi known; known.setZero(nc,1);
  116. Eigen::VectorXi unknown; unknown.setZero(N-nc,1);
  117. int indk = 0, indu = 0;
  118. for (int i = 0; i<N; ++i)
  119. if (isConstrained[i])
  120. {
  121. known[indk] = i;
  122. indk++;
  123. }
  124. else
  125. {
  126. unknown[indu] = i;
  127. indu++;
  128. }
  129. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > Quu, Quk;
  130. igl::slice(Q,unknown, unknown, Quu);
  131. igl::slice(Q,unknown, known, Quk);
  132. std::vector<typename Eigen::Triplet<std::complex<typename DerivedV::Scalar> > > tripletList;
  133. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > fu(N-nc,1);
  134. igl::slice(f,unknown, Eigen::VectorXi::Zero(1,1), fu);
  135. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > rhs = (Quk*xknown).sparseView()+.5*fu;
  136. Eigen::SparseLU< Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > > solver;
  137. solver.compute(-Quu);
  138. if(solver.info()!=Eigen::Success)
  139. {
  140. std::cerr<<"Decomposition failed!"<<std::endl;
  141. return;
  142. }
  143. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > b = solver.solve(rhs);
  144. if(solver.info()!=Eigen::Success)
  145. {
  146. std::cerr<<"Solving failed!"<<std::endl;
  147. return;
  148. }
  149. indk = 0, indu = 0;
  150. x.setZero(N,1);
  151. for (int i = 0; i<N; ++i)
  152. if (isConstrained[i])
  153. x[i] = xknown[indk++];
  154. else
  155. x[i] = b.coeff(indu++,0);
  156. }
  157. template<typename DerivedV, typename DerivedF>
  158. IGL_INLINE bool igl::PolyVectorFieldFinder<DerivedV, DerivedF>::
  159. solve(const Eigen::VectorXi &isConstrained,
  160. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  161. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &output)
  162. {
  163. // polynomial is of the form:
  164. // (-1)^0 z^(2n) +
  165. // (-1)^1 c[0]z^(2n-2) +
  166. // (-1)^2 c[1]z^(2n-4) +
  167. // (-1)^3 c[2]z^(2n-6) +
  168. // ... +
  169. // (-1)^n c[n-1]
  170. std::vector<Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> > coeffs(n,Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1>::Zero(numF, 1));
  171. for (int i =0; i<n; ++i)
  172. {
  173. int degree = 2*(i+1);
  174. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> Ck;
  175. getGeneralCoeffConstraints(isConstrained,
  176. cfW,
  177. i,
  178. Ck);
  179. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > DD;
  180. computeCoefficientLaplacian(degree, DD);
  181. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > f; f.resize(numF,1);
  182. minQuadWithKnownMini(DD, f, isConstrained, Ck, coeffs[i]);
  183. }
  184. std::vector<Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> > pv;
  185. setFieldFromGeneralCoefficients(coeffs, pv);
  186. output.setZero(numF,3*n);
  187. for (int fi=0; fi<numF; ++fi)
  188. {
  189. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b1 = B1.row(fi);
  190. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b2 = B2.row(fi);
  191. for (int i=0; i<n; ++i)
  192. output.block(fi,3*i, 1, 3) = pv[i](fi,0)*b1 + pv[i](fi,1)*b2;
  193. }
  194. return true;
  195. }
  196. template<typename DerivedV, typename DerivedF>
  197. IGL_INLINE void igl::PolyVectorFieldFinder<DerivedV, DerivedF>::setFieldFromGeneralCoefficients(const std::vector<Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> > &coeffs,
  198. std::vector<Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> > &pv)
  199. {
  200. pv.assign(n, Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2>::Zero(numF, 2));
  201. for (int i = 0; i <numF; ++i)
  202. {
  203. // poly coefficients: 1, 0, -Acoeff, 0, Bcoeff
  204. // matlab code from roots (given there are no trailing zeros in the polynomial coefficients)
  205. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> polyCoeff;
  206. polyCoeff.setZero(2*n+1,1);
  207. polyCoeff[0] = 1.;
  208. int sign = 1;
  209. for (int k =0; k<n; ++k)
  210. {
  211. sign = -sign;
  212. int degree = 2*(k+1);
  213. polyCoeff[degree] = (1.*sign)*coeffs[k](i);
  214. }
  215. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> roots;
  216. igl::polyRoots<std::complex<typename DerivedV::Scalar>, typename DerivedV::Scalar >(polyCoeff,roots);
  217. Eigen::VectorXi done; done.setZero(2*n,1);
  218. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> u(n,1);
  219. int ind =0;
  220. for (int k=0; k<2*n; ++k)
  221. {
  222. if (done[k])
  223. continue;
  224. u[ind] = roots[k];
  225. done[k] = 1;
  226. int mini = -1;
  227. double mind = 1e10;
  228. for (int l =k+1; l<2*n; ++l)
  229. {
  230. double dist = abs(roots[l]+u[ind]);
  231. if (dist<mind)
  232. {
  233. mind = dist;
  234. mini = l;
  235. }
  236. }
  237. done[mini] = 1;
  238. ind ++;
  239. }
  240. for (int k=0; k<n; ++k)
  241. {
  242. pv[k](i,0) = real(u[k]);
  243. pv[k](i,1) = imag(u[k]);
  244. }
  245. }
  246. }
  247. template<typename DerivedV, typename DerivedF>
  248. IGL_INLINE void igl::PolyVectorFieldFinder<DerivedV, DerivedF>::computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D)
  249. {
  250. std::vector<Eigen::Triplet<std::complex<typename DerivedV::Scalar> > > tripletList;
  251. // For every non-border edge
  252. for (unsigned eid=0; eid<numE; ++eid)
  253. {
  254. if (!isBorderEdge[eid])
  255. {
  256. int fid0 = E2F(eid,0);
  257. int fid1 = E2F(eid,1);
  258. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  259. fid0,
  260. std::complex<typename DerivedV::Scalar>(1.)));
  261. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  262. fid1,
  263. std::complex<typename DerivedV::Scalar>(1.)));
  264. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  265. fid1,
  266. -1.*std::polar(1.,-1.*n*K[eid])));
  267. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  268. fid0,
  269. -1.*std::polar(1.,1.*n*K[eid])));
  270. }
  271. }
  272. D.resize(numF,numF);
  273. D.setFromTriplets(tripletList.begin(), tripletList.end());
  274. }
  275. template<typename DerivedV, typename DerivedF>
  276. IGL_INLINE void igl::PolyVectorFieldFinder<DerivedV, DerivedF>::getGeneralCoeffConstraints(const Eigen::VectorXi &isConstrained,
  277. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  278. int k,
  279. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> &Ck)
  280. {
  281. int numConstrained = isConstrained.sum();
  282. Ck.resize(numConstrained,1);
  283. int n = cfW.cols()/3;
  284. Eigen::MatrixXi allCombs;
  285. {
  286. Eigen::VectorXi V = Eigen::VectorXi::LinSpaced(n,0,n-1);
  287. igl::nchoosek(V,k+1,allCombs);
  288. }
  289. int ind = 0;
  290. for (int fi = 0; fi <numF; ++fi)
  291. {
  292. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b1 = B1.row(fi);
  293. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b2 = B2.row(fi);
  294. if(isConstrained[fi])
  295. {
  296. std::complex<typename DerivedV::Scalar> ck(0);
  297. for (int j = 0; j < allCombs.rows(); ++j)
  298. {
  299. std::complex<typename DerivedV::Scalar> tk(1.);
  300. //collect products
  301. for (int i = 0; i < allCombs.cols(); ++i)
  302. {
  303. int index = allCombs(j,i);
  304. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &w = cfW.block(fi,3*index,1,3);
  305. typename DerivedV::Scalar w0 = w.dot(b1);
  306. typename DerivedV::Scalar w1 = w.dot(b2);
  307. std::complex<typename DerivedV::Scalar> u(w0,w1);
  308. tk*= u*u;
  309. }
  310. //collect sum
  311. ck += tk;
  312. }
  313. Ck(ind) = ck;
  314. ind ++;
  315. }
  316. }
  317. }
  318. template<typename DerivedV, typename DerivedF>
  319. IGL_INLINE void igl::PolyVectorFieldFinder<DerivedV, DerivedF>::computek()
  320. {
  321. K.setZero(numE);
  322. // For every non-border edge
  323. for (unsigned eid=0; eid<numE; ++eid)
  324. {
  325. if (!isBorderEdge[eid])
  326. {
  327. int fid0 = E2F(eid,0);
  328. int fid1 = E2F(eid,1);
  329. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N0 = FN.row(fid0);
  330. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N1 = FN.row(fid1);
  331. // find common edge on triangle 0 and 1
  332. int fid0_vc = -1;
  333. int fid1_vc = -1;
  334. for (unsigned i=0;i<3;++i)
  335. {
  336. if (F2E(fid0,i) == eid)
  337. fid0_vc = i;
  338. if (F2E(fid1,i) == eid)
  339. fid1_vc = i;
  340. }
  341. assert(fid0_vc != -1);
  342. assert(fid1_vc != -1);
  343. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  344. common_edge.normalize();
  345. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  346. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> P;
  347. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> o = V.row(F(fid0,fid0_vc));
  348. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> tmp = -N0.cross(common_edge);
  349. P << common_edge, tmp, N0;
  350. // P.transposeInPlace();
  351. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V0;
  352. V0.row(0) = V.row(F(fid0,0)) -o;
  353. V0.row(1) = V.row(F(fid0,1)) -o;
  354. V0.row(2) = V.row(F(fid0,2)) -o;
  355. V0 = (P*V0.transpose()).transpose();
  356. // assert(V0(0,2) < 1e-10);
  357. // assert(V0(1,2) < 1e-10);
  358. // assert(V0(2,2) < 1e-10);
  359. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V1;
  360. V1.row(0) = V.row(F(fid1,0)) -o;
  361. V1.row(1) = V.row(F(fid1,1)) -o;
  362. V1.row(2) = V.row(F(fid1,2)) -o;
  363. V1 = (P*V1.transpose()).transpose();
  364. // assert(V1(fid1_vc,2) < 10e-10);
  365. // assert(V1((fid1_vc+1)%3,2) < 10e-10);
  366. // compute rotation R such that R * N1 = N0
  367. // i.e. map both triangles to the same plane
  368. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  369. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> R;
  370. R << 1, 0, 0,
  371. 0, cos(alpha), -sin(alpha) ,
  372. 0, sin(alpha), cos(alpha);
  373. V1 = (R*V1.transpose()).transpose();
  374. // assert(V1(0,2) < 1e-10);
  375. // assert(V1(1,2) < 1e-10);
  376. // assert(V1(2,2) < 1e-10);
  377. // measure the angle between the reference frames
  378. // k_ij is the angle between the triangle on the left and the one on the right
  379. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref0 = V0.row(1) - V0.row(0);
  380. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref1 = V1.row(1) - V1.row(0);
  381. ref0.normalize();
  382. ref1.normalize();
  383. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  384. // just to be sure, rotate ref0 using angle ktemp...
  385. Eigen::Matrix<typename DerivedV::Scalar, 2, 2> R2;
  386. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  387. Eigen::Matrix<typename DerivedV::Scalar, 1, 2> tmp1 = R2*(ref0.head(2)).transpose();
  388. // assert(tmp1(0) - ref1(0) < 1e-10);
  389. // assert(tmp1(1) - ref1(1) < 1e-10);
  390. K[eid] = ktemp;
  391. }
  392. }
  393. }
  394. IGL_INLINE void igl::n_polyvector(const Eigen::MatrixXd &V,
  395. const Eigen::MatrixXi &F,
  396. const Eigen::VectorXi& b,
  397. const Eigen::MatrixXd& bc,
  398. Eigen::MatrixXd &output)
  399. {
  400. Eigen::VectorXi isConstrained = Eigen::VectorXi::Constant(F.rows(),0);
  401. Eigen::MatrixXd cfW = Eigen::MatrixXd::Constant(F.rows(),bc.cols(),0);
  402. for(unsigned i=0; i<b.size();++i)
  403. {
  404. isConstrained(b(i)) = 1;
  405. cfW.row(b(i)) << bc.row(i);
  406. }
  407. if (b.size() == F.rows())
  408. {
  409. output = cfW;
  410. return;
  411. }
  412. int n = cfW.cols()/3;
  413. igl::PolyVectorFieldFinder<Eigen::MatrixXd, Eigen::MatrixXi> pvff(V,F,n);
  414. pvff.solve(isConstrained, cfW, output);
  415. }
  416. #ifdef IGL_STATIC_LIBRARY
  417. // Explicit template specialization
  418. #endif