File: displayVTKHierarchy.py

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (82 lines) | stat: -rw-r--r-- 1,995 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
import vtk
import inspect
import csv
import sys
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

def addToTree(cls, class_to_vertex_map, tree, names):
    vertexid = None

    if cls in class_to_vertex_map:
        vertexid = class_to_vertex_map[cls]
    else:
        vertexid = tree.AddVertex()
        class_to_vertex_map[cls] = vertexid
        names.InsertValue(vertexid, cls)

    return vertexid


def parseClassTree(classtree, tree, names):

    class_to_vertex_map = dict()

    for (parent, child) in classtree:
        parentid = addToTree(parent, class_to_vertex_map, tree, names)
        childid = addToTree(child, class_to_vertex_map, tree, names)

        tree.AddGraphEdge(parentid, childid)

# The tree that will hold vtk class hierarchy
builder = vtk.vtkMutableDirectedGraph()
names = vtk.vtkStringArray()
names.SetName("name")

# Allocate 100 tuples to start with. I picked this number
# out of air.
names.SetNumberOfTuples(100)

# load vtk class information
reader = csv.reader(open(VTK_DATA_ROOT + "/Data/Infovis/classes.csv", 'r'))

classes = []

for row in reader:
    classes.append(row)

# Convert list to tree
parseClassTree(classes, builder, names)

tree = vtk.vtkTree()
tree.CheckedShallowCopy(builder)
tree.GetVertexData().AddArray(names)

## Now iterate over the tree and print it
#iter = vtk.vtkTreeDFSIterator()
#iter.SetTree(tree)
#
#while iter.HasNext():
#    id = iter.Next()
#    mystr = tree.GetLevel(id) * "   "
#    print mystr + str(names.GetValue(id))

# Display the tree in the tree map viewer
view = vtk.vtkGraphLayoutView()
view.AddRepresentationFromInput(tree);
view.ResetCamera()
view.Render()
view.SetVertexLabelArrayName("name")
view.SetVertexLabelVisibility(True)
view.GetInteractor().Start()

win = vtk.vtkRenderWindow()
interact = vtk.vtkRenderWindowInteractor()
interact.SetRenderWindow(win)
view.SetRenderWindow(win)
win.Render()

win.GetInteractor().Initialize()
win.GetInteractor().Start();