503_ARAPParam.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. V = igl.eigen.MatrixXd()
  16. F = igl.eigen.MatrixXi()
  17. V_uv = igl.eigen.MatrixXd()
  18. initial_guess = igl.eigen.MatrixXd()
  19. show_uv = False
  20. def key_down(viewer, key, modifier):
  21. global show_uv, V_uv
  22. if key == ord('1'):
  23. show_uv = False
  24. elif key == ord('2'):
  25. show_uv = True
  26. elif key == ord('q'):
  27. V_uv = initial_guess
  28. if show_uv:
  29. viewer.data.set_mesh(V_uv, F)
  30. viewer.core.align_camera_center(V_uv, F)
  31. else:
  32. viewer.data.set_mesh(V, F)
  33. viewer.core.align_camera_center(V, F)
  34. viewer.data.compute_normals()
  35. return False
  36. # Load a mesh in OFF format
  37. igl.readOFF(TUTORIAL_SHARED_PATH + "camelhead.off", V, F)
  38. # Compute the initial solution for ARAP (harmonic parametrization)
  39. bnd = igl.eigen.MatrixXi()
  40. igl.boundary_loop(F, bnd)
  41. bnd_uv = igl.eigen.MatrixXd()
  42. igl.map_vertices_to_circle(V, bnd, bnd_uv)
  43. igl.harmonic(V, F, bnd, bnd_uv, 1, initial_guess)
  44. # Add dynamic regularization to avoid to specify boundary conditions
  45. arap_data = igl.ARAPData()
  46. arap_data.with_dynamics = True
  47. b = igl.eigen.MatrixXi.Zero(0, 0)
  48. bc = igl.eigen.MatrixXd.Zero(0, 0)
  49. # Initialize ARAP
  50. arap_data.max_iter = 100
  51. # 2 means that we're going to *solve* in 2d
  52. igl.arap_precomputation(V, F, 2, b, arap_data)
  53. # Solve arap using the harmonic map as initial guess
  54. V_uv = igl.eigen.MatrixXd(initial_guess) # important, make a copy of it!
  55. igl.arap_solve(bc, arap_data, V_uv)
  56. # Scale UV to make the texture more clear
  57. V_uv *= 20
  58. # Plot the mesh
  59. viewer = igl.viewer.Viewer()
  60. viewer.data.set_mesh(V, F)
  61. viewer.data.set_uv(V_uv)
  62. viewer.callback_key_down = key_down
  63. # Disable wireframe
  64. viewer.core.show_lines = False
  65. # Draw checkerboard texture
  66. viewer.core.show_texture = True
  67. # Launch the viewer
  68. viewer.launch()