201_Normals.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. N_vertices = igl.eigen.MatrixXd()
  20. N_faces = igl.eigen.MatrixXd()
  21. N_corners = igl.eigen.MatrixXd()
  22. # This function is called every time a keyboard button is pressed
  23. def key_pressed(viewer, key, modifier):
  24. if key == ord('1'):
  25. viewer.data().set_normals(N_faces)
  26. return True
  27. elif key == ord('2'):
  28. viewer.data().set_normals(N_vertices)
  29. return True
  30. elif key == ord('3'):
  31. viewer.data().set_normals(N_corners)
  32. return True
  33. return False
  34. # Load a mesh in OFF format
  35. igl.readOFF(TUTORIAL_SHARED_PATH + "fandisk.off", V, F)
  36. # Compute per-face normals
  37. N_faces = igl.eigen.MatrixXd()
  38. igl.per_face_normals(V, F, N_faces)
  39. # Compute per-vertex normals
  40. N_vertices = igl.eigen.MatrixXd()
  41. igl.per_vertex_normals(V, F, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, N_vertices)
  42. # Compute per-corner normals, |dihedral angle| > 20 degrees --> crease
  43. N_corners = igl.eigen.MatrixXd()
  44. igl.per_corner_normals(V, F, 20, N_corners)
  45. # Plot the mesh
  46. viewer = igl.glfw.Viewer()
  47. viewer.callback_key_pressed = key_pressed
  48. viewer.data().show_lines = False
  49. viewer.data().set_mesh(V, F)
  50. viewer.data().set_normals(N_faces)
  51. print("Press '1' for per-face normals.")
  52. print("Press '2' for per-vertex normals.")
  53. print("Press '3' for per-corner normals.")
  54. viewer.launch()