def main(): g = vtk.vtkMutableUndirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() g.AddEdge(v1, v2) g.AddEdge(v1, v2) scales = vtk.vtkFloatArray() scales.SetNumberOfComponents(1) scales.SetName("Scales") scales.InsertNextValue(1.0) scales.InsertNextValue(4.0) g.GetVertexData().AddArray(scales) layoutView = vtk.vtkGraphLayoutView() layoutView.AddRepresentationFromInput(g) layoutView.ScaledGlyphsOn() layoutView.SetScalingArrayName("Scales") rGraph = vtk.vtkRenderedGraphRepresentation() gGlyph = vtk.vtkGraphToGlyphs() rGraph.SafeDownCast(layoutView.GetRepresentation()).SetGlyphType( gGlyph.CIRCLE) layoutView.ResetCamera() layoutView.Render() layoutView.GetInteractor().Start()
def main(): colors = vtk.vtkNamedColors() g = vtk.vtkMutableUndirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() g.AddEdge(v1, v2) print('Number of vertices:', g.GetNumberOfVertices()) print('Number of edges:', g.GetNumberOfEdges()) g.AddEdge(v1, v2) print('Number of vertices:', g.GetNumberOfVertices()) print('Number of edges:', g.GetNumberOfEdges()) force_directed = vtk.vtkForceDirectedLayoutStrategy() graph_layout_view = vtk.vtkGraphLayoutView() graph_layout_view.AddRepresentationFromInput(g) # 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.ResetCamera() graph_layout_view.GetRenderer().SetBackground(colors.GetColor3d('Navy')) graph_layout_view.GetRenderer().SetBackground2( colors.GetColor3d('MidnightBlue')) graph_layout_view.GetRenderWindow().SetWindowName('ConstructGraph') graph_layout_view.Render() graph_layout_view.GetInteractor().Start()
def main(): colors = vtk.vtkNamedColors() g = vtk.vtkMutableUndirectedGraph() # Create 3 vertices v1 = g.AddVertex() v2 = g.AddVertex() v3 = g.AddVertex() # Create a fully connected graph g.AddEdge(v1, v2) g.AddEdge(v2, v3) g.AddEdge(v1, v3) # Create the edge weight array weights = vtk.vtkDoubleArray() weights.SetNumberOfComponents(1) weights.SetName('Weights') # Set the edge weights weights.InsertNextValue(1.0) weights.InsertNextValue(1.0) weights.InsertNextValue(2.0) # Create an array for the vertex labels vertexIDs = vtk.vtkIntArray() vertexIDs.SetNumberOfComponents(1) vertexIDs.SetName('VertexIDs') # Set the vertex labels vertexIDs.InsertNextValue(0) vertexIDs.InsertNextValue(1) vertexIDs.InsertNextValue(2) # Add the edge weight array to the graph g.GetEdgeData().AddArray(weights) g.GetVertexData().AddArray(vertexIDs) circularLayoutStrategy = vtk.vtkCircularLayoutStrategy() graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(g) graphLayoutView.SetLayoutStrategy(circularLayoutStrategy) graphLayoutView.SetVertexLabelVisibility(1) graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.SetEdgeLabelArrayName('Weights') # default is 'labels' graphLayoutView.SetVertexLabelArrayName('VertexIDs') # default is 'labels' graphLayoutView.GetRepresentation().GetVertexLabelTextProperty().SetColor( colors.GetColor3d('Yellow')) graphLayoutView.GetRepresentation().GetEdgeLabelTextProperty().SetColor( colors.GetColor3d('Lime')) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetInteractor().Start()
def main(): colors = vtk.vtkNamedColors() # Create a graph g = vtk.vtkMutableUndirectedGraph() # Add 4 vertices to the graph v1 = g.AddVertex() v2 = g.AddVertex() v3 = g.AddVertex() v4 = g.AddVertex() # Add 3 edges to the graph g.AddEdge(v1, v2) g.AddEdge(v1, v3) g.AddEdge(v1, v4) # Create 4 points - one for each vertex points = vtk.vtkPoints() points.InsertNextPoint(0.0, 0.0, 0.0) points.InsertNextPoint(1.0, 0.0, 0.0) points.InsertNextPoint(0.0, 1.0, 0.0) points.InsertNextPoint(0.0, 0.0, 1.0) # Add the coordinates of the points to the graph g.SetPoints(points) # Convert the graph to a polydata graphToPolyData = vtk.vtkGraphToPolyData() graphToPolyData.SetInputData(g) graphToPolyData.Update() # Create a mapper and actor mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(graphToPolyData.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) # Create a renderer, render window, and interactor renderer = vtk.vtkRenderer() renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) # Add the actor to the scene renderer.AddActor(actor) renderer.SetBackground( colors.GetColor3d("green")) # Background color green # Render and interact renderWindow.Render() renderWindowInteractor.Start()
def main(): colors = vtk.vtkNamedColors() g = vtk.vtkMutableUndirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() g.AddEdge(v1, v2) g.AddEdge(v1, v2) scales = vtk.vtkFloatArray() scales.SetNumberOfComponents(1) scales.SetName('Scales') scales.InsertNextValue(2.0) scales.InsertNextValue(5.0) # Add the scale array to the graph g.GetVertexData().AddArray(scales) # Create the color array vertexColors = vtk.vtkIntArray() vertexColors.SetNumberOfComponents(1) vertexColors.SetName('Color') lookupTable = vtk.vtkLookupTable() lookupTable.SetNumberOfTableValues(2) lookupTable.SetTableValue(0, colors.GetColor4d('Yellow')) lookupTable.SetTableValue(1, colors.GetColor4d('Lime')) lookupTable.Build() vertexColors.InsertNextValue(0) vertexColors.InsertNextValue(1) # Add the color array to the graph g.GetVertexData().AddArray(vertexColors) theme = vtk.vtkViewTheme() theme.SetPointLookupTable(lookupTable) layoutView = vtk.vtkGraphLayoutView() layoutView.AddRepresentationFromInput(g) layoutView.ApplyViewTheme(theme) layoutView.ScaledGlyphsOn() layoutView.SetScalingArrayName('Scales') layoutView.SetVertexColorArrayName('Color') layoutView.ColorVerticesOn() rGraph = vtk.vtkRenderedGraphRepresentation() gGlyph = vtk.vtkGraphToGlyphs() rGraph.SafeDownCast(layoutView.GetRepresentation()).SetGlyphType(gGlyph.CIRCLE) layoutView.ResetCamera() layoutView.Render() layoutView.GetInteractor().Start()
def main(): g = vtk.vtkMutableUndirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() g.AddEdge(v1, v2) print('Number of vertices:', g.GetNumberOfVertices()) print('Number of edges:', g.GetNumberOfEdges()) g.AddEdge(v1, v2) print('Number of vertices:', g.GetNumberOfVertices()) print('Number of edges:', g.GetNumberOfEdges()) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(g) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetInteractor().Start()
(see below). It creates edges which maintain the proper association and any metadata, the caveat is that all edges are given all metadata attributes with default values. So if an edge has an integer 'distance' attribute, and another does not - the non-distanced edge will have a distance of 0. This follows suit for all of pythons defaults, bool(), str(), float(), etc. As such it requires that all keys have the same type. """ if input.is_directed(): output = vtk.vtkMutableDirectedGraph() else: output = vtk.vtkMutableUndirectedGraph() nodes = input.nodes(data=True) edges = input.edges(data=True) edge_field_types = {} node_field_types = {} # Find out fields and types for (_, data) in nodes: for key in data.keys(): data_type = type(data[key]) # Assert that every node which has key has the same type if key in node_field_types and data_type != node_field_types[key]: raise Exception('Node has heterogeneous types for key %s' % key)
#!/usr/bin/env python ''' Online example from : http://www.vtk.org/Wiki/VTK/Examples/Python/Graphs/EdgeWeights Going to heavly comment out the steps to understand how to edit this into what we need to make other graphs ''' import vtk # Manditory on all python VTK import random g = vtk.vtkMutableUndirectedGraph() # Sets up empty data structure # Create a start node init_v = g.AddVertex() # Create this list of leaves + append inital leaf_list = [] leaf_list.append(init_v) # Randomly Pick a Termination number total_nodes = random.randint(1,1000) created_nodes = 0 # While don't exede the limit + run out of leafs while(created_nodes < total_nodes and len(leaf_list) != 0):
def main(): # Create the first graph g0 = vtk.vtkMutableUndirectedGraph() v1 = g0.AddVertex() v2 = g0.AddVertex() v3 = g0.AddVertex() g0.AddEdge(v1, v2) g0.AddEdge(v2, v3) g0.AddEdge(v1, v3) # Create points points = vtk.vtkPoints() points.InsertNextPoint(0.0, 0.0, 0.0) points.InsertNextPoint(1.0, 0.0, 0.0) points.InsertNextPoint(0.0, 1.0, 0.0) # Add the coordinates of the points to the graph g0.SetPoints(points) # Create the second graph g1 = vtk.vtkMutableUndirectedGraph() v1 = g1.AddVertex() v2 = g1.AddVertex() g1.AddEdge(v1, v2) # Create points points = vtk.vtkPoints() points.InsertNextPoint(0.0, 0.0, 0.0) points.InsertNextPoint(1.0, 0.0, 0.0) # Add the coordinates of the points to the graph g1.SetPoints(points) # There will be one render window renderWindow = vtk.vtkRenderWindow() renderWindow.SetSize(600, 300) renderWindowInteractor = vtk.vtkRenderWindowInteractor() # Define viewport ranges # (xmin, ymin, xmax, ymax) leftViewport = [0.0, 0.0, 0.5, 1.0] rightViewport = [0.5, 0.0, 1.0, 1.0] graphLayoutView0 = vtk.vtkGraphLayoutView() graphLayoutView0.SetRenderWindow(renderWindow) graphLayoutView0.SetInteractor(renderWindowInteractor) graphLayoutView0.GetRenderer().SetViewport(leftViewport) graphLayoutView0.AddRepresentationFromInput(g0) graphLayoutView0.ResetCamera() graphLayoutView0.Render() graphLayoutView1 = vtk.vtkGraphLayoutView() graphLayoutView1.SetRenderWindow(renderWindow) graphLayoutView1.SetInteractor(renderWindowInteractor) graphLayoutView1.GetRenderer().SetViewport(rightViewport) graphLayoutView1.AddRepresentationFromInput(g1) graphLayoutView1.ResetCamera() graphLayoutView1.Render() #graphLayoutView0.GetInteractor().Start() renderWindowInteractor.Start()
def main(): colors = vtk.vtkNamedColors() # Create the first graph g0 = vtk.vtkMutableUndirectedGraph() v1 = g0.AddVertex() v2 = g0.AddVertex() v3 = g0.AddVertex() g0.AddEdge(v1, v2) g0.AddEdge(v2, v3) g0.AddEdge(v1, v3) # Create points points = vtk.vtkPoints() points.InsertNextPoint(0.0, 0.0, 0.0) points.InsertNextPoint(1.0, 0.0, 0.0) points.InsertNextPoint(0.0, 1.0, 0.0) # Add the coordinates of the points to the graph g0.SetPoints(points) # Create the second graph g1 = vtk.vtkMutableUndirectedGraph() v1 = g1.AddVertex() v2 = g1.AddVertex() g1.AddEdge(v1, v2) # Create points points = vtk.vtkPoints() points.InsertNextPoint(0.0, 0.0, 0.0) points.InsertNextPoint(1.0, 0.0, 0.0) # Add the coordinates of the points to the graph g1.SetPoints(points) # There will be one render window ren_win = vtk.vtkRenderWindow() ren_win.SetSize(600, 300) ren_win.SetWindowName('SideBySideGraphs') iren = vtk.vtkRenderWindowInteractor() # Define viewport ranges # (xmin, ymin, xmax, ymax) left_viewport = [0.0, 0.0, 0.5, 1.0] right_viewport = [0.5, 0.0, 1.0, 1.0] graph_layout_view0 = vtk.vtkGraphLayoutView() graph_layout_view0.SetRenderWindow(ren_win) graph_layout_view0.SetInteractor(iren) graph_layout_view0.GetRenderer().SetViewport(left_viewport) graph_layout_view0.AddRepresentationFromInput(g0) # If we create a layout object directly, just set the pointer through this method. # graph_layout_view0.SetLayoutStrategy(force_directed) graph_layout_view0.SetLayoutStrategyToForceDirected() graph_layout_view0.GetRenderer().SetBackground(colors.GetColor3d('Navy')) graph_layout_view0.GetRenderer().SetBackground2( colors.GetColor3d('MidnightBlue')) graph_layout_view0.Render() graph_layout_view0.ResetCamera() graph_layout_view1 = vtk.vtkGraphLayoutView() graph_layout_view1.SetRenderWindow(ren_win) graph_layout_view1.SetInteractor(iren) graph_layout_view1.GetRenderer().SetViewport(right_viewport) graph_layout_view1.AddRepresentationFromInput(g0) # If we create a layout object directly, just set the pointer through this method. # graph_layout_view1.SetLayoutStrategy(force_directed) graph_layout_view1.SetLayoutStrategyToForceDirected() graph_layout_view1.AddRepresentationFromInput(g1) graph_layout_view1.GetRenderer().SetBackground( colors.GetColor3d('DarkGreen')) graph_layout_view1.GetRenderer().SetBackground2( colors.GetColor3d('ForestGreen')) graph_layout_view1.Render() graph_layout_view1.ResetCamera() # graph_layout_view0.GetInteractor().Start() iren.Start()
if not flythrough_flag: num_samples = int(args["samp_end_time"]) / int(args["samp_interval"]) print("reading router data...") router_data = read_sim_data(args["routerfile"], "router", len(terminals), num_samples, int(args["samp_interval"])) #router_data = data_check(router_data, len(terminals), G.number_of_nodes(), num_samples) print("done\nreading terminal data...") terminal_data = read_sim_data(args["termfile"], "terminal", len(terminals), num_samples, int(args["samp_interval"])) #terminal_data = data_check(terminal_data, 0, len(terminals), num_samples) print("done\n") else: flythrough_flag = True # using the vtkGraph approach graph = vtk.vtkMutableUndirectedGraph() graph.SetNumberOfVertices(G.number_of_nodes()) # now terminal_coords contains the correct (2d) coordinates for all terminals and routers # input coords all terminals/routers if args["network"] == "slimfly": points = sfly_set_vtk_points_array(all_coords, G, routers, terminals) graph.SetPoints(points) term_edges, local_edges, global_edges = sfly_split_edges( G, routers, router_group_size) for v1, v2 in term_edges: graph.LazyAddEdge(int(v1), int(v2)) for v1, v2 in local_edges: graph.LazyAddEdge(int(v1), int(v2))
outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetScalarVisibility(False) outlineMapper.SetImmediateModeRendering(True) outlineMapper.SetInputConnection(vertGlyph.GetOutputPort()) outlineActor = vtk.vtkActor() outlineActor.SetMapper(outlineMapper) outlineActor.PickableOff() outlineActor.GetProperty().SetRepresentationToWireframe() outlineActor.GetProperty().SetPointSize(vertActor.GetProperty().GetPointSize()+2) outlineActor.GetProperty().SetColor(theme.GetOutlineColor()) outlineActor.GetProperty().SetOpacity(theme.GetPointOpacity()) outlineActor.SetPosition(0, 0, -0.001) # ---------- # Current ExtIdxs MS Graph Node nodeGraph = vtk.vtkMutableUndirectedGraph() nodeGraph.AddVertex() currIdx = ExtIdxs[basisNum]-1 # Matlab 1-based and Numpy 0-based indexing currPt = VN.numpy_to_vtk(ptsMatrix[currIdx,:]) nodeGraph.GetPoints().SetData(currPt) ntheme = vtk.vtkViewTheme() ntheme.SetPointSize(13) ntheme.SetOutlineColor(0.2, 0.2, 0.2) ntheme.SetPointColor(0.1, 0.1, 0.1) ntheme.SetPointOpacity(1.0) nodeMapper = vtk.vtkGraphMapper() nodeMapper.SetInput(nodeGraph) nodeMapper.SetColorEdges(False) nodeMapper.ApplyViewTheme(ntheme)
def main(): colors = vtk.vtkNamedColors() g = vtk.vtkMutableUndirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() g.AddEdge(v1, v2) g.AddEdge(v1, v2) scales = vtk.vtkFloatArray() scales.SetNumberOfComponents(1) scales.SetName('Scales') scales.InsertNextValue(2.0) scales.InsertNextValue(5.0) # Add the scale array to the graph g.GetVertexData().AddArray(scales) # Create the color array vertexColors = vtk.vtkIntArray() vertexColors.SetNumberOfComponents(1) vertexColors.SetName('Color') lookupTable = vtk.vtkLookupTable() lookupTable.SetNumberOfTableValues(2) lookupTable.SetTableValue(0, colors.GetColor4d('Yellow')) lookupTable.SetTableValue(1, colors.GetColor4d('Lime')) lookupTable.Build() vertexColors.InsertNextValue(0) vertexColors.InsertNextValue(1) # Add the color array to the graph g.GetVertexData().AddArray(vertexColors) theme = vtk.vtkViewTheme() theme.SetPointLookupTable(lookupTable) force_directed = vtk.vtkForceDirectedLayoutStrategy() layout_view = vtk.vtkGraphLayoutView() # If we create a layout object directly, just set the pointer through this method. # graph_layout_view.SetLayoutStrategy(force_directed) layout_view.SetLayoutStrategyToForceDirected() layout_view.AddRepresentationFromInput(g) layout_view.ApplyViewTheme(theme) layout_view.ScaledGlyphsOn() layout_view.SetScalingArrayName('Scales') layout_view.SetVertexColorArrayName('Color') layout_view.ColorVerticesOn() rGraph = vtk.vtkRenderedGraphRepresentation() gGlyph = vtk.vtkGraphToGlyphs() rGraph.SafeDownCast(layout_view.GetRepresentation()).SetGlyphType( gGlyph.CIRCLE) layout_view.GetRenderer().SetBackground(colors.GetColor3d('Navy')) layout_view.GetRenderer().SetBackground2(colors.GetColor3d('MidnightBlue')) layout_view.GetRenderWindow().SetWindowName('ScaleVertices') layout_view.Render() layout_view.ResetCamera() layout_view.GetInteractor().Start()