mousehover3.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Compute 3D world coordinates from 2D screen pixel coordinates
  2. (hover mouse to place the points)"""
  3. from vedo import settings, Point, Text2D, TessellatedBox, ParametricShape, Plotter
  4. from vedo.utils import mag
  5. settings.default_font = "Ubuntu"
  6. settings.use_depth_peeling = True
  7. def func(evt): # this is the callback function
  8. i = evt.at # the renderer nr. which is being hit
  9. pt2d = evt.picked2d # 2D screen coordinate
  10. # passing a list of meshes will force the points to be placed on any of them
  11. pt3d = plt.at(i).compute_world_coordinate(pt2d, objs=[objs[i]])
  12. if mag(pt3d) < 0.01:
  13. return
  14. newpt = Point(pt3d).color(i)
  15. txt.text(f'2D coords: {pt2d}\n3D coords: {pt3d}\nNpt = {len(plt.objects)}')
  16. txt.color(i) # update text and color on the fly
  17. plt.at(i).add(newpt).render() # add new point and render i
  18. # create an empty text (to be updated in the callback)
  19. txt = Text2D("", s=1.4, font='Brachium', c='white', bg='green8')
  20. # create two polygonal meshes
  21. mesh1 = TessellatedBox()
  22. mesh2 = ParametricShape('ConicSpiral')
  23. mesh2.c('indigo1').lc('grey9').lw(1)
  24. objs = [mesh1, mesh2]
  25. plt = Plotter(N=2, bg='blackboard', axes=1, sharecam=False)
  26. plt.add_callback('mouse move', func)
  27. plt.at(0).show(mesh1, __doc__, viewup='z')
  28. plt.at(1).show(mesh2, txt, zoom=1.4)
  29. plt.interactive().close()