8000 Add a Selection Gizmo by schlegelp · Pull Request #753 · pygfx/pygfx · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add a Selection Gizmo #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions examples/feature_demo/selection_helper.py
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
Selection Helper
=================

Example demonstration how to use the selection helper
to select data points:
- shift-click and drag the mouse to select points
- shift-control-click (shift-command-click on macs) to add points to the selection
- double click to deselect all points

"""

# sphinx_gallery_pygfx_docs = 'screenshot'
# sphinx_gallery_pygfx_test = 'run'

import numpy as np
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx
import pylinalg as la


canvas = WgpuCanvas()
renderer = gfx.renderers.WgpuRenderer(canvas)
scene = gfx.Scene()

xx = np.random.rand(10)
yy = np.random.rand(10)

colors = np.ones((10, 4), dtype=np.float32)
colors[:, 0] = 0

geometry = gfx.Geometry(positions=[(x, y, 0) for x, y in zip(xx, yy)], colors=colors)
ob = gfx.Points(
geometry, gfx.PointsMaterial(size=20, pick_write=True, color_mode="vertex")
)
scene.add(ob)

camera = gfx.OrthographicCamera(120, 120)
camera.show_object(ob)

selected = np.zeros(len(xx), dtype=bool)


def update_selection(sel):
"""Highlight selected points."""
# Get the world coordinates of the points
pos_world = la.vec_transform(ob.geometry.positions.data, ob.world.matrix)

# Check if the points are inside the selection box
inside = sel.is_inside(pos_world)

# Update selection
select(inside, additive="Control" in sel._event_modifiers)


def select(new_selected, additive=False):
"""Select given points."""
# See if we actually need to change any colors
if any(selected != new_selected):
if additive:
selected[:] = selected | new_selected
else:
selected[:] = new_selected
ob.geometry.colors.data[:, :] = [0, 1, 1, 1] # reset to cyan
if any(selected):
ob.geometry.colors.data[selected, :] = [1, 1, 0, 1] # set to yellow
ob.geometry.colors.update_range()

# We need to request a redraw to make sure the colors are updated
canvas.request_draw(lambda: renderer.render(scene, camera))


def deselect_all():
"""Deselect all points."""
select(np.zeros(len(xx), dtype=bool))


selection = gfx.SelectionGizmo(
renderer=renderer,
camera=camera,
scene=scene,
show_info=True, # set to false to hide info text
debug=False, # set to true to get debug output
callback_during_drag=update_selection,
callback_after_drag=update_selection,
)

renderer.add_event_handler(lambda x: deselect_all(), "double_click")


if __name__ == "__main__":
canvas.request_draw(lambda: renderer.render(scene, camera))
run()
1 change: 1 addition & 0 deletions pygfx/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ._grid import GridHelper
from ._box import BoxHelper
from ._gizmo import TransformGizmo
from ._selection import SelectionGizmo
from ._lights import (
PointLightHelper,
DirectionalLightHelper,
Expand Down
Loading
Loading
0