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 __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkRandomGraphSource(), 'Processing.', (), ('vtkGraph',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def loadGraph(self): subgraph = vtk.vtkRandomGraphSource() self.view.SetRepresentationFromInputConnection(subgraph.GetOutputPort()) self.view.ResetCamera() self.view.Render() self.view.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 RunTest(): fail = False print "TEST SET 1 - verify reader/writer work for range of canonical datasets" print MemUsage("Before starting TEST SET 1") dog = vtk.vtkDataObjectGenerator() i = 0 for testObject in testObjects: fileName = "xdmfIOtest_" + str(i) print "Test vtk object", testObject dog.SetProgram(testObject) dog.Update() TestXdmfConversion(dog.GetOutput(), fileName) i += 1 print "TEST SET 2 - verify reader/writer work for Graphs" print MemUsage("Before starting TEST SET 2") print "Test Graph data" gsrc = vtk.vtkRandomGraphSource() gsrc.DirectedOn() gsrc.Update() gFilePrefix = "xdmfIOtest_Graph" gFileName = OutputDir + gFilePrefix + ".xdmf" ghFileName = OutputDir + gFilePrefix + ".h5" xWriter = vtk.vtkXdmf3Writer() xWriter.SetLightDataLimit(LightDataLimit) xWriter.SetFileName(gFileName) xWriter.SetInputConnection(0, gsrc.GetOutputPort(0)) timer.StartTimer() xWriter.Write() timer.StopTimer() print "vtkXdmf3Writer took", timer.GetElapsedTime(), "seconds to write",\ gFileName xReader = vtk.vtkXdmf3Reader() xReader.SetFileName(gFileName) xReader.Update() rOutput = xReader.GetOutputDataObject(0) fail = DoDataObjectsDiffer(gsrc.GetOutputDataObject(0), xReader.GetOutputDataObject(0)) if fail: raiseErrorAndExit("Failed graph conversion test") if not DoFilesExist(gFileName, ghFileName, None, CleanUpGood): raiseErrorAndExit("Failed to write Graph file") print "TEST SET 3 - verify reader/writer handle time varying data" print MemUsage("Before starting TEST SET 3") print "Test temporal data" tsrc = vtk.vtkTimeSourceExample() tsrc.GrowingOn() tsrc.SetXAmplitude(2.0) tFilePrefix = "xdmfIOTest_Temporal" tFileName = OutputDir + tFilePrefix + ".xdmf" thFileName = OutputDir + tFilePrefix + ".h5" xWriter = vtk.vtkXdmf3Writer() xWriter.SetLightDataLimit(LightDataLimit) xWriter.WriteAllTimeStepsOn() xWriter.SetFileName(tFileName) xWriter.SetInputConnection(0, tsrc.GetOutputPort(0)) timer.StartTimer() xWriter.Write() timer.StopTimer() print "vtkXdmf3Writer took", timer.GetElapsedTime(), "seconds to write",\ tFileName xReader = vtk.vtkXdmf3Reader() xReader.SetFileName(tFileName) xReader.UpdateInformation() oi = xReader.GetOutputInformation(0) timerange = oi.Get(vtk.vtkCompositeDataPipeline.TIME_STEPS()) ii = tsrc.GetOutputInformation(0) correcttimes = ii.Get(vtk.vtkCompositeDataPipeline.TIME_STEPS()) #compare number of and values for temporal range if len(timerange) != len(correcttimes): print "timesteps failed" print timerange, "!=", correcttimes raiseErrorAndExit("Failed to get same times") for i in xrange(0, len(correcttimes)): if abs(abs(timerange[i]) - abs(correcttimes[i])) > 0.000001: print "time result failed" print timerange, "!=", correcttimes raiseErrorAndExit("Failed to get same times") #exercise temporal processing and compare geometric bounds at each tstep indices = range(0, len(timerange)) + range(len(timerange) - 2, -1, -1) for x in indices: xReader.GetExecutive().SetUpdateTimeStep(0, timerange[x]) xReader.Update() obds = xReader.GetOutputDataObject(0).GetBounds() tsrc.GetExecutive().SetUpdateTimeStep( 0, timerange[x] + 0.0001) #workaround a precision bug in TSE tsrc.Update() ibds = tsrc.GetOutputDataObject(0).GetBounds() print timerange[x], obds for i in (0, 1, 2, 3, 4, 5): if abs(abs(obds[i]) - abs(ibds[i])) > 0.000001: print "time result failed" print obds, "!=", ibds raiseErrorAndExit("Failed to get same data for this timestep") fail = DoFilesExist(tFileName, thFileName, None, CleanUpGood) if not fail: raiseErrorAndExit("Failed Temporal Test") print MemUsage("End of Testing")
#!/usr/bin/env python import vtk source = vtk.vtkRandomGraphSource() source.Update() view = vtk.vtkGraphLayoutView() view.AddRepresentationFromInputConnection(source.GetOutputPort()) def selectionCallback(caller, event): # In C++ there is some extra data passed to the callback, but in Python # the callback data is lost... # There should be two selection nodes, but which one is vertices and which # is edges does not seem to be guaranteed... sel = caller.GetCurrentSelection() node0 = sel.GetNode(0) node0_field_type = node0.GetFieldType() sel_list0 = caller.GetCurrentSelection().GetNode(0).GetSelectionList() node1 = sel.GetNode(1) node1_field_type = node1.GetFieldType() sel_list1 = caller.GetCurrentSelection().GetNode(1).GetSelectionList() if (sel_list0.GetNumberOfTuples() > 0): printFieldType(node0_field_type) for ii in range(sel_list0.GetNumberOfTuples()): print "\t", sel_list0.GetValue(ii) if (sel_list1.GetNumberOfTuples() > 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()
def RunTest(): fail = False print "TEST SET 1 - verify reader/writer work for range of canonical datasets" print MemUsage("Before starting TEST SET 1") dog = vtk.vtkDataObjectGenerator() i = 0 for testObject in testObjects: fileName = "xdmfIOtest_" + str(i) print "Test vtk object", testObject dog.SetProgram(testObject) dog.Update() TestXdmfConversion(dog.GetOutput(), fileName) i += 1 print "TEST SET 2 - verify reader/writer work for Graphs" print MemUsage("Before starting TEST SET 2") print "Test Graph data" gsrc = vtk.vtkRandomGraphSource() gsrc.DirectedOn() gsrc.Update() gFilePrefix = "xdmfIOtest_Graph" gFileName = OutputDir + gFilePrefix + ".xdmf" ghFileName = OutputDir + gFilePrefix + ".h5" xWriter = vtk.vtkXdmf3Writer() xWriter.SetLightDataLimit(LightDataLimit) xWriter.SetFileName(gFileName) xWriter.SetInputConnection(0, gsrc.GetOutputPort(0)) timer.StartTimer() xWriter.Write() timer.StopTimer() print "vtkXdmf3Writer took", timer.GetElapsedTime(), "seconds to write",\ gFileName xReader = vtk.vtkXdmf3Reader() xReader.SetFileName(gFileName) xReader.Update() rOutput = xReader.GetOutputDataObject(0) fail = DoDataObjectsDiffer(gsrc.GetOutputDataObject(0), xReader.GetOutputDataObject(0)) if fail: raiseErrorAndExit("Failed graph conversion test") if not DoFilesExist(gFileName, ghFileName, None, CleanUpGood): raiseErrorAndExit("Failed to write Graph file") print "TEST SET 3 - verify reader/writer handle time varying data" print MemUsage("Before starting TEST SET 3") print "Test temporal data" tsrc = vtk.vtkTimeSourceExample() tsrc.GrowingOn() tsrc.SetXAmplitude(2.0) tFilePrefix = "xdmfIOTest_Temporal" tFileName = OutputDir + tFilePrefix + ".xdmf" thFileName = OutputDir + tFilePrefix + ".h5" xWriter = vtk.vtkXdmf3Writer() xWriter.SetLightDataLimit(LightDataLimit) xWriter.WriteAllTimeStepsOn() xWriter.SetFileName(tFileName) xWriter.SetInputConnection(0, tsrc.GetOutputPort(0)) timer.StartTimer() xWriter.Write() timer.StopTimer() print "vtkXdmf3Writer took", timer.GetElapsedTime(), "seconds to write",\ tFileName xReader = vtk.vtkXdmf3Reader() xReader.SetFileName(tFileName) xReader.UpdateInformation() oi = xReader.GetOutputInformation(0) timerange = oi.Get(vtk.vtkCompositeDataPipeline.TIME_STEPS()) ii = tsrc.GetOutputInformation(0) correcttimes = ii.Get(vtk.vtkCompositeDataPipeline.TIME_STEPS()) #compare number of and values for temporal range if len(timerange) != len(correcttimes): print "timesteps failed" print timerange, "!=", correcttimes raiseErrorAndExit("Failed to get same times") for i in xrange(0, len(correcttimes)): if abs(abs(timerange[i])-abs(correcttimes[i])) > 0.000001: print "time result failed" print timerange, "!=", correcttimes raiseErrorAndExit("Failed to get same times") #exercise temporal processing and compare geometric bounds at each tstep indices = range(0,len(timerange)) + range(len(timerange)-2,-1,-1) for x in indices: xReader.UpdateTimeStep(timerange[x]) obds = xReader.GetOutputDataObject(0).GetBounds() tsrc.Update(timerange[x]+0.0001) #workaround a precision bug in TSE ibds = tsrc.GetOutputDataObject(0).GetBounds() print timerange[x], obds for i in (0,1,2,3,4,5): if abs(abs(obds[i])-abs(ibds[i])) > 0.000001: print "time result failed" print obds, "!=", ibds raiseErrorAndExit("Failed to get same data for this timestep") fail = DoFilesExist(tFileName, thFileName, None, CleanUpGood) if not fail: raiseErrorAndExit("Failed Temporal Test") print MemUsage("End of Testing")