main.cpp 4.7 KB

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