三、vtkInteractorStyleTrackballCamera 的使用

一、概述

1、vtkInteractorStyleTrackballCamera是VTK中用于控制相机(视角)交互的默认样式。它模拟了一个虚拟的”轨迹球“(Trackball),用户可以通过鼠标操作来旋转、平移和缩放场景中的视图。

2、这种交互方式类似于常见的3D软件中的相机控制,非常适合用于三维数据的可视化

二、主要功能

1、旋转场景:按住鼠标左键,可以绕场景中心旋转

2、平移场景:按住鼠标中键(或shift+鼠标左键)并拖到,可以实现平移视图

3、缩放场景:按住鼠标滚轮,可以实现缩放视图

4、重置视图:按下r键可以将视图重置为初始状态

5、退出程序:按下e键

6、线框模式:按下w键

7、表面模式:按下s键

三、使用步骤

 1、创建vtkRenderWindowInteractor:VTK的交互器,用于处理用户输入事件

vtkNew<vtkRenderWindowInteractor> iren;  //创建交互器: 与平台无关的渲染窗互,包括拾取和帧速率控制。
iren->SetRenderWindow(renWin.Get());

2、创建vtkInteractorStyleTrankballCamera :相机的交互操作

 vtkNew<vtkInteractorStyleTrackballCamera> style;
 iren->SetInteractorStyle(style.Get());

3、启动交互器 

 iren->Start();

四、扩展

1、可以将vtkInteractorStyleTrackballCamera与如vtkBoxWidget、vtkSphereWidget等交互工具结合使用,实现更强大的交互功能

namespace {
class vtkMyCallback : public vtkCommand
{
public:
    static vtkMyCallback* New()
    {
        return new vtkMyCallback;
    }

    void Execute(vtkObject* caller, unsigned long, void*) override
    {
        vtkNew<vtkTransform> t;
        auto widget = reinterpret_cast<vtkBoxWidget*>(caller);
        widget->GetTransform(t.Get());
        widget->GetProp3D()->SetUserTransform(t.Get());
    }
};
}
int main(int argc, char *argv[])
{
    vtkNew<vtkNamedColors> colors;

    //球体数据源
    vtkNew<vtkConeSource> cone;
    cone->SetHeight(3.0);
    cone->SetRadius(1.0);
    cone->SetResolution(10);

    //将多边形数据映射到图形基元映射器
    vtkNew<vtkPolyDataMapper> coneMapper;
    //设置给定输入端口索引的连接
    coneMapper->SetInputConnection(cone->GetOutputPort());

    //创建渲染场景中的实体(几何体和属性)
    vtkNew<vtkActor> coneActor;
    //设置映射器: 将参与者连接到可视化管道末尾
    coneActor->SetMapper(coneMapper.Get());
    //设置模型颜色
    coneActor->GetProperty()->SetColor(colors->GetColor3d("Bisque").GetData());

    vtkNew<vtkRenderer> ren1;
    ren1->AddActor(coneActor.Get());
    ren1->SetBackground(1,1,1);

    vtkNew<vtkRenderWindow> renWin;
    renWin->AddRenderer(ren1.Get());
    renWin->SetSize(300, 300);
    renWin->SetWindowName("Tutorial_Step6");

    //创建交互器: 与平台无关的渲染窗互,包括拾取和帧速率控制。
    vtkNew<vtkRenderWindowInteractor> iren;
    iren->SetRenderWindow(renWin.Get());

    //创建相机操作器: 相机的交互式操作
    vtkNew<vtkInteractorStyleTrackballCamera> style;
    iren->SetInteractorStyle(style.Get());

    //创建正交六面体3D小部件
    vtkNew<vtkBoxWidget> boxWidget;
    //此方法用于将小组件与渲染窗互器相关联。
    boxWidget->SetInteractor(iren.Get());
    //设置一个表示放置时小部件缩放比例的因子
    boxWidget->SetPlaceFactor(1.25);
    //设置颜色
    boxWidget->GetOutlineProperty()->SetColor(colors->GetColor3d("Gold").GetData());
    //指定要放置小部件的vtkProp3D。
    boxWidget->SetProp3D(coneActor.Get());
    //此方法用于初始放置小部件
    boxWidget->PlaceWidget();

    //添加观察者
    vtkNew<vtkMyCallback> callback;
    boxWidget->AddObserver(vtkCommand::InteractionEvent, callback.Get());

    //按下'i'键,隐藏或显示(on默认显示)
    boxWidget->On();

    //初始化交互器,准备处理事件并将Enabled标志设置为true
    iren->Initialize();
    //启动交互器事件循环
    iren->Start();

    return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值