503_ARAPParam.py 2.4 KB

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