|
1 | 1 | from trame.app import get_server |
2 | 2 | from trame.ui.html import DivLayout |
3 | | -from trame.widgets import html, client, vtk as vtk_widgets |
4 | | - |
| 3 | +from vtk import mutable |
| 4 | +from vtkmodules.vtkCommonDataModel import vtkCellLocator |
5 | 5 | from vtkmodules.vtkFiltersSources import vtkConeSource, vtkSphereSource |
6 | 6 | from vtkmodules.vtkRenderingCore import ( |
| 7 | + vtkActor, |
| 8 | + vtkPolyDataMapper, |
7 | 9 | vtkRenderer, |
8 | 10 | vtkRenderWindow, |
9 | | - vtkPolyDataMapper, |
10 | | - vtkActor, |
11 | 11 | ) |
12 | 12 |
|
| 13 | +from trame.widgets import client, html |
| 14 | +from trame.widgets import vtk as vtk_widgets |
| 15 | + |
13 | 16 |
|
14 | 17 | class PickingExample: |
15 | 18 | def __init__(self, server=None): |
@@ -47,11 +50,39 @@ def __init__(self, server=None): |
47 | 50 | self.get_scene_object_id(sphere_actor): "Sphere", |
48 | 51 | } |
49 | 52 |
|
| 53 | + self.remote_id_to_actor = { |
| 54 | + self.get_scene_object_id(cone_actor): cone_actor, |
| 55 | + self.get_scene_object_id(sphere_actor): sphere_actor, |
| 56 | + } |
| 57 | + |
| 58 | + self.actor_remote_id_to_locator = {} |
| 59 | + |
50 | 60 | def on_click(self, event): |
51 | 61 | if event is None: |
52 | 62 | print("Click on: --nothing--") |
53 | 63 | else: |
54 | | - print(f"Click on: {self.vtk_mapping.get(event.get('remoteId'))}") |
| 64 | + remote_id = event.get("remoteId") |
| 65 | + print(f"Click on: {self.vtk_mapping.get(remote_id)}") |
| 66 | + |
| 67 | + cell_locator = self.actor_remote_id_to_locator.get(remote_id) |
| 68 | + |
| 69 | + if not cell_locator: |
| 70 | + cell_locator = vtkCellLocator() |
| 71 | + self.actor_remote_id_to_locator[remote_id] = cell_locator |
| 72 | + actor = self.remote_id_to_actor[remote_id] |
| 73 | + cell_locator.SetDataSet(actor.GetMapper().GetInputDataObject(0, 0)) |
| 74 | + |
| 75 | + world_position = event.get("worldPosition") |
| 76 | + closes_point = [0.0, 0.0, 0.0] |
| 77 | + |
| 78 | + _subId = mutable(0) |
| 79 | + _dist2 = mutable(0.0) |
| 80 | + cell_id = mutable(-1) |
| 81 | + cell_locator.FindClosestPoint( |
| 82 | + world_position, closes_point, cell_id, _subId, _dist2 |
| 83 | + ) |
| 84 | + |
| 85 | + print(f"picked {cell_id=}") |
55 | 86 |
|
56 | 87 | def on_select(self, event): |
57 | 88 | print( |
|
0 commit comments