harmonic.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "adjacency_matrix.h"
  10. #include "cotmatrix.h"
  11. #include "diag.h"
  12. #include "invert_diag.h"
  13. #include "isdiag.h"
  14. #include "massmatrix.h"
  15. #include "min_quad_with_fixed.h"
  16. #include "speye.h"
  17. #include "sum.h"
  18. #include <Eigen/Sparse>
  19. template <
  20. typename DerivedV,
  21. typename DerivedF,
  22. typename Derivedb,
  23. typename Derivedbc,
  24. typename DerivedW>
  25. IGL_INLINE bool igl::harmonic(
  26. const Eigen::PlainObjectBase<DerivedV> & V,
  27. const Eigen::PlainObjectBase<DerivedF> & F,
  28. const Eigen::PlainObjectBase<Derivedb> & b,
  29. const Eigen::PlainObjectBase<Derivedbc> & bc,
  30. const int k,
  31. Eigen::PlainObjectBase<DerivedW> & W)
  32. {
  33. using namespace Eigen;
  34. typedef typename DerivedV::Scalar Scalar;
  35. SparseMatrix<Scalar> L,M;
  36. cotmatrix(V,F,L);
  37. switch(F.cols())
  38. {
  39. case 3:
  40. massmatrix(V,F,MASSMATRIX_TYPE_VORONOI,M);
  41. break;
  42. case 4:
  43. default:
  44. massmatrix(V,F,MASSMATRIX_TYPE_BARYCENTRIC,M);
  45. break;
  46. }
  47. return harmonic(L,M,b,bc,k,W);
  48. }
  49. template <
  50. typename DerivedF,
  51. typename Derivedb,
  52. typename Derivedbc,
  53. typename DerivedW>
  54. IGL_INLINE bool igl::harmonic(
  55. const Eigen::PlainObjectBase<DerivedF> & F,
  56. const Eigen::PlainObjectBase<Derivedb> & b,
  57. const Eigen::PlainObjectBase<Derivedbc> & bc,
  58. const int k,
  59. Eigen::PlainObjectBase<DerivedW> & W)
  60. {
  61. using namespace Eigen;
  62. typedef typename Derivedbc::Scalar Scalar;
  63. SparseMatrix<Scalar> A;
  64. adjacency_matrix(F,A);
  65. // sum each row
  66. SparseVector<Scalar> Asum;
  67. sum(A,1,Asum);
  68. // Convert row sums into diagonal of sparse matrix
  69. SparseMatrix<Scalar> Adiag;
  70. diag(Asum,Adiag);
  71. SparseMatrix<Scalar> L = A-Adiag;
  72. SparseMatrix<Scalar> M;
  73. speye(L.rows(),M);
  74. return harmonic(L,M,b,bc,k,W);
  75. }
  76. template <
  77. typename DerivedL,
  78. typename DerivedM,
  79. typename Derivedb,
  80. typename Derivedbc,
  81. typename DerivedW>
  82. IGL_INLINE bool igl::harmonic(
  83. const Eigen::SparseMatrix<DerivedL> & L,
  84. const Eigen::SparseMatrix<DerivedM> & M,
  85. const Eigen::PlainObjectBase<Derivedb> & b,
  86. const Eigen::PlainObjectBase<Derivedbc> & bc,
  87. const int k,
  88. Eigen::PlainObjectBase<DerivedW> & W)
  89. {
  90. const int n = L.rows();
  91. assert(n == L.cols() && "L must be square");
  92. assert(n == M.cols() && "M must be same size as L");
  93. assert(n == M.rows() && "M must be square");
  94. assert(igl::isdiag(M) && "Mass matrix should be diagonal");
  95. Eigen::SparseMatrix<DerivedM> Mi;
  96. invert_diag(M,Mi);
  97. Eigen::SparseMatrix<DerivedL> Q;
  98. Q = -L;
  99. for(int p = 1;p<k;p++)
  100. {
  101. Q = (Q*Mi*-L).eval();
  102. }
  103. typedef DerivedL Scalar;
  104. min_quad_with_fixed_data<Scalar> data;
  105. min_quad_with_fixed_precompute(Q,b,Eigen::SparseMatrix<Scalar>(),true,data);
  106. W.resize(n,bc.cols());
  107. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> VectorXS;
  108. const VectorXS B = VectorXS::Zero(n,1);
  109. for(int w = 0;w<bc.cols();w++)
  110. {
  111. const VectorXS bcw = bc.col(w);
  112. VectorXS Ww;
  113. if(!min_quad_with_fixed_solve(data,B,bcw,VectorXS(),Ww))
  114. {
  115. return false;
  116. }
  117. W.col(w) = Ww;
  118. }
  119. return true;
  120. }
  121. #ifdef IGL_STATIC_LIBRARY
  122. // Explicit template specialization
  123. 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, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  124. 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, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  125. 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, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  126. template bool igl::harmonic<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<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, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  127. template bool igl::harmonic<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<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, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  128. template bool igl::harmonic<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<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, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  129. #endif