hessian.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Alec Jacobson <alecjacobson@gmail.com>
  4. // and Oded Stein <oded.stein@columbia.edu>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "hessian.h"
  10. #include <vector>
  11. #include "grad.h"
  12. #include "igl/doublearea.h"
  13. #include "igl/repdiag.h"
  14. template <typename DerivedV, typename DerivedF, typename Scalar>
  15. IGL_INLINE void igl::hessian(
  16. const Eigen::MatrixBase<DerivedV> & V,
  17. const Eigen::MatrixBase<DerivedF> & F,
  18. Eigen::SparseMatrix<Scalar>& H)
  19. {
  20. typedef typename DerivedV::Scalar denseScalar;
  21. typedef typename Eigen::Matrix<denseScalar, Eigen::Dynamic, 1> VecXd;
  22. typedef typename Eigen::SparseMatrix<Scalar> SparseMat;
  23. typedef typename Eigen::DiagonalMatrix
  24. <Scalar, Eigen::Dynamic, Eigen::Dynamic> DiagMat;
  25. int dim = V.cols();
  26. assert((dim==2 || dim==3) &&
  27. "The dimension of the vertices should be 2 or 3");
  28. //Construct the combined gradient matric
  29. SparseMat G;
  30. igl::grad(DerivedV(V),
  31. DerivedF(F),
  32. G, false);
  33. SparseMat GG(F.rows(), dim*V.rows());
  34. GG.reserve(G.nonZeros());
  35. for(int i=0; i<dim; ++i)
  36. GG.middleCols(i*G.cols(),G.cols()) = G.middleRows(i*F.rows(),F.rows());
  37. SparseMat D;
  38. igl::repdiag(GG,dim,D);
  39. //Compute area matrix
  40. VecXd areas;
  41. igl::doublearea(V, F, areas);
  42. DiagMat A = (0.5*areas).replicate(dim,1).asDiagonal();
  43. //Compute FEM Hessian
  44. H = D.transpose()*A*G;
  45. }
  46. #ifdef IGL_STATIC_LIBRARY
  47. // Explicit template instantiation
  48. template void igl::hessian<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>&);
  49. #endif