hessian_energy.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Alec Jacobson <alecjacobson@gmail.com> and Oded Stein <oded.stein@columbia.edu>
  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 "hessian_energy.h"
  9. #include <vector>
  10. #include <igl/hessian.h>
  11. #include <igl/massmatrix.h>
  12. #include <igl/boundary_loop.h>
  13. template <typename DerivedV, typename DerivedF, typename Scalar>
  14. IGL_INLINE void igl::hessian_energy(
  15. const Eigen::MatrixBase<DerivedV> & V,
  16. const Eigen::MatrixBase<DerivedF> & F,
  17. Eigen::SparseMatrix<Scalar>& Q)
  18. {
  19. typedef typename DerivedV::Scalar denseScalar;
  20. typedef typename Eigen::Matrix<denseScalar, Eigen::Dynamic, 1> VecXd;
  21. typedef typename Eigen::SparseMatrix<Scalar> SparseMat;
  22. typedef typename Eigen::DiagonalMatrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> DiagMat;
  23. int dim = V.cols();
  24. assert((dim==2 || dim==3) && "The dimension of the vertices should be 2 or 3");
  25. SparseMat M;
  26. igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
  27. //Kill non-interior DOFs
  28. VecXd Mint = M.diagonal();
  29. std::vector<std::vector<int> > bdryLoop;
  30. igl::boundary_loop(Eigen::PlainObjectBase<DerivedF>(F),bdryLoop);
  31. for(const std::vector<int>& loop : bdryLoop)
  32. for(const int& bdryVert : loop)
  33. Mint(bdryVert) = 0.;
  34. //Invert Mint
  35. for(int i=0; i<Mint.rows(); ++i)
  36. if(Mint(i) > 0)
  37. Mint(i) = 1./Mint(i);
  38. //Repeat Mint to form diaginal matrix
  39. DiagMat stackedMinv = Mint.replicate(dim*dim,1).asDiagonal();
  40. //Compute squared Hessian
  41. SparseMat H;
  42. igl::hessian(V,F,H);
  43. Q = H.transpose()*stackedMinv*H;
  44. }
  45. #ifdef IGL_STATIC_LIBRARY
  46. // Explicit template instantiation
  47. template void igl::hessian_energy<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>&);
  48. #endif