bbw.cpp 5.9 KB

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