Tutorial_Step6 vtkBoxWidget的交互与控制

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①如何在ui界面操作一个立方体框,并控制大小

二:代码及注释

python 复制代码
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkConeSource
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkInteractionWidgets import vtkBoxWidget
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)

class vtkMyCallback(object):
    """
    Callback for the interaction.
    """

    def __call__(self, caller, ev):
        t = vtkTransform()
        widget = caller
        widget.GetTransform(t)
        widget.GetProp3D().SetUserTransform(t)

def main():
    colors = vtkNamedColors()

    cone = vtkConeSource()
    cone.SetHeight(3)
    cone.SetRadius(1)
    cone.SetResolution(10)

    coneMapper = vtkPolyDataMapper()
    coneMapper.SetInputConnection(cone.GetOutputPort())

    coneActor = vtkActor()
    coneActor.SetMapper(coneMapper)
    coneActor.GetProperty().SetColor(colors.GetColor3d('Bisque'))

    ren1 = vtkRenderer()
    ren1.AddActor(coneActor)
    ren1.SetBackground(colors.GetColor3d('MidnightBlue'))

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren1)
    renWin.SetSize(300, 300)
    renWin.SetWindowName('Tutorial_Step6')

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    style = vtkInteractorStyleTrackballCamera()
    iren.SetInteractorStyle(style)

    """
    boxWidget 是一个交互式三维小部件,允许用户通过鼠标在场景中操作一个立方体框(box)
    """
    boxWidget = vtkBoxWidget()
    boxWidget.SetInteractor(iren)
    """
    SetProp3D 定义了控件操作的目标对象,建立了控件和实体之间的联系
    """
    boxWidget.SetProp3D(coneActor)
    """
    SetPlaceFactor  设置放置系数或缩放因子
    在 VTK 中,当调用 boxWidget.PlaceWidget() 时,控件会自动计算它所附着的对象(这里是 coneActor)的边界框(Bounding Box),并使用这个边界框来确定自身初始的大小和位置
    """
    boxWidget.SetPlaceFactor(1.25)
    boxWidget.GetOutlineProperty().SetColor(colors.GetColor3d('Gold'))
    boxWidget.PlaceWidget() # 初始化和确定空间在3D空间的初始位置、大小和方向

    callback = vtkMyCallback()
    boxWidget.AddObserver('InteractionEvent', callback)

    boxWidget.On()  # 激活交互式控件,使其在渲染窗口中可见并开始响应用户的鼠标和键盘输入
    iren.Initialize()
    iren.Start()


if __name__ == '__main__':
    main()
相关推荐
花酒锄作田14 小时前
Pydantic校验配置文件
python
hboot15 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python