main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. void set_color(igl::opengl::glfw::Viewer &viewer)
  80. {
  81. Eigen::MatrixXd C;
  82. igl::jet(W.col(selected).eval(),true,C);
  83. viewer.data().set_colors(C);
  84. }
  85. bool key_down(igl::opengl::glfw::Viewer &viewer, unsigned char key, int mods)
  86. {
  87. switch(key)
  88. {
  89. case ' ':
  90. viewer.core().is_animating = !viewer.core().is_animating;
  91. break;
  92. case '.':
  93. selected++;
  94. selected = std::min(std::max(selected,0),(int)W.cols()-1);
  95. set_color(viewer);
  96. break;
  97. case ',':
  98. selected--;
  99. selected = std::min(std::max(selected,0),(int)W.cols()-1);
  100. set_color(viewer);
  101. break;
  102. }
  103. return true;
  104. }
  105. int main(int argc, char *argv[])
  106. {
  107. using namespace Eigen;
  108. using namespace std;
  109. igl::readMESH(TUTORIAL_SHARED_PATH "/hand.mesh",V,T,F);
  110. U=V;
  111. igl::readTGF(TUTORIAL_SHARED_PATH "/hand.tgf",C,BE);
  112. // retrieve parents for forward kinematics
  113. igl::directed_edge_parents(BE,P);
  114. // Read pose as matrix of quaternions per row
  115. MatrixXd Q;
  116. igl::readDMAT(TUTORIAL_SHARED_PATH "/hand-pose.dmat",Q);
  117. igl::column_to_quats(Q,pose);
  118. assert(pose.size() == BE.rows());
  119. // List of boundary indices (aka fixed value indices into VV)
  120. VectorXi b;
  121. // List of boundary conditions of each weight function
  122. MatrixXd bc;
  123. igl::boundary_conditions(V,T,C,VectorXi(),BE,MatrixXi(),b,bc);
  124. // compute BBW weights matrix
  125. igl::BBWData bbw_data;
  126. // only a few iterations for sake of demo
  127. bbw_data.active_set_params.max_iter = 8;
  128. bbw_data.verbosity = 2;
  129. if(!igl::bbw(V,T,b,bc,bbw_data,W))
  130. {
  131. return EXIT_FAILURE;
  132. }
  133. //MatrixXd Vsurf = V.topLeftCorner(F.maxCoeff()+1,V.cols());
  134. //MatrixXd Wsurf;
  135. //if(!igl::bone_heat(Vsurf,F,C,VectorXi(),BE,MatrixXi(),Wsurf))
  136. //{
  137. // return false;
  138. //}
  139. //W.setConstant(V.rows(),Wsurf.cols(),1);
  140. //W.topLeftCorner(Wsurf.rows(),Wsurf.cols()) = Wsurf = Wsurf = Wsurf = Wsurf;
  141. // Normalize weights to sum to one
  142. igl::normalize_row_sums(W,W);
  143. // precompute linear blend skinning matrix
  144. igl::lbs_matrix(V,W,M);
  145. // Plot the mesh with pseudocolors
  146. igl::opengl::glfw::Viewer viewer;
  147. viewer.data().set_mesh(U, F);
  148. set_color(viewer);
  149. viewer.data().set_edges(C,BE,sea_green);
  150. viewer.data().show_lines = false;
  151. viewer.data().show_overlay_depth = false;
  152. viewer.data().line_width = 1;
  153. viewer.callback_pre_draw = &pre_draw;
  154. viewer.callback_key_down = &key_down;
  155. viewer.core().is_animating = false;
  156. viewer.core().animation_max_fps = 30.;
  157. cout<<
  158. "Press '.' to show next weight function."<<endl<<
  159. "Press ',' to show previous weight function."<<endl<<
  160. "Press [space] to toggle animation."<<endl;
  161. viewer.launch();
  162. return EXIT_SUCCESS;
  163. }