503_ARAPParam.py 1.8 KB

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