(8)VTK 鼠标左右键控制模型旋转

1. 前言

在一个项目中,需要用鼠标和键盘完成对模型旋转操作和移动操作。
实现中,对鼠标的操作虚函数进行了重写,完成左右键点击,对模型正负方向旋转。

2.代码

#include "InitInclude.h"
#include "CreateSphere.h"
#include "thr.h"
#include "2D.h"
#include "2DplatformTexture.h"
#include "3DGammaKnife.h"
#include "2D(nei_qianghou).h"
#include "MouseInteractorHighLightActor.h"
#include "MouseInter.h"
#include "duobianxing.h"
#include "duobianxingzhuose(shiyongqianghoudiban).h"
#include "MouseInteractorStyle6 .h"
#include "testAdd.h"
#include <vtkImageImport.h>
#include <vtkImageActor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkInteractorStyleUser.h>
#include <vtkObjectFactory.h>
#include "my3DCameraStyle.h"
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include "MouseInteractorStyle2.h"
#include "MouseIntr922.h"
#include <vtkInteractorStyleTrackballActor.h>//另一种交互方式,操纵演员
#include <vtkInteractorStyleTrackballCamera.h>
// Handle mouse events
class MouseInteractorHighLightActor222 : public vtkInteractorStyleTrackballCamera
{
public:
    static MouseInteractorHighLightActor222* New();
    vtkTypeMacro(MouseInteractorHighLightActor222, vtkInteractorStyleTrackballCamera);

    MouseInteractorHighLightActor222()
    {
        m_RotateZStep = 0.0;
        LastPickedActor = NULL;
        LastPickedProperty = vtkProperty::New();
    }

    virtual ~MouseInteractorHighLightActor222()
    {
        LastPickedProperty->Delete();
    }
    //左键按下执行以下操作
    virtual void OnLeftButtonDown()
    { 
        this->LastPickedActor = tActor;
        if (this->LastPickedActor)
        {
            // Save the property of the picked actor so that we can
            // restore it next time
            this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
            // Highlight the picked actor by changing its properties
            m_RotateZStep += 10.05;
            this->LastPickedActor->RotateZ(m_RotateZStep);
        }
        // Forward events
        vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }

    //右键按下执行以下操作
    virtual void OnRightButtonDown()
    {
        this->LastPickedActor = tActor;
        if (this->LastPickedActor)
        {
            // Save the property of the picked actor so that we can
            // restore it next time
            this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
            // Highlight the picked actor by changing its properties
            m_RotateZStep += 10.05;
            this->LastPickedActor->RotateZ(m_RotateZStep);
        }
        // Forward events
        vtkInteractorStyleTrackballCamera::OnRightButtonDown();
    }

    virtual void OnChar()
    {
        this->LastPickedActor = tActor;
        switch (this->Interactor->GetKeyCode())
        {
            case VK_SPACE:
            {
                //if (this->LastPickedActor)
                {
                    // Save the property of the picked actor so that we can
                    // restore it next time
                    this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
                    // Highlight the picked actor by changing its properties
                    m_RotateZStep += 10.05;
                    this->LastPickedActor->RotateZ(m_RotateZStep);
                }
                cout << "sssssss38up " << endl;
                break;
            }

            case 26:
            {
                cout << "wertyuio " << endl;
                break;
            }

        }
        
         
        //vtkInteractorStyleTrackballCamera::OnRightButtonDown();
    }

    vtkActor    *tActor;
    double m_RotateZStep;
private:
    vtkActor    *LastPickedActor;
    vtkProperty *LastPickedProperty;
};

vtkStandardNewMacro(MouseInteractorHighLightActor222);

