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()
Beispiel #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
Beispiel #3
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(self,
                                       module_manager,
                                       vtk.vtkGraphLayout(),
                                       'Processing.',
                                       ('vtkAbstractGraph', ),
                                       ('vtkAbstractGraph', ),
                                       replaceDoc=True,
                                       inputFunctions=None,
                                       outputFunctions=None)
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)
	
	# Break pipeline while set up new graph
	if yr != years_list[0]:
		layout.RemoveAllInputs()
Beispiel #5
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()
    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
Beispiel #7
0
def buildSubGraph(level):
	
	Tmat_orig = GTree[level,0]['T'][0][0][0][0]
	Current_ExtIdxs_orig = GTree[level,0]['ExtIdxs'][0][0][0]
	
	# Only pass rows and columns of T which have large enough sum (L1 norm)
	ebSum = GTree[level,0]['ExtBasis'][0][0].sum(0)
	# ebSum is e.g. (1,1046) matrix 
	# ebSum.A[0] is e.g. (1046,) array
	# Convert ebSum to 1D array (.A[0]) from 2D matrix to get this to work right for indexing matrices
	over = N.nonzero(ebSum.A[0]>ebSumCutoff)[0]
	Tmat = Tmat_orig[N.ix_(over,over)]
	Current_ExtIdxs = Current_ExtIdxs_orig[over]
	# del Tmat_orig, Current_ExtIdxs_orig
	
	Tdata_orig = abs(Tmat.data)
	Trows_orig = Tmat.nonzero()[0]
	Tcols_orig = Tmat.nonzero()[1]
	
	tover = N.nonzero(Tdata_orig>cutoff)[0]
	Tdata = Tdata_orig[tover]
	Tdata.dtype = 'float64'
	Trows = Trows_orig[tover]
	Trows.dtype = 'int32'		# was getting TypeError on numpy_to_vtk if didn't set this explicitly...
	Tcols = Tcols_orig[tover]
	Tcols.dtype = 'int32'
	# del Tdata_orig, Trows_orig, Tcols_orig
	# Set all self-loops to zero weight
	Tdata[N.nonzero(Trows==Tcols)[0]] = 0.0
		
	col0a = VN.numpy_to_vtk(Trows)
	col0a.SetName('index1')
	col1a = VN.numpy_to_vtk(Tcols)
	col1a.SetName('index2')
	vala = VN.numpy_to_vtk(Tdata)
	vala.SetName('weight')
	
	# Make set of indices that made it through the T cutoff filter
	# VN.vtk_to_numpy(table.GetColumnByName('index1')).tolist()
	s1 = set(Trows)
	s2 = set(Tcols)
	ss = s1.union(s2)
	# Then figure out which indices (vertices) from the thresholded matrix haven't made it through
	s_skipped = set(range(Current_ExtIdxs.shape[0])).difference(ss)
	
	# And add these skipped (isolated) vertices back in
	for index in s_skipped:
		col0a.InsertNextValue(index)
		col1a.InsertNextValue(index)
		vala.InsertNextValue(0.0)
	
	table = vtk.vtkTable()	
	table.AddColumn(col0a)
	table.AddColumn(col1a)
	table.AddColumn(vala)
		
	tgraph = vtk.vtkTableToGraph()
	tgraph.SetInput(table)
	tgraph.AddLinkVertex('index2', 'stuff', False)
	tgraph.AddLinkVertex('index1', 'stuff', False)
	tgraph.AddLinkEdge('index2', 'index1')
	tgraph.Update()
	
	rawGraph = tgraph.GetOutputDataObject(0)
	# rawGraph.Update()
	
	# Remember Matlab uses one-based indices (ExtIdxs-1 for Numpy)
	ptsSubMatrix = ptsMatrix[Current_ExtIdxs-1,:]
	ptsSub = VN.numpy_to_vtk(ptsSubMatrix)
	pts = vtk.vtkPoints()
	pts.SetData(ptsSub)
	rawGraph.SetPoints(pts)
	rawGraph.Update()
			
	strategy = vtk.vtkPassThroughLayoutStrategy()
	layout = vtk.vtkGraphLayout()
	layout.SetInput(rawGraph)
	layout.SetLayoutStrategy(strategy)
	
	edgeLayout = vtk.vtkEdgeLayout()
	edgeStrategy = vtk.vtkArcParallelEdgeStrategy()
	edgeStrategy.SetNumberOfSubdivisions(10)
	edgeLayout.SetInputConnection(layout.GetOutputPort())
	edgeLayout.SetLayoutStrategy(edgeStrategy)
	edgeLayout.Update()
	
	return edgeLayout.GetOutputDataObject(0)
 def buildSubGraph(self, level):
     
     table = vtk.vtkTable()
     col0 = vtk.vtkIntArray()
     col0.SetName('index1')
     col1 = vtk.vtkIntArray()
     col1.SetName('index2')
     val = vtk.vtkDoubleArray()
     val.SetName('weight')
     
     Tmat = self.G.Tree[level,0].T
     minmax = "(%3.2e, %3.2e)" % (abs(Tmat.data).min(), abs(Tmat.data).max())
     self.ui.label_cutoff_minmax.setText(minmax)
     
     ebSum = self.ExtBasis.sum(0)
     idxs = []
     
     for ii in range(Tmat.nzmax):
         xx = Tmat.rowcol(ii)[0]
         yy = Tmat.rowcol(ii)[1]
         if ((ebSum[0,xx] > self.ebSumCutoff) and (ebSum[0,yy] > self.ebSumCutoff)):
             # print ebSum[0,xx], ebSum[0,yy]
             # Always include diagonal elements (no matter what cutoff) so all vertices are created
             if ((abs(Tmat.getdata(ii)) > self.cutoff) | (Tmat.rowcol(ii)[0]==Tmat.rowcol(ii)[1])):
                 col0.InsertNextValue(xx)
                 col1.InsertNextValue(yy)
                 # But, take out self-loops for edge color scale
                 if (xx == yy):
                     val.InsertNextValue(0.0)        
                     idxs.append(self.G.Tree[level,0].ExtIdxs[xx]-1)
                 else:
                     val.InsertNextValue(abs(Tmat.getdata(ii)))        
     
     table.AddColumn(col0)
     table.AddColumn(col1)
     table.AddColumn(val)
     
     tgraph = vtk.vtkTableToGraph()
     tgraph.SetInput(table)
     tgraph.AddLinkVertex('index2', 'stuff', False)
     tgraph.AddLinkVertex('index1', 'stuff', False)
     tgraph.AddLinkEdge('index2', 'index1')
     
     rawGraph = tgraph.GetOutput()
     rawGraph.Update()
     # print graph
     
     # Grab points from original set with ExtIdxs
     ptsArray = VN.vtk_to_numpy(self.pts.GetData())
     # Remember Matlab uses one-based indices (ExtIdxs-1 for Numpy)
     # print (self.G.Tree[level,0].ExtIdxs-1)
     # print ptsArray[self.G.Tree[level,0].ExtIdxs-1,:]
     ptsMatrix = N.matrix(ptsArray)
     ptsSubMatrix = ptsMatrix[idxs,:]
     ptsSub = VN.numpy_to_vtk(ptsSubMatrix)
     # print VN.vtk_to_numpy(ptsSub)
     pts = vtk.vtkPoints()
     pts.SetData(ptsSub)
     rawGraph.SetPoints(pts)
     # print pts
         
     strategy = vtk.vtkPassThroughLayoutStrategy()
     layout = vtk.vtkGraphLayout()
     layout.SetInput(rawGraph)
     layout.SetLayoutStrategy(strategy)
     
     edgeLayout = vtk.vtkEdgeLayout()
     edgeStrategy = vtk.vtkArcParallelEdgeStrategy()
     edgeStrategy.SetNumberOfSubdivisions(50)
     edgeLayout.SetInputConnection(layout.GetOutputPort())
     edgeLayout.SetLayoutStrategy(edgeStrategy)
     edgeLayout.Update()
     
     return edgeLayout.GetOutput()
    def loadGraph():
        
        # ----------
        # Load and construct whole graph and multi-resolution data from Matlab structure
        dataDir = '/Users/emonson/Programming/Matlab/EMonson/Fodava/DocumentAnalysis/Analysis/'
        filename = dataDir + 'X20_042709b.mat'
        # filename = '/Users/emonson/Programming/Python/VTK/X20_040609b.mat'
        X = scipy.io.loadmat(filename)
        # Get graph structure G out of matlab variables
        G = X['G']
        
        # ----------
        # Set multi-resolution level bounds in GUI sliders
        levelMax = G.Tree.shape[0]-1
        ui_window.hSlider_level.setMinimum(1)
        ui_window.hSlider_level.setMaximum(levelMax)
        ui_window.spinBox_level.setMinimum(1)
        ui_window.spinBox_level.setMaximum(levelMax)
        
        # Start setting up basis function for display as subgraph
        ExtBasis = GTree[level,0]['ExtBasis'][0][0][0]
        basisMax = ExtBasis.shape[1]-1    # zero-based indices
        
        # Set particular level basis function bounds in GUI sliders
        ui_window.hSlider_basisIndex.setMinimum(0)
        ui_window.hSlider_basisIndex.setMaximum(basisMax)
        ui_window.spinBox_basisIndex.setMinimum(0)
        ui_window.spinBox_basisIndex.setMaximum(basisMax)
        
        # Build table which will become graph
        table = vtk.vtkTable()
        col0 = vtk.vtkIntArray()
        col0.SetName('index1')
        col1 = vtk.vtkIntArray()
        col1.SetName('index2')
        val = vtk.vtkDoubleArray()
        val.SetName('weight')
        
        Tmat = G.T
        # Tmat = G.W
        
        for ii in range(Tmat.nzmax):
            col0.InsertNextValue(Tmat.rowcol(ii)[0])
            col1.InsertNextValue(Tmat.rowcol(ii)[1])
            val.InsertNextValue(abs(Tmat.getdata(ii)))
        
        table.AddColumn(col0)
        table.AddColumn(col1)
        table.AddColumn(val)
        
        # Vertex links need to be done with index2 first or indexing won't be right...
        # TODO: Make this foolproof so that graph always ends up with correct ordering of indices...
        tgraph = vtk.vtkTableToGraph()
        tgraph.SetInput(table)
        tgraph.AddLinkVertex('index2', 'stuff', False)
        tgraph.AddLinkVertex('index1', 'stuff', False)
        tgraph.AddLinkEdge('index2', 'index1')
        
        rawGraph = tgraph.GetOutput()
        rawGraph.Update()
        # print graph
        
        # Load and assign whole graph pre-layout coordinates
        ptsFile = os.path.splitext(filename)[0] + '_pts.vtp'
        if os.path.exists(ptsFile):
            polyreader = vtk.vtkXMLPolyDataReader()
            polyreader.SetFileName(ptsFile)
            polyreader.Update()
            pts = polyreader.GetOutput().GetPoints()
            rawGraph.SetPoints(pts)
            # print pts
            
        strategy = vtk.vtkPassThroughLayoutStrategy()
        layout = vtk.vtkGraphLayout()
        layout.SetInput(rawGraph)
        layout.SetLayoutStrategy(strategy)
        
        edgeLayout = vtk.vtkEdgeLayout()
        edgeStrategy = vtk.vtkArcParallelEdgeStrategy()
        edgeStrategy.SetNumberOfSubdivisions(50)
        edgeLayout.SetInputConnection(layout.GetOutputPort())
        edgeLayout.SetLayoutStrategy(edgeStrategy)
        
        graph = edgeLayout.GetOutput()
        graph.Update()
        
        # --------
        # Add ExtBasis to graph data & Select particular basis function
        
        # print 'ExtBasis shape: ' + str(ExtBasis[:,basisNum].data.shape) + ' (level,basis) (' + str(level) + ',' + str(basisNum) + ')'
        
        # Indexing of ExtBasis is backwards from graph, so need to reverse with [::-1]
        # and array not "contiguous" if don't do .copy()
        Esub = ExtBasis[:,basisNum].data[::-1].copy()
        EsubSq = Esub**2
        # Esub = N.random.random(ExtBasis[:,basisNum].data.shape[0])
        # SubIdxs = (Esub > 0.001).nonzero()
        # SubIdxs = (Esub**2 > 0.8).nonzero()
        
        # Set ExtBasis vertex data from numpy array
        basisFunc = VN.numpy_to_vtk(Esub)
        basisFunc.SetName('ExtBasis')
        basisFuncSq = VN.numpy_to_vtk(EsubSq)
        basisFuncSq.SetName('ExtBasisSq')
        
        vertexData = graph.GetVertexData()
        vertexData.AddArray(basisFunc)
        vertexData.AddArray(basisFuncSq)
        
        selection = vtk.vtkSelectionSource()
        selection.SetContentType(7) # vtkSelection::THRESHOLDS
        # selection.SetContentType(2) # vtkSelection::PEDIGREE_IDS
        selection.SetFieldType(3) # vtkSelection::VERTEX
        selection.SetArrayName("ExtBasisSq")
        selection.AddThreshold(basisCutoff, 10)
        # TODO: There was something wrong with the indexing in the PEDIGREE_IDS selection...
        # for ii in SubIdxs[0]:
        #     selection.AddID(0,ii)
        minmax = "(%3.2e, %3.2e)" % (EsubSq.min(), EsubSq.max())
        ui_window.label_basisCutoff_minmax.setText(minmax)
        selection.Update()
        
        # ----------
        # Back to pipeline
        degree = vtk.vtkVertexDegree()
        degree.SetInput(graph)
        
        subgraph = vtk.vtkExtractSelectedGraph()
        subgraph.SetRemoveIsolatedVertices(False)
        subgraph.SetInputConnection(degree.GetOutputPort())
        subgraph.SetSelectionConnection(selection.GetOutputPort())
        
        # +++++++++++++
        graphToPoly = vtk.vtkGraphToPolyData()
        graphToPoly.SetInputConnection(subgraph.GetOutputPort())
        edgeMapper = vtk.vtkPolyDataMapper()
        edgeMapper.SetInputConnection(graphToPoly.GetOutputPort())
        edgeMapper.SetScalarModeToUseCellData()
        edgeMapper.SetScalarVisibility(False)
        edgeMapper.SetImmediateModeRendering(True)
        edgeActor = vtk.vtkActor()
        edgeActor.SetMapper(edgeMapper)
        edgeActor.SetPosition(0, 0, -0.003);
        
        lut = vtk.vtkLookupTable()
        lutNum = 256
        lut.SetNumberOfTableValues(lutNum)
        ctf = vtk.vtkColorTransferFunction()
        ctf.SetColorSpaceToDiverging()
        ctf.AddRGBPoint(0.0, 0, 0, 1.0)
        ctf.AddRGBPoint(1.0, 1.0, 0, 0)
        for ii,ss in enumerate([float(xx)/float(lutNum) for xx in range(lutNum)]):
            cc = ctf.GetColor(ss)
            lut.SetTableValue(ii,cc[0],cc[1],cc[2],1.0)
        
        vertGlyph = vtk.vtkVertexGlyphFilter()
        vertGlyph.SetInputConnection(subgraph.GetOutputPort())
        vertMapper = vtk.vtkPolyDataMapper()
        vertMapper.SetInputConnection(vertGlyph.GetOutputPort())
        vertMapper.SetImmediateModeRendering(True)
        vertMapper.SetScalarModeToUsePointFieldData()
        vertMapper.SetLookupTable(lut)
        vertMapper.SelectColorArray('ExtBasis')
        vertMapper.Update()
        vertRange = vertMapper.GetInput().GetPointData().GetArray(vertMapper.GetArrayName()).GetRange()
        vertMapper.SetScalarRange(-1.0*vertRange[1], vertRange[1])
        vertActor = vtk.vtkActor()
        vertActor.SetMapper(vertMapper)
        
        outlineMapper = vtk.vtkPolyDataMapper()
        outlineMapper.SetScalarVisibility(False)
        outlineMapper.SetImmediateModeRendering(True)
        outlineMapper.SetInputConnection(vertGlyph.GetOutputPort())
        outlineActor = vtk.vtkActor()
        outlineActor.PickableOff()
        outlineActor.SetPosition(0, 0, -0.001)
        outlineActor.GetProperty().SetRepresentationToWireframe()
        outlineActor.SetMapper(outlineMapper)
        
        # Create an Actor Collection for applying visibility to group
        basisActorCollection = vtk.vtkActorCollection()
        basisActorCollection.AddItem(vertActor)
        # basisActorCollection.AddItem(edgeActor)
        basisActorCollection.AddItem(outlineActor)
        
        
        # Apply a theme to the views
        theme = vtk.vtkViewTheme.CreateMellowTheme()
        theme.SetLineWidth(3)
        theme.SetPointSize(5)
        theme.SetSelectedCellColor(1,1,1)
        theme.SetSelectedPointColor(1,1,1)
        theme.SetOutlineColor(0.8, 0.8, 0.8)
        # theme.SetPointColor(0.9, 0.7, 0.3)
        theme.SetCellColor(0.9, 0.7, 0.3)
        # theme.SetPointOpacity(0.5)
        # theme.SetPointHueRange(0.0, 0.15)
        # theme.SetPointSaturationRange(0.6, 0.8)
        # theme.SetPointValueRange(0.4,0.8)
        # theme.SetPointAlphaRange(0.2,0.8)
        # theme.SetPointAlphaRange(1.0,1.0)

        
        # Apply theme
        # vertActor.GetProperty().SetColor(theme.GetPointColor())
        # vertActor.GetProperty().SetOpacity(theme.GetPointOpacity())
        vertActor.GetProperty().SetPointSize(theme.GetPointSize())
        outlineActor.GetProperty().SetPointSize(vertActor.GetProperty().GetPointSize()+2)
        outlineActor.GetProperty().SetColor(theme.GetOutlineColor())
        outlineActor.GetProperty().SetOpacity(theme.GetPointOpacity())
        edgeActor.GetProperty().SetColor(theme.GetCellColor())
        edgeActor.GetProperty().SetOpacity(theme.GetCellOpacity())
        edgeActor.GetProperty().SetLineWidth(theme.GetLineWidth())
        
        
        # ----------
        # Background graph skeleton
        graphMapper = vtk.vtkGraphMapper()
        graphMapper.SetInputConnection(0, degree.GetOutputPort(0))
        
        # Apply a theme to the background graph
        gtheme = vtk.vtkViewTheme()
        gtheme.SetLineWidth(1)
        gtheme.SetPointSize(0)
        gtheme.SetCellColor(0.8, 0.8, 0.8)
        gtheme.SetCellOpacity(0.2)
        gtheme.SetOutlineColor(0.8, 0.8, 0.8)
        gtheme.SetPointColor(0.8, 0.8, 0.8)
        gtheme.SetPointOpacity(0.0)
        graphMapper.ApplyViewTheme(gtheme)

        graphActor = vtk.vtkActor()
        graphActor.SetMapper(graphMapper)
        graphActor.SetPosition(0,0,-0.005)
        
        # ----------
        # Background vertices
        graphPoly = vtk.vtkGraphToPolyData()
        graphPoly.SetInputConnection(0, tgraph.GetOutputPort(0))
        
        vertGlyph = vtk.vtkGlyph3D()
        vertGlyph.SetInputConnection(0, graphPoly.GetOutputPort())
        glyphSource = vtk.vtkGlyphSource2D()
        glyphSource.SetGlyphTypeToVertex()
        # glyphSource.SetGlyphTypeToCircle()
        # glyphSource.SetScale(0.025)
        vertGlyph.SetInputConnection(1, glyphSource.GetOutputPort())
        
        vertexMapper = vtk.vtkPolyDataMapper()
        vertexMapper.SetInputConnection(vertGlyph.GetOutputPort())
        vertexActor = vtk.vtkActor()
        vertexActor.SetMapper(vertexMapper)
        vertexActor.GetProperty().SetPointSize(4)
        vertexActor.GetProperty().SetOpacity(0.5)
        vertexActor.GetProperty().SetColor(0.6, 0.6, 0.6)
        vertexActor.SetPosition(0, 0, -0.004)
        
        # ----------
        # Vertex index labels
        labelMapper = vtk.vtkDynamic2DLabelMapper()
        labelMapper.SetInputConnection(0, graphPoly.GetOutputPort(0))
        labelMapper.SetLabelModeToLabelFieldData()
        labelMapper.SetFieldDataName("label")
        labelMapper.SetLabelFormat("%s")
        labelMapper.GetLabelTextProperty().SetColor(0.0, 0.0, 0.0)
        
        labelActor = vtk.vtkActor2D()
        labelActor.SetMapper(labelMapper)
        
        # ----------
        # MultiScale Graph
        msGraph = buildSubGraph(level) 
        
        msMapper = vtk.vtkGraphMapper()
        msMapper.SetInput(msGraph)
        msMapper.SetColorEdges(True)
        msMapper.SetEdgeColorArrayName('weight')
        
        # Apply a theme to the background graph
        mtheme = vtk.vtkViewTheme()
        mtheme.SetLineWidth(3)
        mtheme.SetPointSize(11)
        # mtheme.SetCellColor(0.5, 0.5, 0.7)
        # mtheme.SetCellOpacity(0.5)
        mtheme.SetOutlineColor(0.8, 0.8, 0.8)
        mtheme.SetPointColor(0.3, 0.3, 0.6)
        mtheme.SetPointOpacity(1.0)
        mtheme.SetCellHueRange(0.67, 0.67)
        mtheme.SetCellSaturationRange(0.6, 0.1)
        mtheme.SetCellValueRange(0.5,1.0)
        mtheme.SetCellAlphaRange(0.2,0.8)
        msMapper.ApplyViewTheme(mtheme)

        msActor = vtk.vtkActor()
        msActor.SetMapper(msMapper)
        msActor.SetPosition(0,0,-0.002)
        
        # ----------
        # Set up window and add actors        
        view.SetLayoutStrategyToPassThrough()
        # view.ApplyViewTheme(theme)                
        # view.SetupRenderWindow(win)
        view.GetRenderer().SetBackground(theme.GetBackgroundColor())
        view.GetRenderer().SetBackground2(theme.GetBackgroundColor2())
        view.GetRenderer().SetGradientBackground(True)
        view.GetRenderer().AddActor(vertActor)
        view.GetRenderer().AddActor(outlineActor)
        view.GetRenderer().AddActor(edgeActor)
        view.GetRenderer().AddActor(graphActor)
        view.GetRenderer().AddActor(vertexActor)
        view.GetRenderer().AddActor(labelActor)
        view.GetRenderer().AddActor(msActor)
        
        # ----------
        # General interactor
        isty = vtk.vtkInteractorStyleRubberBand2D()
        # RubberBand2D assumes/needs parallel projection ON
        view.GetRenderer().GetActiveCamera().ParallelProjectionOn()
        iren = view.GetRenderWindow().GetInteractor()
        iren.SetInteractorStyle(isty)        
        # Interactor style must be set before scalar bar can be shown
        # view.SetVertexScalarBarVisibility(True)

        sbActor = vtk.vtkScalarBarActor()
        sbActor.SetLookupTable(vertMapper.GetLookupTable())
        sbActor.SetTitle(vertMapper.GetArrayName())
        sbActor.SetNumberOfLabels(3)
        vertexScalarBar = vtk.vtkScalarBarWidget()
        vertexScalarBar.SetScalarBarActor(sbActor)
        vertexScalarBar.SetInteractor(iren)
        vertexScalarBar.SetDefaultRenderer(view.GetRenderer())
        vertexScalarBar.SetCurrentRenderer(view.GetRenderer())
        vertexScalarBar.SetEnabled(True)
        scalarBarRep = vertexScalarBar.GetRepresentation()
        scalarBarRep.SetOrientation(1)  # 0 = Horizontal, 1 = Vertical
        scalarBarRep.GetPositionCoordinate().SetValue(0.05,0.05)
        scalarBarRep.GetPosition2Coordinate().SetValue(0.15,0.25)
        
        # Adding it this way gets it to show up, but it's not interactive
        view.GetRenderer().AddActor(sbActor)
        view.ResetCamera()
        view.Render()
        
        # ----------
        # Add Actors to QListWidget to allow check and uncheck for visibility
        listItem0 = QtGui.QListWidgetItem()
        listItem0.setText('Index Labels')
        listItem0.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        labelActor.SetVisibility(0)
        listItem0.setCheckState(QtCore.Qt.Unchecked)
        # Put actor it in as data in the list widget item
        listItem0.setData(QtCore.Qt.UserRole, QtCore.QVariant(labelActor))
        ui_window.listWidget.insertItem(0,listItem0)
        
        # Test retrieval of actor from list widget item
        # tmpItem = ui_window.listWidget.item(0)
        # tmpQtActor = tmpItem.data(QtCore.Qt.UserRole)
        # tmpActor = tmpQtActor.toPyObject()
        # tmpActor.SetVisibility(0)
        
        # Shorter way to add item to list widget
        listItem1 = QtGui.QListWidgetItem('Vertices (background)', ui_window.listWidget)
        listItem1.setData(QtCore.Qt.UserRole, QtCore.QVariant(vertexActor))
        listItem1.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        listItem1.setCheckState(QtCore.Qt.Checked)

        listItem2 = QtGui.QListWidgetItem('Graph (background)', ui_window.listWidget)
        listItem2.setData(QtCore.Qt.UserRole, QtCore.QVariant(graphActor))
        listItem2.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        listItem2.setCheckState(QtCore.Qt.Checked)

        listItem3 = QtGui.QListWidgetItem()
        listItem3.setText('Basis Function Vertices')
        listItem3.setData(QtCore.Qt.UserRole, QtCore.QVariant(basisActorCollection))
        listItem3.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        listItem3.setCheckState(QtCore.Qt.Checked)
        ui_window.listWidget.insertItem(1,listItem3)
        
        listItem6 = QtGui.QListWidgetItem()
        listItem6.setText('Basis Function Edges')
        listItem6.setData(QtCore.Qt.UserRole, QtCore.QVariant(edgeActor))
        listItem6.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        listItem6.setCheckState(QtCore.Qt.Checked)
        ui_window.listWidget.insertItem(2,listItem6)
        
        listItem4 = QtGui.QListWidgetItem()
        listItem4.setText('MultiScale Graph')
        listItem4.setData(QtCore.Qt.UserRole, QtCore.QVariant(msActor))
        listItem4.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        listItem4.setCheckState(QtCore.Qt.Checked)
        ui_window.listWidget.insertItem(3,listItem4)
                
        listItem5 = QtGui.QListWidgetItem()
        listItem5.setText('Basis Function Scale Bar')
        listItem5.setData(QtCore.Qt.UserRole, QtCore.QVariant(sbActor))
        listItem5.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        listItem5.setCheckState(QtCore.Qt.Checked)
        ui_window.listWidget.insertItem(3,listItem5)
        
        iren.Initialize()
        iren.Start()
Beispiel #10
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