一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①有向动态图的创建,②图结构数据的可视化和布局展示
二:代码及注释
import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkCommonCore import vtkIntArray, vtkLookupTable from vtkmodules.vtkCommonDataModel import vtkMutableDirectedGraph from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView from vtkmodules.vtkViewsCore import vtkViewTheme def main(): colors = vtkNamedColors() # 创建一个动态有向图 graph = vtkMutableDirectedGraph() # AddVertex添加节点(顶点) v1 = graph.AddVertex() v2 = graph.AddVertex() v3 = graph.AddVertex() # AddGraphEdge 添加有向边 graph.AddGraphEdge(v1, v2) graph.AddGraphEdge(v2, v3) # 创建颜色数组 edgeColors = vtkIntArray() edgeColors.SetNumberOfValues(1) edgeColors.SetName('Color') edgeColors.InsertNextValue(0) edgeColors.InsertNextValue(1) # 创建颜色查找表 lookupTable = vtkLookupTable() lookupTable.SetNumberOfTableValues(2) lookupTable.SetTableValue(0, colors.GetColor4d("Red")) lookupTable.SetTableValue(1, colors.GetColor4d("Lime")) lookupTable.Build() graph.GetEdgeData().AddArray(edgeColors) """ vtkGraphLayoutView 专门用于图(Graph)结构数据” 的可视化和布局展示 核心功能是:把一个由节点(Vertices)和边(Edges)组成的图结构,自动排布到 2D 或 3D 空间中,并渲染出来供你交互查看 封装了布局(layout),显示(rendering),交互(interaction) """ graphLayoutView = vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(graph) # SetLayoutStrategy 设置布局 graphLayoutView.SetLayoutStrategy('Simple 2D') # graphLayoutView.GetLayoutStrategy().SetEdgeWeightField('Graphs') # graphLayoutView.GetLayoutStrategy().SetWeightEdges(1) # 按属性字段映射边颜色 graphLayoutView.SetEdgeColorArrayName('Color') graphLayoutView.SetEdgeLabelVisibility(1) graphLayoutView.ColorEdgesOn() """ vtkViewTheme 用于统一设置一个 View(比如 vtkGraphLayoutView、vtkTreeMapView、vtkScatterPlotMatrixView 等)的 配色、字体、线条粗细、背景等整体视觉主题 """ theme = 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() if __name__ == '__main__': main()