当前位置: 首页 > news >正文

CallBack 两种回调方式

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①vtk的两种回调方式函数回调和类方法回调


二:代码及注释

import vtkmodules.vtkInteractionStyle import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkRenderWindow, vtkRenderWindowInteractor, \ vtkRenderer, vtkCamera from vtkmodules.vtkFiltersSources import vtkConeSource from vtkmodules.vtkFiltersModeling import vtkOutlineFilter from vtkmodules.vtkRenderingAnnotation import vtkAxesActor from vtkmodules.vtkInteractionWidgets import vtkOrientationMarkerWidget def main(): use_function_callback = True colors = vtkNamedColors() ren = vtkRenderer() renWin = vtkRenderWindow() renWin.AddRenderer(ren) iren = vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) source = vtkConeSource() source.SetCenter(0, 0, 0) source.SetRadius(1) source.SetHeight(1.61) source.SetResolution(128) source.Update() mapper = vtkPolyDataMapper() mapper.SetInputConnection(source.GetOutputPort()) actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(colors.GetColor3d("peacock")) actor.GetProperty().SetAmbient(0.3) actor.GetProperty().SetDiffuse(0.0) actor.GetProperty().SetSpecular(1.0) actor.GetProperty().SetSpecularPower(20.0) outline = vtkOutlineFilter() outline.SetInputConnection(source.GetOutputPort()) outlineMapper = vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort()) outlineActor = vtkActor() outlineActor.GetProperty().SetColor(colors.GetColor3d('Black')) outlineActor.SetMapper(outlineMapper) ren.AddActor(actor) ren.AddActor(outlineActor) ren.SetBackground(colors.GetColor3d('AliceBlue')) renWin.SetSize(512, 512) camera = vtkCamera() camera.SetPosition(4.6, -2.0, 3.8) camera.SetFocalPoint(0, 0, 0) camera.SetClippingRange(3.2, 10.2) # 摄像机的近平面(Near Clipping Plane)和远平面(Far Clipping Plane)之间的可见空间范围 camera.SetViewUp(0.3, 1.0, 0.13) ren.SetActiveCamera(camera) renWin.Render() renWin.SetWindowName('CallBack') axes1 = MakeAxesActor() om1 = vtkOrientationMarkerWidget() om1.SetOrientationMarker(axes1) om1.SetViewport(0, 0, 0.2, 0.2) om1.SetInteractor(iren) om1.EnabledOn() om1.InteractiveOn() """ 这里是两种回调方法,一种是函数回调,即GetOrientation,另外一种是类方法回调,即OrientationObserver, """ if use_function_callback: """ 在这段代码之前,要先解释一件事情,在 Python 中,函数不仅仅是一段可执行的代码块,它本身也是一个对象(就像数字、字符串、列表一样)。 那么,函数对象也可以拥有属性 下面这段代码 def my_function(a, b): return a + b # my_function 是一个对象。现在我们给它添加一个属性叫做 'data' my_function.data = "这是函数my_function存储的数据" # 我们可以随时访问这个属性 print(my_function.data) # 输出: 这是函数my_function存储的数据 """ GetOrientation.cam = ren.GetActiveCamera() iren.AddObserver('EndInteractionEvent', GetOrientation) """ 为什么要这样写??? VTK 的 iren.AddObserver 要求回调函数必须只接受两个参数:caller(事件发起者)和 ev(事件类型) 想在GetOrientation里面操作相机,又不能参数传入,就只能通过这种方式 在回调之前,把相机对象存进GetOrientation函数里面 """ else: iren.AddObserver('EndInteractionEvent', OrientationObserver(ren.GetActiveCamera())) def GetOrientation(caller, ev): print(caller.GetClassName(), 'Event Id:', ev) # 输出:vtkWin32RenderWindowInteractor Event Id: EndInteractionEvent CameraOrientation(GetOrientation.cam) def CameraOrientation(cam): fmt1 = '{:>15s}' fmt2 = '{:9.6g}' print(fmt1.format('Position:'), ', '.join(map(fmt2.format, cam.GetPosition()))) print(fmt1.format('Focal point:'), ', '.join(map(fmt2.format, cam.GetFocalPoint()))) print(fmt1.format('Clipping range:'), ', '.join(map(fmt2.format, cam.GetClippingRange()))) print(fmt1.format('View up:'), ', '.join(map(fmt2.format, cam.GetViewUp()))) print(fmt1.format('Distance:'), fmt2.format(cam.GetDistance())) class OrientationObserver(object): def __init__(self, cam): self.cam = cam def __call__(self, caller, ev): print(caller.GetClassName(), 'Event Id:', ev) CameraOrientation(self.cam) def MakeAxesActor(): axes = vtkAxesActor() axes.SetShaftTypeToCylinder() axes.SetXAxisLabelText('X') axes.SetYAxisLabelText('Y') axes.SetZAxisLabelText('Z') axes.SetTotalLength(1.0, 1.0, 1.0) axes.SetCylinderRadius(0.5 * axes.GetCylinderRadius()) axes.SetConeRadius(1.025 * axes.GetConeRadius()) axes.SetSphereRadius(1.5 * axes.GetSphereRadius()) return axes if __name__ == '__main__': main()
http://www.jsqmd.com/news/238460/

相关文章:

  • 多租户虚拟线程隔离关键技术突破(仅限资深架构师阅读的内部资料)
  • Z-Image照片级生成实战:云端1小时1块,比本地快5倍
  • MediaPipe Hands与OpenCV协同:图像处理增强实战
  • 揭秘静态反射元数据提取全过程:3步实现零成本运行时洞察
  • 游戏玩家必看:MSVCR120.DLL丢失的5种实战修复方法
  • CellPicking 网格面的选择与变色(vtkCellPicker)
  • AI如何用sprintf简化你的字符串格式化代码
  • AI数智政工软件系统:把思想政治工作装上“智慧大脑”
  • CPU亲和性绑定你真的懂吗:99%的工程师忽略的关键细节
  • 手势交互系统设计:MediaPipe Hands最佳实践
  • Windows 11安装全攻略:轻松解决硬件限制与驱动兼容性问题
  • 树莓派也能跑大模型!通义千问2.5-0.5B轻量部署实测
  • AI人脸隐私卫士如何应对戴墨镜人脸?眼部遮挡检测实测
  • 1分钟创建测试用MSI文件的秘密技巧
  • Blender VRM插件终极指南:从安装到精通的完整攻略
  • GLM-4.6V-Flash-WEB部署教程:单卡A10G高效运行实测
  • 通义千问2.5-0.5B避坑指南:从部署到应用的全流程解析
  • 开源多模态模型推荐:GLM-4.6V-Flash-WEB镜像开箱即用
  • GEOSERVER性能优化:从30秒到3秒的飞跃
  • VibeVoice-TTS对话一致性优化:多说话人身份保持技巧
  • AWK vs Python:文本处理效率终极对比
  • 是否支持多语言?GLM-4.6V-Flash-WEB功能实测指南
  • 手势识别在安防中的应用:MediaPipe Hands实践分享
  • 虚拟线程在函数式API中的应用(你不可不知的10个优化技巧)
  • MelonLoader终极指南:Unity游戏模组加载器完全掌握
  • AI如何帮你轻松应对JAVA基础面试题?
  • GORK官网对比传统开发:效率提升10倍的秘密
  • AI手势识别与追踪环境部署:Linux下极速CPU版配置要点
  • 电脑小白也能懂:WORD打不开文件的简单修复方法
  • 手把手教学:Z-Image-ComfyUI云端部署,小白也能轻松搞定