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