Skip to content

vtkPolyData + vtkCellData + multiple cell types

This issue was created automatically from an original Mantis Issue. Further discussion may take place here.


When using a combination of cell types within a vtkPolyData object (i.e. both vertices, lines, polys, and/or strips) the cell data property of the vtkPolyData object does not get handled properly (there is no fixed relation between the cell ids of the vtkCellData object and the cell ids of the vtkPolyData object). Furthermore, the polydata mappers are also not consistent in how they deal with the CellData of a vtkPolyData object when the cell data is used for coloring information.

The python code below illustrates the problem: If things would be correct you should see a red triangle, a green line, and a blue bar. But both the vtkPolyDataMapper and vtkPolyDataMapper2D give different results.


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 poly 1 (triangle)

points.InsertNextPoint(0.2, 0.3, 0) points.InsertNextPoint(0.4, 0.55, 0) points.InsertNextPoint(0.2, 0.8, 0)

points for line 1 (line)

points.InsertNextPoint(0.5, 0.3, 0) points.InsertNextPoint(0.5, 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 poly 1

ids = vtk.vtkIdList() ids.InsertNextId(0) ids.InsertNextId(1) ids.InsertNextId(2) cell = polydata.InsertNextCell(VTK_POLYGON, ids)

poly 1 should get the first color (red)

colors.InsertTuple1(cell, 0.0)

create line 1

ids.Reset() ids.InsertNextId(3) ids.InsertNextId(4) cell = polydata.InsertNextCell(VTK_LINE, ids)

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