[vtkusers] Extracting parts of an unstructured grid
Paul Cochrane
cochrane at esscc.uq.edu.au
Sun Apr 24 21:49:35 EDT 2005
Hi all,
I'm having problems with the ExtractUnstructuredGrid() class. I have a grid
which represents a set of spheres in 3D space, and I want to take a thin
slab of the data by using the SetExtent() method. However, when I pass my
unstructured grid through the ExtractUnstructuredGrid filter I don't seem to
get any output, an I'm wondering what I'm doing wrong. I'm attaching a
small python script that illustrates the problem. Just change the "extract"
variable to True to pass the grid through the extract filter to see what I
mean.
Any help with respect to this problem would be greatly appreciated.
Paul
--
Paul Cochrane
Computational Scientist/Software Developer
Earth Systems Science Computational Centre
Rm 703, SMI Building
University of Queensland
Brisbane
Queensland 4072
Australia
-------------- next part --------------
#!/usr/bin/env python
import vtk
import sys
numSpheres = 5
extract = False # whether or not to try to extract from the UGrid
# define the locations of the spheres
points = vtk.vtkPoints()
# some radii
radii = vtk.vtkFloatArray()
radii.SetName("radius")
# define the colours for the spheres
tags = vtk.vtkFloatArray()
tags.SetName("tag")
# and generate them
for i in range(numSpheres):
for j in range(numSpheres):
for k in range(numSpheres):
points.InsertNextPoint(i, j, k)
radii.InsertNextValue(float(i+j+k)/(3.0*float(numSpheres)))
tags.InsertNextValue(i)
# work out dynamically the number of different tags so that can use this
# information to automatically set the scalar range for colouring
numPoints = tags.GetNumberOfTuples()
valueDict = {}
for i in range(numPoints):
tagValue = tags.GetValue(i)
valueDict[tagValue] = 1
numTags = len(valueDict.keys())
# construct the data array to pass through the
data = vtk.vtkFloatArray()
data.SetNumberOfComponents(2)
data.SetNumberOfTuples(radii.GetNumberOfTuples())
data.CopyComponent(0, radii, 0)
data.CopyComponent(1, tags, 0)
data.SetName("data")
# construct the grid
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(points)
grid.GetPointData().AddArray(data)
grid.GetPointData().SetActiveScalars("data")
extractGrid = vtk.vtkExtractUnstructuredGrid()
extractGrid.SetInput(grid)
extractGrid.Update()
print extractGrid.GetOutput()
# Create a sphere to use as a glyph source for vtkGlyph3D.
sphere = vtk.vtkSphereSource()
sphere.SetRadius(0.5)
sphere.SetPhiResolution(16)
sphere.SetThetaResolution(16)
# make the glyphs
glyph = vtk.vtkGlyph3D()
if extract:
glyph.SetInput(extractGrid.GetOutput())
else:
glyph.SetInput(grid)
glyph.SetSource(sphere.GetOutput())
glyph.ClampingOff()
glyph.SetScaleModeToScaleByScalar()
glyph.SetScaleFactor(1.0)
glyph.SetColorModeToColorByScalar()
# set up the mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(glyph.GetOutput())
mapper.ScalarVisibilityOn()
mapper.ColorByArrayComponent("data", 1)
mapper.SetScalarRange(0, numTags-1)
# set up the actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# do renderer setup stuff
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(640, 480)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# add the actor to the renderer
ren.AddActor(actor)
# render
iren.Initialize()
renWin.Render()
iren.Start()
More information about the vtkusers
mailing list