File: mousehover1.py

package info (click to toggle)
vedo 2025.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,404 kB
  • sloc: python: 64,792; javascript: 1,932; xml: 437; sh: 139; makefile: 6
file content (36 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Visualize scalar values interactively
by hovering the mouse on a mesh
Press c to clear the path"""
from vedo import *

def func(evt):                       ### called every time mouse moves!
    msh = evt.object                 # get the mesh that triggered the event
    if not msh:
        return                       # mouse hits nothing, return.
    pt  = evt.picked3d               # 3d coords of point under mouse
    pid = msh.closest_point(pt, return_point_id=True)
    txt =(
        f"Point:  {precision(pt[:2]  ,2)}\n"
        f"Height: {precision(arr[pid],3)}\n"
        f"Ground speed: {precision(evt.speed3d*100,2)}"
    )
    msg.text(txt)                    # update text message

    ar = Arrow(pt - evt.delta3d, pt, s=0.001, c='orange5')
    fp = msh.flagpole(
        txt, point=pt,s=0.04, c='k', font="VictorMono",
    )
    fp.follow_camera()                 # make it always face the camera
    plt.remove("FlagPole").add(ar, fp) # remove the old flagpole, add the new
    plt.render()

msg = Text2D(pos='bottom-left', font="VictorMono") # an empty text
hil = ParametricShape('RandomHills').cmap('terrain').add_scalarbar()
arr = hil.pointdata["Scalars"]       # numpy array with heights

plt = Plotter(axes=1, bg2='lightblue')
plt.add_callback('mouse move', func) # add the callback function
plt.add_callback('keyboard', lambda _: plt.remove("Arrow").render())
plt.show(hil, msg, __doc__, viewup='z')
plt.close()