708_Picking.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import numpy as np
  13. from shared import TUTORIAL_SHARED_PATH, check_dependencies, print_usage
  14. dependencies = ["glfw"]
  15. check_dependencies(dependencies)
  16. def mouse_down(viewer, a, b):
  17. bc = igl.eigen.MatrixXd()
  18. # Cast a ray in the view direction starting from the mouse position
  19. fid = igl.eigen.MatrixXi(np.array([-1]))
  20. coord = igl.eigen.MatrixXd([viewer.current_mouse_x, viewer.core.viewport[3] - viewer.current_mouse_y])
  21. hit = igl.unproject_onto_mesh(coord, viewer.core.view,
  22. viewer.core.proj, viewer.core.viewport, V, F, fid, bc)
  23. if hit:
  24. # paint hit red
  25. C.setRow(fid[0, 0], igl.eigen.MatrixXd([[1, 0, 0]]))
  26. viewer.data().set_colors(C)
  27. return True
  28. return False
  29. if __name__ == "__main__":
  30. keys = {"click": "Pick face on shape"}
  31. print_usage(keys)
  32. # Mesh with per-face color
  33. V = igl.eigen.MatrixXd()
  34. F = igl.eigen.MatrixXi()
  35. C = igl.eigen.MatrixXd()
  36. # Load a mesh in OFF format
  37. igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  38. # Initialize white
  39. C.setConstant(F.rows(), 3, 1.0)
  40. # Show mesh
  41. viewer = igl.glfw.Viewer()
  42. viewer.data().set_mesh(V, F)
  43. viewer.data().set_colors(C)
  44. viewer.data().show_lines = False
  45. viewer.callback_mouse_down = mouse_down
  46. viewer.launch()