105_Overlays.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # Load a mesh in OFF format
  11. igl.readOFF(TUTORIAL_SHARED_PATH + "bunny.off", V, F)
  12. # Find the bounding box
  13. m = V.colwiseMinCoeff()
  14. M = V.colwiseMaxCoeff()
  15. # Corners of the bounding box
  16. V_box = igl.eigen.MatrixXd(
  17. [
  18. [m[0, 0], m[0, 1], m[0, 2]],
  19. [M[0, 0], m[0, 1], m[0, 2]],
  20. [M[0, 0], M[0, 1], m[0, 2]],
  21. [m[0, 0], M[0, 1], m[0, 2]],
  22. [m[0, 0], m[0, 1], M[0, 2]],
  23. [M[0, 0], m[0, 1], M[0, 2]],
  24. [M[0, 0], M[0, 1], M[0, 2]],
  25. [m[0, 0], M[0, 1], M[0, 2]]
  26. ]
  27. )
  28. E_box = igl.eigen.MatrixXi(
  29. [
  30. [0, 1],
  31. [1, 2],
  32. [2, 3],
  33. [3, 0],
  34. [4, 5],
  35. [5, 6],
  36. [6, 7],
  37. [7, 4],
  38. [0, 4],
  39. [1, 5],
  40. [2, 6],
  41. [7, 3]
  42. ]
  43. )
  44. # Plot the mesh
  45. viewer = igl.viewer.Viewer()
  46. viewer.data.set_mesh(V, F)
  47. # Plot the corners of the bounding box as points
  48. viewer.data.add_points(V_box, igl.eigen.MatrixXd([[1, 0, 0]]))
  49. # Plot the edges of the bounding box
  50. for i in range(0, E_box.rows()):
  51. viewer.data.add_edges(
  52. V_box.row(E_box[i, 0]),
  53. V_box.row(E_box[i, 1]),
  54. igl.eigen.MatrixXd([[1, 0, 0]]))
  55. # Plot labels with the coordinates of bounding box vertices
  56. l1 = 'x: ' + str(m[0, 0]) + ' y: ' + str(m[0, 1]) + ' z: ' + str(m[0, 2])
  57. viewer.data.add_label(m.transpose(), l1)
  58. l2 = 'x: ' + str(M[0, 0]) + ' y: ' + str(M[0, 1]) + ' z: ' + str(M[0, 2])
  59. viewer.data.add_label(M.transpose(), l2)
  60. # Launch the viewer
  61. viewer.launch()