news 2026/10/26 7:36:43

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/10/24 15:24:03

CRNN OCR在建筑行业的应用:施工图纸文字识别系统

CRNN OCR在建筑行业的应用:施工图纸文字识别系统 📖 项目背景与行业痛点 在建筑工程领域,施工图纸是设计、施工、验收等各环节的核心依据。一张完整的施工图往往包含大量标注信息——如构件尺寸、材料规格、标高说明、技术备注等,…

作者头像 李华
网站建设 2026/10/25 9:58:38

Text-to-CAD:用文字描述快速生成专业CAD图纸的AI革命

Text-to-CAD:用文字描述快速生成专业CAD图纸的AI革命 【免费下载链接】text-to-cad-ui A lightweight UI for interfacing with the Zoo text-to-cad API, built with SvelteKit. 项目地址: https://gitcode.com/gh_mirrors/te/text-to-cad-ui 还在为复杂的C…

作者头像 李华
网站建设 2026/10/25 9:59:51

终极存储设备检测指南:三步快速验证U盘SD卡真实容量

终极存储设备检测指南:三步快速验证U盘SD卡真实容量 【免费下载链接】f3 F3 - Fight Flash Fraud 项目地址: https://gitcode.com/gh_mirrors/f3/f3 在数字化时代,存储设备已成为我们生活和工作中不可或缺的伙伴。然而,市场上充斥着大…

作者头像 李华
网站建设 2026/10/24 2:21:04

RESTful API设计规范:OCR服务接口标准化

RESTful API设计规范:OCR服务接口标准化 📖 项目背景与技术选型动因 在数字化转型加速的今天,光学字符识别(OCR) 已成为文档自动化、信息提取和智能审核等场景的核心技术。传统OCR方案往往依赖重型模型或GPU推理环境&a…

作者头像 李华
网站建设 2026/10/25 10:51:02

终极指南:3步掌握Chrome扩展智能抓取网页素材的高效方法

终极指南:3步掌握Chrome扩展智能抓取网页素材的高效方法 【免费下载链接】ResourcesSaverExt Chrome Extension for one click downloading all resources files and keeping folder structures. 项目地址: https://gitcode.com/gh_mirrors/re/ResourcesSaverExt …

作者头像 李华