[vtkusers] Re: Need some help with vtkGraph representation and visualisation pipeline of graphs.

Jeff Baumes jeff.baumes at kitware.com
Mon May 7 12:09:23 EDT 2007


Geofram,

There are a few different ways to display a random graph in VTK.  One
way is to set to vertex locations manually:

<code>
import vtk
import random

points = vtk.vtkPoints()
for i in range(500):
    points.InsertPoint(i, random.random(),random.random(),random.random())

graph = vtk.vtkGraph()
graph.SetPoints(points)
graph.SetNumberOfVertices(500)

for i in range(499):
    graph.AddEdge(i,i+1)

graphtopoly = vtk.vtkGraphToPolyData()
graphtopoly.SetInput(graph)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(graphtopoly.GetOutputPort())

graphactor = vtk.vtkActor()
graphactor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(graphactor)
win = vtk.vtkRenderWindow()
win.AddRenderer(ren)
interact = vtk.vtkRenderWindowInteractor()
interact.SetRenderWindow(win)
win.GetInteractor().Initialize()
win.GetInteractor().Start()
</code>

The next is to use a random graph layout:
<code>
import vtk

graph = vtk.vtkGraph()
graph.SetNumberOfVertices(500)

for i in range(499):
    graph.AddEdge(i,i+1)

layout = vtk.vtkRandomLayoutStrategy()
layout.ThreeDimensionalLayoutOn()

graphlayout = vtk.vtkGraphLayout()
graphlayout.SetInput(graph)
graphlayout.SetLayoutStrategy(layout)

graphtopoly = vtk.vtkGraphToPolyData()
graphtopoly.SetInputConnection(graphlayout.GetOutputPort())

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(graphtopoly.GetOutputPort())

graphactor = vtk.vtkActor()
graphactor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(graphactor)
win = vtk.vtkRenderWindow()
win.AddRenderer(ren)
interact = vtk.vtkRenderWindowInteractor()
interact.SetRenderWindow(win)
win.GetInteractor().Initialize()
win.GetInteractor().Start()
</code>

The last way is to use the graph layout viewer, which creates the
pipeline/mappers/actors for you:

<code>
import vtk

graph = vtk.vtkGraph()
graph.SetNumberOfVertices(500)

for i in range(499):
    graph.AddEdge(i,i+1)

viewer = vtk.infovis.vtkGraphLayoutViewer()
viewer.SetInput(graph)
viewer.SetLayoutStrategy('Random');

ren = vtk.vtkRenderer()
win = vtk.vtkRenderWindow()
win.AddRenderer(ren)
interact = vtk.vtkRenderWindowInteractor()
interact.SetRenderWindow(win)
viewer.SetRenderWindow(win)
win.GetInteractor().Initialize()
win.GetInteractor().Start()
</code>

A few notes:
1. The vtkGraphLayoutViewer always assigns new coordinates to vertices
based on a layout strategy.  The default behavior is to perform a
force directed layout (that is why your graph was showing up as flat
... the layout spread out the graph in the x and y axes).
2. vtkGraphLayoutViewer turns edges into lines and also glyphs the
vertices.  vtkGraphToPolyData only turns edges into lines.  You need
to use vtkGlyph3D to display vertices if you are not using the viewer.

Hope this helps!
Jeff

-- 
Jeff Baumes
R&D Engineer, Kitware Inc.
(518) 371-3971 x132
jeff.baumes at kitware.com



More information about the vtkusers mailing list