curved_hessian_energy.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Oded Stein <[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 "curved_hessian_energy.h"
  9. #include "orient_halfedges.h"
  10. #include "doublearea.h"
  11. #include "squared_edge_lengths.h"
  12. #include "cr_vector_laplacian.h"
  13. #include "cr_vector_mass.h"
  14. #include "cr_vector_curvature_correction.h"
  15. #include "scalar_to_cr_vector_gradient.h"
  16. template <typename DerivedV, typename DerivedF, typename ScalarQ>
  17. IGL_INLINE void
  18. igl::curved_hessian_energy(
  19. const Eigen::MatrixBase<DerivedV>& V,
  20. const Eigen::MatrixBase<DerivedF>& F,
  21. Eigen::SparseMatrix<ScalarQ>& Q)
  22. {
  23. Eigen::MatrixXi E, oE;
  24. curved_hessian_energy(V, F, E, oE, Q);
  25. }
  26. template <typename DerivedV, typename DerivedF, typename DerivedE,
  27. typename DerivedOE, typename ScalarQ>
  28. IGL_INLINE void
  29. igl::curved_hessian_energy(
  30. const Eigen::MatrixBase<DerivedV>& V,
  31. const Eigen::MatrixBase<DerivedF>& F,
  32. const Eigen::MatrixBase<DerivedE>& E,
  33. const Eigen::MatrixBase<DerivedOE>& oE,
  34. Eigen::SparseMatrix<ScalarQ>& Q)
  35. {
  36. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic>
  37. l_sq;
  38. squared_edge_lengths(V, F, l_sq);
  39. curved_hessian_energy_intrinsic(F, l_sq, E, oE, Q);
  40. }
  41. template <typename DerivedV, typename DerivedF, typename DerivedE,
  42. typename DerivedOE, typename ScalarQ>
  43. IGL_INLINE void
  44. igl::curved_hessian_energy(
  45. const Eigen::MatrixBase<DerivedV>& V,
  46. const Eigen::MatrixBase<DerivedF>& F,
  47. Eigen::PlainObjectBase<DerivedE>& E,
  48. Eigen::PlainObjectBase<DerivedOE>& oE,
  49. Eigen::SparseMatrix<ScalarQ>& Q)
  50. {
  51. if(E.rows()!=F.rows() || E.cols()!=F.cols() || oE.rows()!=F.rows() ||
  52. oE.cols()!=F.cols()) {
  53. orient_halfedges(F, E, oE);
  54. }
  55. const Eigen::PlainObjectBase<DerivedE>& cE = E;
  56. const Eigen::PlainObjectBase<DerivedOE>& coE = oE;
  57. curved_hessian_energy(V, F, cE, coE, Q);
  58. }
  59. template <typename DerivedF, typename DerivedL_sq, typename DerivedE,
  60. typename DerivedOE, typename ScalarQ>
  61. IGL_INLINE void
  62. igl::curved_hessian_energy_intrinsic(
  63. const Eigen::MatrixBase<DerivedF>& F,
  64. const Eigen::MatrixBase<DerivedL_sq>& l_sq,
  65. const Eigen::MatrixBase<DerivedE>& E,
  66. const Eigen::MatrixBase<DerivedOE>& oE,
  67. Eigen::SparseMatrix<ScalarQ>& Q)
  68. {
  69. Eigen::Matrix<typename DerivedL_sq::Scalar, Eigen::Dynamic, Eigen::Dynamic>
  70. dA;
  71. Eigen::Matrix<typename DerivedL_sq::Scalar, Eigen::Dynamic, Eigen::Dynamic>
  72. l_sqrt = l_sq.array().sqrt().matrix();
  73. doublearea(l_sqrt, dA);
  74. curved_hessian_energy_intrinsic(F, l_sq, dA, E, oE, Q);
  75. }
  76. template <typename DerivedF, typename DerivedL_sq, typename DeriveddA,
  77. typename DerivedE, typename DerivedOE, typename ScalarQ>
  78. IGL_INLINE void
  79. igl::curved_hessian_energy_intrinsic(
  80. const Eigen::MatrixBase<DerivedF>& F,
  81. const Eigen::MatrixBase<DerivedL_sq>& l_sq,
  82. const Eigen::MatrixBase<DeriveddA>& dA,
  83. const Eigen::MatrixBase<DerivedE>& E,
  84. const Eigen::MatrixBase<DerivedOE>& oE,
  85. Eigen::SparseMatrix<ScalarQ>& Q)
  86. {
  87. //Matrices that need to be combined
  88. Eigen::SparseMatrix<ScalarQ> M, D, L, K;
  89. cr_vector_mass_intrinsic(F, dA, E, M);
  90. scalar_to_cr_vector_gradient_intrinsic(F, l_sq, dA, E, oE, D);
  91. cr_vector_laplacian_intrinsic(F, l_sq, dA, E, oE, L);
  92. cr_vector_curvature_correction_intrinsic(F, l_sq, E, oE, K);
  93. //Invert M
  94. std::vector<Eigen::Triplet<ScalarQ> > tripletListMi;
  95. for(Eigen::Index k=0; k<M.outerSize(); ++k) {
  96. for(typename Eigen::SparseMatrix<ScalarQ>::InnerIterator it(M,k);
  97. it; ++it) {
  98. if(it.value() > 0) {
  99. tripletListMi.emplace_back(it.row(), it.col(), 1./it.value());
  100. }
  101. }
  102. }
  103. Eigen::SparseMatrix<ScalarQ> Mi(M.rows(), M.cols());
  104. Mi.setFromTriplets(tripletListMi.begin(), tripletListMi.end());
  105. //Hessian energy matrix
  106. Q = D.transpose()*Mi*(L + K)*Mi*D;
  107. }
  108. #ifdef IGL_STATIC_LIBRARY
  109. // Explicit template instantiation
  110. template void igl::curved_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>&);
  111. #endif