news 2026/4/15 9:15:19

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/10 8:39:55

Z-Image-Turbo多GPU部署:释放你的创作生产力

Z-Image-Turbo多GPU部署:释放你的创作生产力 为什么需要多GPU部署Z-Image-Turbo 如果你正在使用Z-Image-Turbo进行高分辨率图像生成,可能会遇到单卡性能瓶颈的问题。设计工作室、广告公司等需要批量生成高清图像的场景,单卡往往难以满足业务需…

作者头像 李华
网站建设 2026/4/12 3:48:06

BewlyBewly第三方插件高效配置指南:4大核心模块完整解析

BewlyBewly第三方插件高效配置指南:4大核心模块完整解析 【免费下载链接】BewlyBewly Improve your Bilibili homepage by redesigning it, adding more features, and personalizing it to match your preferences. 项目地址: https://gitcode.com/gh_mirrors/be…

作者头像 李华
网站建设 2026/4/15 6:32:41

notepad++历史版本对比:新增OCR功能是否值得升级

notepad历史版本对比:新增OCR功能是否值得升级 📌 技术背景与升级动因 Notepad 作为广受欢迎的轻量级文本编辑器,长期以来以高效、简洁、插件丰富著称。然而,在 AI 能力快速融入开发工具的当下,其在智能辅助方面的短板…

作者头像 李华
网站建设 2026/4/15 3:42:21

WebODM终极指南:开源无人机地图制作的完整解决方案

WebODM终极指南:开源无人机地图制作的完整解决方案 【免费下载链接】WebODM User-friendly, commercial-grade software for processing aerial imagery. 🛩 项目地址: https://gitcode.com/gh_mirrors/we/WebODM 你是否也曾为昂贵的商业无人机数…

作者头像 李华
网站建设 2026/4/11 18:03:14

图书扫描数字化:OCR替代手动录入全流程

图书扫描数字化:OCR替代手动录入全流程 引言:从纸质到数字,OCR如何重塑信息录入方式 在图书管理、档案数字化和出版行业,大量历史文献仍以纸质形式存在。传统的人工录入方式不仅效率低下(平均每分钟仅能输入20-30字&am…

作者头像 李华
网站建设 2026/4/3 7:24:34

卷积神经网络进阶:CRNN中CNN模块的设计与优化

卷积神经网络进阶:CRNN中CNN模块的设计与优化 📌 引言:OCR文字识别的技术演进与挑战 光学字符识别(OCR)作为连接物理世界与数字信息的关键桥梁,已广泛应用于文档数字化、票据识别、车牌检测等场景。尽管传…

作者头像 李华