news 2026/4/18 11:09:31

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/18 3:59:53

元宇宙avatar驱动:Unity+骨骼点镜像,实时动作捕捉教程

元宇宙avatar驱动:Unity骨骼点镜像,实时动作捕捉教程 引言:让虚拟形象活起来 想象一下,你正在开发一款元宇宙社交应用,用户只需打开摄像头,就能用自己的真实动作控制虚拟形象跳舞、打招呼甚至打篮球。这种…

作者头像 李华
网站建设 2026/4/18 5:34:05

LLS开发入门:从零开始构建你的第一个项目

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 生成一个适合初学者的LLS入门项目代码,功能包括:1. LED闪烁控制;2. 按钮输入检测;3. 简单的串口调试输出。使用Arduino框架&#xf…

作者头像 李华
网站建设 2026/4/18 10:53:26

AI如何智能处理ROUNDINGMODE?提升开发效率

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个Python代码示例,展示AI如何根据不同的ROUNDINGMODE(如ROUND_HALF_UP, ROUND_FLOOR等)自动处理浮点数运算。要求包含:1) 自动…

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

AI人脸隐私卫士能否识别戴口罩人脸?实际测试部署报告

AI人脸隐私卫士能否识别戴口罩人脸?实际测试部署报告 1. 引言:AI 人脸隐私卫士的现实挑战 随着公众对数字隐私的关注日益提升,图像中的人脸信息保护已成为数据合规与伦理安全的重要议题。尤其在社交媒体、公共监控、医疗影像等场景下&#…

作者头像 李华
网站建设 2026/4/18 8:05:24

手部姿态估计教程:MediaPipe Hands模型原理详解

手部姿态估计教程:MediaPipe Hands模型原理详解 1. 引言:AI手势识别与人机交互的演进 1.1 技术背景与应用场景 随着人工智能在计算机视觉领域的深入发展,手势识别正成为人机交互(HMI)的重要入口。从智能穿戴设备到虚…

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

如何快速完成PMX到VRM转换:新手完整指南

如何快速完成PMX到VRM转换:新手完整指南 【免费下载链接】VRM-Addon-for-Blender VRM Importer, Exporter and Utilities for Blender 2.93 or later 项目地址: https://gitcode.com/gh_mirrors/vr/VRM-Addon-for-Blender 在3D内容创作的世界中,模…

作者头像 李华