main.cpp 5.1 KB

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