main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/forward_kinematics.h>
  10. #include <igl/jet.h>
  11. #include <igl/lbs_matrix.h>
  12. #include <igl/deform_skeleton.h>
  13. #include <igl/normalize_row_sums.h>
  14. #include <igl/readDMAT.h>
  15. #include <igl/readMESH.h>
  16. #include <igl/readTGF.h>
  17. #include <igl/viewer/Viewer.h>
  18. #include <igl/bbw/bbw.h>
  19. #include <Eigen/Geometry>
  20. #include <Eigen/StdVector>
  21. #include <vector>
  22. #include <algorithm>
  23. #include <iostream>
  24. typedef
  25. std::vector<Eigen::Quaterniond,Eigen::aligned_allocator<Eigen::Quaterniond> >
  26. RotationList;
  27. const Eigen::RowVector3d sea_green(70./255.,252./255.,167./255.);
  28. int selected = 0;
  29. Eigen::MatrixXd V,W,U,C,M;
  30. Eigen::MatrixXi T,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. void set_color(igl::Viewer &viewer)
  76. {
  77. Eigen::MatrixXd C;
  78. igl::jet(W.col(selected).eval(),true,C);
  79. viewer.set_colors(C);
  80. }
  81. bool key_down(igl::Viewer &viewer, unsigned char key, int mods)
  82. {
  83. switch(key)
  84. {
  85. case ' ':
  86. viewer.options.is_animating = !viewer.options.is_animating;
  87. break;
  88. case '.':
  89. selected++;
  90. selected = std::min(std::max(selected,0),(int)W.cols()-1);
  91. set_color(viewer);
  92. break;
  93. case ',':
  94. selected--;
  95. selected = std::min(std::max(selected,0),(int)W.cols()-1);
  96. set_color(viewer);
  97. break;
  98. }
  99. }
  100. int main(int argc, char *argv[])
  101. {
  102. using namespace Eigen;
  103. using namespace std;
  104. igl::readMESH("../shared/hand.mesh",V,T,F);
  105. U=V;
  106. igl::readTGF("../shared/hand.tgf",C,BE);
  107. // retrieve parents for forward kinematics
  108. P.resize(BE.rows(),1);
  109. for(int e = 0;e<BE.rows();e++)
  110. {
  111. P(e) = -1;
  112. for(int f = 0;f<BE.rows();f++)
  113. {
  114. if(BE(e,0) == BE(f,1))
  115. {
  116. P(e) = f;
  117. }
  118. }
  119. }
  120. // Read pose as matrix of quaternions per row
  121. MatrixXd Q;
  122. igl::readDMAT("../shared/hand-pose.dmat",Q);
  123. igl::column_to_quats(Q,pose);
  124. assert(pose.size() == BE.rows());
  125. // List of boundary indices (aka fixed value indices into VV)
  126. VectorXi b;
  127. // List of boundary conditions of each weight function
  128. MatrixXd bc;
  129. igl::boundary_conditions(V,T,C,VectorXi(),BE,MatrixXi(),b,bc);
  130. // compute BBW weights matrix
  131. igl::BBWData bbw_data;
  132. // only a few iterations for sake of demo
  133. bbw_data.active_set_params.max_iter = 8;
  134. if(!igl::bbw(V,T,b,bc,bbw_data,W))
  135. {
  136. return false;
  137. }
  138. // Normalize weights to sum to one
  139. igl::normalize_row_sums(W,W);
  140. // precompute linear blend skinning matrix
  141. igl::lbs_matrix(V,W,M);
  142. // Plot the mesh with pseudocolors
  143. igl::Viewer viewer;
  144. viewer.set_mesh(U, F);
  145. set_color(viewer);
  146. viewer.set_edges(C,BE,sea_green);
  147. viewer.options.show_lines = false;
  148. viewer.options.show_overlay_depth = false;
  149. viewer.options.line_width = 1;
  150. viewer.options.trackball_angle.normalize();
  151. viewer.callback_pre_draw = &pre_draw;
  152. viewer.callback_key_down = &key_down;
  153. viewer.options.is_animating = false;
  154. viewer.options.animation_max_fps = 30.;
  155. viewer.launch();
  156. }