InsightSoftwareConsortium / InsightSoftwareConsortium/itkwidgets

How to add points at runtime?

Open
#270 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
624
Forks
83
PR merge metrics
No merged PRs in 30d

Description

I created a new `.ipynb` notebook. With only one cell in it. I want to add points on slider drag. So I wrote this:
```python
#Based on https://docs.pyvista.org/examples/01-filter/poly-ray-trace.html#sphx-glr-examples-01-filter-poly-ray-trace-py
# and on https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/InteractiveParameterExploration.ipynb
from ipywidgets import interactive
import ipywidgets as widgets
from itkwidgets import view
import pyvista as pv
import numpy as np
from random import random

# Create source to ray trace
sphere = pv.Sphere(radius=0.85)

# Define line segment
start = [0, 0, 0]
stop = [random(), 1, random()]

# Perform ray trace
points, ind = sphere.ray_trace(start, stop)

# Create geometry to represent ray trace
ray = pv.Line(start, stop)
intersection = pv.PolyData(points)
points = np.concatenate((points, [start, stop]))

viewer = view(point_sets=[points], point_set_colors=['red'], geometries=[sphere, ray], geometry_colors=['white', 'blue'])

# Define a function to use with ipywidgets `interactive`
def smooth_and_view(radius=2):
stop = [random(), 1, random()]
points, ind = sphere.ray_trace(start, stop)
intersection = pv.PolyData(points)
points = np.concatenate((points, [start, stop]))
print("before:")
print(len(viewer.point_sets))
viewer.point_sets.append(points)
print("after:")
print(len(viewer.point_sets))

slider = interactive(smooth_and_view, radius=(0, 10, 1))

widgets.VBox([viewer, slider])
```
Its rendering looks similar to this:
![image](https://user-images.githubusercontent.com/2915361/73146683-f7992d00-40c4-11ea-9904-d98188eaa9b7.png)

The `len` of `viewer.point_sets` does change, while rendering does not, pointset selector does not update, as do not update 3d visuals.

The [code](https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/itkwidgets/widget_viewer.py#L492) seems to suggest it shall be changeable and updatable. What do I get wrong - how to add points at runtime?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.