biharmonic_coordinates.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "biharmonic_coordinates.h"
  9. #include "cotmatrix.h"
  10. #include "massmatrix.h"
  11. #include "min_quad_with_fixed.h"
  12. #include "normal_derivative.h"
  13. #include "on_boundary.h"
  14. #include <Eigen/Sparse>
  15. template <
  16. typename DerivedV,
  17. typename DerivedT,
  18. typename SType,
  19. typename DerivedW>
  20. IGL_INLINE bool igl::biharmonic_coordinates(
  21. const Eigen::PlainObjectBase<DerivedV> & V,
  22. const Eigen::PlainObjectBase<DerivedT> & T,
  23. const std::vector<std::vector<SType> > & S,
  24. Eigen::PlainObjectBase<DerivedW> & W)
  25. {
  26. using namespace Eigen;
  27. using namespace std;
  28. // This is not the most efficient way to build A, but follows "Linear
  29. // Subspace Design for Real-Time Shape Deformation" [Wang et al. 2015].
  30. SparseMatrix<double> A;
  31. {
  32. SparseMatrix<double> N,Z,L,K,M;
  33. normal_derivative(V,T,N);
  34. Array<bool,Dynamic,1> I;
  35. Array<bool,Dynamic,Dynamic> C;
  36. on_boundary(T,I,C);
  37. {
  38. std::vector<Triplet<double> >ZIJV;
  39. for(int t =0;t<T.rows();t++)
  40. {
  41. for(int f =0;f<T.cols();f++)
  42. {
  43. if(C(t,f))
  44. {
  45. const int i = t+f*T.rows();
  46. for(int c = 1;c<T.cols();c++)
  47. {
  48. ZIJV.emplace_back(T(t,(f+c)%T.cols()),i,1);
  49. }
  50. }
  51. }
  52. }
  53. Z.resize(V.rows(),N.rows());
  54. Z.setFromTriplets(ZIJV.begin(),ZIJV.end());
  55. N = (Z*N).eval();
  56. }
  57. cotmatrix(V,T,L);
  58. K = N+L;
  59. massmatrix(V,T,MASSMATRIX_TYPE_DEFAULT,M);
  60. DiagonalMatrix<double,Dynamic> Minv =
  61. ((VectorXd)M.diagonal().array().inverse()).asDiagonal();
  62. A = K.transpose() * (Minv * K);
  63. }
  64. // Vertices in point handles
  65. const size_t mp =
  66. count_if(S.begin(),S.end(),[](const vector<int> & h){return h.size()==1;});
  67. // number of region handles
  68. const size_t r = S.size()-mp;
  69. // Vertices in region handles
  70. size_t mr = 0;
  71. for(const auto & h : S)
  72. {
  73. if(h.size() > 1)
  74. {
  75. mr += h.size();
  76. }
  77. }
  78. const size_t dim = T.cols()-1;
  79. // Might as well be dense... I think...
  80. MatrixXd J = MatrixXd::Zero(mp+mr,mp+r*(dim+1));
  81. VectorXi b(mp+mr);
  82. MatrixXd H(mp+r*(dim+1),dim);
  83. {
  84. int v = 0;
  85. int c = 0;
  86. for(int h = 0;h<S.size();h++)
  87. {
  88. if(S[h].size()==1)
  89. {
  90. H.row(c) = V.block(S[h][0],0,1,dim);
  91. J(v,c++) = 1;
  92. b(v) = S[h][0];
  93. v++;
  94. }else
  95. {
  96. assert(S[h].size() >= dim+1);
  97. for(int p = 0;p<S[h].size();p++)
  98. {
  99. for(int d = 0;d<dim;d++)
  100. {
  101. J(v,c+d) = V(S[h][p],d);
  102. }
  103. J(v,c+dim) = 1;
  104. b(v) = S[h][p];
  105. v++;
  106. }
  107. H.block(c,0,dim+1,dim).setIdentity();
  108. c+=dim+1;
  109. }
  110. }
  111. }
  112. // minimize ½ W' A W'
  113. // subject to W(b,:) = J
  114. return min_quad_with_fixed(
  115. A,VectorXd::Zero(A.rows()).eval(),b,J,{},VectorXd(),true,W);
  116. }