/[escript]/trunk/pyvisi/examples/basicContourExample.py
ViewVC logotype

Contents of /trunk/pyvisi/examples/basicContourExample.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 337 - (show annotations)
Mon Dec 12 01:57:07 2005 UTC (13 years, 9 months ago) by cochrane
File MIME type: text/x-python
File size: 2883 byte(s)
Initial merge of pyvisi into esys repository.
1 # $Id: basicContourExample.py,v 1.7 2005/04/29 00:32:20 paultcochrane Exp $
2
3 """
4 Example of a basic contour plot
5
6 Will hopefully help me write a decent interface.
7 """
8
9 import sys,os
10
11 # import the python visualisation interface
12 from pyvisi import *
13
14 # original vtk code
15 import vtk
16
17 import numarray
18
19 # generate the x and y grid data
20 x = numarray.arrayrange(-1, 1, stride=0.5, type='Float')
21 y = numarray.arrayrange(-1, 1, stride=0.5, type='Float')
22
23 # generate a matrix of repeated x values (c.f. repmat() in matlab)
24 xm = numarray.zeros([len(x), len(y)], type='Float')
25 for i in range(len(y)):
26 xm[:,i] = x
27
28 # generate a matrix of repeated y values (c.f. repmat() in matlab)
29 ym = numarray.zeros([len(x), len(y)], type='Float')
30 for i in range(len(x)):
31 ym[i,:] = y
32
33 sigma = 0.2 # the spread of the distribution
34
35 # generate the distribution
36 distn = numarray.exp(-(xm*xm + ym*ym)/sigma)
37
38 # convert the x data into vtkFloatArray objects
39 xvtk = vtk.vtkFloatArray()
40 for i in range(len(x)):
41 xvtk.InsertNextValue(x[i])
42
43 # convert the y data into vtkFloatArray objects
44 yvtk = vtk.vtkFloatArray()
45 for i in range(len(y)):
46 yvtk.InsertNextValue(y[i])
47
48 # convert the distribution data into vtkFloatArray objects
49 distnvtk = vtk.vtkFloatArray()
50 for i in range(len(x)):
51 for j in range(len(y)):
52 distnvtk.InsertNextValue(distn[i,j])
53
54 # make the points to put into the grid
55 points = vtk.vtkPoints()
56 values = vtk.vtkFloatArray()
57 count = 0
58 for i in xrange(len(x)):
59 for j in xrange(len(y)):
60 points.InsertPoint(count, x[i], y[j], 0)
61 values.InsertValue(count, distn[i,j])
62 count += 1
63
64 # now make the strips (whatever they are...)
65 #strips = vtk.vtkCellArray()
66 #strips.InsertNextCell(len(x)*len(y)) # number of points
67 #for i in xrange(len(x)*len(y)):
68 #strips.InsertCellPoint(i)
69
70 #strips.InsertCellPoint(0)
71 #strips.InsertCellPoint(1)
72 #strips.InsertCellPoint(7)
73 #strips.InsertCellPoint(6)
74 #strips.InsertCellPoint(2)
75 #strips.InsertCellPoint(3)
76 #strips.InsertCellPoint(5)
77 #strips.InsertCellPoint(4)
78
79 strips = vtk.vtkCellArray()
80 p2c = vtk.vtkPointDataToCellData()
81 p2c.SetInput(points)
82
83 # set up the polygonal data object
84 polyData = vtk.vtkPolyData()
85 polyData.SetPoints(points)
86 polyData.SetStrips(strips)
87 polyData.GetPointData().SetScalars(values)
88
89 warp = vtk.vtkWarpScalar()
90 warp.SetInput(polyData)
91 warp.SetScaleFactor(0.5)
92
93 contMapper = vtk.vtkPolyDataMapper()
94 contMapper.SetInput(warp.GetPolyDataOutput())
95 contMapper.SetScalarRange(polyData.GetScalarRange())
96
97 contActor = vtk.vtkActor()
98 contActor.SetMapper(contMapper)
99
100 ren = vtk.vtkRenderer()
101 renWin = vtk.vtkRenderWindow()
102 renWin.AddRenderer(ren)
103 iren = vtk.vtkRenderWindowInteractor()
104 iren.SetRenderWindow(renWin)
105
106 ren.AddActor(contActor)
107 renWin.SetSize(400,400)
108 ren.SetBackground(1,1,1)
109 iren.Initialize()
110 renWin.Render()
111 iren.Start()
112 #raw_input("Press enter to continue")
113
114
115 # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26