main.cpp 4.7 KB

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