]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/vtk.py
fixed bug that caused wxHtmlWindow to segfault on html tables with cell(s) with rowsp...
[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(wxWindow):
32 def __init__(self, parent, id, position=wxDefaultPosition,
33 size=wxDefaultSize, style=0):
34 wxWindow.__init__(self, parent, id, position, size, style)
35
36 self.renderWindow = vtkRenderWindow()
37
38 hdl = self.GetHandle()
39 self.renderWindow.SetWindowInfo(str(hdl))
40
41 EVT_LEFT_DOWN (self, self.SaveClick)
42 EVT_MIDDLE_DOWN(self, self.SaveClick)
43 EVT_RIGHT_DOWN (self, self.SaveClick)
44 EVT_LEFT_UP (self, self.Release)
45 EVT_MIDDLE_UP (self, self.Release)
46 EVT_RIGHT_UP (self, self.Release)
47 EVT_MOTION(self, self.MouseMotion)
48
49
50 def GetRenderer(self):
51 self.renderWindow.GetRenderers().InitTraversal()
52 return self.renderWindow.GetRenderers().GetNextItem()
53
54
55 def GetRenderWindow(self):
56 return self.renderWindow
57
58
59 def OnPaint(self, event):
60 dc = wxPaintDC(self)
61 self.renderWindow.Render()
62
63
64 def SaveClick(self, event):
65 self.prev_x, self.prev_y = event.GetPositionTuple()
66 self.CaptureMouse()
67
68 def Release(self, event):
69 self.ReleaseMouse()
70
71 def MouseMotion(self, event):
72 event.x, event.y = event.GetPositionTuple()
73 size = self.GetClientSize()
74 if event.LeftIsDown():
75 # rotate
76 camera = self.GetRenderer().GetActiveCamera()
77 camera.Azimuth(float(self.prev_x - event.x) / size.width * 360)
78 camera.Elevation(float(event.y - self.prev_y) / size.width * 360)
79 camera.OrthogonalizeViewUp()
80 self.MotionUpdate(event)
81
82 elif event.MiddleIsDown():
83 # pan
84 camera = self.GetRenderer().GetActiveCamera()
85 camera.Yaw(-float(self.prev_x - event.x) / size.width * 30)
86 camera.Pitch(float(event.y - self.prev_y) / size.width * 30)
87 camera.OrthogonalizeViewUp()
88 self.MotionUpdate(event)
89
90 elif event.RightIsDown():
91 # dolly
92 camera = self.GetRenderer().GetActiveCamera()
93 camera.Dolly(math.exp(float((event.x - self.prev_x) - \
94 (event.y - self.prev_y))/ \
95 size.width))
96 self.MotionUpdate(event)
97
98
99 def MotionUpdate(self,event):
100 renderer = self.GetRenderer()
101 renderer.GetLights().InitTraversal()
102 light = renderer.GetLights().GetNextItem()
103 camera = renderer.GetActiveCamera()
104 light.SetPosition(camera.GetPosition())
105 light.SetFocalPoint(camera.GetFocalPoint())
106 self.renderWindow.Render()
107 self.prev_x = event.x
108 self.prev_y = event.y
109
110
111
112 ## testcode is now in the demo ##
113
114 ## if __name__ == '__main__':
115 ## class TestFrame(wxFrame):
116 ## def __init__(self, parent):
117 ## wxFrame.__init__(self, parent, -1, "VTK Test", size=(450,450))
118
119 ## rw = wxVTKRenderWindow(self, -1)
120
121 ## # Get the render window
122 ## renWin = rw.GetRenderWindow()
123
124 ## # Next, do the VTK stuff
125 ## ren = vtkRenderer()
126 ## renWin.AddRenderer(ren)
127 ## cone = vtkConeSource()
128 ## cone.SetResolution(80)
129 ## coneMapper = vtkPolyDataMapper()
130 ## coneMapper.SetInput(cone.GetOutput())
131 ## coneActor = vtkActor()
132 ## coneActor.SetMapper(coneMapper)
133 ## ren.AddActor(coneActor)
134 ## coneMapper.GetLookupTable().Build()
135
136 ## # Create a scalar bar
137 ## scalarBar = vtkScalarBarActor()
138 ## scalarBar.SetLookupTable(coneMapper.GetLookupTable())
139 ## scalarBar.SetTitle("Temperature")
140 ## scalarBar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
141 ## scalarBar.GetPositionCoordinate().SetValue(0.1, 0.01)
142 ## scalarBar.SetOrientationToHorizontal()
143 ## scalarBar.SetWidth(0.8)
144 ## scalarBar.SetHeight(0.17)
145 ## ren.AddActor2D(scalarBar)
146
147
148
149 ## class TestApp(wxApp):
150 ## def OnInit(self):
151 ## f = TestFrame(None)
152 ## self.SetTopWindow(f)
153 ## f.Show(true)
154 ## return true
155
156 ## app = TestApp(0)
157 ## app.MainLoop()
158