]>
Commit | Line | Data |
---|---|---|
7b9da207 RD |
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 | ||
113 | if __name__ == '__main__': | |
114 | class TestFrame(wxFrame): | |
115 | def __init__(self, parent): | |
116 | wxFrame.__init__(self, parent, -1, "VTK Test", size=(450,450)) | |
117 | ||
118 | rw = wxVTKRenderWindow(self, -1) | |
119 | ||
120 | # Get the render window | |
121 | renWin = rw.GetRenderWindow() | |
122 | ||
123 | # Next, do the VTK stuff | |
124 | ren = vtkRenderer() | |
125 | renWin.AddRenderer(ren) | |
126 | cone = vtkConeSource() | |
127 | cone.SetResolution(80) | |
128 | coneMapper = vtkPolyDataMapper() | |
129 | coneMapper.SetInput(cone.GetOutput()) | |
130 | coneActor = vtkActor() | |
131 | coneActor.SetMapper(coneMapper) | |
132 | ren.AddActor(coneActor) | |
133 | coneMapper.GetLookupTable().Build() | |
134 | ||
135 | # Create a scalar bar | |
136 | scalarBar = vtkScalarBarActor() | |
137 | scalarBar.SetLookupTable(coneMapper.GetLookupTable()) | |
138 | scalarBar.SetTitle("Temperature") | |
139 | scalarBar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport() | |
140 | scalarBar.GetPositionCoordinate().SetValue(0.1, 0.01) | |
141 | scalarBar.SetOrientationToHorizontal() | |
142 | scalarBar.SetWidth(0.8) | |
143 | scalarBar.SetHeight(0.17) | |
144 | ren.AddActor2D(scalarBar) | |
145 | ||
146 | ||
147 | ||
148 | class TestApp(wxApp): | |
149 | def OnInit(self): | |
150 | f = TestFrame(None) | |
151 | self.SetTopWindow(f) | |
152 | f.Show(true) | |
153 | return true | |
154 | ||
155 | app = TestApp(0) | |
156 | app.MainLoop() | |
157 |