n_polyvector_general.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #warning "Constructing Eigen::PlainObjectBase directly is deprecated"
  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. const Eigen::VectorXi &rootsIndex,
  44. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> &Ck);
  45. IGL_INLINE void precomputeInteriorEdges();
  46. IGL_INLINE void minQuadWithKnownMini(const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &Q,
  47. const Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &f,
  48. const Eigen::VectorXi isConstrained,
  49. const Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &xknown,
  50. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic, 1> &x);
  51. public:
  52. IGL_INLINE GeneralPolyVectorFieldFinder(const Eigen::PlainObjectBase<DerivedV> &_V,
  53. const Eigen::PlainObjectBase<DerivedF> &_F,
  54. const int &_n);
  55. IGL_INLINE bool solve(const Eigen::VectorXi &isConstrained,
  56. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  57. const Eigen::VectorXi &rootsIndex,
  58. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &output);
  59. };
  60. }
  61. template<typename DerivedV, typename DerivedF>
  62. IGL_INLINE igl::GeneralPolyVectorFieldFinder<DerivedV, DerivedF>::
  63. GeneralPolyVectorFieldFinder(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::GeneralPolyVectorFieldFinder<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::GeneralPolyVectorFieldFinder<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::GeneralPolyVectorFieldFinder<DerivedV, DerivedF>::
  161. solve(const Eigen::VectorXi &isConstrained,
  162. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  163. const Eigen::VectorXi &rootsIndex,
  164. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &output)
  165. {
  166. // polynomial is of the form:
  167. // z^(2n) +
  168. // -c[0]z^(2n-1) +
  169. // c[1]z^(2n-2) +
  170. // -c[2]z^(2n-3) +
  171. // ... +
  172. // (-1)^n c[n-1]
  173. 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));
  174. for (int i =0; i<n; ++i)
  175. {
  176. int degree = i+1;
  177. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> Ck;
  178. getGeneralCoeffConstraints(isConstrained,
  179. cfW,
  180. i,
  181. rootsIndex,
  182. Ck);
  183. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > DD;
  184. computeCoefficientLaplacian(degree, DD);
  185. Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > f; f.resize(numF,1);
  186. if (isConstrained.sum() == numF)
  187. coeffs[i] = Ck;
  188. else
  189. minQuadWithKnownMini(DD, f, isConstrained, Ck, coeffs[i]);
  190. }
  191. std::vector<Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2> > pv;
  192. setFieldFromGeneralCoefficients(coeffs, pv);
  193. output.setZero(numF,3*n);
  194. for (int fi=0; fi<numF; ++fi)
  195. {
  196. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b1 = B1.row(fi);
  197. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b2 = B2.row(fi);
  198. for (int i=0; i<n; ++i)
  199. output.block(fi,3*i, 1, 3) = pv[i](fi,0)*b1 + pv[i](fi,1)*b2;
  200. }
  201. return true;
  202. }
  203. template<typename DerivedV, typename DerivedF>
  204. IGL_INLINE void igl::GeneralPolyVectorFieldFinder<DerivedV, DerivedF>::setFieldFromGeneralCoefficients(const std::vector<Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1>> &coeffs,
  205. std::vector<Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2>> &pv)
  206. {
  207. pv.assign(n, Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 2>::Zero(numF, 2));
  208. for (int i = 0; i <numF; ++i)
  209. {
  210. // poly coefficients: 1, 0, -Acoeff, 0, Bcoeff
  211. // matlab code from roots (given there are no trailing zeros in the polynomial coefficients)
  212. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> polyCoeff;
  213. polyCoeff.setZero(n+1,1);
  214. polyCoeff[0] = 1.;
  215. int sign = 1;
  216. for (int k =0; k<n; ++k)
  217. {
  218. sign = -sign;
  219. int degree = k+1;
  220. polyCoeff[degree] = (1.*sign)*coeffs[k](i);
  221. }
  222. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> roots;
  223. igl::polyRoots<std::complex<typename DerivedV::Scalar>, typename DerivedV::Scalar >(polyCoeff,roots);
  224. for (int k=0; k<n; ++k)
  225. {
  226. pv[k](i,0) = real(roots[k]);
  227. pv[k](i,1) = imag(roots[k]);
  228. }
  229. }
  230. }
  231. template<typename DerivedV, typename DerivedF>
  232. IGL_INLINE void igl::GeneralPolyVectorFieldFinder<DerivedV, DerivedF>::computeCoefficientLaplacian(int n, Eigen::SparseMatrix<std::complex<typename DerivedV::Scalar> > &D)
  233. {
  234. std::vector<Eigen::Triplet<std::complex<typename DerivedV::Scalar> >> tripletList;
  235. // For every non-border edge
  236. for (unsigned eid=0; eid<numE; ++eid)
  237. {
  238. if (!isBorderEdge[eid])
  239. {
  240. int fid0 = E2F(eid,0);
  241. int fid1 = E2F(eid,1);
  242. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  243. fid0,
  244. std::complex<typename DerivedV::Scalar>(1.)));
  245. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  246. fid1,
  247. std::complex<typename DerivedV::Scalar>(1.)));
  248. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid0,
  249. fid1,
  250. -1.*std::polar(1.,-1.*n*K[eid])));
  251. tripletList.push_back(Eigen::Triplet<std::complex<typename DerivedV::Scalar> >(fid1,
  252. fid0,
  253. -1.*std::polar(1.,1.*n*K[eid])));
  254. }
  255. }
  256. D.resize(numF,numF);
  257. D.setFromTriplets(tripletList.begin(), tripletList.end());
  258. }
  259. //this gives the coefficients without the (-1)^k that multiplies them
  260. template<typename DerivedV, typename DerivedF>
  261. IGL_INLINE void igl::GeneralPolyVectorFieldFinder<DerivedV, DerivedF>::getGeneralCoeffConstraints(const Eigen::VectorXi &isConstrained,
  262. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> &cfW,
  263. int k,
  264. const Eigen::VectorXi &rootsIndex,
  265. Eigen::Matrix<std::complex<typename DerivedV::Scalar>, Eigen::Dynamic,1> &Ck)
  266. {
  267. int numConstrained = isConstrained.sum();
  268. Ck.resize(numConstrained,1);
  269. // int n = rootsIndex.cols();
  270. Eigen::MatrixXi allCombs;
  271. {
  272. Eigen::VectorXi V = Eigen::VectorXi::LinSpaced(n,0,n-1);
  273. igl::nchoosek(V,k+1,allCombs);
  274. }
  275. int ind = 0;
  276. for (int fi = 0; fi <numF; ++fi)
  277. {
  278. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b1 = B1.row(fi);
  279. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> &b2 = B2.row(fi);
  280. if(isConstrained[fi])
  281. {
  282. std::complex<typename DerivedV::Scalar> ck(0);
  283. for (int j = 0; j < allCombs.rows(); ++j)
  284. {
  285. std::complex<typename DerivedV::Scalar> tk(1.);
  286. //collect products
  287. for (int i = 0; i < allCombs.cols(); ++i)
  288. {
  289. int index = allCombs(j,i);
  290. int ri = rootsIndex[index];
  291. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> w;
  292. if (ri>0)
  293. w = cfW.block(fi,3*(ri-1),1,3);
  294. else
  295. w = -cfW.block(fi,3*(-ri-1),1,3);
  296. typename DerivedV::Scalar w0 = w.dot(b1);
  297. typename DerivedV::Scalar w1 = w.dot(b2);
  298. std::complex<typename DerivedV::Scalar> u(w0,w1);
  299. tk*= u;
  300. }
  301. //collect sum
  302. ck += tk;
  303. }
  304. Ck(ind) = ck;
  305. ind ++;
  306. }
  307. }
  308. }
  309. template<typename DerivedV, typename DerivedF>
  310. IGL_INLINE void igl::GeneralPolyVectorFieldFinder<DerivedV, DerivedF>::computek()
  311. {
  312. K.setZero(numE);
  313. // For every non-border edge
  314. for (unsigned eid=0; eid<numE; ++eid)
  315. {
  316. if (!isBorderEdge[eid])
  317. {
  318. int fid0 = E2F(eid,0);
  319. int fid1 = E2F(eid,1);
  320. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N0 = FN.row(fid0);
  321. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N1 = FN.row(fid1);
  322. // find common edge on triangle 0 and 1
  323. int fid0_vc = -1;
  324. int fid1_vc = -1;
  325. for (unsigned i=0;i<3;++i)
  326. {
  327. if (F2E(fid0,i) == eid)
  328. fid0_vc = i;
  329. if (F2E(fid1,i) == eid)
  330. fid1_vc = i;
  331. }
  332. assert(fid0_vc != -1);
  333. assert(fid1_vc != -1);
  334. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  335. common_edge.normalize();
  336. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  337. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> P;
  338. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> o = V.row(F(fid0,fid0_vc));
  339. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> tmp = -N0.cross(common_edge);
  340. P << common_edge, tmp, N0;
  341. // P.transposeInPlace();
  342. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V0;
  343. V0.row(0) = V.row(F(fid0,0)) -o;
  344. V0.row(1) = V.row(F(fid0,1)) -o;
  345. V0.row(2) = V.row(F(fid0,2)) -o;
  346. V0 = (P*V0.transpose()).transpose();
  347. // assert(V0(0,2) < 1e-10);
  348. // assert(V0(1,2) < 1e-10);
  349. // assert(V0(2,2) < 1e-10);
  350. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V1;
  351. V1.row(0) = V.row(F(fid1,0)) -o;
  352. V1.row(1) = V.row(F(fid1,1)) -o;
  353. V1.row(2) = V.row(F(fid1,2)) -o;
  354. V1 = (P*V1.transpose()).transpose();
  355. // assert(V1(fid1_vc,2) < 10e-10);
  356. // assert(V1((fid1_vc+1)%3,2) < 10e-10);
  357. // compute rotation R such that R * N1 = N0
  358. // i.e. map both triangles to the same plane
  359. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  360. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> R;
  361. R << 1, 0, 0,
  362. 0, cos(alpha), -sin(alpha) ,
  363. 0, sin(alpha), cos(alpha);
  364. V1 = (R*V1.transpose()).transpose();
  365. // assert(V1(0,2) < 1e-10);
  366. // assert(V1(1,2) < 1e-10);
  367. // assert(V1(2,2) < 1e-10);
  368. // measure the angle between the reference frames
  369. // k_ij is the angle between the triangle on the left and the one on the right
  370. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref0 = V0.row(1) - V0.row(0);
  371. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref1 = V1.row(1) - V1.row(0);
  372. ref0.normalize();
  373. ref1.normalize();
  374. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  375. // just to be sure, rotate ref0 using angle ktemp...
  376. Eigen::Matrix<typename DerivedV::Scalar, 2, 2> R2;
  377. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  378. Eigen::Matrix<typename DerivedV::Scalar, 1, 2> tmp1 = R2*(ref0.head(2)).transpose();
  379. // assert(tmp1(0) - ref1(0) < 1e-10);
  380. // assert(tmp1(1) - ref1(1) < 1e-10);
  381. K[eid] = ktemp;
  382. }
  383. }
  384. }
  385. IGL_INLINE void igl::n_polyvector_general(const Eigen::MatrixXd &V,
  386. const Eigen::MatrixXi &F,
  387. const Eigen::VectorXi& b,
  388. const Eigen::MatrixXd& bc,
  389. const Eigen::VectorXi &I,
  390. Eigen::MatrixXd &output)
  391. {
  392. Eigen::VectorXi isConstrained = Eigen::VectorXi::Constant(F.rows(),0);
  393. Eigen::MatrixXd cfW = Eigen::MatrixXd::Constant(F.rows(),bc.cols(),0);
  394. for(unsigned i=0; i<b.size();++i)
  395. {
  396. isConstrained(b(i)) = 1;
  397. cfW.row(b(i)) << bc.row(i);
  398. }
  399. int n = I.rows();
  400. igl::GeneralPolyVectorFieldFinder<Eigen::MatrixXd, Eigen::MatrixXi> pvff(V,F,n);
  401. pvff.solve(isConstrained, cfW, I, output);
  402. }
  403. #ifdef IGL_STATIC_LIBRARY
  404. // Explicit template specialization
  405. #endif