news 2026/4/30 16:08:11

ColoredAnnotatedCube 等高线与方向标记

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ColoredAnnotatedCube 等高线与方向标记

一:主要的知识点

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()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/29 15:37:28

Redmi AX3000终极改造:解锁隐藏性能的完整系统升级方案

Redmi AX3000终极改造:解锁隐藏性能的完整系统升级方案 【免费下载链接】openwrt-redmi-ax3000 Openwrt for Redmi AX3000 / Xiaomi CR8806 / Xiaomi CR8808 / Xiaomi CR8809 项目地址: https://gitcode.com/gh_mirrors/op/openwrt-redmi-ax3000 想要彻底释放…

作者头像 李华
网站建设 2026/4/26 10:57:05

Roblox帧率解锁终极指南:突破60帧限制的完整解决方案

Roblox帧率解锁终极指南:突破60帧限制的完整解决方案 【免费下载链接】rbxfpsunlocker FPS Unlocker for Roblox 项目地址: https://gitcode.com/gh_mirrors/rb/rbxfpsunlocker 想要在Roblox游戏中获得更流畅、更响应的游戏体验吗?Roblox FPS Unl…

作者头像 李华
网站建设 2026/4/28 21:24:12

数学建模Matlab算法,第十章 数据的统计描述和分析

数据的统计描述和分析:从样本洞察总体的数学范式 在大数据时代,数据已成为洞察规律、辅助决策的核心要素,而受随机因素影响的统计数据更是各类科学研究与工程实践的核心对象。数理统计(简称统计)以概率论为理论基石,通过对有限样本数据的整理、分析和推断,揭示总体的数…

作者头像 李华
网站建设 2026/4/28 5:44:53

数学建模Matlab算法,第十一章 方差分析

方差分析:多因素影响下的统计推断方法与实践 在科学研究与生产实践中,人们常常需要分析多个因素对某一指标的影响。例如,比较不同工艺对灯泡寿命的影响、分析化肥与小麦品种对产量的作用、探究地理位置与广告形式对商品销量的作用等。这类问题的核心是检验多个总体的均值是…

作者头像 李华
网站建设 2026/4/7 15:58:39

数学建模Matlab算法,第十二章 回归分析

回归分析:从数据拟合到统计推断的系统方法 在数据分析领域,曲线拟合是处理变量间关系的基础手段,但仅通过最小二乘法计算待定系数,无法回答 “拟合结果是否可靠”“变量对结果的影响是否显著”“模型能否用于预测” 等核心问题。回归分析作为拟合问题的统计延伸,将随机变…

作者头像 李华
网站建设 2026/4/22 5:58:42

3D点云智能标注终极指南:从入门到精通的全流程解析

3D点云智能标注终极指南:从入门到精通的全流程解析 【免费下载链接】point-cloud-annotation-tool 项目地址: https://gitcode.com/gh_mirrors/po/point-cloud-annotation-tool 在自动驾驶技术飞速发展的当下,高效精准的点云数据标注已成为算法训…

作者头像 李华