405_AsRigidAsPossible.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. from math import sin, cos, pi
  10. # Add the igl library to the modules search path
  11. sys.path.insert(0, os.getcwd() + "/../")
  12. import pyigl as igl
  13. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  14. dependencies = ["glfw"]
  15. check_dependencies(dependencies)
  16. sea_green = igl.eigen.MatrixXd([[70. / 255., 252. / 255., 167. / 255.]])
  17. V = igl.eigen.MatrixXd()
  18. U = igl.eigen.MatrixXd()
  19. F = igl.eigen.MatrixXi()
  20. S = igl.eigen.MatrixXd()
  21. b = igl.eigen.MatrixXi()
  22. mid = igl.eigen.MatrixXd()
  23. anim_t = 0.0
  24. anim_t_dir = 0.03
  25. arap_data = igl.ARAPData()
  26. def pre_draw(viewer):
  27. global anim_t
  28. bc = igl.eigen.MatrixXd(b.size(), V.cols())
  29. for i in range(0, b.size()):
  30. bc.setRow(i, V.row(b[i]))
  31. if S[b[i]] == 0:
  32. r = mid[0] * 0.25
  33. bc[i, 0] += r * sin(0.5 * anim_t * 2. * pi)
  34. bc[i, 1] = bc[i, 1] - r + r * cos(pi + 0.5 * anim_t * 2. * pi)
  35. elif S[b[i]] == 1:
  36. r = mid[1] * 0.15
  37. bc[i, 1] = bc[i, 1] + r + r * cos(pi + 0.15 * anim_t * 2. * pi)
  38. bc[i, 2] -= r * sin(0.15 * anim_t * 2. * pi)
  39. elif S[b[i]] == 2:
  40. r = mid[1] * 0.15
  41. bc[i, 2] = bc[i, 2] + r + r * cos(pi + 0.35 * anim_t * 2. * pi)
  42. bc[i, 0] += r * sin(0.35 * anim_t * 2. * pi)
  43. igl.arap_solve(bc, arap_data, U)
  44. viewer.data().set_vertices(U)
  45. viewer.data().compute_normals()
  46. if viewer.core.is_animating:
  47. anim_t += anim_t_dir
  48. return False
  49. def key_down(viewer, key, mods):
  50. if key == ord(' '):
  51. viewer.core.is_animating = not viewer.core.is_animating
  52. return True
  53. return False
  54. igl.readOFF(TUTORIAL_SHARED_PATH + "decimated-knight.off", V, F)
  55. U = igl.eigen.MatrixXd(V)
  56. igl.readDMAT(TUTORIAL_SHARED_PATH + "decimated-knight-selection.dmat", S)
  57. # Vertices in selection
  58. 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()
  59. # Centroid
  60. mid = 0.5 * (V.colwiseMaxCoeff() + V.colwiseMinCoeff())
  61. # Precomputation
  62. arap_data.max_iter = 100
  63. igl.arap_precomputation(V, F, V.cols(), b, arap_data)
  64. # Set color based on selection
  65. C = igl.eigen.MatrixXd(F.rows(), 3)
  66. purple = igl.eigen.MatrixXd([[80.0 / 255.0, 64.0 / 255.0, 255.0 / 255.0]])
  67. gold = igl.eigen.MatrixXd([[255.0 / 255.0, 228.0 / 255.0, 58.0 / 255.0]])
  68. for f in range(0, F.rows()):
  69. if S[F[f, 0]] >= 0 and S[F[f, 1]] >= 0 and S[F[f, 2]] >= 0:
  70. C.setRow(f, purple)
  71. else:
  72. C.setRow(f, gold)
  73. # Plot the mesh with pseudocolors
  74. viewer = igl.glfw.Viewer()
  75. viewer.data().set_mesh(U, F)
  76. viewer.data().set_colors(C)
  77. viewer.callback_pre_draw = pre_draw
  78. viewer.callback_key_down = key_down
  79. viewer.core.is_animating = True
  80. viewer.core.animation_max_fps = 30.
  81. print("Press [space] to toggle animation")
  82. viewer.launch()