[vtkusers] string labels on cellcenters
Raphael Walker
web at busino.ch
Wed Oct 17 07:44:11 EDT 2007
Hi all,
I'm trying to have string labels on cell centers of a unstructuredgrid
plot.
1. first i create a vtkCharArray with the strings.
2. add it to the cell data
3. create a vtkLabeledDataMapper with the cellcenter
4. set the fieldDataArray
I only get "(null)" labels!
For an example see the script at the end.
Thanks for any help and ideas
Raffs
EXAMPLE++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/usr/bin/env python
import vtk
from vtk import *
from numpy import *
#
# array to create the model
#
# points coordinates
ptarray =
array([0,0,0,0,0.153135,0,0.358,0.153135,0,0.358,0,0,0.0770633,0,0,0.19452,0,0,0.288133,0,0,0,0.0768721,0,0.0766132,0.153135,0,0.191553,0.153135,0,0.270763,0.153135,0,0.358,0.0697678,0,0.267349,0.0919374,0,0.134156,0.0780027,0])
# element table
earray =
array([3,3,11,6,3,6,11,12,3,10,9,12,3,9,8,13,3,12,9,13,3,8,1,7,3,13,8,7,3,7,0,4,3,13,7,4,3,4,5,13,3,5,6,12,3,13,5,12,3,11,2,12,3,2,10,12],
int32)
# element types -> triangle
ctarray = array((6,6,6,6,6,6,6,6,6,6,6,6,6,6), ubyte)
# element start index
locarray = array((0,4,8,12,16,20,24,28,32,36,40,44,48,52), int32)
# scalar values for the cell centers
scalarray = array((1.,2.,3.,4.,5.,6.,7.,8.,9.,8.,7.,6.,5.,4.))
# label array
labelarray = array(("QW", "SD", "sd", "qe", "gd", "sd", "qe", "fd",
"sd", "we", "gd", "ad", "qe", "gd"))
# idtypes
IdTArray=vtkIdTypeArray()
IdTArray.SetNumberOfComponents(3)
IdTArray.SetNumberOfTuples(len(earray))
IdTArray.SetVoidArray(earray,len(earray),1)
# celltypes
cidTArray=vtkUnsignedCharArray()
cidTArray.SetVoidArray(ctarray,len(ctarray),1)
# cell array
vtkCArray=vtkCellArray()
vtkCArray.SetCells(len(earray),IdTArray)
# cell location
vtkLocArray=vtkIdTypeArray()
vtkLocArray.SetVoidArray(locarray,len(locarray),1)
# grid
grid=vtkUnstructuredGrid()
grid.SetCells(cidTArray,vtkLocArray,vtkCArray)
# points
vpoints=vtkDoubleArray()
vpoints.SetNumberOfComponents(3)
vpoints.SetNumberOfTuples(len(ptarray))
vpoints.SetVoidArray(ptarray,len(ptarray),1)
pointe = vtkPoints()
pointe.SetData(vpoints)
grid.SetPoints(pointe)
grid.Update()
# scalars
scalars = vtkDoubleArray()
scalars.SetVoidArray(scalarray, len(scalarray), 1)
grid.GetCellData().SetScalars(scalars)
# labels
charArray = vtkCharArray()
charArray.SetNumberOfComponents(3)
charArray.SetNumberOfTuples(len(labelarray))
charArray.SetVoidArray(labelarray, 3*14,1)
grid.GetCellData().AddArray(charArray)
geom = vtkGeometryFilter()
geom.SetInput(grid)
# Create cell centers
cellCenters = vtk.vtkCellCenters()
cellCenters.SetInputConnection(geom.GetOutputPort())
# Create the mapper to display the cell
cellMapper = vtk.vtkLabeledDataMapper()
cellMapper.SetInputConnection(cellCenters.GetOutputPort())
cellMapper.SetLabelFormat("%s")
cellMapper.SetLabelModeToLabelFieldData()
#cellMapper.TreatCharDataAsStringOn();
cellMapper.SetFieldDataArray(0)
# actor for the labels
labelActor = vtk.vtkActor2D()
labelActor.SetMapper(cellMapper)
# data color mapper
ColorMapper=vtkPolyDataMapper()
ColorMapper.SetInputConnection(geom.GetOutputPort())
ColorMapper.SetScalarModeToUseCellData()
ColorMapper.SetScalarRange(0.,9.)
# actor for the colors
colorActor = vtkActor()
colorActor.SetMapper(ColorMapper)
# renderer
ren = vtk.vtkRenderer()
# render window
win = vtkRenderWindow()
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(win)
style = vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)
iren.Initialize()
win.AddRenderer(ren)
ren.AddActor(colorActor)
ren.AddActor(labelActor)
ren.SetBackground(0.1, 0.3, 0.4)
win.Render()
iren.Start()
More information about the vtkusers
mailing list