201_Normals.py 1.9 KB

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