bbw.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "min_quad_with_fixed.h"
  10. #include "harmonic.h"
  11. #include "parallel_for.h"
  12. #include <Eigen/Sparse>
  13. #include <iostream>
  14. #include <mutex>
  15. #include <cstdio>
  16. igl::BBWData::BBWData():
  17. partition_unity(false),
  18. W0(),
  19. active_set_params(),
  20. verbosity(0)
  21. {
  22. // We know that the Bilaplacian is positive semi-definite
  23. active_set_params.Auu_pd = true;
  24. }
  25. void igl::BBWData::print()
  26. {
  27. using namespace std;
  28. cout<<"partition_unity: "<<partition_unity<<endl;
  29. cout<<"W0=["<<endl<<W0<<endl<<"];"<<endl;
  30. }
  31. template <
  32. typename DerivedV,
  33. typename DerivedEle,
  34. typename Derivedb,
  35. typename Derivedbc,
  36. typename DerivedW>
  37. IGL_INLINE bool igl::bbw(
  38. const Eigen::PlainObjectBase<DerivedV> & V,
  39. const Eigen::PlainObjectBase<DerivedEle> & Ele,
  40. const Eigen::PlainObjectBase<Derivedb> & b,
  41. const Eigen::PlainObjectBase<Derivedbc> & bc,
  42. igl::BBWData & data,
  43. Eigen::PlainObjectBase<DerivedW> & W
  44. )
  45. {
  46. using namespace std;
  47. using namespace Eigen;
  48. assert(!data.partition_unity && "partition_unity not implemented yet");
  49. // number of domain vertices
  50. int n = V.rows();
  51. // number of handles
  52. int m = bc.cols();
  53. // Build biharmonic operator
  54. Eigen::SparseMatrix<typename DerivedV::Scalar> Q;
  55. harmonic(V,Ele,2,Q);
  56. W.derived().resize(n,m);
  57. // No linear terms
  58. VectorXd c = VectorXd::Zero(n);
  59. // No linear constraints
  60. SparseMatrix<typename DerivedW::Scalar> A(0,n),Aeq(0,n),Aieq(0,n);
  61. VectorXd Beq(0,1),Bieq(0,1);
  62. // Upper and lower box constraints (Constant bounds)
  63. VectorXd ux = VectorXd::Ones(n);
  64. VectorXd lx = VectorXd::Zero(n);
  65. active_set_params eff_params = data.active_set_params;
  66. if(data.verbosity >= 1)
  67. {
  68. cout<<"BBW: max_iter: "<<data.active_set_params.max_iter<<endl;
  69. cout<<"BBW: eff_max_iter: "<<eff_params.max_iter<<endl;
  70. }
  71. if(data.verbosity >= 1)
  72. {
  73. cout<<"BBW: Computing initial weights for "<<m<<" handle"<<
  74. (m!=1?"s":"")<<"."<<endl;
  75. }
  76. min_quad_with_fixed_data<typename DerivedW::Scalar > mqwf;
  77. min_quad_with_fixed_precompute(Q,b,Aeq,true,mqwf);
  78. min_quad_with_fixed_solve(mqwf,c,bc,Beq,W);
  79. // decrement
  80. eff_params.max_iter--;
  81. bool error = false;
  82. // Loop over handles
  83. std::mutex critical;
  84. const auto & optimize_weight = [&](const int i)
  85. {
  86. // Quicker exit for paralle_for
  87. if(error)
  88. {
  89. return;
  90. }
  91. if(data.verbosity >= 1)
  92. {
  93. std::lock_guard<std::mutex> lock(critical);
  94. cout<<"BBW: Computing weight for handle "<<i+1<<" out of "<<m<<
  95. "."<<endl;
  96. }
  97. VectorXd bci = bc.col(i);
  98. VectorXd Wi;
  99. // use initial guess
  100. Wi = W.col(i);
  101. SolverStatus ret = active_set(
  102. Q,c,b,bci,Aeq,Beq,Aieq,Bieq,lx,ux,eff_params,Wi);
  103. switch(ret)
  104. {
  105. case SOLVER_STATUS_CONVERGED:
  106. break;
  107. case SOLVER_STATUS_MAX_ITER:
  108. cerr<<"active_set: max iter without convergence."<<endl;
  109. break;
  110. case SOLVER_STATUS_ERROR:
  111. default:
  112. cerr<<"active_set error."<<endl;
  113. error = true;
  114. }
  115. W.col(i) = Wi;
  116. };
  117. #ifdef WIN32
  118. for (int i = 0; i < m; ++i)
  119. optimize_weight(i);
  120. #else
  121. parallel_for(m,optimize_weight,2);
  122. #endif
  123. if(error)
  124. {
  125. return false;
  126. }
  127. #ifndef NDEBUG
  128. const double min_rowsum = W.rowwise().sum().array().abs().minCoeff();
  129. if(min_rowsum < 0.1)
  130. {
  131. cerr<<"bbw.cpp: Warning, minimum row sum is very low. Consider more "
  132. "active set iterations or enforcing partition of unity."<<endl;
  133. }
  134. #endif
  135. return true;
  136. }
  137. #ifdef IGL_STATIC_LIBRARY
  138. // Explicit template instantiation
  139. 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> >&);
  140. #endif