-
Notifications
You must be signed in to change notification settings - Fork 3
/
gtkvtk.py
443 lines (363 loc) · 15.6 KB
/
gtkvtk.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# gtkvtk.py -- A Gtk Widget for Rendering VTK Scenes
# -*- coding: utf-8 -*-
#
# Written by Jonathan Kleinehellefort <[email protected]>, based on
# GtkVTKRenderWindow.py from VTK <http://www.vtk.org/> by Prabhu
# Ramachandran, in turn based on vtkTkRenderWidget.py by David Gobbi.
#
# Copyright 2008 Lehrstuhl Bildverstehen und wissensbasierte Systeme,
# Technische Universität München
# Copyright 1993-2008 Ken Martin
# Copyright 1993-2008 Will Schroeder
# Copyright 1993-2008 Bill Lorensen
# Copyright 2001-2002 Prabhu Ramachandran
# Copyright 1999-2000 David Gobbi
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor
# the names of any contributors may be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import gtk
import gtk.gtkgl
import vtk
import math
class VtkRenderArea(gtk.gtkgl.DrawingArea):
""" A base class that enables one to embed a vtkRenderWindow into
a pyGTK widget. This class embeds the RenderWindow correctly.
Provided are some empty methods that can be overloaded to provide
a user defined interaction behaviour. The event handling
functions have names that are somewhat similar to the ones in the
vtkInteractorStyle class included with VTK. """
def __init__(self, *args, **kwargs):
#display_mode = (gtk.gdkgl.MODE_RGBA | gtk.gdkgl.MODE_DOUBLE)
#glconfig = gtk.gdkgl.Config(mode=display_mode)
#gtk.gtkgl.DrawingArea.__init__(self, glconfig, *args, **kwargs)
gtk.gtkgl.DrawingArea.__init__(self, *args, **kwargs)
self.render_window = vtk.vtkRenderWindow()
# private attributes
self.__created = False
def on_realize(*unused):
if not self.__created:
# you can't get the xid without the window being realized.
self.realize()
self.render_window.SetWindowInfo('%d' % self.window.xid)
self.__created = True
return True
self.connect('realize', on_realize)
def on_configure(unused, event):
width, height = self.render_window.GetSize()
if event.width != width or event.height != height:
self.render_window.SetSize(event.width, event.height)
return True
self.connect('configure_event', on_configure)
def on_expose(*unused):
self.render()
return True
self.connect('expose_event', on_expose)
def on_destroy(*unused):
self.hide()
del self.render_window
self.destroy()
return True
self.connect('delete_event', on_destroy)
self.add_events(gtk.gdk.EXPOSURE_MASK)
def render(self):
if self.__created:
self.render_window.Render()
class VtkSceneViewer(VtkRenderArea):
""" An example of a fully functional GtkVTKRenderWindow that is
based on the vtkRenderWidget.py provided with the VTK sources."""
def __init__(self, *args, **kwargs):
VtkRenderArea.__init__(self, *args, **kwargs)
self._CurrentRenderer = None
self._CurrentCamera = None
self._CurrentZoom = 1.0
self._CurrentLight = None
self._ViewportCenterX = 0
self._ViewportCenterY = 0
self._Picker = vtk.vtkCellPicker()
self._PickedAssembly = None
self._PickedProperty = vtk.vtkProperty()
self._PickedProperty.SetColor(1, 0, 0)
self._PrePickedProperty = None
self._OldFocus = None
# need this to be able to handle key_press events.
self.set_flags(gtk.CAN_FOCUS)
# these record the previous mouse position
self._LastX = 0
self._LastY = 0
self.connect('button_press_event', self.OnButtonDown)
self.connect('button_release_event', self.OnButtonUp)
self.connect('motion_notify_event', self.OnMouseMove)
self.connect('key_press_event', self.OnKeyPress)
self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.KEY_PRESS_MASK |
gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.POINTER_MOTION_HINT_MASK)
def OnButtonDown(self, wid, event):
self.grab_focus()
return self.StartMotion(wid, event)
def OnButtonUp(self, wid, event):
return self.EndMotion(wid, event)
def OnMouseMove(self, wid, event=None):
if (event.state & gtk.gdk.BUTTON1_MASK) == gtk.gdk.BUTTON1_MASK:
if (event.state & gtk.gdk.SHIFT_MASK) == gtk.gdk.SHIFT_MASK:
m = self.get_pointer()
self.Pan(m[0], m[1])
return True
else:
m = self.get_pointer()
self.Rotate(m[0], m[1])
return True
elif (event.state & gtk.gdk.BUTTON2_MASK) == gtk.gdk.BUTTON2_MASK:
m = self.get_pointer()
self.Pan(m[0], m[1])
return True
elif (event.state & gtk.gdk.BUTTON3_MASK) == gtk.gdk.BUTTON3_MASK:
m = self.get_pointer()
self.Zoom(m[0], m[1])
return True
else:
return False
def OnKeyPress(self, wid, event=None):
if event.keyval == ord('r'):
self.Reset()
return True
elif event.keyval == ord('w'):
self.Wireframe()
return True
elif event.keyval == ord('s'):
self.Surface()
return True
elif event.keyval == ord('p'):
m = self.get_pointer()
self.pick_actor(m[0], m[1])
return True
else:
return False
def render(self):
if (self._CurrentLight):
light = self._CurrentLight
light.SetPosition(self._CurrentCamera.GetPosition())
light.SetFocalPoint(self._CurrentCamera.GetFocalPoint())
VtkRenderArea.render(self)
def UpdateRenderer(self, x, y):
"""
UpdateRenderer will identify the renderer under the mouse and set
up _CurrentRenderer, _CurrentCamera, and _CurrentLight.
"""
windowX, windowY = self.window.get_geometry()[2:4]
renderers = self.render_window.GetRenderers()
numRenderers = renderers.GetNumberOfItems()
self._CurrentRenderer = None
renderers.InitTraversal()
for i in range(0,numRenderers):
renderer = renderers.GetNextItem()
vx,vy = (0,0)
if (windowX > 1):
vx = float(x)/(windowX-1)
if (windowY > 1):
vy = (windowY-float(y)-1)/(windowY-1)
(vpxmin,vpymin,vpxmax,vpymax) = renderer.GetViewport()
if (vx >= vpxmin and vx <= vpxmax and
vy >= vpymin and vy <= vpymax):
self._CurrentRenderer = renderer
self._ViewportCenterX = float(windowX)*(vpxmax-vpxmin)/2.0\
+vpxmin
self._ViewportCenterY = float(windowY)*(vpymax-vpymin)/2.0\
+vpymin
self._CurrentCamera = self._CurrentRenderer.GetActiveCamera()
lights = self._CurrentRenderer.GetLights()
lights.InitTraversal()
self._CurrentLight = lights.GetNextItem()
break
self._LastX = x
self._LastY = y
def GetCurrentRenderer(self):
return self._CurrentRenderer
def StartMotion(self, wid, event=None):
x = event.x
y = event.y
self.UpdateRenderer(x, y)
return True
def EndMotion(self, wid, event=None):
if self._CurrentRenderer:
self.render()
return True
def Rotate(self,x,y):
if self._CurrentRenderer:
self._CurrentCamera.Azimuth(self._LastX - x)
self._CurrentCamera.Elevation(y - self._LastY)
self._CurrentCamera.OrthogonalizeViewUp()
self._LastX = x
self._LastY = y
self._CurrentRenderer.ResetCameraClippingRange()
self.render()
def Pan(self,x,y):
if self._CurrentRenderer:
renderer = self._CurrentRenderer
camera = self._CurrentCamera
(pPoint0,pPoint1,pPoint2) = camera.GetPosition()
(fPoint0,fPoint1,fPoint2) = camera.GetFocalPoint()
if (camera.GetParallelProjection()):
renderer.SetWorldPoint(fPoint0,fPoint1,fPoint2,1.0)
renderer.WorldToDisplay()
fx,fy,fz = renderer.GetDisplayPoint()
renderer.SetDisplayPoint(fx-x+self._LastX,
fy+y-self._LastY,
fz)
renderer.DisplayToWorld()
fx,fy,fz,fw = renderer.GetWorldPoint()
camera.SetFocalPoint(fx,fy,fz)
renderer.SetWorldPoint(pPoint0,pPoint1,pPoint2,1.0)
renderer.WorldToDisplay()
fx,fy,fz = renderer.GetDisplayPoint()
renderer.SetDisplayPoint(fx-x+self._LastX,
fy+y-self._LastY,
fz)
renderer.DisplayToWorld()
fx,fy,fz,fw = renderer.GetWorldPoint()
camera.SetPosition(fx,fy,fz)
else:
(fPoint0,fPoint1,fPoint2) = camera.GetFocalPoint()
# Specify a point location in world coordinates
renderer.SetWorldPoint(fPoint0,fPoint1,fPoint2,1.0)
renderer.WorldToDisplay()
# Convert world point coordinates to display coordinates
dPoint = renderer.GetDisplayPoint()
focalDepth = dPoint[2]
aPoint0 = self._ViewportCenterX + (x - self._LastX)
aPoint1 = self._ViewportCenterY - (y - self._LastY)
renderer.SetDisplayPoint(aPoint0,aPoint1,focalDepth)
renderer.DisplayToWorld()
(rPoint0,rPoint1,rPoint2,rPoint3) = renderer.GetWorldPoint()
if (rPoint3 != 0.0):
rPoint0 = rPoint0/rPoint3
rPoint1 = rPoint1/rPoint3
rPoint2 = rPoint2/rPoint3
camera.SetFocalPoint((fPoint0 - rPoint0) + fPoint0,
(fPoint1 - rPoint1) + fPoint1,
(fPoint2 - rPoint2) + fPoint2)
camera.SetPosition((fPoint0 - rPoint0) + pPoint0,
(fPoint1 - rPoint1) + pPoint1,
(fPoint2 - rPoint2) + pPoint2)
self._LastX = x
self._LastY = y
self.render()
def Zoom(self,x,y):
if self._CurrentRenderer:
renderer = self._CurrentRenderer
camera = self._CurrentCamera
zoomFactor = math.pow(1.02,(0.5*(self._LastY - y)))
self._CurrentZoom = self._CurrentZoom * zoomFactor
if camera.GetParallelProjection():
parallelScale = camera.GetParallelScale()/zoomFactor
camera.SetParallelScale(parallelScale)
else:
camera.Dolly(zoomFactor)
renderer.ResetCameraClippingRange()
self._LastX = x
self._LastY = y
self.render()
def Reset(self):
if self._CurrentRenderer:
self._CurrentRenderer.ResetCamera()
self.render()
def Wireframe(self):
actors = self._CurrentRenderer.GetActors()
numActors = actors.GetNumberOfItems()
actors.InitTraversal()
for i in range(0,numActors):
actor = actors.GetNextItem()
actor.GetProperty().SetRepresentationToWireframe()
self.render()
def Surface(self):
actors = self._CurrentRenderer.GetActors()
numActors = actors.GetNumberOfItems()
actors.InitTraversal()
for i in range(0,numActors):
actor = actors.GetNextItem()
actor.GetProperty().SetRepresentationToSurface()
self.render()
def pick_actor(self, x, y):
if self._CurrentRenderer:
renderer = self._CurrentRenderer
picker = self._Picker
windowY = self.window.get_geometry()[3]
picker.Pick(x,(windowY - y - 1),0.0,renderer)
assembly = picker.GetAssembly()
if (self._PickedAssembly != None and
self._PrePickedProperty != None):
self._PickedAssembly.SetProperty(self._PrePickedProperty)
# release hold of the property
self._PrePickedProperty.UnRegister(self._PrePickedProperty)
self._PrePickedProperty = None
if (assembly != None):
self._PickedAssembly = assembly
self._PrePickedProperty = self._PickedAssembly.GetProperty()
# hold onto the property
self._PrePickedProperty.Register(self._PrePickedProperty)
self._PickedAssembly.SetProperty(self._PickedProperty)
self.render()
if __name__ == '__main__':
# The main window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title('Demo of GTK/VTK integration')
window.connect('destroy', gtk.main_quit)
#window.connect('delete_event', gtk.main_quit)
# A VBox into which widgets are packed.
vbox = gtk.VBox()
window.add(vbox)
vbox.show()
# The VtkRenderWindow
gvtk = VtkSceneViewer()
#gvtk.SetDesiredUpdateRate(1000)
gvtk.set_size_request(400, 400)
vbox.pack_start(gvtk)
gvtk.show()
# The VTK stuff.
cone = vtk.vtkConeSource()
cone.SetResolution(80)
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInput(cone.GetOutput())
#coneActor = vtk.vtkLODActor()
coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(0.5, 0.5, 1.0)
ren = vtk.vtkRenderer()
gvtk.render_window.AddRenderer(ren)
ren.AddActor(coneActor)
# A simple quit button
quit = gtk.Button("Quit!")
quit.connect("clicked", gtk.main_quit)
vbox.pack_start(quit, expand=False)
quit.show()
# show the main window and start event processing.
window.show()
gtk.main()