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

一:主要的知识点

1、说明

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

2、知识点纪要

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

二:代码及注释

python 复制代码
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()
相关推荐
52Hz11821 小时前
力扣73.矩阵置零、54.螺旋矩阵、48.旋转图像
python·算法·leetcode·矩阵
weixin_462446231 天前
Python 使用 openpyxl 从 URL 读取 Excel 并获取 Sheet 及单元格样式信息
python·excel·openpyxl
毕设源码-钟学长1 天前
【开题答辩全过程】以 基于Python的健康食谱规划系统的设计与实现为例,包含答辩的问题和答案
开发语言·python
百***78751 天前
Grok-4.1技术深度解析:双版本架构突破与Python API快速集成指南
大数据·python·架构
2501_942191771 天前
基于YOLO11-HSFPN的数字检测与识别模型实现详解
python
忧郁的橙子.1 天前
26期_01_Pyhton基本语法
python
sunfove1 天前
实战篇:用 Python 徒手实现模拟退火算法解决 TSP 问题
开发语言·python·模拟退火算法
我是菜鸟0713号1 天前
Qt + Python 算法集成的一种低耦合实践:FastAPI 服务化方案
python·qt·fastapi
我是一只小青蛙8881 天前
TraeCNIDE Python开发全流程指南
python
欣然~1 天前
法律案例 PDF 批量转 TXT 工具代码
linux·前端·python