File: dimacs.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 (58 lines) | stat: -rwxr-xr-x 1,499 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
"""
Just a demo script to test out the DIMACS reader/writer function
"""
from __future__ import print_function
import time
from vtk import *

# generate a random graph
source = vtkRandomGraphSource()
source.SetNumberOfVertices(100)
source.SetAllowSelfLoops(False)
source.SetEdgeProbability(0.1)
source.SetUseEdgeProbability(True)
source.AllowParallelEdgesOff()
source.Update()

print("Original graph:")
print("\tnum_verts =", source.GetOutput().GetNumberOfVertices())
print("\tnum_edges =", source.GetOutput().GetNumberOfEdges())
print("")

# write the graph to a file
writer = vtkDIMACSGraphWriter()
writer.AddInputConnection(source.GetOutputPort())
writer.SetFileName("graph.gr")
writer.Update()
print("wrote graph to 'graph.gr'")

# let's pause a second to let the disk catch up <shrug/>
time.sleep(1)

# read the graph in from the file
reader = vtkDIMACSGraphReader()
reader.SetFileName("graph.gr")
reader.Update()
print("read graph from 'graph.gr'")

print("Graph from disk:")
print("\tnum_verts =", reader.GetOutput().GetNumberOfVertices())
print("\tnum_edges =", reader.GetOutput().GetNumberOfEdges())

view = vtkGraphLayoutView()
view.AddRepresentationFromInputConnection(reader.GetOutputPort())
view.SetLayoutStrategyToSimple2D()

theme = vtkViewTheme.CreateMellowTheme()
theme.SetLineWidth(1)
theme.SetPointSize(5)
view.ApplyViewTheme(theme)
theme.FastDelete()


view.GetRenderWindow().SetSize(600, 600)
view.ResetCamera()
view.Render()

view.GetInteractor().Start()