401_BiharmonicDeformation.py 3.0 KB

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