cotmatrix.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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 "cotmatrix.h"
  9. #include <vector>
  10. // For error printing
  11. #include <cstdio>
  12. #include "cotmatrix_entries.h"
  13. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  14. #include <iostream>
  15. template <typename DerivedV, typename DerivedF, typename Scalar>
  16. IGL_INLINE void igl::cotmatrix(
  17. const Eigen::MatrixBase<DerivedV> & V,
  18. const Eigen::MatrixBase<DerivedF> & F,
  19. Eigen::SparseMatrix<Scalar>& L)
  20. {
  21. using namespace Eigen;
  22. using namespace std;
  23. L.resize(V.rows(),V.rows());
  24. Matrix<int,Dynamic,2> edges;
  25. int simplex_size = F.cols();
  26. // 3 for triangles, 4 for tets
  27. assert(simplex_size == 3 || simplex_size == 4);
  28. if(simplex_size == 3)
  29. {
  30. // This is important! it could decrease the comptuation time by a factor of 2
  31. // Laplacian for a closed 2d manifold mesh will have on average 7 entries per
  32. // row
  33. L.reserve(10*V.rows());
  34. edges.resize(3,2);
  35. edges <<
  36. 1,2,
  37. 2,0,
  38. 0,1;
  39. }else if(simplex_size == 4)
  40. {
  41. L.reserve(17*V.rows());
  42. edges.resize(6,2);
  43. edges <<
  44. 1,2,
  45. 2,0,
  46. 0,1,
  47. 3,0,
  48. 3,1,
  49. 3,2;
  50. }else
  51. {
  52. return;
  53. }
  54. // Gather cotangents
  55. Matrix<Scalar,Dynamic,Dynamic> C;
  56. cotmatrix_entries(V,F,C);
  57. vector<Triplet<Scalar> > IJV;
  58. IJV.reserve(F.rows()*edges.rows()*4);
  59. // Loop over triangles
  60. for(int i = 0; i < F.rows(); i++)
  61. {
  62. // loop over edges of element
  63. for(int e = 0;e<edges.rows();e++)
  64. {
  65. int source = F(i,edges(e,0));
  66. int dest = F(i,edges(e,1));
  67. IJV.push_back(Triplet<Scalar>(source,dest,C(i,e)));
  68. IJV.push_back(Triplet<Scalar>(dest,source,C(i,e)));
  69. IJV.push_back(Triplet<Scalar>(source,source,-C(i,e)));
  70. IJV.push_back(Triplet<Scalar>(dest,dest,-C(i,e)));
  71. }
  72. }
  73. L.setFromTriplets(IJV.begin(),IJV.end());
  74. }
  75. #include "massmatrix.h"
  76. #include "cotmatrix_entries.h"
  77. #include "massmatrix.h"
  78. #include <Eigen/Geometry>
  79. #include <Eigen/QR>
  80. template <
  81. typename DerivedV,
  82. typename DerivedI,
  83. typename DerivedC,
  84. typename Scalar>
  85. IGL_INLINE void igl::cotmatrix(
  86. const Eigen::MatrixBase<DerivedV> & V,
  87. const Eigen::MatrixBase<DerivedI> & I,
  88. const Eigen::MatrixBase<DerivedC> & C,
  89. Eigen::SparseMatrix<Scalar>& L,
  90. Eigen::SparseMatrix<Scalar>& M,
  91. Eigen::SparseMatrix<Scalar>& P)
  92. {
  93. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  94. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> MatrixXS;
  95. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> VectorXS;
  96. typedef Eigen::Index Index;
  97. // number of vertices
  98. const Index n = V.rows();
  99. // number of polyfaces
  100. const Index m = C.size()-1;
  101. assert(V.cols() == 2 || V.cols() == 3);
  102. std::vector<Eigen::Triplet<Scalar> > Lfijv;
  103. std::vector<Eigen::Triplet<Scalar> > Mfijv;
  104. std::vector<Eigen::Triplet<Scalar> > Pijv;
  105. // loop over vertices; set identity for original vertices
  106. for(Index i = 0;i<V.rows();i++) { Pijv.emplace_back(i,i,1); }
  107. // loop over faces
  108. for(Index p = 0;p<C.size()-1;p++)
  109. {
  110. // number of faces/vertices in this simple polygon
  111. const Index np = C(p+1)-C(p);
  112. // Working "local" list of vertices; last vertex is new one
  113. // this needs to have 3 columns so Eigen doesn't complain about cross
  114. // products below.
  115. Eigen::Matrix<Scalar,Eigen::Dynamic,3> X = decltype(X)::Zero(np+1,3);
  116. for(Index i = 0;i<np;i++){ X.row(i).head(V.cols()) = V.row(I(C(p)+i)); };
  117. // determine weights definig position of inserted vertex
  118. {
  119. MatrixXS A = decltype(A)::Zero(np+1,np);
  120. // My equation (38) would be A w = b.
  121. VectorXS b = decltype(b)::Zero(np+1);
  122. for(Index k = 0;k<np;k++)
  123. {
  124. const RowVector3S Xkp1mk = X.row((k+1)%np)-X.row(k);
  125. const RowVector3S Xkp1mkck = Xkp1mk.cross(X.row(k));
  126. for(Index i = 0;i<np;i++)
  127. {
  128. b(i) -= 2.*(X.row(i).cross(Xkp1mk)).dot(Xkp1mkck);
  129. for(Index j = 0;j<np;j++)
  130. {
  131. A(i,j) += 2.*(X.row(j).cross(Xkp1mk)).dot(X.row(i).cross(Xkp1mk));
  132. }
  133. }
  134. }
  135. A.row(np).setConstant(1);
  136. b(np) = 1;
  137. const VectorXS w =
  138. Eigen::CompleteOrthogonalDecomposition<Eigen::MatrixXd>(A).solve(b);
  139. X.row(np) = w.transpose()*X.topRows(np);
  140. // scatter w into new row of P
  141. for(Index i = 0;i<np;i++) { Pijv.emplace_back(n+p,I(C(p)+i),w(i)); }
  142. }
  143. // "local" fan of faces. These could be statically cached, but this will
  144. // not be the bottleneck.
  145. Eigen::MatrixXi F(np,3);
  146. for(Index i = 0;i<np;i++)
  147. {
  148. F(i,0) = i;
  149. F(i,1) = (i+1)%np;
  150. F(i,2) = np;
  151. }
  152. // Cotangent contributions
  153. MatrixXS K;
  154. igl::cotmatrix_entries(X,F,K);
  155. // Massmatrix entried
  156. VectorXS Mp;
  157. {
  158. Eigen::SparseMatrix<Scalar> M;
  159. igl::massmatrix(X,F,igl::MASSMATRIX_TYPE_DEFAULT,M);
  160. Mp = M.diagonal();
  161. }
  162. // Scatter into fine Laplacian and mass matrices
  163. const auto J = [&n,&np,&p,&I,&C](Index i)->Index{return i==np?n+p:I(C(p)+i);};
  164. // Should just build Mf as a vector...
  165. for(Index i = 0;i<np+1;i++) { Mfijv.emplace_back(J(i),J(i),Mp(i)); }
  166. // loop over faces
  167. for(Index f = 0;f<np;f++)
  168. {
  169. for(Index c = 0;c<3;c++)
  170. {
  171. const Index i = F(f,(c+1)%3);
  172. const Index j = F(f,(c+2)%3);
  173. // symmetric off-diagonal
  174. Lfijv.emplace_back(J(i),J(j),K(f,c));
  175. Lfijv.emplace_back(J(j),J(i),K(f,c));
  176. // diagonal
  177. Lfijv.emplace_back(J(i),J(i),-K(f,c));
  178. Lfijv.emplace_back(J(j),J(j),-K(f,c));
  179. }
  180. }
  181. }
  182. P.resize(n+m,n);
  183. P.setFromTriplets(Pijv.begin(),Pijv.end());
  184. Eigen::SparseMatrix<Scalar> Lf(n+m,n+m);
  185. Lf.setFromTriplets(Lfijv.begin(),Lfijv.end());
  186. Eigen::SparseMatrix<Scalar> Mf(n+m,n+m);
  187. Mf.setFromTriplets(Mfijv.begin(),Mfijv.end());
  188. L = P.transpose() * Lf * P;
  189. // "unlumped" M
  190. const Eigen::SparseMatrix<Scalar> PTMP = P.transpose() * Mf * P;
  191. // Lump M
  192. const VectorXS Mdiag = PTMP * VectorXS::Ones(n,1);
  193. M = Eigen::SparseMatrix<Scalar>(Mdiag.asDiagonal());
  194. MatrixXS Vf = P*V;
  195. Eigen::MatrixXi Ff(I.size(),3);
  196. {
  197. Index f = 0;
  198. for(Index p = 0;p<C.size()-1;p++)
  199. {
  200. const Index np = C(p+1)-C(p);
  201. for(Index c = 0;c<np;c++)
  202. {
  203. Ff(f,0) = I(C(p)+c);
  204. Ff(f,1) = I(C(p)+(c+1)%np);
  205. Ff(f,2) = V.rows()+p;
  206. f++;
  207. }
  208. }
  209. assert(f == Ff.rows());
  210. }
  211. }
  212. #ifdef IGL_STATIC_LIBRARY
  213. // Explicit template instantiation
  214. // generated by autoexplicit.sh
  215. template void igl::cotmatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int>&, Eigen::SparseMatrix<double, 0, int>&, Eigen::SparseMatrix<double, 0, int>&);
  216. // generated by autoexplicit.sh
  217. template void igl::cotmatrix<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  218. template void igl::cotmatrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 4, 0, -1, 4>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 4, 0, -1, 4> > const&, Eigen::SparseMatrix<double, 0, int>&);
  219. template void igl::cotmatrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::SparseMatrix<double, 0, int>&);
  220. template void igl::cotmatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  221. #endif