n_polyvector.cpp 17 KB

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