Skip to content
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

Custom widget interaction events #2882

Merged
merged 7 commits into from
Jun 28, 2022
Merged

Conversation

banesullivan
Copy link
Member

@banesullivan banesullivan commented Jun 27, 2022

Overview

Resolves #2455 by enabling users to specify custom interaction events for triggering the callbacks associated with widgets

cc @tomazbregar, please review if you have the opportunity

Example

Using vtk.vtkCommand.InteractionEvent

import numpy as np
from scipy.interpolate import griddata
import vtk
import pyvista as pv


def get_colors(n):
    """A helper function to get n colors"""
    from itertools import cycle

    import matplotlib

    cycler = matplotlib.rcParams['axes.prop_cycle']
    colors = cycle(cycler)
    colors = [next(colors)['color'] for i in range(n)]
    return colors


# Create a grid to interpolate to
xmin, xmax, ymin, ymax = 0, 100, 0, 100
x = np.linspace(xmin, xmax, num=25)
y = np.linspace(ymin, ymax, num=25)
xx, yy, zz = np.meshgrid(x, y, [0])

# Make sure boundary conditions exist
boundaries = np.array([[xmin, ymin, 0], [xmin, ymax, 0], [xmax, ymin, 0], [xmax, ymax, 0]])

# Create the PyVista mesh to hold this grid
surf = pv.StructuredGrid(xx, yy, zz)

# Create some initial perturbations
# - this array will be updated inplace
points = np.array([[33, 25, 45], [70, 80, 13], [51, 57, 10], [25, 69, 20]])


# Create an interpolation function to update that surface mesh
def update_surface(point, i):
    points[i] = point
    tp = np.vstack((points, boundaries))
    zz = griddata(tp[:, 0:2], tp[:, 2], (xx[:, :, 0], yy[:, :, 0]), method='cubic')
    surf.points[:, -1] = zz.ravel(order='F')
    return


# Get a list of unique colors for each widget
colors = get_colors(len(points))

# Begin the plotting routine
p = pv.Plotter(notebook=0)

# Add the surface to the scene
p.add_mesh(surf, color=True)

# Add the widgets which will update the surface
p.add_sphere_widget(update_surface, center=points, color=colors, radius=3, interaction_event=vtk.vtkCommand.InteractionEvent)
# Add axes grid
p.show_grid()

# Show it!
p.show()

2022-06-27 11 05 12

@github-actions github-actions bot added the enhancement Changes that enhance the library label Jun 27, 2022
@codecov
Copy link

codecov bot commented Jun 27, 2022

Codecov Report

Merging #2882 (4e0307c) into main (5c230a2) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2882      +/-   ##
==========================================
- Coverage   94.11%   94.06%   -0.05%     
==========================================
  Files          76       76              
  Lines       16506    16506              
==========================================
- Hits        15535    15527       -8     
- Misses        971      979       +8     

@akaszynski
Copy link
Member

Would be great if we can add this example here as well (probably a gallery example).

@banesullivan
Copy link
Member Author

The example given above is pulled straight from the example gallery. Only difference is using the new param:

p.add_sphere_widget(..., interaction_event=vtk.vtkCommand.InteractionEvent)

Should we update a few of those examples in the gallery to use this param? Would need to update the screen captures as well

@akaszynski
Copy link
Member

Should we update a few of those examples in the gallery to use this param? Would need to update the screen captures as well

Maybe just one if it's not too much work.

@github-actions github-actions bot added the documentation Anything related to the documentation/website label Jun 27, 2022
@banesullivan banesullivan marked this pull request as ready for review June 27, 2022 18:34
@banesullivan
Copy link
Member Author

I added a demo of the custom interaction event to the slicing example -- that was the simplest and the concept/result is the same for all examples

Co-authored-by: Bane Sullivan <banesulli@gmail.com>
akaszynski
akaszynski previously approved these changes Jun 27, 2022
Copy link
Member

@akaszynski akaszynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@banesullivan
Copy link
Member Author

In the docstrings, I annotated the type as vtk.vtkCommand, but they are really vtk.vtkCommand.EventIds

>>> type(vtk.vtkCommand.EndInteractionEvent)
<class 'vtkCommonCorePython.vtkCommand.EventIds'>

akaszynski
akaszynski previously approved these changes Jun 27, 2022
Copy link
Member

@akaszynski akaszynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still LGTM.

@banesullivan
Copy link
Member Author

@akaszynski and @tkoyama010, Will this automatically merge in 24 hours if no further action is taken?

@akaszynski
Copy link
Member

@akaszynski and @tkoyama010, Will this automatically merge in 24 hours if no further action is taken?

I wish. We could probably create a GitHub action cron job that merges approved PRs after they've been open for 24H, but for now I've been doing it manually at the 24H+ mark.

@banesullivan
Copy link
Member Author

Good enough for me - no rush here

Copy link
Member

@akaszynski akaszynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review removed due to merge with main, but merge was trivial.

Going to bypass 24H rule.

@akaszynski akaszynski merged commit dcf18ff into main Jun 28, 2022
@akaszynski akaszynski deleted the feat/widget-interaction-events branch June 28, 2022 23:24
@tkoyama010 tkoyama010 mentioned this pull request Jun 29, 2022
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Anything related to the documentation/website enhancement Changes that enhance the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sphere widget constant update
2 participants