harmonic.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[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 "harmonic.h"
  9. #include "adjacency_matrix.h"
  10. #include "cotmatrix.h"
  11. #include "invert_diag.h"
  12. #include "isdiag.h"
  13. #include "massmatrix.h"
  14. #include "min_quad_with_fixed.h"
  15. #include "speye.h"
  16. #include "sum.h"
  17. #include <Eigen/Sparse>
  18. template <
  19. typename DerivedV,
  20. typename DerivedF,
  21. typename Derivedb,
  22. typename Derivedbc,
  23. typename DerivedW>
  24. IGL_INLINE bool igl::harmonic(
  25. const Eigen::MatrixBase<DerivedV> & V,
  26. const Eigen::MatrixBase<DerivedF> & F,
  27. const Eigen::MatrixBase<Derivedb> & b,
  28. const Eigen::MatrixBase<Derivedbc> & bc,
  29. const int k,
  30. Eigen::PlainObjectBase<DerivedW> & W)
  31. {
  32. typedef typename DerivedV::Scalar Scalar;
  33. Eigen::SparseMatrix<Scalar> L,M;
  34. cotmatrix(V,F,L);
  35. if(k>1)
  36. {
  37. massmatrix(V,F,MASSMATRIX_TYPE_DEFAULT,M);
  38. }
  39. return harmonic(L,M,b,bc,k,W);
  40. }
  41. template <
  42. typename DerivedF,
  43. typename Derivedb,
  44. typename Derivedbc,
  45. typename DerivedW>
  46. IGL_INLINE bool igl::harmonic(
  47. const Eigen::MatrixBase<DerivedF> & F,
  48. const Eigen::MatrixBase<Derivedb> & b,
  49. const Eigen::MatrixBase<Derivedbc> & bc,
  50. const int k,
  51. Eigen::PlainObjectBase<DerivedW> & W)
  52. {
  53. typedef typename Derivedbc::Scalar Scalar;
  54. Eigen::SparseMatrix<Scalar> A;
  55. adjacency_matrix(F,A);
  56. // sum each row
  57. Eigen::Matrix<Scalar,Eigen::Dynamic,1> Asum;
  58. igl::sum(A,1,Asum);
  59. // Eigen 3.4 still struggles to do arithmetic with sparse and diagonal matrices
  60. Eigen::SparseMatrix<Scalar> L = A - Eigen::SparseMatrix<Scalar>(Asum.asDiagonal());
  61. Eigen::SparseMatrix<Scalar> M;
  62. speye(L.rows(),M);
  63. return harmonic(L,M,b,bc,k,W);
  64. }
  65. template <
  66. typename DerivedL,
  67. typename DerivedM,
  68. typename Derivedb,
  69. typename Derivedbc,
  70. typename DerivedW>
  71. IGL_INLINE bool igl::harmonic(
  72. const Eigen::SparseCompressedBase<DerivedL> & L,
  73. const Eigen::SparseCompressedBase<DerivedM> & M,
  74. const Eigen::MatrixBase<Derivedb> & b,
  75. const Eigen::MatrixBase<Derivedbc> & bc,
  76. const int k,
  77. Eigen::PlainObjectBase<DerivedW> & W)
  78. {
  79. const int n = L.rows();
  80. assert(n == L.cols() && "L must be square");
  81. assert((k==1 || n == M.cols() ) && "M must be same size as L");
  82. assert((k==1 || n == M.rows() ) && "M must be square");
  83. assert((k==1 || igl::isdiag(M)) && "Mass matrix should be diagonal");
  84. typedef typename DerivedL::Scalar Scalar;
  85. Eigen::SparseMatrix<Scalar> Q;
  86. igl::harmonic(L,M,k,Q);
  87. min_quad_with_fixed_data<Scalar> data;
  88. min_quad_with_fixed_precompute(Q,b,Eigen::SparseMatrix<Scalar>(),true,data);
  89. W.resize(n,bc.cols());
  90. typedef Eigen::Matrix<typename Derivedbc::Scalar,Eigen::Dynamic,1> VectorXS;
  91. const VectorXS B = VectorXS::Zero(n,1);
  92. for(int w = 0;w<bc.cols();w++)
  93. {
  94. const VectorXS bcw = bc.col(w);
  95. VectorXS Ww;
  96. if(!min_quad_with_fixed_solve(data,B,bcw,VectorXS(),Ww))
  97. {
  98. return false;
  99. }
  100. W.col(w) = Ww;
  101. }
  102. return true;
  103. }
  104. template <
  105. typename DerivedL,
  106. typename DerivedM,
  107. typename DerivedQ>
  108. IGL_INLINE void igl::harmonic(
  109. const Eigen::SparseCompressedBase<DerivedL> & L,
  110. const Eigen::SparseCompressedBase<DerivedM> & M,
  111. const int k,
  112. DerivedQ & Q)
  113. {
  114. assert(L.rows() == L.cols()&&"L should be square");
  115. Q = -L;
  116. if(k == 1) return;
  117. assert(L.rows() == M.rows()&&"L should match M's dimensions");
  118. assert(M.rows() == M.cols()&&"M should be square");
  119. Eigen::SparseMatrix<typename DerivedM::Scalar> Mi;
  120. invert_diag(M,Mi);
  121. // This is **not** robust for k>2. See KKT system in [Jacobson et al. 2010]
  122. // of the kharmonic function in gptoolbox
  123. for(int p = 1;p<k;p++)
  124. {
  125. Q = (Q*Mi*-L).eval();
  126. }
  127. }
  128. #include "find.h"
  129. template <
  130. typename DerivedV,
  131. typename DerivedF,
  132. typename DerivedQ>
  133. IGL_INLINE void igl::harmonic(
  134. const Eigen::MatrixBase<DerivedV> & V,
  135. const Eigen::MatrixBase<DerivedF> & F,
  136. const int k,
  137. DerivedQ & Q)
  138. {
  139. DerivedQ L,M;
  140. cotmatrix(V,F,L);
  141. if(k>1)
  142. {
  143. massmatrix(V,F,MASSMATRIX_TYPE_DEFAULT,M);
  144. }
  145. return harmonic(L,M,k,Q);
  146. }
  147. #ifdef IGL_STATIC_LIBRARY
  148. // Explicit template instantiation
  149. // generated by autoexplicit.sh
  150. 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  151. // generated by autoexplicit.sh
  152. template bool igl::harmonic<Eigen::Matrix<double, -1, -1, 1, -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::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  153. // generated by autoexplicit.sh
  154. template bool igl::harmonic<Eigen::Matrix<double, -1, -1, 1, -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, 1, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> >&);
  155. // generated by autoexplicit.sh
  156. 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::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  157. // generated by autoexplicit.sh
  158. template void igl::harmonic<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::SparseMatrix<double, 0, int> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int, Eigen::SparseMatrix<double, 0, int>&);
  159. // generated by autoexplicit.sh
  160. 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  161. // generated by autoexplicit.sh
  162. 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  163. 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  164. #endif