[vtkusers] Map StructuredGrid CellData onto a vtkPlaneWidget

Luca Pallozzi Lavorante lplavorante at gmail.com
Tue Aug 2 10:11:24 EDT 2016


Hi everybody, maybe this is a trivial question, but I don't know how to
select a StructuredGrid's cell data (insead of its points dada) in order to
map these values  onto a vtkPlaneWidget. I have modified the
ProbingWithPlaneWidget.py script in order to  test this feature. I build a
StructuredGrid with 12 points and only two cells, then I set the CellData
(two values which are correctly rendered as two colors by  the
StructuredGrid's mapper). If I build point data for the 12 points the probe
works fine, but if I remove the pointdata code and try to use the celldata
one, the probe is shown as white, with no color associated to the cell data.
Thanks in advance for any help.

  Luca Pallozzi Lavorante

#!/usr/bin/env python

# This example demonstrates how to use the vtkPlaneWidget to probe
# a dataset and then generate contours on the probed data.

import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

structuredGrid = vtk.vtkStructuredGrid()
points = vtk.vtkPoints()
for k in range(2):
  for j in range(3):
    for i in range(2):
       points.InsertNextPoint(i, j, k)

structuredGrid.SetDimensions(2, 3, 2)
structuredGrid.SetPoints(points)

# Insert cell data into StructuredGrid
cellData = vtk.vtkFloatArray()
cellData.SetNumberOfComponents(1)
cellData.InsertNextValue(10)
cellData.InsertNextValue(20)
cellData.SetName("CellData")
structuredGrid.GetCellData().SetScalars(cellData)

# Insert cell data into StructuredGrid
pointData = vtk.vtkFloatArray()
pointData.SetNumberOfComponents(1)
for i in range(12):
  pointData.InsertNextValue(i*10)
pointData.SetName("PointData")
structuredGrid.GetPointData().SetScalars(pointData)

# The plane widget is used probe the dataset.
planeWidget = vtk.vtkPlaneWidget()
planeWidget.SetInputData(structuredGrid)
planeWidget.NormalToXAxisOn()
planeWidget.SetResolution(20)
planeWidget.SetRepresentationToOutline()
planeWidget.PlaceWidget()
plane = vtk.vtkPolyData()
planeWidget.GetPolyData(plane)

probe = vtk.vtkProbeFilter()
probe.SetInputData(plane)
probe.SetSourceData(structuredGrid)

contourMapper = vtk.vtkPolyDataMapper()
contourMapper.SetInputConnection(probe.GetOutputPort())
contourMapper.SetScalarRange(structuredGrid.GetScalarRange())
contourActor = vtk.vtkActor()
contourActor.SetMapper(contourMapper)
contourActor.VisibilityOff()

# An outline is shown for context.
outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(structuredGrid)
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)

mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(structuredGrid)
mapper.SetScalarRange(structuredGrid.GetScalarRange())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# Create the RenderWindow, Renderer and both Actors
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Actually generate contour lines.
def BeginInteraction(obj, event):
    global plane, contourActor
    obj.GetPolyData(plane)
    contourActor.VisibilityOn()

def ProbeData(obj, event):
    global plane
    obj.GetPolyData(plane)


# Associate the widget with the interactor
planeWidget.SetInteractor(iren)
# Handle the events.
planeWidget.AddObserver("EnableEvent", BeginInteraction)
planeWidget.AddObserver("StartInteractionEvent", BeginInteraction)
planeWidget.AddObserver("InteractionEvent", ProbeData)

# Add the actors to the renderer, set the background and size
#ren.AddActor(outlineActor)
ren.AddActor(actor)
ren.AddActor(contourActor)

ren.SetBackground(1, 1, 1)
renWin.SetSize(300, 300)
ren.SetBackground(0.1, 0.2, 0.4)

iren.Initialize()
renWin.Render()
iren.Start()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160802/0402bb7b/attachment.html>


More information about the vtkusers mailing list