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