main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <igl/direct_delta_mush.h>
  2. #include <igl/directed_edge_orientations.h>
  3. #include <igl/directed_edge_parents.h>
  4. #include <igl/forward_kinematics.h>
  5. #include <igl/PI.h>
  6. #include <igl/lbs_matrix.h>
  7. #include <igl/deform_skeleton.h>
  8. #include <igl/dqs.h>
  9. #include <igl/readDMAT.h>
  10. #include <igl/readOBJ.h>
  11. #include <igl/readTGF.h>
  12. #include <igl/opengl/glfw/Viewer.h>
  13. #include <Eigen/Geometry>
  14. #include <Eigen/StdVector>
  15. #include <Eigen/Sparse>
  16. #include <vector>
  17. #include <algorithm>
  18. #include <iostream>
  19. #include "tutorial_shared_path.h"
  20. typedef std::vector<Eigen::Quaterniond, Eigen::aligned_allocator<Eigen::Quaterniond>>
  21. RotationList;
  22. typedef std::vector<Eigen::Affine3d, Eigen::aligned_allocator<Eigen::Affine3d>>
  23. TransformationList;
  24. const Eigen::RowVector3d sea_green(70. / 255., 252. / 255., 167. / 255.);
  25. Eigen::MatrixXd V, W, C, U, M, Omega;
  26. Eigen::MatrixXi F, BE;
  27. Eigen::VectorXi P;
  28. Eigen::SparseMatrix<double> W_sparse;
  29. std::vector<RotationList> poses;
  30. double anim_t = 0.0;
  31. double anim_t_dir = 0.015;
  32. bool use_ddm = false;
  33. bool recompute = true;
  34. float p = 3.;
  35. float lambda = 0.5;
  36. float kappa = 0.4; // kappa < lambda to keep R_i well-defined
  37. bool pre_draw(igl::opengl::glfw::Viewer &viewer)
  38. {
  39. using namespace Eigen;
  40. using namespace std;
  41. if (recompute) {
  42. // Find pose interval
  43. const int begin = (int)floor(anim_t) % poses.size();
  44. const int end = (int)(floor(anim_t) + 1) % poses.size();
  45. const double t = anim_t - floor(anim_t);
  46. // Interpolate pose and identity
  47. RotationList anim_pose(poses[begin].size());
  48. for (int e = 0; e < poses[begin].size(); e++) {
  49. anim_pose[e] = poses[begin][e].slerp(t, poses[end][e]);
  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. TransformationList T_list(BE.rows());
  58. for (int e = 0; e < BE.rows(); e++) {
  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. if (use_ddm) {
  67. igl::direct_delta_mush_pose_evaluation(T_list, Omega, U);
  68. igl::direct_delta_mush(V, F, C, BE, W_sparse, T_list, U);
  69. }
  70. else {
  71. U = M * T;
  72. }
  73. // Also deform skeleton edges
  74. MatrixXd CT;
  75. MatrixXi BET;
  76. igl::deform_skeleton(C, BE, T, CT, BET);
  77. viewer.data().set_vertices(U);
  78. viewer.data().set_edges(CT, BET, sea_green);
  79. viewer.data().compute_normals();
  80. if (viewer.core().is_animating) {
  81. anim_t += anim_t_dir;
  82. }
  83. else {
  84. recompute = false;
  85. }
  86. }
  87. return false;
  88. }
  89. bool key_down(igl::opengl::glfw::Viewer &viewer, unsigned char key, int mods)
  90. {
  91. recompute = true;
  92. switch (key) {
  93. case 'D':
  94. case 'd':
  95. use_ddm = !use_ddm;
  96. return true;
  97. case ' ':
  98. viewer.core().is_animating = !viewer.core().is_animating;
  99. return true;
  100. }
  101. return false;
  102. }
  103. int main(int argc, char *argv[])
  104. {
  105. using namespace Eigen;
  106. using namespace std;
  107. igl::readOBJ(TUTORIAL_SHARED_PATH "/arm.obj", V, F);
  108. U = V;
  109. igl::readTGF(TUTORIAL_SHARED_PATH "/arm.tgf", C, BE);
  110. // retrieve parents for forward kinematics
  111. igl::directed_edge_parents(BE, P);
  112. RotationList rest_pose;
  113. igl::directed_edge_orientations(C, BE, rest_pose);
  114. poses.resize(4, RotationList(4, Quaterniond::Identity()));
  115. // poses[1] // twist
  116. const Quaterniond twist(AngleAxisd(igl::PI, Vector3d(1, 0, 0)));
  117. poses[1][2] = rest_pose[2] * twist * rest_pose[2].conjugate();
  118. const Quaterniond bend(AngleAxisd(-igl::PI * 0.7, Vector3d(0, 0, 1)));
  119. poses[3][2] = rest_pose[2] * bend * rest_pose[2].conjugate();
  120. igl::readDMAT(TUTORIAL_SHARED_PATH "/arm-weights.dmat", W);
  121. W_sparse = W.sparseView();
  122. igl::lbs_matrix(V, W, M);
  123. // Precomputation for Direct Delta Mush
  124. cout<<"Initializing Direct Delta Mush..."<<endl;
  125. igl::direct_delta_mush_precomputation(V, F, C, BE, W_sparse, p, lambda, kappa, Omega);
  126. // Plot the mesh with pseudocolors
  127. igl::opengl::glfw::Viewer viewer;
  128. viewer.data().set_mesh(U, F);
  129. viewer.data().set_edges(C, BE, sea_green);
  130. viewer.data().show_lines = false;
  131. viewer.data().show_overlay_depth = false;
  132. viewer.data().line_width = 1;
  133. viewer.core().trackball_angle.normalize();
  134. viewer.callback_pre_draw = &pre_draw;
  135. viewer.callback_key_down = &key_down;
  136. viewer.core().is_animating = false;
  137. viewer.core().camera_zoom = 2.5;
  138. viewer.core().animation_max_fps = 30.;
  139. cout << "Press [d] to toggle between LBS and DQS" << endl
  140. << "Press [space] to toggle animation" << endl;
  141. viewer.launch();
  142. }