news 2026/5/5 12:11:36

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/20 16:35:02

零基础入门:用预装镜像快速搭建你的第一个Z-Image二次开发环境

零基础入门:用预装镜像快速搭建你的第一个Z-Image二次开发环境 如果你是一名刚接触AI开发的大学生,想要基于Z-Image-Turbo进行课程项目开发,却被Python环境配置和依赖管理搞得焦头烂额,那么这篇文章正是为你准备的。本文将带你使用…

作者头像 李华
网站建设 2026/5/1 7:53:54

终极DLC解锁指南:3步实现全平台自动化解锁

终极DLC解锁指南:3步实现全平台自动化解锁 【免费下载链接】CreamApi 项目地址: https://gitcode.com/gh_mirrors/cr/CreamApi 还在为游戏DLC内容无法完整体验而苦恼吗?CreamApi作为一款革命性的自动化解锁工具,让零基础玩家也能轻松…

作者头像 李华
网站建设 2026/5/3 12:35:18

从被动拦截到主动降维:AURA 开启 AI 知识资产防护新纪元

一、引言:AI时代知识图谱的“攻防战”进入深水区 随着大语言模型(LLM)与知识图谱(KG)的深度融合,GraphRAG技术已成为企业构建核心竞争力的关键支撑——从金融行业的智能风控、医疗领域的临床决策&#xff0…

作者头像 李华
网站建设 2026/5/3 9:23:19

如何高效使用Magicodes.IE:.NET数据处理的完整解决方案

如何高效使用Magicodes.IE:.NET数据处理的完整解决方案 【免费下载链接】Magicodes.IE 项目地址: https://gitcode.com/gh_mirrors/mag/Magicodes.IE 在.NET开发中,数据导入导出是每个项目都无法回避的核心需求。Magicodes.IE作为一个强大的数据…

作者头像 李华
网站建设 2026/4/30 11:04:20

轻量级OCR部署实践:自动预处理+高精度识别全流程

轻量级OCR部署实践:自动预处理高精度识别全流程 📖 技术背景与核心挑战 光学字符识别(OCR)作为连接物理世界与数字信息的关键桥梁,广泛应用于文档数字化、票据识别、车牌提取、工业质检等场景。然而,在真实…

作者头像 李华
网站建设 2026/4/30 18:55:58

CRNN模型在医疗影像报告识别中的应用

CRNN模型在医疗影像报告识别中的应用 📖 项目背景:OCR技术在医疗场景中的关键价值 随着电子病历系统(EMR)和医学影像归档与通信系统(PACS)的普及,医疗机构积累了海量的非结构化数据——其中&…

作者头像 李华