news 2026/1/12 13:38:16

ResetCameraOrientation 保存、修改和恢复摄像机的精确视角参数

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ResetCameraOrientation 保存、修改和恢复摄像机的精确视角参数

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①相机参数的获取与保存


二:代码及注释

import vtkmodules.vtkInteractionStyle import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkFiltersSources import vtkConeSource from vtkmodules.vtkRenderingCore import vtkPolyDataMapper, vtkActor, vtkCamera, vtkRenderWindow, \ vtkRenderWindowInteractor, vtkRenderer from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera from time import sleep def comma_separated_list(v, precision=6, width=10): res = list() for p in v: res.append(f'{p:{width}.{precision}f}') return ','.join(res) def get_orientation(render_): p = {} camera = render_.GetActiveCamera() p['position'] = camera.GetPosition() p['focal_point'] = camera.GetFocalPoint() p['view_up'] = camera.GetViewUp() p['distance'] = camera.GetDistance() p['clipping_range'] = camera.GetClippingRange() p['orientation'] = camera.GetOrientation() return p def set_orientation(ren, p): """ Set the orientation of the camera. :param ren: The renderer. :param p: The orientation parameters. :return: """ camera = ren.GetActiveCamera() camera.SetPosition(p['position']) camera.SetFocalPoint(p['focal_point']) camera.SetViewUp(p['view_up']) camera.SetDistance(p['distance']) camera.SetClippingRange(p['clipping_range']) def main(): """ 下列代码中多次出现ren_win.Render() 它的主要作用是触发渲染窗口的绘图过程 在3D渲染中,执行一次 Render() 命令意味着让 VTK 检查所有渲染器、摄像机和3D对象,然后根据最新的状态将最终图像绘制到屏幕上 """ colors = vtkNamedColors() cone = vtkConeSource() cone.SetHeight(3.0) cone.SetRadius(1.0) cone.SetResolution(10) cone.Update() cone_mapper = vtkPolyDataMapper() cone_mapper.SetInputConnection(cone.GetOutputPort()) cone_actor = vtkActor() cone_actor.SetMapper(cone_mapper) cone_actor.GetProperty().SetColor(colors.GetColor3d("Bisque")) ren = vtkRenderer() ren.AddActor(cone_actor) ren.SetBackground(colors.GetColor3d("MidnightBlue")) ren_win = vtkRenderWindow() ren_win.AddRenderer(ren) ren_win.SetSize(600, 600) ren_win.SetWindowName("ResetCameraOrientation") iren = vtkRenderWindowInteractor() iren.SetRenderWindow(ren_win) style = vtkInteractorStyleTrackballCamera() iren.SetInteractorStyle(style) camera = ren.GetActiveCamera() camera.SetRoll(15) # 横向旋转角15° camera.Elevation(-15) # 俯仰角-15° camera.Azimuth(30) # 绕着世界坐标系的Y轴旋转30° ren.ResetCamera() ren_win.Render() original_orient = get_orientation(ren) s = f'{"Original orientation:":23s}' s += comma_separated_list(original_orient["orientation"]) print(s) sleep(1) camera.SetPosition(-3.568189, 5.220048, 2.352639) camera.SetFocalPoint(-0.399044, -0.282865, 0.131438) camera.SetViewUp(0.623411, 0.573532, -0.531431) camera.SetDistance(6.727500) camera.SetClippingRange(3.001430, 11.434082) ren_win.Render() new_orient = get_orientation(ren) s = f'{"New orientation:":23s}' s += comma_separated_list(new_orient["orientation"]) print(s) sleep(1) print('Reloading the original orientation.') set_orientation(ren, original_orient) ren_win.Render() check = get_orientation(ren) s = f'{"Final orientation:":23s}' s += comma_separated_list(check["orientation"]) print(s) sleep(1) iren.Initialize() iren.Start() if __name__ == '__main__': main()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/10 21:16:21

CUDA安装太复杂?试试这个预集成的PyTorch镜像

CUDA安装太复杂?试试这个预集成的PyTorch镜像 在深度学习项目中,你是否也经历过这样的场景:满怀期待地打开新电脑,准备复现一篇论文或训练一个模型,结果卡在第一步——torch.cuda.is_available() 返回了 False&#xf…

作者头像 李华
网站建设 2026/1/10 21:16:17

PyTorch-CUDA-v2.7镜像是否支持动态图机制

PyTorch-CUDA-v2.7镜像是否支持动态图机制 在深度学习工程实践中,一个看似简单却常被忽视的问题是:我们每天使用的预构建容器镜像,是否真的保留了框架最核心的开发体验?比如,当你拉下 pytorch-cuda:v2.7 这个镜像时&am…

作者头像 李华
网站建设 2026/1/10 21:16:16

PyTorch-CUDA-v2.7镜像是否支持MPS(Apple芯片)

PyTorch-CUDA-v2.7镜像是否支持MPS(Apple芯片) 在深度学习开发日益普及的今天,越来越多开发者面临一个现实问题:我手上的 M1/M2 Mac 能不能跑 GPU 加速?如果我在用 Docker,还能不能享受到 Apple Silicon 的…

作者头像 李华
网站建设 2026/1/10 21:16:14

Markdown编写技术博客:记录你的第一次PyTorch模型训练

Markdown编写技术博客:记录你的第一次PyTorch模型训练 在深度学习的探索之路上,很多人第一次真正“跑通”模型的瞬间,都伴随着环境配置失败、CUDA不可用、版本冲突等一连串令人头大的问题。你是不是也曾因为 torch.cuda.is_available() 返回 …

作者头像 李华
网站建设 2026/1/10 21:16:12

基于Docker Compose的PyTorch-CUDA开发环境搭建实战

基于Docker Compose的PyTorch-CUDA开发环境搭建实战 在AI项目开发中,最让人头疼的往往不是模型设计或算法调优,而是“环境配置”这个前置环节。你是否也遇到过这样的场景:同事发来一个Jupyter Notebook,声称训练效果极佳&#xf…

作者头像 李华