503_ARAPParam.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import sys, os
  2. # Add the igl library to the modules search path
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  6. dependencies = ["viewer"]
  7. check_dependencies(dependencies)
  8. V = igl.eigen.MatrixXd()
  9. F = igl.eigen.MatrixXi()
  10. V_uv = igl.eigen.MatrixXd()
  11. initial_guess = igl.eigen.MatrixXd()
  12. show_uv = False
  13. def key_down(viewer, key, modifier):
  14. global show_uv, V_uv
  15. if key == ord('1'):
  16. show_uv = False
  17. elif key == ord('2'):
  18. show_uv = True
  19. elif key == ord('q'):
  20. V_uv = initial_guess
  21. if show_uv:
  22. viewer.data.set_mesh(V_uv, F)
  23. viewer.core.align_camera_center(V_uv, F)
  24. else:
  25. viewer.data.set_mesh(V, F)
  26. viewer.core.align_camera_center(V, F)
  27. viewer.data.compute_normals()
  28. return False
  29. # Load a mesh in OFF format
  30. igl.readOFF(TUTORIAL_SHARED_PATH + "camelhead.off", V, F)
  31. # Compute the initial solution for ARAP (harmonic parametrization)
  32. bnd = igl.eigen.MatrixXi()
  33. igl.boundary_loop(F, bnd)
  34. bnd_uv = igl.eigen.MatrixXd()
  35. igl.map_vertices_to_circle(V, bnd, bnd_uv)
  36. igl.harmonic(V, F, bnd, bnd_uv, 1, initial_guess)
  37. # Add dynamic regularization to avoid to specify boundary conditions
  38. arap_data = igl.ARAPData()
  39. arap_data.with_dynamics = True
  40. b = igl.eigen.MatrixXi.Zero(0, 0)
  41. bc = igl.eigen.MatrixXd.Zero(0, 0)
  42. # Initialize ARAP
  43. arap_data.max_iter = 100
  44. # 2 means that we're going to *solve* in 2d
  45. igl.arap_precomputation(V, F, 2, b, arap_data)
  46. # Solve arap using the harmonic map as initial guess
  47. V_uv = igl.eigen.MatrixXd(initial_guess) # important, make a copy of it!
  48. igl.arap_solve(bc, arap_data, V_uv)
  49. # Scale UV to make the texture more clear
  50. V_uv *= 20
  51. # Plot the mesh
  52. viewer = igl.viewer.Viewer()
  53. viewer.data.set_mesh(V, F)
  54. viewer.data.set_uv(V_uv)
  55. viewer.callback_key_down = key_down
  56. # Disable wireframe
  57. viewer.core.show_lines = False
  58. # Draw checkerboard texture
  59. viewer.core.show_texture = True
  60. # Launch the viewer
  61. viewer.launch()