main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // If you don't have mosek installed and don't want to install it. Then
  2. // uncomment the following six lines. Don't use static library for this
  3. // example because of Mosek complications
  4. //
  5. //#define IGL_NO_MOSEK
  6. //#ifdef IGL_NO_MOSEK
  7. //#ifdef IGL_STATIC_LIBRARY
  8. //#undef IGL_STATIC_LIBRARY
  9. //#endif
  10. //#endif
  11. #include <igl/boundary_conditions.h>
  12. #include <igl/colon.h>
  13. #include <igl/column_to_quats.h>
  14. #include <igl/directed_edge_parents.h>
  15. #include <igl/forward_kinematics.h>
  16. #include <igl/jet.h>
  17. #include <igl/lbs_matrix.h>
  18. #include <igl/deform_skeleton.h>
  19. #include <igl/normalize_row_sums.h>
  20. #include <igl/readDMAT.h>
  21. #include <igl/readMESH.h>
  22. #include <igl/readTGF.h>
  23. #include <igl/viewer/Viewer.h>
  24. #include <igl/bbw/bbw.h>
  25. #include <Eigen/Geometry>
  26. #include <Eigen/StdVector>
  27. #include <vector>
  28. #include <algorithm>
  29. #include <iostream>
  30. typedef
  31. std::vector<Eigen::Quaterniond,Eigen::aligned_allocator<Eigen::Quaterniond> >
  32. RotationList;
  33. const Eigen::RowVector3d sea_green(70./255.,252./255.,167./255.);
  34. int selected = 0;
  35. Eigen::MatrixXd V,W,U,C,M;
  36. Eigen::MatrixXi T,F,BE;
  37. Eigen::VectorXi P;
  38. RotationList pose;
  39. double anim_t = 1.0;
  40. double anim_t_dir = -0.03;
  41. bool pre_draw(igl::Viewer & viewer)
  42. {
  43. using namespace Eigen;
  44. using namespace std;
  45. if(viewer.core.is_animating)
  46. {
  47. // Interpolate pose and identity
  48. RotationList anim_pose(pose.size());
  49. for(int e = 0;e<pose.size();e++)
  50. {
  51. anim_pose[e] = pose[e].slerp(anim_t,Quaterniond::Identity());
  52. }
  53. // Propogate relative rotations via FK to retrieve absolute transformations
  54. RotationList vQ;
  55. vector<Vector3d> vT;
  56. igl::forward_kinematics(C,BE,P,anim_pose,vQ,vT);
  57. const int dim = C.cols();
  58. MatrixXd T(BE.rows()*(dim+1),dim);
  59. for(int e = 0;e<BE.rows();e++)
  60. {
  61. Affine3d a = Affine3d::Identity();
  62. a.translate(vT[e]);
  63. a.rotate(vQ[e]);
  64. T.block(e*(dim+1),0,dim+1,dim) =
  65. a.matrix().transpose().block(0,0,dim+1,dim);
  66. }
  67. // Compute deformation via LBS as matrix multiplication
  68. U = M*T;
  69. // Also deform skeleton edges
  70. MatrixXd CT;
  71. MatrixXi BET;
  72. igl::deform_skeleton(C,BE,T,CT,BET);
  73. viewer.data.set_vertices(U);
  74. viewer.data.set_edges(CT,BET,sea_green);
  75. viewer.data.compute_normals();
  76. anim_t += anim_t_dir;
  77. anim_t_dir *= (anim_t>=1.0 || anim_t<=0.0?-1.0:1.0);
  78. }
  79. return false;
  80. }
  81. void set_color(igl::Viewer &viewer)
  82. {
  83. Eigen::MatrixXd C;
  84. igl::jet(W.col(selected).eval(),true,C);
  85. viewer.data.set_colors(C);
  86. }
  87. bool key_down(igl::Viewer &viewer, unsigned char key, int mods)
  88. {
  89. switch(key)
  90. {
  91. case ' ':
  92. viewer.core.is_animating = !viewer.core.is_animating;
  93. break;
  94. case '.':
  95. selected++;
  96. selected = std::min(std::max(selected,0),(int)W.cols()-1);
  97. set_color(viewer);
  98. break;
  99. case ',':
  100. selected--;
  101. selected = std::min(std::max(selected,0),(int)W.cols()-1);
  102. set_color(viewer);
  103. break;
  104. }
  105. return true;
  106. }
  107. int main(int argc, char *argv[])
  108. {
  109. using namespace Eigen;
  110. using namespace std;
  111. igl::readMESH("../shared/hand.mesh",V,T,F);
  112. U=V;
  113. igl::readTGF("../shared/hand.tgf",C,BE);
  114. // retrieve parents for forward kinematics
  115. igl::directed_edge_parents(BE,P);
  116. // Read pose as matrix of quaternions per row
  117. MatrixXd Q;
  118. igl::readDMAT("../shared/hand-pose.dmat",Q);
  119. igl::column_to_quats(Q,pose);
  120. assert(pose.size() == BE.rows());
  121. // List of boundary indices (aka fixed value indices into VV)
  122. VectorXi b;
  123. // List of boundary conditions of each weight function
  124. MatrixXd bc;
  125. igl::boundary_conditions(V,T,C,VectorXi(),BE,MatrixXi(),b,bc);
  126. // compute BBW weights matrix
  127. igl::BBWData bbw_data;
  128. // only a few iterations for sake of demo
  129. bbw_data.active_set_params.max_iter = 8;
  130. bbw_data.verbosity = 2;
  131. if(!igl::bbw(V,T,b,bc,bbw_data,W))
  132. {
  133. return false;
  134. }
  135. // Normalize weights to sum to one
  136. igl::normalize_row_sums(W,W);
  137. // precompute linear blend skinning matrix
  138. igl::lbs_matrix(V,W,M);
  139. // Plot the mesh with pseudocolors
  140. igl::Viewer viewer;
  141. viewer.data.set_mesh(U, F);
  142. set_color(viewer);
  143. viewer.data.set_edges(C,BE,sea_green);
  144. viewer.core.show_lines = false;
  145. viewer.core.show_overlay_depth = false;
  146. viewer.core.line_width = 1;
  147. viewer.core.trackball_angle.normalize();
  148. viewer.callback_pre_draw = &pre_draw;
  149. viewer.callback_key_down = &key_down;
  150. viewer.core.is_animating = false;
  151. viewer.core.animation_max_fps = 30.;
  152. cout<<
  153. "Press '.' to show next weight function."<<endl<<
  154. "Press ',' to show previous weight function."<<endl<<
  155. "Press [space] to toggle animation."<<endl;
  156. viewer.launch();
  157. }