501_HarmonicParam.py 1.8 KB

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