401_BiharmonicDeformation.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import sys, os
  2. # Add the igl library to the modules search path
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  6. dependencies = ["viewer"]
  7. check_dependencies(dependencies)
  8. bc_frac = 1.0
  9. bc_dir = -0.03
  10. deformation_field = False
  11. V = igl.eigen.MatrixXd()
  12. U = igl.eigen.MatrixXd()
  13. V_bc = igl.eigen.MatrixXd()
  14. U_bc = igl.eigen.MatrixXd()
  15. # Z = igl.eigen.MatrixXd()
  16. F = igl.eigen.MatrixXi()
  17. b = igl.eigen.MatrixXi()
  18. def pre_draw(viewer):
  19. global bc_frac, bc_dir, deformation_field, V, U, V_bc, U_bc, F, b
  20. # Determine boundary conditions
  21. if (viewer.core.is_animating):
  22. bc_frac += bc_dir
  23. bc_dir *= (-1.0 if bc_frac >= 1.0 or bc_frac <= 0.0 else 1.0)
  24. U_bc_anim = V_bc + bc_frac * (U_bc - V_bc)
  25. if (deformation_field):
  26. D = igl.eigen.MatrixXd()
  27. D_bc = U_bc_anim - V_bc
  28. igl.harmonic(V, F, b, D_bc, 2, D)
  29. U = V + D
  30. else:
  31. igl.harmonic(V, F, b, U_bc_anim, 2, U)
  32. viewer.data.set_vertices(U)
  33. viewer.data.compute_normals()
  34. return False
  35. def key_down(viewer, key, mods):
  36. global bc_frac, bc_dir, deformation_field, V, U, V_bc, U_bc, F, b
  37. if key == ord(' '):
  38. viewer.core.is_animating = not viewer.core.is_animating
  39. return True
  40. if key == ord('D') or key == ord('d'):
  41. deformation_field = not deformation_field
  42. return True
  43. return False
  44. igl.readOBJ(TUTORIAL_SHARED_PATH + "decimated-max.obj", V, F)
  45. U = igl.eigen.MatrixXd(V)
  46. # S(i) = j: j<0 (vertex i not in handle), j >= 0 (vertex i in handle j)
  47. S = igl.eigen.MatrixXd()
  48. igl.readDMAT(TUTORIAL_SHARED_PATH + "decimated-max-selection.dmat", S)
  49. S = S.castint()
  50. b = igl.eigen.MatrixXi([[t[0] for t in [(i, S[i]) for i in range(0, V.rows())] if t[1] >= 0]]).transpose()
  51. # Boundary conditions directly on deformed positions
  52. U_bc.resize(b.rows(), V.cols())
  53. V_bc.resize(b.rows(), V.cols())
  54. for bi in range(0, b.rows()):
  55. V_bc.setRow(bi, V.row(b[bi]))
  56. if S[b[bi]] == 0:
  57. # Don't move handle 0
  58. U_bc.setRow(bi, V.row(b[bi]))
  59. elif S[b[bi]] == 1:
  60. # Move handle 1 down
  61. U_bc.setRow(bi, V.row(b[bi]) + igl.eigen.MatrixXd([[0, -50, 0]]))
  62. else:
  63. # Move other handles forward
  64. U_bc.setRow(bi, V.row(b[bi]) + igl.eigen.MatrixXd([[0, 0, -25]]))
  65. # Pseudo-color based on selection
  66. C = igl.eigen.MatrixXd(F.rows(), 3)
  67. purple = igl.eigen.MatrixXd([[80.0 / 255.0, 64.0 / 255.0, 255.0 / 255.0]])
  68. gold = igl.eigen.MatrixXd([[255.0 / 255.0, 228.0 / 255.0, 58.0 / 255.0]])
  69. for f in range(0, F.rows()):
  70. if (S[F[f, 0]]) >= 0 and S[F[f, 1]] >= 0 and S[F[f, 2]] >= 0:
  71. C.setRow(f, purple)
  72. else:
  73. C.setRow(f, gold)
  74. # Plot the mesh with pseudocolors
  75. viewer = igl.viewer.Viewer()
  76. viewer.data.set_mesh(U, F)
  77. viewer.core.show_lines = False
  78. viewer.data.set_colors(C)
  79. # viewer.core.trackball_angle = igl.eigen.Quaterniond(sqrt(2.0),0,sqrt(2.0),0)
  80. # viewer.core.trackball_angle.normalize()
  81. viewer.callback_pre_draw = pre_draw
  82. viewer.callback_key_down = key_down
  83. viewer.core.animation_max_fps = 30.0
  84. print("Press [space] to toggle deformation.")
  85. print("Press 'd' to toggle between biharmonic surface or displacements.")
  86. viewer.launch()