main.cpp 5.0 KB

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