[vtkusers] Add scalars to polygons
William E Lucarell
welucarell at equityeng.com
Wed May 14 08:10:43 EDT 2008
Below is a code example for setting scalars in polygonal data. I got it
from http://public.kitware.com/pipermail/vtkusers/2004-January/071326.html
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()
Hope this helps!
Bill
-----Original Message-----
From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On Behalf
Of Matthias Riechmann
Sent: Wednesday, May 14, 2008 7:45 AM
To: vtkusers at vtk.org
Subject: [vtkusers] Add scalars to polygons
Hi there,
is there some kind of filter or similar mechanism to add scalars to
polydatas? I would like to load an stl file and set the scalars afterwards.
Matthias
--
Dipl.-Inform. Matthias Riechmann
Institut für Prozessrechentechnik, Automation und Robotik
Medizin-Gruppe
Universität Karlsruhe (TH)
Gebäude 40.28, Zimmer 103
Engler-Bunte-Ring 8
76131 Karlsruhe
Fon: +49 (721) 608-4049
Fax: +49 (721) 608-7141
Web: http://wwwipr.ira.uka.de/~richmann
More information about the vtkusers
mailing list