Skip to content
Snippets Groups Projects
Commit a4c20bea authored by Andrew Maclean's avatar Andrew Maclean
Browse files

Added ConstructTree, CreateTree

parent ef6eca28
No related branches found
No related tags found
1 merge request!354All classes in Python are now represented in PythonicAPI
......@@ -308,6 +308,8 @@ This section includes ?vtkUnstructuredGrid?.
| Example Name | Description | Image |
| -------------- | ------------- | ------- |
[ConstructTree](/PythonicAPI/Graphs/ConstructTree) | Construct a tree.
[CreateTree](/PythonicAPI/Graphs/CreateTree) | Create a tree and label the vertices and edges.
[GraphToPolyData](/PythonicAPI/Graphs/GraphToPolyData) | Convert a graph to a PolyData.
[LabelVerticesAndEdges](/PythonicAPI/Graphs/LabelVerticesAndEdges) | Label vertices and edges.
[SideBySideGraphs](/PythonicAPI/Graphs/SideBySideGraphs) | Display two graphs side by side.
......
#!/usr/bin/env python3
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonDataModel import (
vtkMutableDirectedGraph,
vtkTree
)
from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView
def main():
graph = vtkMutableDirectedGraph()
v1 = graph.AddVertex()
v2 = graph.AddChild(v1)
graph.AddChild(v1)
graph.AddChild(v2)
# equivalent to:
# V1 = g.AddVertex()
# V2 = g.AddVertex()
# V3 = g.AddVertex()
# V4 = g.AddVertex()
# g.AddEdge ( V1, V2 )
# g.AddEdge ( V1, V3 )
# g.AddEdge ( V2, V4 )
tree = vtkTree()
success = tree.CheckedShallowCopy(graph)
print('Success?', success)
tree_layout_view = vtkGraphLayoutView(layout_strategy='Tree')
tree_layout_view.AddRepresentationFromInput(tree)
tree_layout_view.ResetCamera()
tree_layout_view.Render()
tree_layout_view.interactor.Start()
if __name__ == '__main__':
main()
### Description
We create the tree, and label the vertices and edges.
!!! info
This is an update of the original example found in `vtk/Examples/Infovis/Cxx/CreateTree.cxx`.
#!/usr/bin/env python3
# This example creates a tree and labels the vertices and edges.
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonCore import vtkStringArray
from vtkmodules.vtkCommonDataModel import (
vtkMutableDirectedGraph,
vtkTree
)
from vtkmodules.vtkViewsCore import vtkViewTheme
from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView
def main():
graph = vtkMutableDirectedGraph()
a = graph.AddVertex()
b = graph.AddChild(a)
c = graph.AddChild(a)
d = graph.AddChild(b)
e = graph.AddChild(c)
f = graph.AddChild(c)
vertex_labels = vtkStringArray()
vertex_labels.name = 'VertexLabel'
vertex_labels.InsertValue(a, 'a')
vertex_labels.InsertValue(b, 'b')
vertex_labels.InsertValue(c, 'c')
vertex_labels.InsertValue(d, 'd')
vertex_labels.InsertValue(e, 'e')
vertex_labels.InsertValue(f, 'f')
graph.vertex_data.AddArray(vertex_labels)
edge_labels = vtkStringArray()
edge_labels.name = 'EdgeLabel'
edge_labels.InsertValue(graph.GetEdgeId(a, b), 'a -> b')
edge_labels.InsertValue(graph.GetEdgeId(a, c), 'a -> c')
edge_labels.InsertValue(graph.GetEdgeId(b, d), 'b -> d')
edge_labels.InsertValue(graph.GetEdgeId(c, e), 'c -> e')
edge_labels.InsertValue(graph.GetEdgeId(c, f), 'c -> f')
graph.edge_data.AddArray(edge_labels)
tree = vtkTree()
valid_tree = tree.CheckedShallowCopy(graph)
if not valid_tree:
print('Invalid tree')
return
view = vtkGraphLayoutView(vertex_color_array_name='VertexDegree', color_vertices=True,
vertex_label_array_name='VertexLabel', vertex_label_visibility=True,
edge_label_array_name='EdgeLabel', edge_label_visibility=True,
layout_strategy='Tree')
view.SetRepresentationFromInput(tree)
# Apply a theme to the views
theme = vtkViewTheme()
view.ApplyViewTheme(theme.CreateMellowTheme())
view.ResetCamera()
view.render_window.size = (600, 600)
view.render_window.window_name = 'CreateTree'
view.render_window.Render()
view.interactor.Initialize()
view.interactor.Start()
if __name__ == '__main__':
main()
src/Testing/Baseline/PythonicAPI/Graphs/TestConstructTree.png

129 B

src/Testing/Baseline/PythonicAPI/Graphs/TestCreateTree.png

129 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment