501_HarmonicParam.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. def key_down(viewer, key, modifier):
  12. if key == ord('1'):
  13. # Plot the 3D mesh
  14. viewer.data.set_mesh(V, F)
  15. viewer.core.align_camera_center(V, F)
  16. elif key == ord('2'):
  17. # Plot the mesh in 2D using the UV coordinates as vertex coordinates
  18. viewer.data.set_mesh(V_uv, F)
  19. viewer.core.align_camera_center(V_uv, F)
  20. viewer.data.compute_normals()
  21. return False
  22. # Load a mesh in OFF format
  23. igl.readOFF(TUTORIAL_SHARED_PATH + "camelhead.off", V, F)
  24. # Find the open boundary
  25. bnd = igl.eigen.MatrixXi()
  26. igl.boundary_loop(F, bnd)
  27. # Map the boundary to a circle, preserving edge proportions
  28. bnd_uv = igl.eigen.MatrixXd()
  29. igl.map_vertices_to_circle(V, bnd, bnd_uv)
  30. # Harmonic parametrization for the internal vertices
  31. igl.harmonic(V, F, bnd, bnd_uv, 1, V_uv)
  32. # Scale UV to make the texture more clear
  33. V_uv *= 5
  34. # Plot the mesh
  35. viewer = igl.viewer.Viewer()
  36. viewer.data.set_mesh(V, F)
  37. viewer.data.set_uv(V_uv)
  38. viewer.callback_key_down = key_down
  39. # Disable wireframe
  40. viewer.core.show_lines = False
  41. # Draw checkerboard texture
  42. viewer.core.show_texture = True
  43. # Launch the viewer
  44. viewer.launch()