main.cpp 4.7 KB

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