main.cpp 3.0 KB

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