int main(int, char *[])
{ // This creates a polygonal cylinder model with eight circumferential facets
        // (i.e, in practice an octagonal prism).
        vtkNew<vtkCylinderSource> cylinder;
        cylinder->SetResolution(8);

        // The mapper is responsible for pushing the geometry into the graphics
        // library. It may also do color mapping, if scalars or other attributes are
        // defined.
        vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

        // The actor is a grouping mechanism: besides the geometry (mapper), it
        // also has a property, transformation matrix, and/or texture map.
        // Here we set its color and rotate it around the X and Y axes.
        vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
        {
            cylinderActor->SetMapper(cylinderMapper);
 
            cylinderActor->RotateX(0);
            cylinderActor->RotateY(-0);
            cylinderActor->RotateZ(-0.0);
            cylinderActor->SetOrigin(0, 0, 0); 
        }
        //vtkSmartPointer<vtkActor> cylinderActor1 = vtkSmartPointer<vtkActor>::New();
        //{
        //  cylinderActor1->SetMapper(cylinderMapper); 
        //  double xx = 20;
     //     cylinderActor1->RotateX(xx);
        //  cylinderActor1->RotateY(-120.0);
        //  cylinderActor1->RotateZ(-40.0);
        //  cylinderActor1->SetOrigin(1, 1, 1);
        //} 
        vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
        vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
        //vtkSmartPointer<MouseInteractorStyle2> StyleActor = vtkSmartPointer<MouseInteractorStyle2>::New();
        vtkSmartPointer<MouseInteractorHighLightActor222> style = vtkSmartPointer<MouseInteractorHighLightActor222>::New();
        
        style->SetDefaultRenderer(renderer);
        style->tActor = cylinderActor;
        renderWindowInteractor->SetInteractorStyle(style);

        //style->SetActor(cylinderActor);
        //renderWindowInteractor->SetInteractorStyle(style);

        //StyleActor->SetDefaultRenderer(renderer);

        //StyleActor->ActorA = cylinderActor;
        //StyleActor->ActorB = cylinderActor1;

        //renderWindowInteractor->SetInteractorStyle(StyleActor);
        // The renderer generates the image
        // which is then displayed on the render window.
        // It can be thought of as a scene to which the actor is added 
        renderer->AddActor(cylinderActor);
        //renderer->AddActor(cylinderActor1);

        // Zoom in a little by accessing the camera and invoking its "Zoom" method.
        renderer->ResetCamera();
        renderer->GetActiveCamera()->Zoom(1.5);

        // The render window is the actual GUI window
        // that appears on the computer screen
        vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
        renderWindow->SetSize(900, 900);
        renderWindow->AddRenderer(renderer);
        renderWindow->SetWindowName("Cylinder");

        // The render window interactor captures mouse events
        // and will perform appropriate camera or actor manipulation
        // depending on the nature of the events.
        renderWindowInteractor->SetRenderWindow(renderWindow);
 
            // This starts the event loop and as a side effect causes an initial render.
        renderWindow->Render();
        renderWindowInteractor->Start();
}

3.效果

https://www.bilibili.com/video/BV1Wq4y1o7KN/

鼠标和VTK模型交互,通过左右键实现对模型的两个方向旋转
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的示例代码,可以在 VS 中使用 VTK 生成多个 STL 模型,且每个模型都可以独立控制: ```cpp #include <vtkActor.h> #include <vtkCamera.h> #include <vtkPolyDataMapper.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkSTLReader.h> int main() { // 创建渲染器 vtkSmartPointer<vtkRenderer> renderer1 = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New(); // 创建窗口 vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer1); renderWindow->AddRenderer(renderer2); // 创建交互器 vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); interactor->SetRenderWindow(renderWindow); // 读取 STL 文件 vtkSmartPointer<vtkSTLReader> reader1 = vtkSmartPointer<vtkSTLReader>::New(); reader1->SetFileName("model1.stl"); vtkSmartPointer<vtkSTLReader> reader2 = vtkSmartPointer<vtkSTLReader>::New(); reader2->SetFileName("model2.stl"); // 创建模型映射器 vtkSmartPointer<vtkPolyDataMapper> mapper1 = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper1->SetInputConnection(reader1->GetOutputPort()); vtkSmartPointer<vtkPolyDataMapper> mapper2 = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper2->SetInputConnection(reader2->GetOutputPort()); // 创建演员 vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New(); actor1->SetMapper(mapper1); renderer1->AddActor(actor1); vtkSmartPointer<vtkActor> actor2 = vtkSmartPointer<vtkActor>::New(); actor2->SetMapper(mapper2); renderer2->AddActor(actor2); // 设置相机位置 renderer1->GetActiveCamera()->SetPosition(0, 0, 1); renderer2->GetActiveCamera()->SetPosition(0, 0, -1); // 开始渲染 renderWindow->Render(); interactor->Start(); return 0; } ``` 在这个示例中,我们创建了两个渲染器 `renderer1` 和 `renderer2`,并将它们添加到同一个窗口 `renderWindow` 中。我们从两个不同的 STL 文件中读取模型数据,并使用两个不同的模型映射器 `mapper1` 和 `mapper2` 将它们映射到演员 `actor1` 和 `actor2` 上。最后,我们设置了两个渲染器的相机位置,以便我们可以从不同的角度观察两个模型。 如果您希望能够独立控制每个模型,您可以添加一些交互性控制。例如,您可以使用 `vtkRenderWindowInteractor` 来捕获用户的鼠标事件,并根据鼠标位置来旋转和缩放模型。您还可以使用 `vtkActor` 的 `SetVisibility` 方法来隐藏或显示每个模型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rexinx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值