401_BiharmonicDeformation.py 3.5 KB

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