main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <igl/colon.h>
  2. #include <igl/cotmatrix.h>
  3. #include <igl/harmonic.h>
  4. #include <igl/jet.h>
  5. #include <igl/min_quad_with_fixed.h>
  6. #include <igl/readOBJ.h>
  7. #include <igl/readDMAT.h>
  8. #include <igl/viewer/Viewer.h>
  9. #include <algorithm>
  10. #include <iostream>
  11. double bc_frac = 1.0;
  12. double bc_dir = -0.03;
  13. bool deformation_field = false;
  14. Eigen::MatrixXd V,U,V_bc,U_bc;
  15. Eigen::VectorXd Z;
  16. Eigen::MatrixXi F;
  17. Eigen::VectorXi b;
  18. bool pre_draw(igl::Viewer & viewer)
  19. {
  20. using namespace Eigen;
  21. // Determine boundary conditions
  22. if(viewer.options.is_animating)
  23. {
  24. bc_frac += bc_dir;
  25. bc_dir *= (bc_frac>=1.0 || bc_frac<=0.0?-1.0:1.0);
  26. }
  27. const MatrixXd U_bc_anim = V_bc+bc_frac*(U_bc-V_bc);
  28. if(deformation_field)
  29. {
  30. MatrixXd D;
  31. MatrixXd D_bc = U_bc_anim - V_bc;
  32. igl::harmonic(V,F,b,D_bc,2,D);
  33. U = V+D;
  34. }else
  35. {
  36. igl::harmonic(V,F,b,U_bc_anim,2,U);
  37. }
  38. viewer.set_vertices(U);
  39. viewer.compute_normals();
  40. return false;
  41. }
  42. bool key_down(igl::Viewer &viewer, unsigned char key, int mods)
  43. {
  44. switch(key)
  45. {
  46. case ' ':
  47. viewer.options.is_animating = !viewer.options.is_animating;
  48. break;
  49. case 'D':
  50. case 'd':
  51. deformation_field = !deformation_field;
  52. break;
  53. }
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. using namespace Eigen;
  58. using namespace std;
  59. igl::readOBJ("../shared/decimated-max.obj",V,F);
  60. U=V;
  61. // S(i) = j: j<0 (vertex i not in handle), j >= 0 (vertex i in handle j)
  62. VectorXi S;
  63. igl::readDMAT("../shared/decimated-max-selection.dmat",S);
  64. igl::colon<int>(0,V.rows()-1,b);
  65. b.conservativeResize(stable_partition( b.data(), b.data()+b.size(),
  66. [&S](int i)->bool{return S(i)>=0;})-b.data());
  67. // Boundary conditions directly on deformed positions
  68. U_bc.resize(b.size(),V.cols());
  69. V_bc.resize(b.size(),V.cols());
  70. for(int bi = 0;bi<b.size();bi++)
  71. {
  72. V_bc.row(bi) = V.row(b(bi));
  73. switch(S(b(bi)))
  74. {
  75. case 0:
  76. // Don't move handle 0
  77. U_bc.row(bi) = V.row(b(bi));
  78. break;
  79. case 1:
  80. // move handle 1 down
  81. U_bc.row(bi) = V.row(b(bi)) + RowVector3d(0,-50,0);
  82. break;
  83. case 2:
  84. default:
  85. // move other handles forward
  86. U_bc.row(bi) = V.row(b(bi)) + RowVector3d(0,0,-25);
  87. break;
  88. }
  89. }
  90. // Pseudo-color based on selection
  91. MatrixXd C(F.rows(),3);
  92. RowVector3d purple(80.0/255.0,64.0/255.0,255.0/255.0);
  93. RowVector3d gold(255.0/255.0,228.0/255.0,58.0/255.0);
  94. for(int f = 0;f<F.rows();f++)
  95. {
  96. if( S(F(f,0))>=0 && S(F(f,1))>=0 && S(F(f,2))>=0)
  97. {
  98. C.row(f) = purple;
  99. }else
  100. {
  101. C.row(f) = gold;
  102. }
  103. }
  104. // Plot the mesh with pseudocolors
  105. igl::Viewer viewer;
  106. viewer.set_mesh(U, F);
  107. viewer.options.show_lines = false;
  108. viewer.set_colors(C);
  109. viewer.options.trackball_angle << 0,sqrt(2.0),0,sqrt(2.0);
  110. viewer.options.trackball_angle.normalize();
  111. viewer.callback_pre_draw = &pre_draw;
  112. viewer.callback_key_down = &key_down;
  113. //viewer.options.is_animating = true;
  114. viewer.options.animation_max_fps = 30.;
  115. viewer.launch();
  116. }