harmonic.cpp 8.5 KB

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