news 2026/5/1 6:51:00

CallBack 两种回调方式

作者头像

张小明

前端开发工程师

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

企业级WPS离线部署实战:200台电脑批量安装指南

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个企业级WPS Office批量部署解决方案,包含:1.制作包含所有组件的完整离线安装包 2.开发静默安装脚本(无界面自动安装) 3.局域网分发工具 4.安装进度监…

作者头像 李华
网站建设 2026/4/29 12:46:09

比CHMOD -R 777更高效:智能批量权限管理工具

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个智能的Linux文件权限批量管理工具,替代危险的CHMOD -R 777命令。功能包括:1) 递归分析目录结构 2) 智能识别文件类型 3) 自动应用最小必要权限 4) …

作者头像 李华
网站建设 2026/4/20 1:20:09

AI如何自动化生成DHCP检测工具?

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个DHCP检测工具,能够自动扫描局域网内的DHCP服务器,检测IP地址分配情况,识别IP冲突,并生成可视化报告。要求:1.支…

作者头像 李华
网站建设 2026/5/1 5:52:13

零基础玩转通义千问2.5-0.5B:树莓派AI助手实战

零基础玩转通义千问2.5-0.5B:树莓派AI助手实战 你是否想过,让一台树莓派也能运行大模型?不是“能跑就行”的玩具级体验,而是真正具备代码生成、数学推理、多语言对话、结构化输出能力的轻量级AI助手? 今天&#xff0…

作者头像 李华
网站建设 2026/4/20 18:00:35

小白必看!通义千问2.5轻量版保姆级安装教程

小白必看!通义千问2.5轻量版保姆级安装教程 你是否也想在自己的手机、树莓派甚至老旧笔记本上运行一个真正“全功能”的大模型?不是只能聊聊天的玩具模型,而是能写代码、做数学、输出 JSON、支持多语言的真实生产力工具? 今天&a…

作者头像 李华