main.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <igl/active_set.h>
  2. #include <igl/boundary_facets.h>
  3. #include <igl/cotmatrix.h>
  4. #include <igl/invert_diag.h>
  5. #include <igl/jet.h>
  6. #include <igl/massmatrix.h>
  7. #include <igl/readOFF.h>
  8. #include <igl/viewer/Viewer.h>
  9. #include <Eigen/Sparse>
  10. #include <iostream>
  11. #include "tutorial_shared_path.h"
  12. Eigen::VectorXi b;
  13. Eigen::VectorXd B,bc,lx,ux,Beq,Bieq,Z;
  14. Eigen::SparseMatrix<double> Q,Aeq,Aieq;
  15. void solve(igl::viewer::Viewer &viewer)
  16. {
  17. using namespace std;
  18. igl::active_set_params as;
  19. as.max_iter = 8;
  20. igl::active_set(Q,B,b,bc,Aeq,Beq,Aieq,Bieq,lx,ux,as,Z);
  21. // Pseudo-color based on solution
  22. Eigen::MatrixXd C;
  23. igl::jet(Z,0,1,C);
  24. viewer.data.set_colors(C);
  25. }
  26. bool key_down(igl::viewer::Viewer &viewer, unsigned char key, int mod)
  27. {
  28. switch(key)
  29. {
  30. case '.':
  31. Beq(0) *= 2.0;
  32. solve(viewer);
  33. return true;
  34. case ',':
  35. Beq(0) /= 2.0;
  36. solve(viewer);
  37. return true;
  38. case ' ':
  39. solve(viewer);
  40. return true;
  41. default:
  42. return false;
  43. }
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. using namespace Eigen;
  48. using namespace std;
  49. MatrixXd V;
  50. MatrixXi F;
  51. igl::readOFF(TUTORIAL_SHARED_PATH "/cheburashka.off",V,F);
  52. // Plot the mesh
  53. igl::viewer::Viewer viewer;
  54. viewer.data.set_mesh(V, F);
  55. viewer.core.show_lines = false;
  56. viewer.callback_key_down = &key_down;
  57. // One fixed point
  58. b.resize(1,1);
  59. // point on belly.
  60. b<<2556;
  61. bc.resize(1,1);
  62. bc<<1;
  63. // Construct Laplacian and mass matrix
  64. SparseMatrix<double> L,M,Minv;
  65. igl::cotmatrix(V,F,L);
  66. igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
  67. //M = (M/M.diagonal().maxCoeff()).eval();
  68. igl::invert_diag(M,Minv);
  69. // Bi-Laplacian
  70. Q = L.transpose() * (Minv * L);
  71. // Zero linear term
  72. B = VectorXd::Zero(V.rows(),1);
  73. // Lower and upper bound
  74. lx = VectorXd::Zero(V.rows(),1);
  75. ux = VectorXd::Ones(V.rows(),1);
  76. // Equality constraint constrain solution to sum to 1
  77. Beq.resize(1,1);
  78. Beq(0) = 0.08;
  79. Aeq = M.diagonal().sparseView().transpose();
  80. // (Empty inequality constraints)
  81. solve(viewer);
  82. cout<<
  83. "Press '.' to increase scale and resolve."<<endl<<
  84. "Press ',' to decrease scale and resolve."<<endl;
  85. viewer.launch();
  86. }