main.cpp 4.8 KB

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