styles.py
6.54 KB
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
#--------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
import vtk
import constants as const
class ViewerStyle:
def __init__(self):
self.interactor = None
self.style = None
self.render = None
def SetInteractor(self, interactor):
# Setting style already defined in VTK
if (self.style is None):
self.style = vtk.vtkInteractorStyle()
#self.style.SetInteractor(interactor)
self.SetStyleConfig()
interactor.SetInteractorStyle(style)
# Saving data into attributes
self.render = interactor.GetRenderWindow().GetRenderer()
self.interactor = interactor
# Call events
self.__init_evt()
def SetStyleConfig(self):
print "calling parent"
pass
def __init_evt(self):
style = self.style
style.AddObserver("LeftButtonPressEvent", self.OnLeftButtonDown)
style.AddObserver("LeftButtonReleaseEvent", self.OnLeftButtonUp)
style.AddObserver("MiddleButtonPressEvent", self.OnMiddleButtonDown)
style.AddObserver("MiddleButtonReleaseEvent", self.OnMiddleButtonUp)
style.AddObserver("RightButtonPressEvent", self.OnRightButtonDown)
style.AddObserver("RightButtonReleaseEvent", self.OnRightButtonUp)
style.AddObserver("MouseWheelForwardEvent", self.OnScroll)
style.AddObserver("MouseWheelBackwardEvent", self.OnScroll)
style.AddObserver("MouseMoveEvent",self.OnMouseMove)
def OnScroll(self, evt, str_evt):
pass
def OnLeftButtonDown(self, evt, str_evt):
pass
def OnLeftButtonUp(self, evt, str_evt):
pass
def OnMiddleButtonDown(self, evt, str_evt):
pass
def OnMiddleButtonUp(self, evt, str_evt):
pass
def OnRightButtonDown(self, evt, str_evt):
pass
def OnRightButtonUp(self, evt, str_evt):
pass
def OnMouseMove(self, evt, str_evt):
pass
class ViewerStyleSlice(ViewerStyle):
def __init__(self):
ViewerStyle.__init__(self)
self.orientation = 'AXIAL'
self.style = vtk.vtkInteractorStyleImage()
self.style.AutoAdjustCameraClippingRangeOn()
def SetOrientation(self, orientation='AXIAL'):
self.orientation = orientation
def OnScroll(self, evt, evt_string):
if evt_string == "MouseWheelForwardEvent":
value = 1
else: # elif evt_string =="MouseWheelBackwardEvent":
value = -1
ps.Publisher().sendMessage(('Set scroll position', self.orientation), value)
class ViewerStyleSliceEditor(ViewerStyleSlice):
def __init__(self):
# FIXME: the idea is not using Slice from here...!
#self.slice_ = slc.Slice()
self.picker = vtk.vtkCellPicker() # define position where user clicked
self.mouse_pressed = 0 # define if operation will executed run or not
self.style = const.OP_ADD # define brush operation .OP_DEL, .OP_THRESH
def SetData(self, actor_bounds, imagedata_dimensions):
self.pos_converter = ViewerToImagedataPosConverter(actor_bounds,
imagedata_dimensions,
self.renderer)
def SetStyleConfig(self):
print "calling son"
def SetOrientation(self, orient):
pass
def OnLeftButtonDown(self, evt, str_evt):
self.mouse_pressed = 1
def OnLeftButtonUp(self, evt, str_evt):
self.mouse_pressed = 0
def OnMouseMove(self, evt, str_evt):
pos = self.interactor.GetEventPosition()
wx = pos[0]
wy = pos[1]
self.pick.Pick(wx, wy, 0, self.render)
x, y, z = self.picker.GetPickPosition()
if self.mouse_pressed:
#wx, wy = self.From3dToImagePixel(pos, (x, y, z))
wx, wy = self.pos_converter.GetImagedataCoordinates(x, y, z)
#self.CircleOperation(wx, wy, self.slice_) # TODO!
ps.Publisher().sendMessage('Update edited imagedata', self.image)
ps.Publisher().sendMessage('Update slice viewer', None)
# update cursor
self.cursor.SetPosition(x, y, z)
self.cursor.Update()
obj.OnMouseMove()
self.interactor.Render()
class ViewerToImagedataPosConverter():
def __init__(self, actor_bounds, imagedata_dimensions, renderer):
self.actor_bounds = actor_bounds
self.imagedata_dimensions = imagedata_dimensions
self.renderer = renderer
def GetImagedataCoordinates(self, picker_position):
x, y, z = picker_position
c = vtk.vtkCoordinate()
c.SetCoordinateSystemToWorld()
c.SetValue(bounds[::2])
xi, yi = c.GetComputedViewportValue(self.render)
c.SetValue(bounds[1::2])
xf, yf = c.GetComputedViewportValue(self.render)
c.SetValue(x, y, z)
wx, wy = c.GetComputedViewportValue(self.render)
wx = wx - xi
wy = wy - yi
xf = xf - xi
yf = yf - yi
wx = (wx * self.imagedata_dimensions[0]) / xf
wy = (wy * self.imagedata_dimensions[1]) / yf
return wx, wy
################################################################################
# style = vtk.vtkInteractorStyleImage()
# style.SetInteractor(interactor)
# interactor.SetInteractorStyle(style)
# self.style = style
#self.interactor.SetCursor(cursors.ZOOM_IN_CURSOR)
################################################################################