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()
Пример #2
0
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
theme.SetPointSize(6)
theme.SetCellColor(0.8, 0.8, 0.8)
theme.SetSelectedCellColor(1,1,1)
theme.SetSelectedPointColor(1,1,1)
theme.SetOutlineColor(0.6,0.6,0.6)
# theme.SetPointOpacity(0.5)
theme.SetPointHueRange(1.0,1.0)
theme.SetPointSaturationRange(1.0,1.0)
theme.SetPointValueRange(0.0,1.0)
theme.SetPointAlphaRange(0.2,0.8)

theme.SetScalePointLookupTable(True)

# view.ApplyViewTheme(theme)

strategy = vtk.vtkSimple2DLayoutStrategy()
strategy.SetMaxNumberOfIterations(500)
strategy.SetIterationsPerLayout(50)
layout = vtk.vtkGraphLayout()
layout.SetLayoutStrategy(strategy)

view.GetRenderWindow().SetSize(600,600)
view.ResetCamera()
view.Render()

years_list = [1820,1825,1830,1835]

for yr in range(1820,2010,10):
	
	print 'Year: ' + str(yr)
	
Пример #4
0
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()
Пример #5
0
# which shows how to use a vtkAnnotationLink to view the contents
# of a selection from a vtkGraphLayoutView

from __future__ import print_function
import vtk

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)
Пример #6
0
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()
Пример #7
0
    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