main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Don't use static library for this example because of Mosek complications
  2. //#define IGL_NO_MOSEK
  3. #ifdef IGL_NO_MOSEK
  4. #undef IGL_STATIC_LIBRARY
  5. #endif
  6. #include <igl/boundary_conditions.h>
  7. #include <igl/colon.h>
  8. #include <igl/column_to_quats.h>
  9. #include <igl/directed_edge_parents.h>
  10. #include <igl/forward_kinematics.h>
  11. #include <igl/jet.h>
  12. #include <igl/lbs_matrix.h>
  13. #include <igl/deform_skeleton.h>
  14. #include <igl/normalize_row_sums.h>
  15. #include <igl/readDMAT.h>
  16. #include <igl/readMESH.h>
  17. #include <igl/readTGF.h>
  18. #include <igl/viewer/Viewer.h>
  19. #include <igl/bbw/bbw.h>
  20. #include <Eigen/Geometry>
  21. #include <Eigen/StdVector>
  22. #include <vector>
  23. #include <algorithm>
  24. #include <iostream>
  25. typedef
  26. std::vector<Eigen::Quaterniond,Eigen::aligned_allocator<Eigen::Quaterniond> >
  27. RotationList;
  28. const Eigen::RowVector3d sea_green(70./255.,252./255.,167./255.);
  29. Eigen::MatrixXd V,W;
  30. Eigen::MatrixXi F,BE;
  31. Eigen::VectorXi P;
  32. RotationList pose;
  33. double anim_t = 1.0;
  34. double anim_t_dir = -0.03;
  35. bool pre_draw(igl::Viewer & viewer)
  36. {
  37. using namespace Eigen;
  38. using namespace std;
  39. if(viewer.options.is_animating)
  40. {
  41. // Interpolate pose and identity
  42. RotationList anim_pose(pose.size());
  43. for(int e = 0;e<pose.size();e++)
  44. {
  45. anim_pose[e] = pose[e].slerp(anim_t,Quaterniond::Identity());
  46. }
  47. // Propogate relative rotations via FK to retrieve absolute transformations
  48. RotationList vQ;
  49. vector<Vector3d> vT;
  50. igl::forward_kinematics(C,BE,P,anim_pose,vQ,vT);
  51. const int dim = C.cols();
  52. MatrixXd T(BE.rows()*(dim+1),dim);
  53. for(int e = 0;e<BE.rows();e++)
  54. {
  55. Affine3d a = Affine3d::Identity();
  56. a.translate(vT[e]);
  57. a.rotate(vQ[e]);
  58. T.block(e*(dim+1),0,dim+1,dim) =
  59. a.matrix().transpose().block(0,0,dim+1,dim);
  60. }
  61. // Compute deformation via LBS as matrix multiplication
  62. U = M*T;
  63. // Also deform skeleton edges
  64. MatrixXd CT;
  65. MatrixXi BET;
  66. igl::deform_skeleton(C,BE,T,CT,BET);
  67. viewer.set_vertices(U);
  68. viewer.set_edges(CT,BET,sea_green);
  69. viewer.compute_normals();
  70. anim_t += anim_t_dir;
  71. anim_t_dir *= (anim_t>=1.0 || anim_t<=0.0?-1.0:1.0);
  72. }
  73. return false;
  74. }
  75. bool key_down(igl::Viewer &viewer, unsigned char key, int mods)
  76. {
  77. switch(key)
  78. {
  79. case ' ':
  80. viewer.options.is_animating = !viewer.options.is_animating;
  81. break;
  82. }
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. using namespace Eigen;
  87. using namespace std;
  88. igl::readOBJ("../shared/arm.obj",V,F);
  89. U=V;
  90. igl::readTGF("../shared/arm.tgf",C,BE);
  91. // retrieve parents for forward kinematics
  92. directed_edge_parents(BE,P);
  93. igl::readDMAT("../shared/arm-weights.dmat",W);
  94. pose_0 identity
  95. pose_1 twist
  96. pose_2 bend
  97. // Plot the mesh with pseudocolors
  98. igl::Viewer viewer;
  99. viewer.set_mesh(U, F);
  100. viewer.set_edges(C,BE,sea_green);
  101. viewer.options.show_lines = false;
  102. viewer.options.show_overlay_depth = false;
  103. viewer.options.line_width = 1;
  104. viewer.options.trackball_angle.normalize();
  105. viewer.callback_pre_draw = &pre_draw;
  106. viewer.callback_key_down = &key_down;
  107. viewer.options.is_animating = false;
  108. viewer.options.animation_max_fps = 30.;
  109. viewer.launch();
  110. }