diff --git a/src/PythonicAPI.md b/src/PythonicAPI.md index 8f80fc0997ea983a032ac63187023ec8bb97f9ed..bfbb93cb836c980f5d616fd386daf9418c902dc9 100644 --- a/src/PythonicAPI.md +++ b/src/PythonicAPI.md @@ -312,8 +312,10 @@ This section includes ?vtkUnstructuredGrid?. [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. +[RandomGraphSource](/PythonicAPI/Graphs/RandomGraphSource) | Create a random graph. [SideBySideGraphs](/PythonicAPI/Graphs/SideBySideGraphs) | Display two graphs side by side. [ScaleVertices](/PythonicAPI/Graphs/ScaleVertices) | Size/scale vertices based on a data array. +[SelectedVerticesAndEdges](/PythonicAPI/Graphs/SelectedVerticesAndEdges) | [VisualizeDirectedGraph](/PythonicAPI/Graphs/VisualizeDirectedGraph) | Visualize a directed graph. ### Graph Conversions diff --git a/src/PythonicAPI/Graphs/RandomGraphSource.py b/src/PythonicAPI/Graphs/RandomGraphSource.py new file mode 100755 index 0000000000000000000000000000000000000000..b4b1f28bc96f589684b4eaeec70a85554314f886 --- /dev/null +++ b/src/PythonicAPI/Graphs/RandomGraphSource.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkInfovisCore import vtkRandomGraphSource +# noinspection PyUnresolvedReferences +from vtkmodules.vtkInfovisLayout import vtkForceDirectedLayoutStrategy +from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView + + +def main(): + colors = vtkNamedColors() + + # seed=123 ensures repeatable results for testing. Remove this off for real use. + random_graph_source = vtkRandomGraphSource(number_of_vertices=5, number_of_edges=4, seed=123) + random_graph_source.update() + + # force_directed = vtkForceDirectedLayoutStrategy() + + graph_layout_view = vtkGraphLayoutView() + graph_layout_view.AddRepresentationFromInput(random_graph_source.GetOutput()) + # If we create a layout object directly, just set the pointer through this method. + # graph_layout_view.SetLayoutStrategy(force_directed) + graph_layout_view.SetLayoutStrategyToForceDirected() + graph_layout_view.renderer.background = colors.GetColor3d('Navy') + graph_layout_view.renderer.background2 = colors.GetColor3d('MidnightBlue') + graph_layout_view.render_window.window_name = 'RandomGraphSource' + graph_layout_view.Render() + graph_layout_view.ResetCamera() + graph_layout_view.interactor.Start() + + +if __name__ == '__main__': + main() diff --git a/src/PythonicAPI/Graphs/SelectedVerticesAndEdges.md b/src/PythonicAPI/Graphs/SelectedVerticesAndEdges.md new file mode 100644 index 0000000000000000000000000000000000000000..bb077a107ece051080c609e47a66eba0c916bc2d --- /dev/null +++ b/src/PythonicAPI/Graphs/SelectedVerticesAndEdges.md @@ -0,0 +1,3 @@ +### Description + +* Contributed by Eric Monson diff --git a/src/PythonicAPI/Graphs/SelectedVerticesAndEdges.py b/src/PythonicAPI/Graphs/SelectedVerticesAndEdges.py new file mode 100755 index 0000000000000000000000000000000000000000..6f8234393055d03af3d518cd069553e8b96aa2c4 --- /dev/null +++ b/src/PythonicAPI/Graphs/SelectedVerticesAndEdges.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkInfovisCore import vtkRandomGraphSource +from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView + + +def selection_callback(caller, event): + # Use the shift key to select both nodes and edges. + # The nodes can either vertices or edges. + sel = caller.current_selection + node0 = sel.GetNode(0) + node0_field_type = node0.field_type + sel_list0 = caller.current_selection.GetNode(0).selection_list + node1 = sel.GetNode(1) + node1_field_type = node1.field_type + sel_list1 = caller.current_selection.GetNode(1).selection_list + + if sel_list0.number_of_tuples > 0: + # print('node0:') + print_field_type(node0_field_type) + for ii in range(sel_list0.number_of_tuples): + print(' ', sel_list0.GetValue(ii)) + + if sel_list1.number_of_tuples > 0: + # print('node1:') + print_field_type(node1_field_type) + for ii in range(sel_list1.number_of_tuples): + print(' ', sel_list1.GetValue(ii)) + + print('- - -') + + +def print_field_type(field_type): + if field_type == 3: + print('Vertex Id(s) Selected:') + elif field_type == 4: + print('Edge Id(s) Selected:') + else: + print('Unknown type:') + + +def main(): + source = vtkRandomGraphSource() + source.Update() + + view = vtkGraphLayoutView() + view.AddRepresentationFromInputConnection(source.GetOutputPort()) + + rep = view.GetRepresentation(0) + + # The vtkRenderedGraphRepresentation should already have a vtkAnnotationLink, + # so we just want to grab it and add an observer with our callback function + # attached + link = rep.GetAnnotationLink() + link.AddObserver('AnnotationChangedEvent', selection_callback) + + view.GetRenderWindow().SetSize(600, 600) + view.ResetCamera() + view.Render() + view.GetInteractor().Start() + + +if __name__ == '__main__': + main() diff --git a/src/Testing/Baseline/PythonicAPI/Graphs/TestRandomGraphSource.png b/src/Testing/Baseline/PythonicAPI/Graphs/TestRandomGraphSource.png new file mode 100644 index 0000000000000000000000000000000000000000..436ab69fb65c8822e44079753392a64412d97a61 --- /dev/null +++ b/src/Testing/Baseline/PythonicAPI/Graphs/TestRandomGraphSource.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b2edcf6a9019a439345e6c74fcda7392c4a713596a8bbbdfd3015835d9a96be8 +size 2296 diff --git a/src/Testing/Baseline/PythonicAPI/Graphs/TestSelectedVerticesAndEdges.png b/src/Testing/Baseline/PythonicAPI/Graphs/TestSelectedVerticesAndEdges.png new file mode 100644 index 0000000000000000000000000000000000000000..470a0f990fca9ed314bd9d9bceca9c84673b7548 --- /dev/null +++ b/src/Testing/Baseline/PythonicAPI/Graphs/TestSelectedVerticesAndEdges.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1869b98771788562a9fc20b198a4c27398a90c50d42983e6d0be0cf727f0ee8 +size 6043