def main(): # Create a graph graph = vtk.vtkMutableDirectedGraph() v1 = graph.AddVertex() v2 = graph.AddVertex() graph.AddEdge(v1, v2) # 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) # Add the array to the graph graph.GetVertexData().AddArray(vertexIDs) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(graph) graphLayoutView.SetVertexLabelVisibility(1) rGraph = vtk.vtkRenderedGraphRepresentation() rGraph.SafeDownCast(graphLayoutView.GetRepresentation() ).GetVertexLabelTextProperty().SetColor(1, 0, 0) graphLayoutView.SetLayoutStrategyToSimple2D() graphLayoutView.SetVertexLabelArrayName("VertexIDs") graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetInteractor().Start()
def main(): graph = vtk.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 = vtk.vtkTree() success = tree.CheckedShallowCopy(graph) print('Success?', success) #std::cout << "Success? " << success << std::endl treeLayoutView = vtk.vtkGraphLayoutView() treeLayoutView.AddRepresentationFromInput(tree) treeLayoutView.SetLayoutStrategyToTree() treeLayoutView.ResetCamera() treeLayoutView.Render() treeLayoutView.GetInteractor().Start()
def main(): g = vtk.vtkMutableDirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() v3 = g.AddVertex() g.AddEdge(v1, v2) g.AddEdge(v2, v3) g.AddEdge(v3, v1) # Do layout manually before handing graph to the view. # This allows us to know the positions of edge arrows. graphLayoutView = vtk.vtkGraphLayoutView() layout = vtk.vtkGraphLayout() strategy = vtk.vtkSimple2DLayoutStrategy() layout.SetInputData(g) layout.SetLayoutStrategy(strategy) # Tell the view to use the vertex layout we provide graphLayoutView.SetLayoutStrategyToPassThrough() # The arrows will be positioned on a straight line between two # vertices so tell the view not to draw arcs for parallel edges graphLayoutView.SetEdgeLayoutStrategyToPassThrough() # Add the graph to the view. This will render vertices and edges, # but not edge arrows. graphLayoutView.AddRepresentationFromInputConnection(layout.GetOutputPort()) # Manually create an actor containing the glyphed arrows. graphToPoly = vtk.vtkGraphToPolyData() graphToPoly.SetInputConnection(layout.GetOutputPort()) graphToPoly.EdgeGlyphOutputOn() # Set the position (0: edge start, 1: edge end) where # the edge arrows should go. graphToPoly.SetEdgeGlyphPosition(0.98) # Make a simple edge arrow for glyphing. arrowSource = vtk.vtkGlyphSource2D() arrowSource.SetGlyphTypeToEdgeArrow() arrowSource.SetScale(0.1) arrowSource.Update() # Use Glyph3D to repeat the glyph on all edges. arrowGlyph = vtk.vtkGlyph3D() arrowGlyph.SetInputConnection(0, graphToPoly.GetOutputPort(1)) arrowGlyph.SetInputConnection(1, arrowSource.GetOutputPort()) # Add the edge arrow actor to the view. arrowMapper = vtk.vtkPolyDataMapper() arrowMapper.SetInputConnection(arrowGlyph.GetOutputPort()) arrowActor = vtk.vtkActor() arrowActor.SetMapper(arrowMapper) graphLayoutView.GetRenderer().AddActor(arrowActor) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetInteractor().Start()
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 createGraph(points): g = vtk.vtkMutableDirectedGraph() vertexes = [] alreadyConnected = [] for i in range(0, points.GetNumberOfPoints()): vertexes.append(g.AddVertex()) alreadyConnected.append(i) for i in range(0, points.GetNumberOfPoints()): index = -1 distance = float("inf") distanceOrder = 0 record = float("inf") for j in range(0, points.GetNumberOfPoints()): distance = math.sqrt( vtk.vtkMath.Distance2BetweenPoints(points.GetPoint(i), points.GetPoint(j))) if (distance > 0 and distance < record and alreadyConnected[j] != i and alreadyConnected[alreadyConnected[j]] != i): record = distance index = j elif (distance > 0 and distance < record): distanceOrder += 1 if (index != -1 and distanceOrder < 2): alreadyConnected[i] = index g.AddGraphEdge(vertexes[i], vertexes[index]) for i in range(0, points.GetNumberOfPoints()): index = -1 distance = float("inf") distanceOrder = 0 record = float("inf") for j in range(0, points.GetNumberOfPoints()): distance = math.sqrt( vtk.vtkMath.Distance2BetweenPoints(points.GetPoint(i), points.GetPoint(j))) if (distance > 0 and distance < record and alreadyConnected[j] != i and alreadyConnected[i] != j and alreadyConnected[alreadyConnected[j]] != alreadyConnected[alreadyConnected[i]]): record = distance index = j elif (distance > 0 and distance < record): distanceOrder += 1 if (index != -1 and distanceOrder < 2): alreadyConnected[i] = index g.AddGraphEdge(vertexes[i], vertexes[index]) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(g) graphLayoutView.SetLayoutStrategy("Simple 2D") graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetLayoutStrategy().SetRandomSeed(0) graphLayoutView.GetInteractor().Start()
def main(): colors = vtk.vtkNamedColors() random_graph_source = vtk.vtkRandomGraphSource() random_graph_source.SetNumberOfVertices(5) random_graph_source.SetNumberOfEdges(4) # This ensures repeatable results for testing. Turn this off for real use. random_graph_source.SetSeed(123) random_graph_source.Update() force_directed = vtk.vtkForceDirectedLayoutStrategy() graph_layout_view = vtk.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.GetRenderer().SetBackground(colors.GetColor3d('Navy')) graph_layout_view.GetRenderer().SetBackground2( colors.GetColor3d('MidnightBlue')) graph_layout_view.GetRenderWindow().SetWindowName('RandomGraphSource') graph_layout_view.Render() graph_layout_view.ResetCamera() graph_layout_view.GetInteractor().Start()
def visualize_graph(self): """shows a visualization of the graph""" self._graph.GetVertexData().AddArray(self._labels) self._graph.GetEdgeData().AddArray(self._weights) colors = vtk.vtkUnsignedCharArray() colors.SetNumberOfComponents(1) colors.SetName('Colors') types = int(245 / len(self._color_dict)) for c in self._colors: colors.InsertNextValue(int(c * types)) self._graph.GetVertexData().AddArray(colors) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(self._graph) graphLayoutView.SetLayoutStrategy(vtk.vtkSpanTreeLayoutStrategy()) graphLayoutView.GetLayoutStrategy().SetEdgeWeightField("Weights") graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) graphLayoutView.GetRenderer().GetActiveCamera().ParallelProjectionOff() graphLayoutView.SetEdgeLabelArrayName("Weights") graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.SetVertexLabelArrayName('labels') graphLayoutView.SetVertexLabelVisibility(1) graphLayoutView.SetVertexColorArrayName('Colors') graphLayoutView.SetColorVertices(1) graphLayoutView.SetInteractorStyle(MouseAndKeysInteractor(graphLayoutView)) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.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 draw2Dgraph(source): """ draws the 2D graph from the given data. """ # Do layout manually before handing graph to the view. graphLayoutView = vtk.vtkGraphLayoutView() layout = vtk.vtkGraphLayout() strategy = vtk.vtkSimple2DLayoutStrategy() layout.SetInputData(source) layout.SetLayoutStrategy(strategy) # Tell the view to use the vertex layout we provide graphLayoutView.SetLayoutStrategyToPassThrough() # The arrows will be positioned on a straight line between two # vertices so tell the view not to draw arcs for parallel edges graphLayoutView.SetEdgeLayoutStrategyToPassThrough() # Add the graph to the view. This will render vertices and edges, # but not edge arrows. graphLayoutView.AddRepresentationFromInputConnection( layout.GetOutputPort()) # Manually create an actor containing the glyphed arrows. graphToPoly = vtk.vtkGraphToPolyData() graphToPoly.SetInputConnection(layout.GetOutputPort()) graphToPoly.EdgeGlyphOutputOn() # Set the position graphToPoly.SetEdgeGlyphPosition(0.98) # Make a simple edge arrow for glyphing. arrowSource = vtk.vtkGlyphSource2D() arrowSource.SetGlyphTypeToEdgeArrow() arrowSource.SetScale(1) arrowSource.Update() # Use Glyph3D to repeat the glyph on all edges. arrowGlyph = vtk.vtkGlyph3D() arrowGlyph.SetInputConnection(0, graphToPoly.GetOutputPort(1)) arrowGlyph.SetInputConnection(1, arrowSource.GetOutputPort()) # Add the edge arrow actor to the view. arrowMapper = vtk.vtkPolyDataMapper() arrowMapper.SetInputConnection(arrowGlyph.GetOutputPort()) arrowActor = vtk.vtkActor() arrowActor.SetMapper(arrowMapper) graphLayoutView.GetRenderer().AddActor(arrowActor) # Add edge weights & vertex labels graphLayoutView.SetVertexLabelVisibility(1) graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.SetVertexLabelArrayName("VertexLabels") graphLayoutView.SetEdgeLabelArrayName("EdgeWeights") return graphLayoutView
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 graph = vtk.vtkMutableDirectedGraph() v1 = graph.AddVertex() v2 = graph.AddVertex() v3 = graph.AddVertex() graph.AddEdge(v1, v2) graph.AddEdge(v2, v3) # Manually set the position of the vertices points = vtk.vtkPoints() points.InsertNextPoint(0, 0, 0) points.InsertNextPoint(1, 0, 0) points.InsertNextPoint(2, 0, 0) graph.SetPoints(points) # Create the color array vertexColors = vtk.vtkIntArray() vertexColors.SetNumberOfComponents(1) vertexColors.SetName('Color') lookupTable = vtk.vtkLookupTable() lookupTable.SetNumberOfTableValues(3) lookupTable.SetTableValue(0, colors.GetColor4d('Red')) lookupTable.SetTableValue(1, colors.GetColor4d('White')) lookupTable.SetTableValue(2, colors.GetColor4d('Lime')) lookupTable.Build() vertexColors.InsertNextValue(0) vertexColors.InsertNextValue(1) vertexColors.InsertNextValue(2) # Add the color array to the graph graph.GetVertexData().AddArray(vertexColors) # Visualize graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(graph) graphLayoutView.SetLayoutStrategyToPassThrough() graphLayoutView.SetVertexColorArrayName('Color') graphLayoutView.ColorVerticesOn() theme = vtk.vtkViewTheme() theme.SetPointLookupTable(lookupTable) graphLayoutView.ApplyViewTheme(theme) graphLayoutView.ResetCamera() graphLayoutView.GetInteractor().Initialize() graphLayoutView.GetRenderer().GetActiveCamera().Zoom(0.8) graphLayoutView.GetInteractor().Start()
def main(): graph = vtk.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 = vtk.vtkStringArray() vertex_labels.SetName('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.GetVertexData().AddArray(vertex_labels) edge_labels = vtk.vtkStringArray() edge_labels.SetName('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.GetEdgeData().AddArray(edge_labels) tree = vtk.vtkTree() valid_tree = tree.CheckedShallowCopy(graph) if not valid_tree: print('Invalid tree') return view = vtk.vtkGraphLayoutView() view.SetRepresentationFromInput(tree) # Apply a theme to the views theme = vtk.vtkViewTheme() view.ApplyViewTheme(theme.CreateMellowTheme()) view.SetVertexColorArrayName('VertexDegree') view.SetColorVertices(True) view.SetVertexLabelArrayName('VertexLabel') view.SetVertexLabelVisibility(True) view.SetEdgeLabelArrayName('EdgeLabel') view.SetEdgeLabelVisibility(True) view.SetLayoutStrategyToTree() view.ResetCamera() view.GetRenderWindow().SetSize(600, 600) view.GetRenderWindow().SetWindowName('CreateTree') view.GetRenderWindow().Render() view.GetInteractor().Initialize() view.GetInteractor().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(): randomGraphSource = vtk.vtkRandomGraphSource() randomGraphSource.SetNumberOfVertices(5) randomGraphSource.SetNumberOfEdges(4) # This ensures repeatable results for testing. Turn this off for real use. randomGraphSource.SetSeed(0) randomGraphSource.Update() graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(randomGraphSource.GetOutput()) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetInteractor().Start()
def main(): colors = vtk.vtkNamedColors() graph = vtk.vtkMutableDirectedGraph() # Create a graph v1 = graph.AddVertex() v2 = graph.AddVertex() v3 = graph.AddVertex() graph.AddGraphEdge(v1, v2) graph.AddGraphEdge(v2, v3) # Create the color array edgeColors = vtk.vtkIntArray() edgeColors.SetNumberOfComponents(1) edgeColors.SetName('Color') lookupTable = vtk.vtkLookupTable() lookupTable.SetNumberOfTableValues(2) lookupTable.SetTableValue(0, colors.GetColor4d('Red')) lookupTable.SetTableValue(1, colors.GetColor4d('Lime')) lookupTable.Build() edgeColors.InsertNextValue(0) edgeColors.InsertNextValue(1) # Add the color array to the graph graph.GetEdgeData().AddArray(edgeColors) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(graph) graphLayoutView.SetLayoutStrategy('Simple 2D') graphLayoutView.GetLayoutStrategy().SetEdgeWeightField('Graphs') graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) graphLayoutView.SetEdgeColorArrayName('Color') graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.ColorEdgesOn() theme = vtk.vtkViewTheme() theme.SetCellLookupTable(lookupTable) graphLayoutView.ApplyViewTheme(theme) graphLayoutView.ResetCamera() graphLayoutView.GetRenderer().GetActiveCamera().Zoom(0.8) graphLayoutView.Render() graphLayoutView.GetLayoutStrategy().SetRandomSeed(0) graphLayoutView.GetInteractor().Initialize() graphLayoutView.GetInteractor().Start()
def main(): filename = get_program_parameters() reader = vtk.vtkXGMLReader() reader.SetFileName(filename) reader.Update() g = reader.GetOutput() graph_layout_view = vtk.vtkGraphLayoutView() graph_layout_view.AddRepresentationFromInput(g) graph_layout_view.SetLayoutStrategy("Simple 2D") graph_layout_view.ResetCamera() graph_layout_view.Render() graph_layout_view.GetInteractor().Start()
def main(): graph = vtk.vtkMutableDirectedGraph() # Create a graph v1 = graph.AddVertex() v2 = graph.AddVertex() v3 = graph.AddVertex() graph.AddGraphEdge(v1, v2) graph.AddGraphEdge(v2, v3) # Create the color array edgeColors = vtk.vtkIntArray() edgeColors.SetNumberOfComponents(1) edgeColors.SetName("Color") lookupTable = vtk.vtkLookupTable() lookupTable.SetNumberOfTableValues(2) lookupTable.SetTableValue(0, 1.0, 0.0, 0.0) # red lookupTable.SetTableValue(1, 0.0, 1.0, 0.0) # green lookupTable.Build() edgeColors.InsertNextValue(0) edgeColors.InsertNextValue(1) # Add the color array to the graph graph.GetEdgeData().AddArray(edgeColors) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(graph) graphLayoutView.SetLayoutStrategy("Simple 2D") graphLayoutView.GetLayoutStrategy().SetEdgeWeightField("Graphs") graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) graphLayoutView.SetEdgeColorArrayName("Color") graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.ColorEdgesOn() theme = vtk.vtkViewTheme() theme.SetCellLookupTable(lookupTable) graphLayoutView.ApplyViewTheme(theme) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetLayoutStrategy().SetRandomSeed(0) graphLayoutView.GetInteractor().Initialize() graphLayoutView.GetInteractor().Start()
def main(): g = vtk.vtkMutableDirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() g.AddGraphEdge(v1, v2) g.AddGraphEdge(v1, v2) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(g) graphLayoutView.SetLayoutStrategy('Simple 2D') graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetLayoutStrategy().SetRandomSeed(0) graphLayoutView.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()
def __init__(parent = None): QtGui.QMainWindow.__init__(parent) ui = Ui_MainWindow() ui_window.setupUi() view = vtk.vtkGraphLayoutView() view.SetInteractor(ui_window.vtkWidget.GetRenderWindow().GetInteractor()) ui_window.vtkWidget.SetRenderWindow(view.GetRenderWindow()) level = 2 basisNum = 0 cutoff = 1.7e-5 basisCutoff = 1.0e-6 ebSumCutoff = 0.1 loadGraph() cutoffValid = QtGui.QDoubleValidator(0.0, 1.0, 12, ui_window.lineEdit_cutoff) ui_window.lineEdit_cutoff.setValidator(cutoffValid) basisCutoffValid = QtGui.QDoubleValidator(0.0, 1.0, 12, ui_window.lineEdit_basisCutoff) ui_window.lineEdit_basisCutoff.setValidator(basisCutoffValid) # Connect signals and slots # QtCore.QObject.connect(ui_window.actionNew, QtCore.SIGNAL("triggered()"), fileOpen) QtCore.QObject.connect(ui_window.actionExit, QtCore.SIGNAL("triggered()"), fileExit) QtCore.QObject.connect(ui_window.hSlider_basisIndex, QtCore.SIGNAL("valueChanged(int)"), basisUpdate) # QtCore.QObject.connect(ui_window.spinBox_basisIndex, QtCore.SIGNAL("valueChanged(int)"), basisUpdate) QtCore.QObject.connect(ui_window.hSlider_level, QtCore.SIGNAL("valueChanged(int)"), levelUpdate) # QtCore.QObject.connect(ui_window.spinBox_level, QtCore.SIGNAL("valueChanged(int)"), levelUpdate) QtCore.QObject.connect(ui_window.listWidget, QtCore.SIGNAL("itemClicked(QListWidgetItem *)"), toggleVisibility) QtCore.QObject.connect(ui_window.lineEdit_cutoff, QtCore.SIGNAL("editingFinished()"), changeCutoff) QtCore.QObject.connect(ui_window.lineEdit_basisCutoff, QtCore.SIGNAL("editingFinished()"), changeBasisCutoff) ui_window.hSlider_basisIndex.setValue(basisNum) ui_window.spinBox_basisIndex.setValue(basisNum) ui_window.hSlider_level.setValue(level) ui_window.spinBox_level.setValue(level) # TODO: Problem with size of ExtBasis for levels below 2... ui_window.hSlider_level.setMinimum(2) ui_window.spinBox_level.setMinimum(2)
def vizualize_grapth(self): self.g.GetEdgeData().AddArray(self.edge_weights) self.g.GetVertexData().AddArray(self.labels) self.g.GetVertexData().AddArray(self.vertex_ids) self.g.GetVertexData().SetPedigreeIds(self.vertex_ids) self.g.GetVertexData().AddArray(self.glyph_scales) if self.edge_color_filename: self.read_edge_colors() self.g.GetEdgeData().AddArray(self.edge_colors) self.insert_graph() if self.edge_color_filename: self.lookup_table.Build() graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(self.g) graphLayoutView.SetLayoutStrategy("Random") graphLayoutView.GetLayoutStrategy().SetEdgeWeightField(WEIGHTS) graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) graphLayoutView.SetEdgeLabelArrayName(WEIGHTS) graphLayoutView.SetEdgeLabelVisibility(0) if self.edge_color_filename: graphLayoutView.SetEdgeColorArrayName(EDGE_COLORS) graphLayoutView.ColorEdgesOn() graphLayoutView.SetVertexLabelArrayName(LABELS) graphLayoutView.SetVertexLabelVisibility(1) graphLayoutView.ScaledGlyphsOn() graphLayoutView.SetScalingArrayName(SCALES) graphLayoutView.GetRepresentation().SetGlyphType(vtkGraphToGlyphs.SPHERE) graphLayoutView.GetRenderWindow().SetSize(1024, 768) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetInteractor().SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera()) graphLayoutView.GetInteractor().Start()
def __init__(self, parent = None): QtGui.QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.view = vtk.vtkGraphLayoutView() self.view.SetInteractor(self.ui.vtkWidget.GetRenderWindow().GetInteractor()) self.ui.vtkWidget.SetRenderWindow(self.view.GetRenderWindow()) self.level = 2 self.basisNum = 0 self.cutoff = 1.7e-5 self.basisCutoff = 1.0e-6 self.ebSumCutoff = 0.1 self.loadGraph() self.cutoffValid = QtGui.QDoubleValidator(0.0, 1.0, 12, self.ui.lineEdit_cutoff) self.ui.lineEdit_cutoff.setValidator(self.cutoffValid) self.basisCutoffValid = QtGui.QDoubleValidator(0.0, 1.0, 12, self.ui.lineEdit_basisCutoff) self.ui.lineEdit_basisCutoff.setValidator(self.basisCutoffValid) # Connect signals and slots # QtCore.QObject.connect(self.ui.actionNew, QtCore.SIGNAL("triggered()"), self.fileOpen) QtCore.QObject.connect(self.ui.actionExit, QtCore.SIGNAL("triggered()"), self.fileExit) QtCore.QObject.connect(self.ui.hSlider_basisIndex, QtCore.SIGNAL("valueChanged(int)"), self.basisUpdate) # QtCore.QObject.connect(self.ui.spinBox_basisIndex, QtCore.SIGNAL("valueChanged(int)"), self.basisUpdate) QtCore.QObject.connect(self.ui.hSlider_level, QtCore.SIGNAL("valueChanged(int)"), self.levelUpdate) # QtCore.QObject.connect(self.ui.spinBox_level, QtCore.SIGNAL("valueChanged(int)"), self.levelUpdate) QtCore.QObject.connect(self.ui.listWidget, QtCore.SIGNAL("itemClicked(QListWidgetItem *)"), self.toggleVisibility) QtCore.QObject.connect(self.ui.lineEdit_cutoff, QtCore.SIGNAL("editingFinished()"), self.changeCutoff) QtCore.QObject.connect(self.ui.lineEdit_basisCutoff, QtCore.SIGNAL("editingFinished()"), self.changeBasisCutoff) self.ui.hSlider_basisIndex.setValue(self.basisNum) self.ui.spinBox_basisIndex.setValue(self.basisNum) self.ui.hSlider_level.setValue(self.level) self.ui.spinBox_level.setValue(self.level) # TODO: Problem with size of ExtBasis for levels below 2... self.ui.hSlider_level.setMinimum(2) self.ui.spinBox_level.setMinimum(2)
def main(): # colors = vtk.vtkNamedColors() g = vtk.vtkMutableDirectedGraph() # Create 3 vertices v1 = g.AddVertex() v2 = g.AddVertex() v3 = g.AddVertex() # Create a fully connected graph g.AddGraphEdge(v1, v2) g.AddGraphEdge(v2, v3) g.AddGraphEdge(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) # Add the edge weight array to the graph g.GetEdgeData().AddArray(weights) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(g) graphLayoutView.SetLayoutStrategy("Simple 2D") graphLayoutView.GetLayoutStrategy().SetEdgeWeightField("Weights") graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) graphLayoutView.SetEdgeLabelArrayName("Weights") graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetLayoutStrategy().SetRandomSeed(0) graphLayoutView.GetInteractor().Start()
def __init__(self, parent=None): super(VTKFrame, self).__init__(parent) self.vtkWidget = QVTKRenderWindowInteractor(self) vl = QtGui.QVBoxLayout(self) vl.addWidget(self.vtkWidget) vl.setContentsMargins(0, 0, 0, 0) self.ren = vtk.vtkRenderer() self.ren.SetBackground(0.1, 0.2, 0.4) self.vtkWidget.GetRenderWindow().AddRenderer(self.ren) self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() g = vtk.vtkMutableDirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() v3 = g.AddVertex() ### g.AddEdge(v1, v2) g.AddGraphEdge(v1, v2) g.AddGraphEdge(v2, v3) g.AddGraphEdge(v3, v1) # Do layout manually before handing graph to the view. # This allows us to know the positions of edge arrows. graphLayoutView = vtk.vtkGraphLayoutView() layout = vtk.vtkGraphLayout() strategy = vtk.vtkSimple2DLayoutStrategy() layout.SetInput(g) layout.SetLayoutStrategy(strategy) # Tell the view to use the vertex layout we provide graphLayoutView.SetLayoutStrategyToPassThrough() # The arrows will be positioned on a straight line between two # vertices so tell the view not to draw arcs for parallel edges graphLayoutView.SetEdgeLayoutStrategyToPassThrough() # Add the graph to the view. This will render vertices and edges, # but not edge arrows. graphLayoutView.AddRepresentationFromInputConnection( layout.GetOutputPort()) # Manually create an actor containing the glyphed arrows. graphToPoly = vtk.vtkGraphToPolyData() graphToPoly.SetInputConnection(layout.GetOutputPort()) graphToPoly.EdgeGlyphOutputOn() # Set the position (0: edge start, 1:edge end) where # the edge arrows should go. graphToPoly.SetEdgeGlyphPosition(0.98) # Make a simple edge arrow for glyphing. arrowSource = vtk.vtkGlyphSource2D() arrowSource.SetGlyphTypeToEdgeArrow() arrowSource.SetScale(0.1) arrowSource.Update() # Use Glyph3D to repeat the glyph on all edges. arrowGlyph = vtk.vtkGlyph3D() arrowGlyph.SetInputConnection(0, graphToPoly.GetOutputPort(1)) arrowGlyph.SetInputConnection(1, arrowSource.GetOutputPort()) # Create a mapper mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(arrowGlyph.GetOutputPort()) # Create an actor actor = vtk.vtkActor() actor.SetMapper(mapper) self.ren.AddActor(actor) self.ren.ResetCamera() self._initialized = False
g.AddGraphEdge(v1, v2) g.AddGraphEdge(v2, v3) g.AddGraphEdge(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) # Add the edge weight array to the graph g.GetEdgeData().AddArray(weights) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(g) graphLayoutView.SetLayoutStrategy("Simple 2D") graphLayoutView.GetLayoutStrategy().SetEdgeWeightField("Weights") graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) graphLayoutView.SetEdgeLabelArrayName("Weights") graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.ResetCamera() graphLayoutView.Render() graphLayoutView.GetLayoutStrategy().SetRandomSeed(0) graphLayoutView.GetInteractor().Start()
tree = vtk.vtkTree() tree.CheckedShallowCopy(builder) tree.GetVertexData().AddArray(names) ## Now iterate over the tree and print it #iter = vtk.vtkTreeDFSIterator() #iter.SetTree(tree) # #while iter.HasNext(): # id = iter.Next() # mystr = tree.GetLevel(id) * " " # print mystr + str(names.GetValue(id)) # Display the tree in the tree map viewer view = vtk.vtkGraphLayoutView() view.AddRepresentationFromInput(tree); view.ResetCamera() view.Render() view.SetVertexLabelArrayName("name") view.SetVertexLabelVisibility(True) view.GetInteractor().Start() win = vtk.vtkRenderWindow() interact = vtk.vtkRenderWindowInteractor() interact.SetRenderWindow(win) view.SetRenderWindow(win) win.Render() win.GetInteractor().Initialize() win.GetInteractor().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()
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() 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()
def DisplayGraph(graph): ''' Display the graph. ''' theme = vtk.vtkViewTheme() theme.SetBackgroundColor(0, 0, .1) theme.SetBackgroundColor2(0, 0, .5) # Layout the graph # Pick a strategy you like. # strategy = vtk.vtkCircularLayoutStrategy() strategy = vtk.vtkSimple2DLayoutStrategy() # strategy = vtk.vtkRandomLayoutStrategy() layout = vtk.vtkGraphLayout() layout.SetLayoutStrategy(strategy) layout.SetInputData(graph) view = vtk.vtkGraphLayoutView() view.AddRepresentationFromInputConnection(layout.GetOutputPort()) # Tell the view to use the vertex layout we provide. view.SetLayoutStrategyToPassThrough() view.SetEdgeLabelVisibility(True) view.SetVertexLabelArrayName("Labels") view.SetVertexLabelVisibility(True) view.ApplyViewTheme(theme) # Manually create an actor containing the glyphed arrows. # Get the edge geometry edgeGeom = vtk.vtkGraphToPolyData() edgeGeom.SetInputConnection(layout.GetOutputPort()) edgeGeom.EdgeGlyphOutputOn() # Set the position (0: edge start, 1: edge end) where # the edge arrows should go. # edgeGeom.SetEdgeGlyphPosition(0.8) edgeGeom.SetEdgeGlyphPosition(0.85) # Make a simple edge arrow for glyphing. # arrowSource = vtk.vtkGlyphSource2D() # arrowSource.SetGlyphTypeToEdgeArrow() # arrowSource.SetScale(0.075) # Or use a cone. coneSource = vtk.vtkConeSource() coneSource.SetRadius(0.025) coneSource.SetHeight(0.1) coneSource.SetResolution(12) # Use Glyph3D to repeat the glyph on all edges. arrowGlyph = vtk.vtkGlyph3D() arrowGlyph.SetInputConnection(0, edgeGeom.GetOutputPort(1)) # arrowGlyph.SetInputConnection(1, arrowSource.GetOutputPort()) arrowGlyph.SetInputConnection(1, coneSource.GetOutputPort()) # Add the edge arrow actor to the view. arrowMapper = vtk.vtkPolyDataMapper() arrowMapper.SetInputConnection(arrowGlyph.GetOutputPort()) arrowActor = vtk.vtkActor() arrowActor.SetMapper(arrowMapper) view.GetRenderer().AddActor(arrowActor) view.ResetCamera() view.Render() view.SetInteractionModeTo3D() view.GetInteractor().Initialize() view.GetInteractor().Start()
def main(): source = vtk.vtkRandomGraphSource() source.DirectedOff() source.SetNumberOfVertices(100) source.SetEdgeProbability(0) # Basically generates a tree source.SetUseEdgeProbability(True) source.SetStartWithTree(True) source.IncludeEdgeWeightsOn() # Create force directed layout strategy = vtk.vtkSimple2DLayoutStrategy() strategy.SetInitialTemperature(5) # Create a graph layout view view = vtk.vtkGraphLayoutView() view.AddRepresentationFromInputConnection(source.GetOutputPort()) view.SetVertexLabelArrayName("vertex id") view.SetVertexLabelVisibility(True) view.SetVertexColorArrayName("vertex id") view.SetColorVertices(True) view.SetEdgeColorArrayName("edge weight") view.SetColorEdges(True) view.SetLayoutStrategy(strategy) # Make sure the views are using a pedigree id selection view.GetRepresentation(0).SetSelectionType(2) # Create a selection link and set both view to use it annotationLink = vtk.vtkAnnotationLink() view.GetRepresentation(0).SetAnnotationLink(annotationLink) def select_callback(caller, event): """ In this particular data representation the current selection in the annotation link should always contain two nodes: one for the edges and one for the vertices. Which is which is not consistent, so you need to check the FieldType of each SelectionNode :param caller: :param event: :return: """ sel = caller.GetCurrentSelection() for nn in range(sel.GetNumberOfNodes()): sel_ids = sel.GetNode(nn).GetSelectionList() field_type = sel.GetNode(nn).GetFieldType() if field_type == 3: print("Vertex selection Pedigree IDs") if field_type == 4: print("Edge selection Pedigree IDs") if sel_ids.GetNumberOfTuples() > 0: for ii in range(sel_ids.GetNumberOfTuples()): print(int(sel_ids.GetTuple1(ii))) else: print("-- empty") print("") # AnnotationChangedEvent will fire when the selection is changed annotationLink.AddObserver("AnnotationChangedEvent", select_callback) # Set the theme on the view theme = vtk.vtkViewTheme.CreateMellowTheme() theme.SetLineWidth(5) theme.SetPointSize(10) theme.SetCellOpacity(0.99) theme.SetOutlineColor(0.6, 0.6, 0.6) # Vertices theme.SetSelectedPointColor(0, 0.5, 1) theme.SetPointHueRange(1.0, 1.0) theme.SetPointSaturationRange(1.0, 1.0) theme.SetPointValueRange(0.0, 1.0) # theme.SetPointAlphaRange(0.2, 0.8) # Edges theme.SetSelectedCellColor(1.0, 0.95, 0.75) theme.SetCellHueRange(0.1, 0.1) theme.SetCellSaturationRange(0.2, 1.0) theme.SetCellValueRange(0.5, 1.0) # theme.SetPointAlphaRange(0.2, 0.8) view.ApplyViewTheme(theme) theme.FastDelete() view.GetRenderWindow().SetSize(600, 600) view.ResetCamera() view.Render() view.GetInteractor().Start()
def __init__(self, clsInteractor): self.clsInteractor = clsInteractor self.graphLayoutView = vtk.vtkGraphLayoutView() self.representation = None
def main(): user_file = "./data/M2/Flitter_Names.txt" friend_file = "./data/M2/Links_Table.txt" community_file = "./data/M2/People_Cities.txt" ## Read user_file and friend_file with numpy names = pd.read_csv(user_file, delimiter='\t') friends = pd.read_csv(friend_file, delimiter='\t') communities = pd.read_csv(community_file, delimiter='\t') ## Add the size of each users network to the pandas dataframe ## and the city of each user to the pandas dataframe size = [] for i in range(0, len(names)): size.append(friends[friends.ID2 == i].ID2.size + friends[friends.ID1 == i].ID1.size) names.loc[:, 'followers'] = size names.loc[:, 'city'] = communities.City.tolist() ## First round of checks, do the sizes of their networks make sense? employees = names[(names['followers'] >= 37) & (names['followers'] <= 43)] handlers = names[(names['followers'] >= 28) & (names['followers'] <= 42)] middlemen = names[(names['followers'] >= 4) & (names['followers'] <= 5)] leaders = names[(names['followers'] >= 125)] middlemen = check(middlemen, leaders, friends, 1, "ge") potentials = leaders potentials = update_suspects(potentials, handlers, employees, middlemen, leaders) allgraph = vtk.vtkMutableDirectedGraph() allgraph = makegraph(potentials, friends, "city") ## Second round of check, do they havethe appropriate links? handlers = check(handlers, handlers, friends, 2, "le") employees = check(employees, handlers, friends, 3, "eq") middlemen = check(middlemen, handlers, friends, 2, "ge") ## Finally we remove all extras based on our employees handlers = check(handlers, employees, friends, 1, "eq") middlemen = check(middlemen, handlers, friends, 1, "ge") leaders = check(leaders, middlemen, friends, 1, "ge") potentials = update_suspects(potentials, handlers, employees, middlemen, leaders) potentialgraph = vtk.vtkMutableDirectedGraph() potentialgraph = makesolution(potentials, friends, "criminal") ### VTK pipeline stuff strategy = vtk.vtkAttributeClustering2DLayoutStrategy() strategy.SetVertexAttribute("city") strategy.SetGraph(allgraph) graphLayoutView = vtk.vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(allgraph) graphLayoutView.GetRenderWindow().SetSize(1024, 1024) graphLayoutView.SetLayoutStrategy(strategy) graphLayoutView.SetVertexLabelArrayName("city") graphLayoutView.SetVertexLabelVisibility(1) graphLayoutView.ResetCamera() ## Render Just potential employees, handlers, middlemen, and leaders strategy2 = vtk.vtkAttributeClustering2DLayoutStrategy() strategy2.SetVertexAttribute("criminal") strategy2.SetGraph(potentialgraph) graphLayoutView2 = vtk.vtkGraphLayoutView() graphLayoutView2.AddRepresentationFromInput(potentialgraph) graphLayoutView2.GetRenderWindow().SetSize(1024, 1024) graphLayoutView2.SetLayoutStrategy(strategy2) graphLayoutView2.SetVertexLabelArrayName("username") graphLayoutView2.SetVertexLabelVisibility(1) graphLayoutView2.ResetCamera() ### implementing kayleigh's fancy linking code annotationlink = vtk.vtkAnnotationLink() graphLayoutView.GetRepresentation(0).SetAnnotationLink(annotationlink) graphLayoutView2.GetRepresentation(0).SetAnnotationLink(annotationlink) updater = vtk.vtkViewUpdater() updater.AddAnnotationLink(annotationlink) updater.AddView(graphLayoutView) updater.AddView(graphLayoutView2) graphLayoutView.Render() graphLayoutView.GetInteractor().Start() graphLayoutView2.Render() graphLayoutView2.GetInteractor().Start()
def __init__(self, parent = None): super(VTKFrame, self).__init__(parent) self.vtkWidget = QVTKRenderWindowInteractor(self) vl = QtGui.QVBoxLayout(self) vl.addWidget(self.vtkWidget) vl.setContentsMargins(0, 0, 0, 0) self.ren = vtk.vtkRenderer() self.ren.SetBackground(0.1, 0.2, 0.4) self.vtkWidget.GetRenderWindow().AddRenderer(self.ren) self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() g = vtk.vtkMutableDirectedGraph() v1 = g.AddVertex() v2 = g.AddVertex() v3 = g.AddVertex() ### g.AddEdge(v1, v2) g.AddGraphEdge(v1, v2) g.AddGraphEdge(v2, v3) g.AddGraphEdge(v3, v1) # Do layout manually before handing graph to the view. # This allows us to know the positions of edge arrows. graphLayoutView = vtk.vtkGraphLayoutView() layout = vtk.vtkGraphLayout() strategy = vtk.vtkSimple2DLayoutStrategy() layout.SetInput(g) layout.SetLayoutStrategy(strategy) # Tell the view to use the vertex layout we provide graphLayoutView.SetLayoutStrategyToPassThrough() # The arrows will be positioned on a straight line between two # vertices so tell the view not to draw arcs for parallel edges graphLayoutView.SetEdgeLayoutStrategyToPassThrough() # Add the graph to the view. This will render vertices and edges, # but not edge arrows. graphLayoutView.AddRepresentationFromInputConnection(layout.GetOutputPort()) # Manually create an actor containing the glyphed arrows. graphToPoly = vtk.vtkGraphToPolyData() graphToPoly.SetInputConnection(layout.GetOutputPort()) graphToPoly.EdgeGlyphOutputOn() # Set the position (0: edge start, 1:edge end) where # the edge arrows should go. graphToPoly.SetEdgeGlyphPosition(0.98) # Make a simple edge arrow for glyphing. arrowSource = vtk.vtkGlyphSource2D() arrowSource.SetGlyphTypeToEdgeArrow() arrowSource.SetScale(0.1) arrowSource.Update() # Use Glyph3D to repeat the glyph on all edges. arrowGlyph = vtk.vtkGlyph3D() arrowGlyph.SetInputConnection(0, graphToPoly.GetOutputPort(1)) arrowGlyph.SetInputConnection(1, arrowSource.GetOutputPort()) # Create a mapper mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(arrowGlyph.GetOutputPort()) # Create an actor actor = vtk.vtkActor() actor.SetMapper(mapper) self.ren.AddActor(actor) self.ren.ResetCamera() self._initialized = False