403_BoundedBiharmonicWeights.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # This file is part of libigl, a simple c++ geometry processing library.
  2. #
  3. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public License
  6. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. # obtain one at http://mozilla.org/MPL/2.0/.
  8. import sys, os
  9. # Add the igl library to the modules search path
  10. sys.path.insert(0, os.getcwd() + "/../")
  11. import pyigl as igl
  12. from shared import TUTORIAL_SHARED_PATH, check_dependencies, print_usage
  13. dependencies = ["viewer"]
  14. check_dependencies(dependencies)
  15. def pre_draw(viewer):
  16. global pose, anim_t, C, BE, P, U, M, anim_t_dir
  17. if viewer.core.is_animating:
  18. # Interpolate pose and identity
  19. anim_pose = igl.RotationList(len(pose))
  20. for e in range(len(pose)):
  21. anim_pose[e] = pose[e].slerp(anim_t, igl.eigen.Quaterniond.Identity())
  22. # Propogate relative rotations via FK to retrieve absolute transformations
  23. vQ = igl.RotationList()
  24. vT = []
  25. igl.forward_kinematics(C, BE, P, anim_pose, vQ, vT)
  26. dim = C.cols()
  27. T = igl.eigen.MatrixXd(BE.rows() * (dim + 1), dim)
  28. for e in range(BE.rows()):
  29. a = igl.eigen.Affine3d.Identity()
  30. a.translate(vT[e])
  31. a.rotate(vQ[e])
  32. T.setBlock(e * (dim + 1), 0, dim + 1, dim, a.matrix().transpose().block(0, 0, dim + 1, dim))
  33. # Compute deformation via LBS as matrix multiplication
  34. U = M * T
  35. # Also deform skeleton edges
  36. CT = igl.eigen.MatrixXd()
  37. BET = igl.eigen.MatrixXi()
  38. igl.deform_skeleton(C, BE, T, CT, BET)
  39. viewer.data.set_vertices(U)
  40. viewer.data.set_edges(CT, BET, sea_green)
  41. viewer.data.compute_normals()
  42. anim_t += anim_t_dir
  43. anim_t_dir *= -1.0 if (0.0 >= anim_t or anim_t >= 1.0) else 1.0
  44. return False
  45. def key_down(viewer, key, mods):
  46. global selected, W
  47. if key == ord('.'):
  48. selected += 1
  49. selected = min(max(selected, 0), W.cols()-1)
  50. set_color(viewer)
  51. elif key == ord(','):
  52. selected -= 1
  53. selected = min(max(selected, 0), W.cols()-1)
  54. set_color(viewer)
  55. elif key == ord(' '):
  56. viewer.core.is_animating = not viewer.core.is_animating
  57. return True
  58. def set_color(viewer):
  59. global selected, W
  60. C = igl.eigen.MatrixXd()
  61. igl.jet(W.col(selected), True, C)
  62. viewer.data.set_colors(C)
  63. if __name__ == "__main__":
  64. keys = {".": "show next weight function",
  65. ",": "show previous weight function",
  66. "space": "toggle animation"}
  67. print_usage(keys)
  68. V = igl.eigen.MatrixXd()
  69. W = igl.eigen.MatrixXd()
  70. U = igl.eigen.MatrixXd()
  71. C = igl.eigen.MatrixXd()
  72. M = igl.eigen.MatrixXd()
  73. Q = igl.eigen.MatrixXd()
  74. T = igl.eigen.MatrixXi()
  75. F = igl.eigen.MatrixXi()
  76. BE = igl.eigen.MatrixXi()
  77. P = igl.eigen.MatrixXi()
  78. sea_green = igl.eigen.MatrixXd([[70. / 255., 252. / 255., 167. / 255.]])
  79. selected = 0
  80. pose = igl.RotationList()
  81. anim_t = 1.0
  82. anim_t_dir = -0.03
  83. igl.readMESH(TUTORIAL_SHARED_PATH + "hand.mesh", V, T, F)
  84. U = igl.eigen.MatrixXd(V)
  85. igl.readTGF(TUTORIAL_SHARED_PATH + "hand.tgf", C, BE)
  86. # Retrieve parents for forward kinematics
  87. igl.directed_edge_parents(BE, P)
  88. # Read pose as matrix of quaternions per row
  89. igl.readDMAT(TUTORIAL_SHARED_PATH + "hand-pose.dmat", Q)
  90. igl.column_to_quats(Q, pose)
  91. assert (len(pose) == BE.rows())
  92. # List of boundary indices (aka fixed value indices into VV)
  93. b = igl.eigen.MatrixXi()
  94. # List of boundary conditions of each weight function
  95. bc = igl.eigen.MatrixXd()
  96. igl.boundary_conditions(V, T, C, igl.eigen.MatrixXi(), BE, igl.eigen.MatrixXi(), b, bc)
  97. # compute BBW weights matrix
  98. bbw_data = igl.BBWData()
  99. # only a few iterations for sake of demo
  100. bbw_data.active_set_params.max_iter = 8
  101. bbw_data.verbosity = 2
  102. if not igl.bbw(V, T, b, bc, bbw_data, W):
  103. exit(-1)
  104. # Normalize weights to sum to one
  105. igl.normalize_row_sums(W, W)
  106. # precompute linear blend skinning matrix
  107. igl.lbs_matrix(V, W, M)
  108. # Plot the mesh with pseudocolors
  109. viewer = igl.viewer.Viewer()
  110. viewer.data.set_mesh(U, F)
  111. set_color(viewer)
  112. viewer.data.set_edges(C, BE, sea_green)
  113. viewer.core.show_lines = False
  114. viewer.core.show_overlay_depth = False
  115. viewer.core.line_width = 1
  116. viewer.core.trackball_angle.normalize()
  117. viewer.callback_pre_draw = pre_draw
  118. viewer.callback_key_down = key_down
  119. viewer.core.is_animating = False
  120. viewer.core.animation_max_fps = 30.0
  121. viewer.launch()