n_polyvector_general.cpp 17 KB

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