[vtkusers] Scaling and colouring glyphs with different scalar arrays

Paul Cochrane cochrane at esscc.uq.edu.au
Thu Mar 31 20:36:33 EST 2005


John,

Many thanks for your help!  I managed to get the spheres coloured the way I
want them, however, it doesn't seem that elegant (code attached).  I notice
that ColorByArrayComponent() is a legacy method, and that a lookup table is
better.  How does one do this?  

Also, a related, but different question: if one has a vtk file containing an
UnstructuredGrid with several arrays of PointData (each named), and then
tries to scale spherical glpyhs with one of the PointData arrays, and colour
with one of the others, how can one do this elegantly?  What I'm doing atm
is to extract the data from the unstructured grid, create a vtkFloatArray
with two components (say) and then add the relevant data to the FloatArray,
add this multicomponent array to the grid, and then use
ColorByArrayComponent() later down the track to colour by the second
component of the new data array.  This technique feels a bit hacky, and
seems to be a waste of memory, cpu, and time, so I was wondering if there
was a better way to do this?

TIA

Paul

-- 
Paul Cochrane
Computational Scientist/Software Developer
Earth Systems Science Computational Centre
Rm 703, SMI Building
University of Queensland
Brisbane
Queensland 4072
Australia

E: cochrane at esscc.uq.edu.au
P: +61 7 3346 4109
F: +61 7 3365 7347
-------------- next part --------------
#!/usr/bin/env python

import vtk

import sys

# some radii
radii = vtk.vtkFloatArray()
radii.InsertNextValue(1.0)
radii.InsertNextValue(0.1)
radii.InsertNextValue(0.2)
radii.SetName("radius")

# define the colours for the spheres
tags = vtk.vtkFloatArray()
tags.InsertNextValue(1.0)
tags.InsertNextValue(0.5)
tags.InsertNextValue(0.7)
tags.SetName("tag")

data = vtk.vtkFloatArray()
data.SetNumberOfComponents(2)
data.SetNumberOfTuples(3)
data.CopyComponent(0, radii, 0)
data.CopyComponent(1, tags, 0)
data.SetName("data")

# define the locations of the spheres
points = vtk.vtkPoints()
points.InsertNextPoint(0, 0, 0)
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(1, 1, 0)

# construct the grid
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(points)
grid.GetPointData().AddArray(data)
grid.GetPointData().SetActiveScalars("data")

# 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()
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)

# 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