708_Picking.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python
  2. #
  3. # This file is part of libigl, a simple c++ geometry processing library.
  4. #
  5. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  6. #
  7. # This Source Code Form is subject to the terms of the Mozilla Public License
  8. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. # obtain one at http://mozilla.org/MPL/2.0/.
  10. import sys, os
  11. # Add the igl library to the modules search path
  12. sys.path.insert(0, os.getcwd() + "/../")
  13. import pyigl as igl
  14. import numpy as np
  15. from shared import TUTORIAL_SHARED_PATH, check_dependencies, print_usage
  16. dependencies = ["glfw"]
  17. check_dependencies(dependencies)
  18. def mouse_down(viewer, a, b):
  19. bc = igl.eigen.MatrixXd()
  20. # Cast a ray in the view direction starting from the mouse position
  21. fid = igl.eigen.MatrixXi(np.array([-1]))
  22. coord = igl.eigen.MatrixXd([viewer.current_mouse_x, viewer.core.viewport[3] - viewer.current_mouse_y])
  23. hit = igl.unproject_onto_mesh(coord, viewer.core.view,
  24. viewer.core.proj, viewer.core.viewport, V, F, fid, bc)
  25. if hit:
  26. # paint hit red
  27. C.setRow(fid[0, 0], igl.eigen.MatrixXd([[1, 0, 0]]))
  28. viewer.data().set_colors(C)
  29. return True
  30. return False
  31. if __name__ == "__main__":
  32. keys = {"click": "Pick face on shape"}
  33. print_usage(keys)
  34. # Mesh with per-face color
  35. V = igl.eigen.MatrixXd()
  36. F = igl.eigen.MatrixXi()
  37. C = igl.eigen.MatrixXd()
  38. # Load a mesh in OFF format
  39. igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  40. # Initialize white
  41. C.setConstant(F.rows(), 3, 1.0)
  42. # Show mesh
  43. viewer = igl.glfw.Viewer()
  44. viewer.data().set_mesh(V, F)
  45. viewer.data().set_colors(C)
  46. viewer.data().show_lines = False
  47. viewer.callback_mouse_down = mouse_down
  48. viewer.launch()