]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/vtk.py
Fixed remaining int vs. long warnings.
[wxWidgets.git] / utils / wxPython / lib / vtk.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.vtk
3 # Purpose: Provides a wrapper around the vtkRenderWindow from the
4 # VTK Visualization Toolkit. Requires the VTK Python
5 # extensions from http://www.kitware.com/
6 #
7 # Author: Robin Dunn
8 #
9 # Created: 16-Nov-1999
10 # RCS-ID: $Id$
11 # Copyright: (c) 1999 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
14
15
16 try: # For Win32,
17 from vtkpython import *
18 except ImportError:
19 try: # for Unix. Why they do it this way is anybody's guess...
20 from libVTKCommonPython import *
21 from libVTKGraphicsPython import *
22 from libVTKImagingPython import *
23 except ImportError:
24 raise ImportError, "VTK extension module not found"
25
26 from wxPython.wx import *
27 import math
28
29 #----------------------------------------------------------------------
30
31 class wxVTKRenderWindow(wxScrolledWindow):
32 def __init__(self, parent, id, position=wxDefaultPosition,
33 size=wxDefaultSize, style=0):
34 wxScrolledWindow.__init__(self, parent, id, position, size, style)
35
36 self.renderWindow = vtkRenderWindow()
37
38 if wxPlatform != '__WXMSW__':
39 # We can't get the handle in wxGTK until after the widget
40 # is created, the window create event happens later so we'll
41 # catch it there
42 EVT_WINDOW_CREATE(self, self.OnCreateWindow)
43 else:
44 # but in MSW, the window create event happens durring the above
45 # call to __init__ so we have to do it here.
46 hdl = self.GetHandle()
47 self.renderWindow.SetWindowInfo(str(hdl))
48
49
50 EVT_LEFT_DOWN (self, self.SaveClick)
51 EVT_MIDDLE_DOWN(self, self.SaveClick)
52 EVT_RIGHT_DOWN (self, self.SaveClick)
53 EVT_LEFT_UP (self, self.Release)
54 EVT_MIDDLE_UP (self, self.Release)
55 EVT_RIGHT_UP (self, self.Release)
56 EVT_MOTION (self, self.MouseMotion)
57
58
59 def GetRenderer(self):
60 self.renderWindow.GetRenderers().InitTraversal()
61 return self.renderWindow.GetRenderers().GetNextItem()
62
63
64 def GetRenderWindow(self):
65 return self.renderWindow
66
67
68 def OnPaint(self, event):
69 dc = wxPaintDC(self)
70 self.renderWindow.Render()
71
72 def OnCreateWindow(self, event):
73 hdl = self.GetHandle()
74 self.renderWindow.SetWindowInfo(str(hdl))
75
76 def OnEraseBackground(self, event):
77 pass
78
79
80
81 def SaveClick(self, event):
82 self.prev_x, self.prev_y = event.GetPositionTuple()
83 self.CaptureMouse()
84
85 def Release(self, event):
86 self.ReleaseMouse()
87
88 def MouseMotion(self, event):
89 event.x, event.y = event.GetPositionTuple()
90 size = self.GetClientSize()
91 if event.LeftIsDown():
92 # rotate
93 camera = self.GetRenderer().GetActiveCamera()
94 camera.Azimuth(float(self.prev_x - event.x) / size.width * 360)
95 camera.Elevation(float(event.y - self.prev_y) / size.width * 360)
96 camera.OrthogonalizeViewUp()
97 self.MotionUpdate(event)
98
99 elif event.MiddleIsDown():
100 # pan
101 camera = self.GetRenderer().GetActiveCamera()
102 camera.Yaw(-float(self.prev_x - event.x) / size.width * 30)
103 camera.Pitch(float(event.y - self.prev_y) / size.width * 30)
104 camera.OrthogonalizeViewUp()
105 self.MotionUpdate(event)
106
107 elif event.RightIsDown():
108 # dolly
109 camera = self.GetRenderer().GetActiveCamera()
110 camera.Dolly(math.exp(float((event.x - self.prev_x) - \
111 (event.y - self.prev_y))/ \
112 size.width))
113 self.MotionUpdate(event)
114
115
116 def MotionUpdate(self,event):
117 renderer = self.GetRenderer()
118 renderer.GetLights().InitTraversal()
119 light = renderer.GetLights().GetNextItem()
120 camera = renderer.GetActiveCamera()
121 light.SetPosition(camera.GetPosition())
122 light.SetFocalPoint(camera.GetFocalPoint())
123 self.renderWindow.Render()
124 self.prev_x = event.x
125 self.prev_y = event.y
126
127
128