main.cpp 4.7 KB

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