Difference between revisions of "VTK/Examples/Python/Infovis/SelectedGraphIDs"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
m (Added picture, cleaned up some code)
 
Line 5: Line 5:
 
#!/usr/bin/env python
 
#!/usr/bin/env python
  
# This is a variation on VTK/Examples/Infovis/Python/selection.py  
+
# This is a variation on VTK/Examples/Infovis/Python/selection.py
# which shows how to use a vtkAnnotationLink to view the contents  
+
# which shows how to use a vtkAnnotationLink to view the contents
 
# of a selection from a vtkGraphLayoutView
 
# of a selection from a vtkGraphLayoutView
+
 
 +
from __future__ import print_function
 
import vtk
 
import vtk
+
 
 
source = vtk.vtkRandomGraphSource()
 
source = vtk.vtkRandomGraphSource()
 
source.DirectedOff()
 
source.DirectedOff()
Line 18: Line 19:
 
source.SetStartWithTree(True)
 
source.SetStartWithTree(True)
 
source.IncludeEdgeWeightsOn()
 
source.IncludeEdgeWeightsOn()
+
 
 
# Create force directed layout
 
# Create force directed layout
 
strategy = vtk.vtkSimple2DLayoutStrategy()
 
strategy = vtk.vtkSimple2DLayoutStrategy()
 
strategy.SetInitialTemperature(5)
 
strategy.SetInitialTemperature(5)
+
 
 
# Create a graph layout view
 
# Create a graph layout view
 
view = vtk.vtkGraphLayoutView()
 
view = vtk.vtkGraphLayoutView()
Line 33: Line 34:
 
view.SetColorEdges(True)
 
view.SetColorEdges(True)
 
view.SetLayoutStrategy(strategy)
 
view.SetLayoutStrategy(strategy)
+
 
 
# Make sure the views are using a pedigree id selection
 
# Make sure the views are using a pedigree id selection
 
view.GetRepresentation(0).SetSelectionType(2)
 
view.GetRepresentation(0).SetSelectionType(2)
+
 
 
# Create a selection link and set both view to use it
 
# Create a selection link and set both view to use it
 
annotationLink = vtk.vtkAnnotationLink()
 
annotationLink = vtk.vtkAnnotationLink()
 
view.GetRepresentation(0).SetAnnotationLink(annotationLink)
 
view.GetRepresentation(0).SetAnnotationLink(annotationLink)
+
 
+
 
 
def select_callback(caller,event):
 
def select_callback(caller,event):
 
     # In this particular data representation the current selection in the
 
     # In this particular data representation the current selection in the
Line 47: Line 48:
 
     # one for the vertices. Which is which is not consistent, so you need to
 
     # one for the vertices. Which is which is not consistent, so you need to
 
     # check the FieldType of each SelectionNode
 
     # check the FieldType of each SelectionNode
+
 
 
     sel = caller.GetCurrentSelection()
 
     sel = caller.GetCurrentSelection()
+
 
 
     for nn in range(sel.GetNumberOfNodes()):
 
     for nn in range(sel.GetNumberOfNodes()):
 
         sel_ids = sel.GetNode(nn).GetSelectionList()
 
         sel_ids = sel.GetNode(nn).GetSelectionList()
 
         field_type = sel.GetNode(nn).GetFieldType()
 
         field_type = sel.GetNode(nn).GetFieldType()
 
         if field_type == 3:
 
         if field_type == 3:
             print "Vertex selection Pedigree IDs"
+
             print("Vertex selection Pedigree IDs")
 
         if field_type == 4:
 
         if field_type == 4:
             print "Edge selection Pedigree IDs"
+
             print("Edge selection Pedigree IDs")
 
         if sel_ids.GetNumberOfTuples() > 0:
 
         if sel_ids.GetNumberOfTuples() > 0:
 
             for ii in range(sel_ids.GetNumberOfTuples()):
 
             for ii in range(sel_ids.GetNumberOfTuples()):
                 print int(sel_ids.GetTuple1(ii))
+
                 print(int(sel_ids.GetTuple1(ii)))
 
         else:
 
         else:
             print "-- empty"
+
             print("-- empty")
+
 
     print ""
+
     print("")
+
 
 
# AnnotationChangedEvent will fire when the selection is changed
 
# AnnotationChangedEvent will fire when the selection is changed
 
annotationLink.AddObserver("AnnotationChangedEvent", select_callback)
 
annotationLink.AddObserver("AnnotationChangedEvent", select_callback)
+
 
 
# Set the theme on the view
 
# Set the theme on the view
 
theme = vtk.vtkViewTheme.CreateMellowTheme()
 
theme = vtk.vtkViewTheme.CreateMellowTheme()
Line 88: Line 89:
 
view.ApplyViewTheme(theme)
 
view.ApplyViewTheme(theme)
 
theme.FastDelete()
 
theme.FastDelete()
+
 
 
view.GetRenderWindow().SetSize(600, 600)
 
view.GetRenderWindow().SetSize(600, 600)
 
view.ResetCamera()
 
view.ResetCamera()
 
view.Render()
 
view.Render()
+
 
 
view.GetInteractor().Start()
 
view.GetInteractor().Start()
  
 
</source>
 
</source>

Latest revision as of 01:29, 23 August 2015

VTK Examples Python Infovis SelectedGraphIDs.png

SelectedGraphIDs.py

#!/usr/bin/env python

# This is a variation on VTK/Examples/Infovis/Python/selection.py
# 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)

# 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

    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()