bbw.cpp 6.3 KB

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