401_BiharmonicDeformation.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import igl
  2. bc_frac = 1.0
  3. bc_dir = -0.03
  4. deformation_field = False
  5. V = igl.eigen.MatrixXd()
  6. U = igl.eigen.MatrixXd()
  7. V_bc = igl.eigen.MatrixXd()
  8. U_bc = igl.eigen.MatrixXd()
  9. # Z = igl.eigen.MatrixXd()
  10. F = igl.eigen.MatrixXi()
  11. b = igl.eigen.MatrixXi()
  12. def pre_draw(viewer):
  13. global V, bc_frac, bc_dir, U, U_bc, V_bc, b, deformation_field
  14. # Determine boundary conditions
  15. if (viewer.core.is_animating):
  16. bc_frac += bc_dir
  17. bc_dir *= (-1.0 if bc_frac>=1.0 or bc_frac <= 0.0 else 1.0)
  18. U_bc_anim = V_bc+bc_frac*(U_bc-V_bc)
  19. if (deformation_field):
  20. D = igl.eigen.MatrixXd()
  21. D_bc = U_bc_anim - V_bc
  22. igl.harmonic(V,F,b,D_bc,2,D)
  23. U = V+D;
  24. else:
  25. D = igl.eigen.MatrixXd()
  26. igl.harmonic(V,F,b,U_bc_anim,2,D)
  27. U = D
  28. # global U
  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 deformation_field
  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=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()