n_polyvector_general.cpp 17 KB

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