503_ARAPParam.py 1.7 KB

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