bbw.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "bbw.h"
  9. #include <igl/cotmatrix.h>
  10. #include <igl/massmatrix.h>
  11. #include <igl/invert_diag.h>
  12. #include <igl/speye.h>
  13. #include <igl/slice_into.h>
  14. #include <igl/min_quad_with_fixed.h>
  15. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  16. #include <Eigen/Sparse>
  17. #include <iostream>
  18. #include <cstdio>
  19. igl::BBWData::BBWData():
  20. partition_unity(false),
  21. W0(),
  22. #ifndef IGL_NO_MOSEK
  23. mosek_data(),
  24. #endif
  25. active_set_params(),
  26. qp_solver(QP_SOLVER_IGL_ACTIVE_SET),
  27. verbosity(0)
  28. {
  29. // We know that the Bilaplacian is positive semi-definite
  30. active_set_params.Auu_pd = true;
  31. }
  32. void igl::BBWData::print()
  33. {
  34. using namespace std;
  35. cout<<"partition_unity: "<<partition_unity<<endl;
  36. cout<<"W0=["<<endl<<W0<<endl<<"];"<<endl;
  37. cout<<"qp_solver: "<<QPSolverNames[qp_solver]<<endl;
  38. }
  39. template <
  40. typename DerivedV,
  41. typename DerivedEle,
  42. typename Derivedb,
  43. typename Derivedbc,
  44. typename DerivedW>
  45. IGL_INLINE bool igl::bbw(
  46. const Eigen::PlainObjectBase<DerivedV> & V,
  47. const Eigen::PlainObjectBase<DerivedEle> & Ele,
  48. const Eigen::PlainObjectBase<Derivedb> & b,
  49. const Eigen::PlainObjectBase<Derivedbc> & bc,
  50. igl::BBWData & data,
  51. Eigen::PlainObjectBase<DerivedW> & W
  52. )
  53. {
  54. using namespace igl;
  55. using namespace std;
  56. using namespace Eigen;
  57. // number of domain vertices
  58. int n = V.rows();
  59. // number of handles
  60. int m = bc.cols();
  61. SparseMatrix<typename DerivedW::Scalar> L;
  62. cotmatrix(V,Ele,L);
  63. MassMatrixType mmtype = MASSMATRIX_TYPE_VORONOI;
  64. if(Ele.cols() == 4)
  65. {
  66. mmtype = MASSMATRIX_TYPE_BARYCENTRIC;
  67. }
  68. SparseMatrix<typename DerivedW::Scalar> M;
  69. SparseMatrix<typename DerivedW::Scalar> Mi;
  70. massmatrix(V,Ele,mmtype,M);
  71. invert_diag(M,Mi);
  72. // Biharmonic operator
  73. SparseMatrix<typename DerivedW::Scalar> Q = L.transpose() * Mi * L;
  74. W.derived().resize(n,m);
  75. if(data.partition_unity)
  76. {
  77. // Not yet implemented
  78. assert(false);
  79. }else
  80. {
  81. // No linear terms
  82. VectorXd c = VectorXd::Zero(n);
  83. // No linear constraints
  84. SparseMatrix<typename DerivedW::Scalar> A(0,n),Aeq(0,n),Aieq(0,n);
  85. VectorXd uc(0,1),Beq(0,1),Bieq(0,1),lc(0,1);
  86. // Upper and lower box constraints (Constant bounds)
  87. VectorXd ux = VectorXd::Ones(n);
  88. VectorXd lx = VectorXd::Zero(n);
  89. active_set_params eff_params = data.active_set_params;
  90. switch(data.qp_solver)
  91. {
  92. case QP_SOLVER_IGL_ACTIVE_SET:
  93. {
  94. if(data.verbosity >= 1)
  95. {
  96. cout<<"BBW: max_iter: "<<data.active_set_params.max_iter<<endl;
  97. cout<<"BBW: eff_max_iter: "<<eff_params.max_iter<<endl;
  98. }
  99. if(data.verbosity >= 1)
  100. {
  101. cout<<"BBW: Computing initial weights for "<<m<<" handle"<<
  102. (m!=1?"s":"")<<"."<<endl;
  103. }
  104. min_quad_with_fixed_data<typename DerivedW::Scalar > mqwf;
  105. min_quad_with_fixed_precompute(Q,b,Aeq,true,mqwf);
  106. min_quad_with_fixed_solve(mqwf,c,bc,Beq,W);
  107. // decrement
  108. eff_params.max_iter--;
  109. bool error = false;
  110. // Loop over handles
  111. #pragma omp parallel for
  112. for(int i = 0;i<m;i++)
  113. {
  114. // Quicker exit for openmp
  115. if(error)
  116. {
  117. continue;
  118. }
  119. if(data.verbosity >= 1)
  120. {
  121. #pragma omp critical
  122. cout<<"BBW: Computing weight for handle "<<i+1<<" out of "<<m<<
  123. "."<<endl;
  124. }
  125. VectorXd bci = bc.col(i);
  126. VectorXd Wi;
  127. // use initial guess
  128. Wi = W.col(i);
  129. SolverStatus ret = active_set(
  130. Q,c,b,bci,Aeq,Beq,Aieq,Bieq,lx,ux,eff_params,Wi);
  131. switch(ret)
  132. {
  133. case SOLVER_STATUS_CONVERGED:
  134. break;
  135. case SOLVER_STATUS_MAX_ITER:
  136. cerr<<"active_set: max iter without convergence."<<endl;
  137. break;
  138. case SOLVER_STATUS_ERROR:
  139. default:
  140. cerr<<"active_set error."<<endl;
  141. error = true;
  142. }
  143. W.col(i) = Wi;
  144. }
  145. if(error)
  146. {
  147. return false;
  148. }
  149. break;
  150. }
  151. case QP_SOLVER_MOSEK:
  152. {
  153. #ifdef IGL_NO_MOSEK
  154. assert(false && "Use another QPSolver. Recompile without IGL_NO_MOSEK defined.");
  155. cerr<<"Use another QPSolver. Recompile without IGL_NO_MOSEK defined."<<endl;
  156. return false;
  157. #else
  158. // Loop over handles
  159. for(int i = 0;i<m;i++)
  160. {
  161. if(data.verbosity >= 1)
  162. {
  163. cout<<"BBW: Computing weight for handle "<<i+1<<" out of "<<m<<
  164. "."<<endl;
  165. }
  166. VectorXd bci = bc.col(i);
  167. VectorXd Wi;
  168. // impose boundary conditions via bounds
  169. slice_into(bci,b,ux);
  170. slice_into(bci,b,lx);
  171. bool r = mosek_quadprog(Q,c,0,A,lc,uc,lx,ux,data.mosek_data,Wi);
  172. if(!r)
  173. {
  174. return false;
  175. }
  176. W.col(i) = Wi;
  177. }
  178. #endif
  179. break;
  180. }
  181. default:
  182. {
  183. assert(false && "Unknown qp_solver");
  184. return false;
  185. }
  186. }
  187. #ifndef NDEBUG
  188. const double min_rowsum = W.rowwise().sum().array().abs().minCoeff();
  189. if(min_rowsum < 0.1)
  190. {
  191. cerr<<"bbw.cpp: Warning, minimum row sum is very low. Consider more "
  192. "active set iterations or enforcing partition of unity."<<endl;
  193. }
  194. #endif
  195. }
  196. return true;
  197. }
  198. #ifdef IGL_STATIC_LIBRARY
  199. // Explicit template specialization
  200. template bool igl::bbw<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&, igl::BBWData&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  201. #endif