File: TestGraphLayoutFilter.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,992 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 185; javascript: 165; objc: 153; tcl: 59
file content (156 lines) | stat: -rwxr-xr-x 4,276 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonDataModel import (
    vtkCellArray,
    vtkPolyData,
)
from vtkmodules.vtkFiltersCore import (
    vtkAppendPolyData,
    vtkGlyph3D,
    vtkTubeFilter,
)
from vtkmodules.vtkFiltersGeneral import vtkGraphLayoutFilter
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

def GetRGBColor(colorName):
    '''
        Return the red, green and blue components for a
        color as doubles.
    '''
    rgb = [0.0, 0.0, 0.0]  # black
    vtkNamedColors().GetColorRGB(colorName, rgb)
    return rgb

# Create a simple graph (it's jittered from optimum)
pts = vtkPoints()
pts.SetNumberOfPoints(10)
pts.SetPoint(0, -0.5, 1.0, -0.3)
pts.SetPoint(1, -3.0, 0.1, 0.2)
pts.SetPoint(2, 0.0, 0.0, 0.0)
pts.SetPoint(3, 1.2, -0.1, -0.2)
pts.SetPoint(4, 0.2, -3.0, 0.2)
pts.SetPoint(5, -4.2, -5.5, 0.7)
pts.SetPoint(6, 1.2, -7.3, -0.6)
pts.SetPoint(7, 4.2, -5.5, 0.7)
pts.SetPoint(8, 0.0, 0.0, -0.4)
pts.SetPoint(9, 0.0, 0.0, 0.8)

lines = vtkCellArray()
lines.InsertNextCell(4)
lines.InsertCellPoint(0)
lines.InsertCellPoint(2)
lines.InsertCellPoint(4)
lines.InsertCellPoint(6)
lines.InsertNextCell(2)
lines.InsertCellPoint(1)
lines.InsertCellPoint(2)
lines.InsertNextCell(2)
lines.InsertCellPoint(2)
lines.InsertCellPoint(3)
lines.InsertNextCell(2)
lines.InsertCellPoint(5)
lines.InsertCellPoint(6)
lines.InsertNextCell(2)
lines.InsertCellPoint(6)
lines.InsertCellPoint(7)
lines.InsertNextCell(2)
lines.InsertCellPoint(2)
lines.InsertCellPoint(8)
lines.InsertNextCell(2)
lines.InsertCellPoint(2)
lines.InsertCellPoint(9)

pd = vtkPolyData()
pd.SetPoints(pts)
pd.SetLines(lines)

layout2D = vtkGraphLayoutFilter()
layout2D.SetInputData(pd)
layout2D.SetMaxNumberOfIterations(100)
layout2D.ThreeDimensionalLayoutOff()
layout2D.AutomaticBoundsComputationOff()
layout2D.SetGraphBounds(-2.0, 0.0, -1.0, 1.0, -1.0, 1.0)

layout3D = vtkGraphLayoutFilter()
layout3D.SetInputData(pd)
layout3D.SetMaxNumberOfIterations(100)
layout3D.ThreeDimensionalLayoutOn()
layout3D.AutomaticBoundsComputationOff()
layout3D.SetGraphBounds(0.0, 2.0, -1.0, 1.0, -1.0, 1.0)

apf = vtkAppendPolyData()
apf.AddInputConnection(layout2D.GetOutputPort())
apf.AddInputConnection(layout3D.GetOutputPort())

tubes = vtkTubeFilter()
tubes.SetInputConnection(apf.GetOutputPort())
tubes.SetRadius(0.01)
tubes.SetNumberOfSides(6)

mapEdges = vtkPolyDataMapper()
mapEdges.SetInputConnection(tubes.GetOutputPort())

edgeActor = vtkActor()
edgeActor.SetMapper(mapEdges)
edgeActor.GetProperty().SetColor(GetRGBColor('peacock'))
edgeActor.GetProperty().SetSpecularColor(1, 1, 1)
edgeActor.GetProperty().SetSpecular(0.3)
edgeActor.GetProperty().SetSpecularPower(20)
edgeActor.GetProperty().SetAmbient(0.2)
edgeActor.GetProperty().SetDiffuse(0.8)

ball = vtkSphereSource()
ball.SetRadius(0.025)
ball.SetThetaResolution(12)
ball.SetPhiResolution(12)
balls = vtkGlyph3D()

balls.SetInputConnection(apf.GetOutputPort())
balls.SetSourceConnection(ball.GetOutputPort())

mapBalls = vtkPolyDataMapper()
mapBalls.SetInputConnection(balls.GetOutputPort())

ballActor = vtkActor()
ballActor.SetMapper(mapBalls)
ballActor.GetProperty().SetColor(GetRGBColor('hot_pink'))
ballActor.GetProperty().SetSpecularColor(1, 1, 1)
ballActor.GetProperty().SetSpecular(0.3)
ballActor.GetProperty().SetSpecularPower(20)
ballActor.GetProperty().SetAmbient(0.2)
ballActor.GetProperty().SetDiffuse(0.8)

ren1 = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(ren1)

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

ren1.AddActor(edgeActor)
ren1.AddActor(ballActor)
ren1.SetBackground(1, 1, 1)

renWin.SetSize(400, 250)

cam1 = ren1.GetActiveCamera()
cam1.SetClippingRange(3.55085, 6.01004)
cam1.SetFocalPoint(0.0427, -0.0149608, 0.0)
cam1.SetPosition(0.0427, -0.0149608, 4.63462)
cam1.SetViewUp(0, 1, 0)

iren.Initialize()
#iren.Start()