401_BiharmonicDeformation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. #
  3. # This file is part of libigl, a simple c++ geometry processing library.
  4. #
  5. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  6. #
  7. # This Source Code Form is subject to the terms of the Mozilla Public License
  8. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. # obtain one at http://mozilla.org/MPL/2.0/.
  10. import sys, os
  11. # Add the igl library to the modules search path
  12. sys.path.insert(0, os.getcwd() + "/../")
  13. import pyigl as igl
  14. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  15. dependencies = ["glfw"]
  16. check_dependencies(dependencies)
  17. bc_frac = 1.0
  18. bc_dir = -0.03
  19. deformation_field = False
  20. V = igl.eigen.MatrixXd()
  21. U = igl.eigen.MatrixXd()
  22. V_bc = igl.eigen.MatrixXd()
  23. U_bc = igl.eigen.MatrixXd()
  24. # Z = igl.eigen.MatrixXd()
  25. F = igl.eigen.MatrixXi()
  26. b = igl.eigen.MatrixXi()
  27. def pre_draw(viewer):
  28. global bc_frac, bc_dir, deformation_field, V, U, V_bc, U_bc, F, b
  29. # Determine boundary conditions
  30. if (viewer.core.is_animating):
  31. bc_frac += bc_dir
  32. bc_dir *= (-1.0 if bc_frac >= 1.0 or bc_frac <= 0.0 else 1.0)
  33. U_bc_anim = V_bc + bc_frac * (U_bc - V_bc)
  34. if (deformation_field):
  35. D = igl.eigen.MatrixXd()
  36. D_bc = U_bc_anim - V_bc
  37. igl.harmonic(V, F, b, D_bc, 2, D)
  38. U = V + D
  39. else:
  40. igl.harmonic(V, F, b, U_bc_anim, 2, U)
  41. viewer.data().set_vertices(U)
  42. viewer.data().compute_normals()
  43. return False
  44. def key_down(viewer, key, mods):
  45. global bc_frac, bc_dir, deformation_field, V, U, V_bc, U_bc, F, b
  46. if key == ord(' '):
  47. viewer.core.is_animating = not viewer.core.is_animating
  48. return True
  49. if key == ord('D') or key == ord('d'):
  50. deformation_field = not deformation_field
  51. return True
  52. return False
  53. igl.readOBJ(TUTORIAL_SHARED_PATH + "decimated-max.obj", V, F)
  54. U = igl.eigen.MatrixXd(V)
  55. # S(i) = j: j<0 (vertex i not in handle), j >= 0 (vertex i in handle j)
  56. S = igl.eigen.MatrixXd()
  57. igl.readDMAT(TUTORIAL_SHARED_PATH + "decimated-max-selection.dmat", S)
  58. S = S.castint()
  59. b = igl.eigen.MatrixXd([[t[0] for t in [(i, S[i]) for i in range(0, V.rows())] if t[1] >= 0]]).transpose().castint()
  60. # Boundary conditions directly on deformed positions
  61. U_bc.resize(b.rows(), V.cols())
  62. V_bc.resize(b.rows(), V.cols())
  63. for bi in range(0, b.rows()):
  64. V_bc.setRow(bi, V.row(b[bi]))
  65. if S[b[bi]] == 0:
  66. # Don't move handle 0
  67. U_bc.setRow(bi, V.row(b[bi]))
  68. elif S[b[bi]] == 1:
  69. # Move handle 1 down
  70. U_bc.setRow(bi, V.row(b[bi]) + igl.eigen.MatrixXd([[0, -50, 0]]))
  71. else:
  72. # Move other handles forward
  73. U_bc.setRow(bi, V.row(b[bi]) + igl.eigen.MatrixXd([[0, 0, -25]]))
  74. # Pseudo-color based on selection
  75. C = igl.eigen.MatrixXd(F.rows(), 3)
  76. purple = igl.eigen.MatrixXd([[80.0 / 255.0, 64.0 / 255.0, 255.0 / 255.0]])
  77. gold = igl.eigen.MatrixXd([[255.0 / 255.0, 228.0 / 255.0, 58.0 / 255.0]])
  78. for f in range(0, F.rows()):
  79. if (S[F[f, 0]]) >= 0 and S[F[f, 1]] >= 0 and S[F[f, 2]] >= 0:
  80. C.setRow(f, purple)
  81. else:
  82. C.setRow(f, gold)
  83. # Plot the mesh with pseudocolors
  84. viewer = igl.glfw.Viewer()
  85. viewer.data().set_mesh(U, F)
  86. viewer.data().show_lines = False
  87. viewer.data().set_colors(C)
  88. # viewer.core.trackball_angle = igl.eigen.Quaterniond(sqrt(2.0),0,sqrt(2.0),0)
  89. # viewer.core.trackball_angle.normalize()
  90. viewer.callback_pre_draw = pre_draw
  91. viewer.callback_key_down = key_down
  92. viewer.core.animation_max_fps = 30.0
  93. print("Press [space] to toggle deformation.")
  94. print("Press 'd' to toggle between biharmonic surface or displacements.")
  95. viewer.launch()