harmonic.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@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 "harmonic.h"
  9. #include "cotmatrix.h"
  10. #include "massmatrix.h"
  11. #include "invert_diag.h"
  12. #include "adjacency_matrix.h"
  13. #include "sum.h"
  14. #include "diag.h"
  15. #include "min_quad_with_fixed.h"
  16. #include <Eigen/Sparse>
  17. template <
  18. typename DerivedV,
  19. typename DerivedF,
  20. typename Derivedb,
  21. typename Derivedbc,
  22. typename DerivedW>
  23. IGL_INLINE bool igl::harmonic(
  24. const Eigen::PlainObjectBase<DerivedV> & V,
  25. const Eigen::PlainObjectBase<DerivedF> & F,
  26. const Eigen::PlainObjectBase<Derivedb> & b,
  27. const Eigen::PlainObjectBase<Derivedbc> & bc,
  28. const int k,
  29. const bool use_cotan_weights,
  30. Eigen::PlainObjectBase<DerivedW> & W)
  31. {
  32. using namespace Eigen;
  33. typedef typename DerivedV::Scalar Scalar;
  34. typedef Matrix<Scalar,Dynamic,1> VectorXS;
  35. SparseMatrix<Scalar> Q;
  36. if(use_cotan_weights == true){
  37. SparseMatrix<Scalar> L,M,Mi;
  38. cotmatrix(V,F,L);
  39. switch(F.cols())
  40. {
  41. case 3:
  42. massmatrix(V,F,MASSMATRIX_TYPE_VORONOI,M);
  43. break;
  44. case 4:
  45. default:
  46. massmatrix(V,F,MASSMATRIX_TYPE_BARYCENTRIC,M);
  47. break;
  48. }
  49. invert_diag(M,Mi);
  50. Q = -L;
  51. for(int p = 1;p<k;p++)
  52. {
  53. Q = (Q*Mi*-L).eval();
  54. }
  55. }
  56. else
  57. {
  58. SparseMatrix<Scalar> A;
  59. adjacency_matrix(F,A);
  60. // sum each row
  61. SparseVector<Scalar> Asum;
  62. sum(A,1,Asum);
  63. // Convert row sums into diagonal of sparse matrix
  64. SparseMatrix<Scalar> Adiag;
  65. diag(Asum,Adiag);
  66. // Build uniform laplacian
  67. Q = -A+Adiag;
  68. }
  69. min_quad_with_fixed_data<Scalar> data;
  70. min_quad_with_fixed_precompute(Q,b,SparseMatrix<Scalar>(),true,data);
  71. W.resize(V.rows(),bc.cols());
  72. const VectorXS B = VectorXS::Zero(V.rows(),1);
  73. for(int w = 0;w<bc.cols();w++)
  74. {
  75. const VectorXS bcw = bc.col(w);
  76. VectorXS Ww;
  77. if(!min_quad_with_fixed_solve(data,B,bcw,VectorXS(),Ww))
  78. {
  79. return false;
  80. }
  81. W.col(w) = Ww;
  82. }
  83. return true;
  84. }
  85. #ifdef IGL_STATIC_LIBRARY
  86. // Explicit template specialization
  87. template bool igl::harmonic<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, int, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  88. template bool igl::harmonic<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  89. template bool igl::harmonic<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  90. #endif