[vtkusers] Cell data in a vtkPolyData object
Sander Niemeijer
niemeijer at science-and-technology.nl
Fri Aug 27 08:04:33 EDT 2004
Ok. The reordering in my previous e-mail was not quite what it was
supposed to be (the first two cells got the wrong colors assigned) :(
Below the full example as I intended it to be (first the line, then the
two polys, using the colors red/green/blue).
In the reordered case the 3D Mapper indeed produces the correct color
mapping. However, the 2D Mapper does not! And for the application we
have written the 2D mapper is essential.
Best regards,
Sander
---
import vtk
VTK_LINE = 3
VTK_POLYGON = 7
# initialize polydata object
polydata = vtk.vtkPolyData()
polydata.SetLines(vtk.vtkCellArray())
polydata.SetPolys(vtk.vtkCellArray())
points = vtk.vtkPoints()
# points for line 1 (line)
points.InsertNextPoint(0.3, 0.3, 0)
points.InsertNextPoint(0.3, 0.8, 0)
# points for triangle 1 (triangle)
points.InsertNextPoint(0.4, 0.3, 0)
points.InsertNextPoint(0.6, 0.55, 0)
points.InsertNextPoint(0.4, 0.8, 0)
# points for poly 2 (bar)
points.InsertNextPoint(0.6, 0.3, 0)
points.InsertNextPoint(0.8, 0.3, 0)
points.InsertNextPoint(0.8, 0.8, 0)
points.InsertNextPoint(0.6, 0.8, 0)
polydata.SetPoints(points)
# set scalar cell data
colors = vtk.vtkFloatArray()
polydata.GetCellData().SetScalars(colors)
# create line 1
ids = vtk.vtkIdList()
ids.InsertNextId(0)
ids.InsertNextId(1)
cell = polydata.InsertNextCell(VTK_LINE, ids)
# line 1 should get the first color (red)
colors.InsertTuple1(cell, 0.0)
# create poly 1
ids.Reset()
ids.InsertNextId(2)
ids.InsertNextId(3)
ids.InsertNextId(4)
cell = polydata.InsertNextCell(VTK_POLYGON, ids)
# poly 1 should get the second color (green)
colors.InsertTuple1(cell, 1.0)
# create poly 2
ids.Reset()
ids.InsertNextId(5)
ids.InsertNextId(6)
ids.InsertNextId(7)
ids.InsertNextId(8)
cell = polydata.InsertNextCell(VTK_POLYGON, ids)
# poly 2 should get the third color (blue)
colors.InsertTuple1(cell, 2.0)
# create reference poly with colors as they should be
refpoly = vtk.vtkPolyData()
refpoly.SetLines(vtk.vtkCellArray())
refpoly.GetCellData().SetScalars(colors)
scalars = refpoly.GetCellData().GetScalars()
print scalars.GetNumberOfTuples()
refpoints = vtk.vtkPoints()
refpoints.InsertNextPoint(0.2, 0.2, 0)
refpoints.InsertNextPoint(0.4, 0.2, 0)
refpoints.InsertNextPoint(0.6, 0.2, 0)
refpoints.InsertNextPoint(0.8, 0.2, 0)
refpoly.SetPoints(refpoints)
ids.Reset()
ids.InsertNextId(0)
ids.InsertNextId(1)
cell = refpoly.InsertNextCell(VTK_LINE, ids)
ids.Reset()
ids.InsertNextId(1)
ids.InsertNextId(2)
cell = refpoly.InsertNextCell(VTK_LINE, ids)
ids.Reset()
ids.InsertNextId(2)
ids.InsertNextId(3)
cell = refpoly.InsertNextCell(VTK_LINE, ids)
# define colors
lut = vtk.vtkLookupTable()
lut.SetNumberOfTableValues(3)
lut.SetTableRange(0, 2)
lut.SetTableValue(0, 1.0, 0.0, 0.0, 1.0) # red
lut.SetTableValue(1, 0.0, 1.0, 0.0, 1.0) # green
lut.SetTableValue(2, 0.0, 0.0, 1.0, 1.0) # blue
lut.Build()
# create mapper and assign colortable
polyMapper = vtk.vtkPolyDataMapper()
polyMapper.SetInput(polydata)
polyMapper.SetScalarModeToUseCellData()
polyMapper.UseLookupTableScalarRangeOn()
polyMapper.SetLookupTable(lut)
coordinate = vtk.vtkCoordinate()
coordinate.SetCoordinateSystemToNormalizedViewport()
polyMapper2D = vtk.vtkPolyDataMapper2D()
polyMapper2D.SetTransformCoordinate(coordinate)
polyMapper2D.SetInput(polydata)
polyMapper2D.SetScalarModeToUseCellData()
polyMapper2D.UseLookupTableScalarRangeOn()
polyMapper2D.SetLookupTable(lut)
# same for the refpoly
refpolyMapper = vtk.vtkPolyDataMapper()
refpolyMapper.SetInput(refpoly)
refpolyMapper.SetScalarModeToUseCellData()
refpolyMapper.UseLookupTableScalarRangeOn()
refpolyMapper.SetLookupTable(lut)
refpolyMapper2D = vtk.vtkPolyDataMapper2D()
refpolyMapper2D.SetTransformCoordinate(coordinate)
refpolyMapper2D.SetInput(refpoly)
refpolyMapper2D.SetScalarModeToUseCellData()
refpolyMapper2D.UseLookupTableScalarRangeOn()
refpolyMapper.SetLookupTable(lut)
refpolyMapper2D.SetLookupTable(lut)
# create actors
polyActor = vtk.vtkActor()
polyActor.SetMapper(polyMapper)
polyActor2D = vtk.vtkActor2D()
polyActor2D.SetMapper(polyMapper2D)
refpolyActor = vtk.vtkActor()
refpolyActor.SetMapper(refpolyMapper)
refpolyActor2D = vtk.vtkActor2D()
refpolyActor2D.SetMapper(refpolyMapper2D)
# create renderers
renderer = vtk.vtkRenderer()
renderer.SetViewport(0, 0, 0.5, 1.0)
renderer.AddActor(polyActor)
renderer.AddActor(refpolyActor)
renderer2D = vtk.vtkRenderer()
renderer2D.SetViewport(0.5, 0.0, 1.0, 1.0)
renderer2D.AddActor(polyActor2D)
renderer2D.AddActor(refpolyActor2D)
# render it
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetSize(600,300)
renderWindow.AddRenderer(renderer)
renderWindow.AddRenderer(renderer2D)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renderWindow)
renderWindow.Render()
iren.Start()
More information about the vtkusers
mailing list