607_Picking.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Add the igl library to the modules search path
  2. import sys, os
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
  6. # Mesh with per-face color
  7. V = igl.eigen.MatrixXd()
  8. F = igl.eigen.MatrixXi()
  9. C = igl.eigen.MatrixXd()
  10. viewer = igl.viewer.Viewer()
  11. def mouse_down(viewer, a, b):
  12. bc = igl.eigen.MatrixXd()
  13. # Cast a ray in the view direction starting from the mouse position
  14. fid = igl.eigen.MatrixXi([-1])
  15. coord = igl.eigen.MatrixXd([viewer.current_mouse_x, viewer.core.viewport[3] - viewer.current_mouse_y])
  16. hit = igl.unproject_onto_mesh(coord, viewer.core.view * viewer.core.model,
  17. viewer.core.proj, viewer.core.viewport, V, F, fid, bc)
  18. if hit:
  19. C.setRow(fid[0, 0], igl.eigen.MatrixXd([[1, 0, 0]]))
  20. viewer.data.set_colors(C)
  21. return True
  22. return False
  23. print("Usage: [LeftMouseClick] to select a face")
  24. # Load a mesh in OFF format
  25. igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  26. # Initialize white
  27. C.setConstant(F.rows(), 3, 1.0)
  28. viewer.data.set_mesh(V, F)
  29. viewer.data.set_colors(C)
  30. viewer.core.show_lines = False
  31. viewer.callback_mouse_down = mouse_down
  32. viewer.launch()