401_BiharmonicDeformation.py 2.9 KB

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