405_AsRigidAsPossible.py 3.1 KB

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