/[escript]/trunk/pyvisi/test/python/povray/ballPlot.py
ViewVC logotype

Contents of /trunk/pyvisi/test/python/povray/ballPlot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations)
Mon Mar 20 02:23:14 2006 UTC (13 years, 10 months ago) by gross
File MIME type: text/x-python
File size: 5690 byte(s)
example cleanup
1 # $Id: ballPlot.py,v 1.1 2005/11/08 05:45:03 paultcochrane Exp $
2
3 """
4 Example of plotting spheres
5 """
6
7 import os, sys
8 import random
9
10 # set up some data to plot
11 from Numeric import *
12
13 # the three axes in space
14 # this will give us 10 particles (_not_ 1000)
15 x = arange(10, typecode=Float)
16 y = arange(10, typecode=Float)
17 z = arange(10, typecode=Float)
18
19 # 3D position information
20 posArray = []
21 for i in range(len(x)):
22 for j in range(len(y)):
23 for k in range(len(z)):
24 posArray.append( (x[i], y[j], z[k]) )
25
26 # radius information
27 random.seed()
28 radiiArray = zeros(len(x)*len(y)*len(z), typecode=Float)
29 for i in range(len(x)*len(y)*len(z)):
30 radiiArray[i] = random.random()*0.8
31
32 # tag information
33 random.seed()
34 tagsArray = zeros(len(x)*len(y)*len(z), typecode=Int)
35 for i in range(len(x)*len(y)*len(z)):
36 tagsArray[i] = int(random.random()*10)
37
38 ### povray code
39
40 # load the data from the vtk file (yes, I know this is a bit dodgy)
41 import vtk
42
43 # create the reader of the file
44 _reader = vtk.vtkXMLUnstructuredGridReader()
45 _reader.SetFileName("../../cp_test_0.xml")
46 #_reader.SetFileName("/home/cochrane/raid2/vis4people/steffen/frame_0.xml")
47 _reader.Update()
48
49 # read the output into an unstructured grid
50 _grid = _reader.GetOutput()
51
52 _modelCentre = _grid.GetCenter()
53 _xMin, _xMax, _yMin, _yMax, _zMin, _zMax = _grid.GetBounds()
54
55 # grab the points where the data sit
56 _vtkPoints = _grid.GetPoints()
57
58 # grab the radius data for the radii of the balls
59 _vtkRadii = _grid.GetPointData().GetScalars("radius")
60
61 # grab the tag data and use for colouring the balls
62 _vtkTags = _grid.GetPointData().GetScalars("particleTag")
63
64 # work out dynamically the number of different tags so that can use this
65 # information to automatically set the scalar range for colouring
66 _numPoints = _vtkTags.GetNumberOfTuples()
67 _valueDict = {}
68 for i in range(_numPoints):
69 _tagValue = _vtkTags.GetValue(i)
70 _valueDict[_tagValue] = 1
71
72 _numTags = len(_valueDict.keys())
73
74 _tagValues = _valueDict.keys()
75 _tagValues.sort()
76
77 # count the number of tags, and make an evenly spaced array of points
78 # between zero and one, then use these as the scalars to colour by
79 _vtkScaledTags = vtk.vtkFloatArray()
80 _vtkScaledTags.SetNumberOfTuples(_numPoints)
81 _vtkScaledTags.SetNumberOfComponents(1)
82 _vtkScaledTags.SetName("tags")
83 for i in range(_numPoints):
84 _tagValue = _vtkTags.GetValue(i)
85 for j in range(_numTags):
86 if _tagValues[j] == _tagValue:
87 _vtkScaledTags.InsertTuple1(i, float(j)/float(_numTags-1))
88
89 # use vtk to generate the colour map, will have to do this myself at
90 # some point
91 _lut = vtk.vtkLookupTable()
92 _lut.Build()
93
94 _red = zeros(_numPoints, typecode=Float)
95 _green = zeros(_numPoints, typecode=Float)
96 _blue = zeros(_numPoints, typecode=Float)
97 for i in range(_numPoints):
98 _red[i], _green[i], _blue[i] = _lut.GetColor(_vtkScaledTags.GetValue(i))
99
100 # now convert the information we want (radii, colours, positions) into
101 # array objects so that I can play with them as per normal in python
102
103 # note: this is an inefficient way to do this, I can do it in one loop,
104 # but this way makes the meaning of the code a lot clearer
105
106 ### the points
107 _xData = zeros(_numPoints, typecode=Float)
108 _yData = zeros(_numPoints, typecode=Float)
109 _zData = zeros(_numPoints, typecode=Float)
110 for i in range(_numPoints):
111 _xData[i], _yData[i], _zData[i] = _vtkPoints.GetPoint(i)
112
113 ### the radii
114 _radii = zeros(_numPoints, typecode=Float)
115 for i in range(_numPoints):
116 _radii[i] = _vtkRadii.GetValue(i)
117
118 ### the tags
119 _scaledTags = zeros(_numPoints, typecode=Float)
120 _tags = zeros(_numPoints, typecode=Int)
121 for i in range(_numPoints):
122 _scaledTags[i] = _vtkScaledTags.GetValue(i)
123 _tags[i] = _vtkTags.GetValue(i)
124
125 ### generate the pov file
126
127 # open the pov file to write to
128 pov = open("ballPlot.pov", "w")
129
130 # the include files to add
131 pov.write("#include \"shapes.inc\"\n")
132 pov.write("#include \"colors.inc\"\n")
133
134 # the camera
135 pov.write("camera {\n")
136 pov.write(" location <%f, %f, -100>\n" %
137 (_modelCentre[0], _modelCentre[1]))
138 pov.write(" direction <0, 0, 2>\n")
139 pov.write(" up <0, 1, 0>\n")
140 pov.write(" right <4/3, 0, 0>\n")
141 pov.write(" look_at <%f, %f, %f>\n" %
142 (_modelCentre[0], _modelCentre[1], _modelCentre[2]))
143 pov.write("}\n")
144
145 # the light source
146 pov.write("light_source {\n")
147 pov.write(" <%f, %f, -300>\n" % (_modelCentre[0], _modelCentre[1]))
148 pov.write(" colour White\n")
149 pov.write("}\n")
150
151 # the spheres
152 for i in range(_numPoints):
153 pov.write("sphere { \n")
154 pov.write(" <%f, %f, %f>, %f\n" %
155 (_xData[i], _yData[i], _zData[i], _radii[i]))
156 pov.write(" pigment {\n")
157 if _tags[i] != 20:
158 pov.write(" colour red %f green %f blue %f\n" %
159 (_red[i], _green[i], _blue[i]))
160 else:
161 pov.write(" rgbt <%f, %f, %f, 0.90>\n" %
162 (_red[i], _green[i], _blue[i]))
163 pov.write(" }\n")
164 pov.write("}\n")
165
166 # put a title on it
167 pov.write("text {\n")
168 pov.write(" ttf \"timrom.ttf\" \"Example ball plot\" 0.1, 0\n")
169 pov.write(" pigment {\n")
170 pov.write(" colour White\n")
171 pov.write(" }\n")
172 pov.write(" scale <3, 3, 1>\n")
173 pov.write(" translate <%f, %f, 0>\n" %
174 (_modelCentre[0]-10, 1.2*_yMax))
175 pov.write("}\n")
176
177 # close the file
178 pov.close()
179
180 ### generate the ini file
181
182 # open the ini file to write to
183 ini = open("ballPlot.ini", "w")
184
185 # the output resolution
186 ini.write("Width=640\n")
187 ini.write("Height=480\n")
188
189 # anti-aliasing settings
190 ini.write("Antialias=on\n")
191
192 # generate png files
193 ini.write("Output_File_Type=N\n")
194
195 # the name of the input pov file
196 ini.write("Input_File_Name=ballPlot.pov\n")
197
198 # pause when done
199 ini.write("Pause_When_Done=on\n")
200
201 # close the file
202 ini.close()
203
204 # run povray on the file
205 os.system("povray ballPlot.ini")
206
207 # vim: expandtab shiftwidth=4:
208

  ViewVC Help
Powered by ViewVC 1.1.26