、
一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①如何给图的顶点添加标签并展示
二:代码及注释
import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkCommonCore import vtkIntArray from vtkmodules.vtkCommonDataModel import vtkMutableDirectedGraph from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView, vtkRenderedGraphRepresentation from vtkmodules.vtkViewsCore import vtkViewTheme def main(): colors = vtkNamedColors() graph = vtkMutableDirectedGraph() v1 = graph.AddVertex() v2 = graph.AddVertex() graph.AddEdge(v1, v2) vertexIDs = vtkIntArray() vertexIDs.SetNumberOfComponents(1) vertexIDs.SetName('VertexIDs') vertexIDs.InsertNextValue(0) vertexIDs.InsertNextValue(1) graph.GetVertexData().AddArray(vertexIDs) graphLayoutView = vtkGraphLayoutView() graphLayoutView.AddRepresentationFromInput(graph) graphLayoutView.SetVertexLabelVisibility(1) """ vtkRenderedGraphRepresentation 是 VTK 中一个 核心的“渲染层封装类”,专门用于在 3D 或 2D 场景中显示(render)图结构(Graph)数据的 位于 VTK 信息可视化(Infovis)渲染框架 的中间层,主要负责将抽象的图数据(vtkGraph)转换成实际可以渲染的几何对象(vtkActor / vtkProp) """ rGraph = vtkRenderedGraphRepresentation() rGraph.SafeDownCast(graphLayoutView.GetRepresentation()).GetVertexLabelTextProperty().SetColor( colors.GetColor3d('Red')) graphLayoutView.SetLayoutStrategyToSimple2D() graphLayoutView.SetVertexLabelArrayName('VertexIDs') graphLayoutView.SetVertexLabelVisibility(True) graphLayoutView.ResetCamera() graphLayoutView.GetRenderer().GetActiveCamera().Zoom(0.8) graphLayoutView.Render() graphLayoutView.GetInteractor().Start() if __name__ == '__main__': main()