-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathviewerLinker.py
383 lines (313 loc) · 14.4 KB
/
viewerLinker.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# code largely copied from https://sourceforge.net/p/pyve/code/ci/master/tree/PyVE/components/viewer.py
# MIT licensed
from ccpi.viewer.CILViewer import CILInteractorStyle as CIL3DInteractorStyle
from ccpi.viewer.CILViewer2D import CILInteractorStyle as CIL2DInteractorStyle
from ccpi.viewer.widgets.slider import SLIDER_EVENT
class Linked3DInteractorStyle(CIL3DInteractorStyle):
"""
Add attributes and methods needed to link two viewer together to the interactor style
"""
def __init__(self, callback):
CIL3DInteractorStyle.__init__(self, callback)
self.LinkedInteractor = None
self.LinkedEvent = 0
def SetLinkedInteractor(self, iren):
self.LinkedInteractor = iren
def GetLinkedInteractor(self):
return self.LinkedInteractor
def GetLinkedEvent(self):
return self.LinkedEvent
def LinkedEventOn(self):
self.LinkedEvent = 1
def LinkedEventOff(self):
self.LinkedEvent = 0
class Linked2DInteractorStyle(CIL2DInteractorStyle):
"""
Add attributes and methods needed to link two viewer together to the interactor style
"""
def __init__(self, callback):
CIL2DInteractorStyle.__init__(self, callback)
self.LinkedInteractor = None
self.LinkedEvent = 0
def SetLinkedInteractor(self, iren):
self.LinkedInteractor = iren
def GetLinkedInteractor(self):
return self.LinkedInteractor
def GetLinkedEvent(self):
return self.LinkedEvent
def LinkedEventOn(self):
self.LinkedEvent = 1
def LinkedEventOff(self):
self.LinkedEvent = 0
class ViewerLinker():
"""
This class sets up a link between two viewers. It offers methods
for enabling and disabling the link, and setting up which events
should be listened to.
It makes use of the ViewerLinkObserver to establish a 'to' and 'from'
connection.
"""
def __init__(self, viewer1, viewer2):
self._viewer1 = viewer1
self._viewer2 = viewer2
self._to = ViewerLinkObserver(viewer1, viewer2)
self._from = ViewerLinkObserver(viewer2, viewer1)
def __del__(self):
self.disable()
def enable(self):
"""
Enable the viewer link
"""
# Make sure the observers aren't added twice
self.disable()
self._observerToId = self._viewer1.getInteractor().AddObserver("AnyEvent", self._to)
self._observerFromId = self._viewer2.getInteractor().AddObserver("AnyEvent", self._from)
def disable(self):
"""
Disable the viewer link
"""
try:
self._viewer1.getInteractor().RemoveObserver(self._observerToId)
self._viewer2.getInteractor().RemoveObserver(self._observerFromId)
except:
# Already removed from observer list
pass
def setLinkZoom(self, linkZoom):
"""
Boolean flag to set zoom linkage
:param linkZoom: (boolean)
"""
self._to.linkZoom = linkZoom
self._from.linkZoom = linkZoom
def setLinkPan(self, linkPan):
"""
Boolean flag to set pan linkage
:param linkPan: (boolean)
"""
self._to.linkPan = linkPan
self._from.linkPan = linkPan
def setLinkPick(self, linkPick):
"""
Boolean flag to set pick linkage
:param linkPick: (boolean)
"""
self._to.linkPick = linkPick
self._from.linkPick = linkPick
def setLinkWindowLevel(self, linkWindowLevel):
"""
Boolean flag to set window level linkage
:param linkWindowLevel: (boolean)
"""
self._to.linkWindowLevel = linkWindowLevel
self._from.linkWindowLevel = linkWindowLevel
def setLinkSlice(self, linkSlice):
"""
Boolean flag to set slice linkage
:param linkSlice: (boolean)
"""
self._to.linkSlice = linkSlice
self._from.linkSlice = linkSlice
def setLinkOrientation(self, linkOrientation):
"""
Boolean flag to set slice orientation linkage
:param linkOrientation: (boolean)
"""
self._to.linkOrientation = linkOrientation
self._from.linkOrientation = linkOrientation
def setLinkInterpolation(self, linkInterpolation):
"""
Boolean flag to set linkage of interpolation of slice actor
:param linkInterpolation: (boolean)
"""
self._to.linkInterpolation = linkInterpolation
self._from.linkInterpolation = linkInterpolation
#################################
# ViewerLinkObserver
#################################
class ViewerLinkObserver():
"""
This class passes events from a source viewer to a target. The user
can pass it as an observer to the interactor of a viewer like this:
linker = ViewerLinker(someViewer, someOtherViewer)
someOtherViewer.getInteractor().AddObserver("AnyEvent", linker)
It would be nice to listen to the vtkPyveInteractorStyle class, instead
of the vktRenderWindowInteractor, so we can handle processed events like
"WindowLevelEvent" or "StartSliceEvent". Unfortunately this is not
possible, because in order to pass the event to the target viewer, the
target interactor needs all the information the source interactor has,
and invoking a "WindowLevelEvent" in the target vtkPyveInteractorStyle
class will have no effect.
This means we have to define the event handling again, as all we know are
what mouse buttons are clicked etc, and whenever changes are made to the
current handling, the same change should be made here.
"""
def __init__(self, sourceViewer, targetViewer):
self.sourceViewer = sourceViewer
self.targetViewer = targetViewer
self.sourceVtkViewer = self.sourceViewer
self.targetVtkViewer = self.targetViewer
self.sourceInteractor = self.sourceViewer.getInteractor()
self.targetInteractor = self.targetViewer.getInteractor()
self.sourceCamera = self.sourceVtkViewer.getRenderer().GetActiveCamera()
self.targetCamera = self.targetVtkViewer.getRenderer().GetActiveCamera()
self.linkZoom = True
self.linkPan = True
self.linkPick = True
self.linkWindowLevel = True
self.linkSlice = True
self.linkOrientation = True
self.linkInterpolation = True
def __call__(self, interactor, event):
"""
Handle the linked event.
"""
sourceInteractorStyle = interactor.GetInteractorStyle()
targetInteractorStyle = self.targetInteractor.GetInteractorStyle()
state = sourceInteractorStyle.GetState()
# Check for recursive event passing. Because two viewers can be linked
# to each other, it is necessary to make sure one event isn't passed
# back to the original viewer.
try:
if (self.targetInteractor.root):
# Recursive
return
except:
# The flag isn't set, so we can continue handling the current event.
pass
# We set a flag 'root' in the interactor, so we can check for recursive
# event passing later on.
interactor.root = True
# We use this flag to store the result when we determine whether
# we want to pass the current event.
shouldPassEvent = True
# Strange bug with middle button, only target viewer responds to
# MiddleButtonPressEvent, but not to ReleaseEvent. Let's ignore it.
if (event == "MiddleButtonPressEvent" or event == "MiddleButtonReleaseEvent"):
shouldPassEvent = False
# Zoom
if (((event == "LeftButtonPressEvent") and interactor.GetAltKey() == 0 and interactor.GetControlKey() == 0
and interactor.GetShiftKey() == 1) or state == 4 or state == 5):
# Check if zooming is linked
if (not self.linkZoom):
# Not linked
shouldPassEvent = False
else:
# Set current zoom
if (event == "LeftButtonPressEvent"):
self.targetCamera.SetParallelScale(self.sourceCamera.GetParallelScale())
# Pan
if (((event == "LeftButtonPressEvent") and interactor.GetAltKey() == 0 and interactor.GetControlKey() == 1
and interactor.GetShiftKey() == 0) or state == 2):
# Check if panning is linked
if (not self.linkPan):
# Not linked
shouldPassEvent = False
# Pick
if (((event == "LeftButtonPressEvent") and interactor.GetAltKey() == 0 and interactor.GetControlKey() == 0
and interactor.GetShiftKey() == 0) or state == 1025):
if isinstance(sourceInteractorStyle, Linked3DInteractorStyle):
shouldPassEvent = False
else:
# Check if picking is linked
if (not self.linkPick):
# Not linked
shouldPassEvent = False
else:
# Set current slice to the picked voxel
# get pick position
pick_position = sourceInteractorStyle.last_picked_voxel
targetInteractorStyle.last_picked_voxel = pick_position[:]
sliceno = pick_position[self.targetViewer.sliceOrientation]
targetInteractorStyle.SetActiveSlice(sliceno)
targetInteractorStyle.UpdatePipeline(True)
# the event has not been generated in the targetInteractor so it
# should not passed on to any linked interactors
shouldPassEvent = False
# WindowLevel
if (((event == "RightButtonPressEvent") and interactor.GetAltKey() == 1 and interactor.GetControlKey() == 0
and interactor.GetShiftKey() == 0) or state == 1024):
# Check if windowing/leveling is linked
if (not self.linkWindowLevel):
# Not linked
shouldPassEvent = False
else:
# Set current window/level
window = self.sourceViewer.getSliceColorWindow()
level = self.sourceViewer.getSliceColorLevel()
self.targetVtkViewer.setSliceColorWindowLevel(window, level)
# Update window level on mouse move
if (event == "MouseMoveEvent" and self.sourceViewer.event.isActive("WINDOW_LEVEL_EVENT")):
if not self.linkWindowLevel:
shouldPassEvent = False
else:
window = self.sourceViewer.getSliceColorWindow()
level = self.sourceViewer.getSliceColorLevel()
self.targetVtkViewer.setSliceColorWindowLevel(window, level)
# Slice
if (event == "MouseWheelForwardEvent" or event == "MouseWheelBackwardEvent" or state == 1026):
# Check if slicing is linked
if (not self.linkSlice):
shouldPassEvent = False
else:
# Linked, check if orientation is the same
if (self.sourceVtkViewer.getSliceOrientation() != self.targetVtkViewer.getSliceOrientation()):
shouldPassEvent = False
# Slice from the slider
if event == "NoEvent" and self.linkSlice:
# Although I invoked a SLIDER_EVENT in widgets/slider.py, I noticed that a
# NoEvent is actually emitted/received. Therefore, we will link the slice in case of a
# NoEvent and self.linkSlice=True
if (not self.linkSlice):
shouldPassEvent = False
else:
# Set current slice
sliceno = self.sourceViewer.getActiveSlice()
self.targetInteractor.GetInteractorStyle().SetActiveSlice(sliceno)
self.targetInteractor.GetInteractorStyle().UpdatePipeline(True)
# KeyPress and orientation
if (event == "KeyPressEvent" or state == 1026):
orientation_link_event = self.linkOrientation and (interactor.GetKeyCode() == "x"
or interactor.GetKeyCode() == "y"
or interactor.GetKeyCode() == "z")
window_level_link_event = self.linkWindowLevel and (interactor.GetKeyCode() == "a"
or interactor.GetKeyCode() == "w")
interpolation_link_event = self.linkInterpolation and interactor.GetKeyCode() == "i"
if not (orientation_link_event or interpolation_link_event):
shouldPassEvent = False
if window_level_link_event:
# Set current window/level
window = self.sourceViewer.getSliceColorWindow()
level = self.sourceViewer.getSliceColorLevel()
self.targetVtkViewer.setSliceColorWindowLevel(window, level)
# Check if event should be passed
if (shouldPassEvent):
# Pass event
self.setupEvent(interactor, event)
# Check if source interactor is already linked itself
if (sourceInteractorStyle.GetLinkedEvent()):
# Already linked, pass source interactor
targetInteractorStyle.SetLinkedInteractor(sourceInteractorStyle.GetLinkedInteractor())
else:
# Not linked, this is the source interactor
targetInteractorStyle.SetLinkedInteractor(interactor)
# Invoke event in target interactor.
self.targetInteractor.InvokeEvent(event)
# Set event back to normal
targetInteractorStyle.LinkedEventOff()
targetInteractorStyle.SetLinkedInteractor(None)
interactor.root = False
def setupEvent(self, interactor, event):
"""
Set all necessary event information in target interactor.
"""
# Set all required event information.
self.targetInteractor.SetEventPosition(interactor.GetEventPosition())
self.targetInteractor.SetAltKey(interactor.GetAltKey())
self.targetInteractor.SetControlKey(interactor.GetControlKey())
self.targetInteractor.SetShiftKey(interactor.GetShiftKey())
self.targetInteractor.SetKeyCode(interactor.GetKeyCode())
self.targetInteractor.SetRepeatCount(interactor.GetRepeatCount())
self.targetInteractor.SetKeySym(interactor.GetKeySym())
# print (type(self.targetInteractor.GetInteractorStyle()))
# Mark the event as linked
self.targetInteractor.GetInteractorStyle().LinkedEventOn()