105_Overlays.py 1.4 KB

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