501_HarmonicParam.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # This file is part of libigl, a simple c++ geometry processing library.
  2. #
  3. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public License
  6. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. # obtain one at http://mozilla.org/MPL/2.0/.
  8. import sys, os
  9. # Add the igl library to the modules search path
  10. sys.path.insert(0, os.getcwd() + "/../")
  11. import pyigl as igl
  12. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  13. dependencies = ["glfw"]
  14. check_dependencies(dependencies)
  15. V = igl.eigen.MatrixXd()
  16. F = igl.eigen.MatrixXi()
  17. V_uv = igl.eigen.MatrixXd()
  18. def key_down(viewer, key, modifier):
  19. if key == ord('1'):
  20. # Plot the 3D mesh
  21. viewer.data().set_mesh(V, F)
  22. viewer.core.align_camera_center(V, F)
  23. elif key == ord('2'):
  24. # Plot the mesh in 2D using the UV coordinates as vertex coordinates
  25. viewer.data().set_mesh(V_uv, F)
  26. viewer.core.align_camera_center(V_uv, 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. # Find the open boundary
  32. bnd = igl.eigen.MatrixXi()
  33. igl.boundary_loop(F, bnd)
  34. # Map the boundary to a circle, preserving edge proportions
  35. bnd_uv = igl.eigen.MatrixXd()
  36. igl.map_vertices_to_circle(V, bnd, bnd_uv)
  37. # Harmonic parametrization for the internal vertices
  38. igl.harmonic(V, F, bnd, bnd_uv, 1, V_uv)
  39. # Scale UV to make the texture more clear
  40. V_uv *= 5
  41. # Plot the mesh
  42. viewer = igl.glfw.Viewer()
  43. viewer.data().set_mesh(V, F)
  44. viewer.data().set_uv(V_uv)
  45. viewer.callback_key_down = key_down
  46. # Disable wireframe
  47. viewer.data().show_lines = False
  48. # Draw checkerboard texture
  49. viewer.data().show_texture = True
  50. # Launch the viewer
  51. viewer.launch()