一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①vtkBandedPolyDataContourFilter分类着色,②等高线的生成
二:代码及注释
import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkCommonColor import vtkNamedColors, vtkColorSeries from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkPropAssembly, vtkRenderer, vtkRenderWindow, \ vtkRenderWindowInteractor from vtkmodules.vtkFiltersSources import vtkConeSource,vtkCubeSource from vtkmodules.vtkCommonTransforms import vtkTransform from vtkmodules.vtkFiltersGeneral import vtkTransformPolyDataFilter from vtkmodules.vtkFiltersCore import vtkElevationFilter from vtkmodules.vtkFiltersModeling import vtkBandedPolyDataContourFilter from vtkmodules.vtkCommonCore import vtkLookupTable, vtkUnsignedCharArray from vtkmodules.vtkRenderingAnnotation import vtkAnnotatedCubeActor, vtkAxesActor from vtkmodules.vtkInteractionWidgets import vtkOrientationMarkerWidget def main(): colors = vtkNamedColors() ren = vtkRenderer() renWin = vtkRenderWindow() renWin.AddRenderer(ren) renWin.SetSize(640, 480) iRen = vtkRenderWindowInteractor() iRen.SetRenderWindow(renWin) coneSource = vtkConeSource() coneSource.SetCenter(0.0, 0.0, 0.0) coneSource.SetRadius(5.0) coneSource.SetHeight(15.0) coneSource.SetDirection(0, 1, 0) coneSource.SetResolution(60) coneSource.Update() transform = vtkTransform() transform.Scale(1.0, 1.0, 0.75) transF = vtkTransformPolyDataFilter() transF.SetInputConnection(coneSource.GetOutputPort()) transF.SetTransform(transform) bounds = transF.GetOutput().GetBounds() elevation = vtkElevationFilter() elevation.SetInputConnection(transF.GetOutputPort()) elevation.SetLowPoint(0, bounds[2], 0) elevation.SetHighPoint(0, bounds[3], 0) """ vtkBandedPolyDataContourFilter 用于可视化连续标量场的分类着色滤波器 核心作用是:将连续的标量值分成若干“带状区间(band)”,并在几何体上生成对应的等值带或彩带效果 """ bandedContours = vtkBandedPolyDataContourFilter() bandedContours.SetInputConnection(elevation.GetOutputPort()) bandedContours.SetScalarModeToValue() # 使用离散的标量值来定义颜色带 bandedContours.GenerateContourEdgesOn() # 生成一组表示这些颜色带之间边界的几何体 """ GenerateValues 定义要生成的等高线的数量和范围 第一个参数 11,制定了要生成的等高线的数量,意味着数据范围将被分成10个区间 第二个参数,传入生成的标量范围 """ bandedContours.GenerateValues(11, elevation.GetScalarRange()) colorSeries = vtkColorSeries() # 一种颜色方案的集合 colorSeries.SetColorScheme(vtkColorSeries.BREWER_DIVERGING_SPECTRAL_11) # 加载了名为 “Brewer 发散型光谱 11 色” 的专业颜色方案 lut = vtkLookupTable() colorSeries.BuildLookupTable(lut, vtkColorSeries.ORDINAL) coneMapper = vtkPolyDataMapper() coneMapper.SetInputConnection(bandedContours.GetOutputPort()) coneMapper.SetScalarRange(elevation.GetScalarRange()) coneMapper.SetLookupTable(lut) coneActor = vtkActor() coneActor.SetMapper(coneMapper) # 对于边缘的着色 contourLineMapper = vtkPolyDataMapper() contourLineMapper.SetInputData(bandedContours.GetContourEdgesOutput()) # 获取分界线的polydata contourLineMapper.SetScalarRange(elevation.GetScalarRange()) contourLineMapper.SetResolveCoincidentTopologyToPolygonOffset() contourLineActor = vtkActor() contourLineActor.SetMapper(contourLineMapper) contourLineActor.GetProperty().SetColor(colors.GetColor3d('DimGray')) # 添加方向按钮 prop_assembly = MakeAnnotatedCubeActor(colors) om1 = vtkOrientationMarkerWidget() om1.SetOrientationMarker(prop_assembly) om1.SetInteractor(iRen) om1.SetDefaultRenderer(ren) om1.On() om1.InteractiveOn() xyzLabels = ['X', 'Y', 'Z'] scale = [1.0, 1.0, 1.0] axes = MakeAxesActor(scale, xyzLabels) om2 = vtkOrientationMarkerWidget() om2.SetOrientationMarker(axes) om2.SetViewport(0.8, 0, 1.0, 0.2) om2.SetInteractor(iRen) om2.EnabledOn() om2.InteractiveOn() ren.AddActor(coneActor) ren.AddActor(contourLineActor) ren.SetBackground2(colors.GetColor3d('RoyalBlue')) ren.SetBackground(colors.GetColor3d('MistyRose')) ren.GradientBackgroundOn() ren.GetActiveCamera().Azimuth(45) ren.GetActiveCamera().Pitch(-22.5) ren.ResetCamera() renWin.SetSize(600, 600) renWin.Render() renWin.SetWindowName('ColoredAnnotatedCube') renWin.Render() iRen.Start() def MakeAnnotatedCubeActor(colors): annotated_cube = vtkAnnotatedCubeActor() annotated_cube.SetFaceTextScale(0.366) annotated_cube.SetXPlusFaceText("X+") annotated_cube.SetXMinusFaceText("X-") annotated_cube.SetYPlusFaceText("Y+") annotated_cube.SetYMinusFaceText("Y-") annotated_cube.SetZPlusFaceText("Z+") annotated_cube.SetZMinusFaceText("Z-") annotated_cube.GetTextEdgesProperty().SetColor(colors.GetColor3d('Black')) annotated_cube.GetTextEdgesProperty().SetLineWidth(1) annotated_cube.GetXPlusFaceProperty().SetColor( colors.GetColor3d('Turquoise')) annotated_cube.GetXMinusFaceProperty().SetColor( colors.GetColor3d('Turquoise')) annotated_cube.GetYPlusFaceProperty().SetColor( colors.GetColor3d('Mint')) annotated_cube.GetYMinusFaceProperty().SetColor( colors.GetColor3d('Mint')) annotated_cube.GetZPlusFaceProperty().SetColor( colors.GetColor3d('Tomato')) annotated_cube.GetZMinusFaceProperty().SetColor( colors.GetColor3d('Tomato')) annotated_cube.SetXFaceTextRotation(90) annotated_cube.SetYFaceTextRotation(180) annotated_cube.SetZFaceTextRotation(-90) annotated_cube.GetCubeProperty().SetOpacity(0) cube_source = vtkCubeSource() cube_source.Update() face_colors = vtkUnsignedCharArray() face_colors.SetNumberOfComponents(3) face_x_plus = colors.GetColor3ub("Red") face_x_minus = colors.GetColor3ub('Green') face_y_plus = colors.GetColor3ub('Blue') face_y_minus = colors.GetColor3ub('Yellow') face_z_plus = colors.GetColor3ub('Cyan') face_z_minus = colors.GetColor3ub('Magenta') face_colors.InsertNextTuple(face_x_minus) face_colors.InsertNextTypedTuple(face_x_plus) face_colors.InsertNextTypedTuple(face_y_minus) face_colors.InsertNextTypedTuple(face_y_plus) face_colors.InsertNextTypedTuple(face_z_minus) face_colors.InsertNextTypedTuple(face_z_plus) cube_source.GetOutput().GetCellData().SetScalars(face_colors) cube_source.Update() cube_mapper = vtkPolyDataMapper() cube_mapper.SetInputData(cube_source.GetOutput()) cube_mapper.Update() cube_actor = vtkActor() cube_actor.SetMapper(cube_mapper) prop_assembly = vtkPropAssembly() prop_assembly.AddPart(annotated_cube) prop_assembly.AddPart(cube_actor) # 如果没有这个,那么展示的只是字体,没有具体的cube return prop_assembly def MakeAxesActor(scale, xyzLabels): axes = vtkAxesActor() axes.SetScale(scale[0], scale[1], scale[2]) axes.SetShaftTypeToCylinder() axes.SetXAxisLabelText(xyzLabels[0]) axes.SetYAxisLabelText(xyzLabels[1]) axes.SetZAxisLabelText(xyzLabels[2]) axes.SetCylinderRadius(0.5 * axes.GetCylinderRadius()) axes.SetConeRadius(1.025 * axes.GetConeRadius()) axes.SetSphereRadius(1.5 * axes.GetSphereRadius()) tprop = axes.GetXAxisCaptionActor2D().GetCaptionTextProperty() tprop.ItalicOn() tprop.ShadowOn() tprop.SetFontFamilyToTimes() axes.GetYAxisCaptionActor2D().GetCaptionTextProperty().ShallowCopy(tprop) axes.GetZAxisCaptionActor2D().GetCaptionTextProperty().ShallowCopy(tprop) return axes if __name__ == '__main__': main()