# Contents of /trunk/pyvisi/examples/renderers/vtk/arrowPlot.py

Revision 419 - (show annotations)
Fri Jan 6 03:01:27 2006 UTC (14 years, 10 months ago) by cochrane
File MIME type: text/x-python
File size: 4073 byte(s)
```Initial import to repository.  This is the vtk-specific code for the arrow
plot example.

```
 1 # \$Id: arrowPlot.py,v 1.2 2005/06/20 05:20:26 paultcochrane Exp \$ 2 3 """ 4 Example of plotting a vector field 5 """ 6 7 # set up some data to plot 8 from Numeric import * 9 10 # the positions of the vectors 11 x = arange(20, typecode=Float) 12 y = arange(20, typecode=Float) 13 14 # the vector displacements 15 # (I may need to rethink how this works in the interface) 16 dx = arange(20, typecode=Float) 17 dy = arange(20, typecode=Float) 18 19 # set the positions randomly, and set the displacements to be the square of 20 # the positions 21 import random 22 random.seed() 23 for i in range(len(x)): 24 x[i] = random.random() 25 y[i] = random.random() 26 dx[i] = x[i]*x[i] 27 dy[i] = y[i]*y[i] 28 29 import vtk 30 31 if len(x) != len(y) != len(dx) != len(dy): 32 raise ValueError, "x, y, dx and dy vectors must be of equal length" 33 34 numPoints = len(x) 35 36 # construct the points data 37 points = vtk.vtkPoints() 38 points.SetNumberOfPoints(numPoints) 39 for i in range(numPoints): 40 points.InsertPoint(i, x[i], y[i], 0.0) 41 42 # make the vectors 43 vectors = vtk.vtkFloatArray() 44 vectors.SetNumberOfComponents(3) 45 vectors.SetNumberOfTuples(numPoints) 46 vectors.SetName("vectors") 47 for i in range(numPoints): 48 vectors.InsertTuple3(i, dx[i], dy[i], 0.0) 49 50 # construct the grid 51 grid = vtk.vtkUnstructuredGrid() 52 grid.SetPoints(points) 53 grid.GetPointData().AddArray(vectors) 54 grid.GetPointData().SetActiveVectors("vectors") 55 56 # make the arrow source 57 arrow = vtk.vtkArrowSource() 58 59 # make the glyph 60 glyph = vtk.vtkGlyph2D() 61 glyph.ScalingOn() 62 glyph.SetScaleModeToScaleByVector() 63 glyph.SetColorModeToColorByVector() 64 glyph.SetScaleFactor(0.5) 65 glyph.SetSource(arrow.GetOutput()) 66 glyph.SetInput(grid) 67 glyph.ClampingOff() 68 69 # set up a stripper for faster rendering 70 stripper = vtk.vtkStripper() 71 stripper.SetInput(glyph.GetOutput()) 72 73 # get the maximum norm of the data 74 maxNorm = grid.GetPointData().GetVectors().GetMaxNorm() 75 76 # now set up the mapper 77 mapper = vtk.vtkPolyDataMapper() 78 mapper.SetInput(stripper.GetOutput()) 79 mapper.SetScalarRange(0, maxNorm) 80 81 # set up the actor 82 actor = vtk.vtkActor() 83 actor.SetMapper(mapper) 84 85 # set up the renderer stuff 86 ren = vtk.vtkRenderer() 87 renWin = vtk.vtkRenderWindow() 88 renWin.SetSize(640,480) 89 renWin.AddRenderer(ren) 90 ren.SetBackground(1,1,1) 91 92 ren.AddActor(actor) 93 94 # set up some text properties 95 font_size = 14 96 textProp = vtk.vtkTextProperty() 97 textProp.SetFontSize(font_size) 98 textProp.SetFontFamilyToArial() 99 textProp.BoldOff() 100 textProp.ItalicOff() 101 textProp.ShadowOff() 102 textProp.SetColor(0,0,0) 103 104 # add a title 105 titleMapper = vtk.vtkTextMapper() 106 title = 'Example 2D arrow/quiver/vector field plot' 107 titleMapper.SetInput(title) 108 109 titleProp = titleMapper.GetTextProperty() 110 titleProp.ShallowCopy(textProp) 111 titleProp.SetJustificationToCentered() 112 titleProp.SetVerticalJustificationToTop() 113 titleProp.SetFontSize(18) 114 115 # set up the text actor 116 titleActor = vtk.vtkTextActor() 117 titleActor.SetMapper(titleMapper) 118 titleActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay() 119 titleActor.GetPositionCoordinate().SetValue(0.5, 0.95) 120 121 ren.AddActor(titleActor) 122 123 # set up some axes 124 axes = vtk.vtkCubeAxesActor2D() 125 #axes.SetInput(grid) 126 axes.SetCamera(ren.GetActiveCamera()) 127 axes.SetFlyModeToOuterEdges() 128 axes.SetBounds(min(x), max(x)+maxNorm, min(y), max(y)+maxNorm, 0, 0) 129 axes.SetXLabel("x") 130 axes.SetYLabel("y") 131 axes.SetZLabel("") 132 axes.YAxisVisibilityOff() # but this is the z axis!! 133 134 axesProp = axes.GetProperty() 135 axesProp.SetColor(0,0,0) 136 137 axesTitleProp = axes.GetAxisTitleTextProperty() 138 axesTitleProp.ShallowCopy(textProp) 139 140 axesLabelProp = axes.GetAxisLabelTextProperty() 141 axesLabelProp.ShallowCopy(textProp) 142 axesLabelProp.SetFontSize(8) 143 144 ren.AddActor(axes) 145 146 ren.ResetCamera() 147 148 # set up the interactive renderer and render the scene 149 iren = vtk.vtkRenderWindowInteractor() 150 iren.SetRenderWindow(renWin) 151 iren.Initialize() 152 renWin.Render() 153 iren.Start() 154 155 # convert the render window to an image 156 win2img = vtk.vtkWindowToImageFilter() 157 win2img.SetInput(renWin) 158 159 # save the image to file 160 outWriter = vtk.vtkPNGWriter() 161 outWriter.SetInput(win2img.GetOutput()) 162 outWriter.SetFileName("arrowPlot.png") 163 outWriter.Write() 164 165 # vim: expandtab shiftwidth=4: 166