bbw.cpp 6.2 KB

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