IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【vtk】鼠标拖动移动Actor -> 正文阅读

[C++知识库]【vtk】鼠标拖动移动Actor

https://blog.csdn.net/weixin_42834523/article/details/104844660

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkSphereSource.h>
#include <vtkRendererCollection.h>
#include <vtkCellArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkPlaneSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTransform.h>

#include <vtkCylinderSource.h>

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);

// Handle mouse events
bool move = true;
vtkActor* a = nullptr;

class MouseInteractorStyle2 : public vtkInteractorStyleTrackballCamera
{
public:
    static MouseInteractorStyle2* New();
    vtkTypeMacro(MouseInteractorStyle2, vtkInteractorStyleTrackballCamera);

    virtual void OnLeftButtonDown()
    {
        int* clickPos = this->GetInteractor()->GetEventPosition();

        // Pick from this location.
        vtkSmartPointer<vtkPropPicker>  picker =
                vtkSmartPointer<vtkPropPicker>::New();
        picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());

        double* pos = picker->GetPickPosition();
        std::cout << "Pick position (world coordinates) is: "
                  << pos[0] << " " << pos[1]
                  << " " << pos[2] << std::endl;

        std::cout << "Picked actor: " << picker->GetActor() << std::endl;
        //Create a sphere
        vtkSmartPointer<vtkSphereSource> sphereSource =
                vtkSmartPointer<vtkSphereSource>::New();
        sphereSource->SetCenter(pos[0], pos[1], pos[2]);
        sphereSource->SetRadius(0.1);

        //Create a mapper and actor
        vtkSmartPointer<vtkPolyDataMapper> mapper =
                vtkSmartPointer<vtkPolyDataMapper>::New();
        mapper->SetInputConnection(sphereSource->GetOutputPort());

        vtkSmartPointer<vtkActor> actor =
                vtkSmartPointer<vtkActor>::New();
        actor->SetMapper(mapper);
        if (a == picker->GetActor())
        {
            move = true;
            return;
        }

        //this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetDefaultRenderer()->AddActor(actor);
        this->GetDefaultRenderer()->AddActor(actor);
        // Forward events
        vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }
    virtual void OnMouseMove()
    {
        vtkRenderWindowInteractor *rwi = this->Interactor;
        if (move == true)
        {
            double *obj_center = a->GetCenter();
            double disp_obj_center[3], new_pick_point[4];
            double old_pick_point[4], motion_vector[3];
            this->ComputeWorldToDisplay(obj_center[0], obj_center[1], obj_center[2],
                    disp_obj_center);

            this->ComputeDisplayToWorld(rwi->GetEventPosition()[0],
                    rwi->GetEventPosition()[1],
                    disp_obj_center[2],
                    new_pick_point);

            this->ComputeDisplayToWorld(rwi->GetLastEventPosition()[0],
                    rwi->GetLastEventPosition()[1],
                    disp_obj_center[2],
                    old_pick_point);

            motion_vector[0] = new_pick_point[0] - old_pick_point[0];
            motion_vector[1] = new_pick_point[1] - old_pick_point[1];
            motion_vector[2] = new_pick_point[2] - old_pick_point[2];
            if (a->GetUserMatrix() != NULL)
            {
                vtkTransform *t = vtkTransform::New();
                t->PostMultiply();
                t->SetMatrix(a->GetUserMatrix());
                t->Translate(motion_vector[0], motion_vector[1], motion_vector[2]);
                a->GetUserMatrix()->DeepCopy(t->GetMatrix());
                t->Delete();
            }
            else
            {
                a->AddPosition(motion_vector[0],
                        motion_vector[1],
                        motion_vector[2]);
            }

            rwi->Render();
        }
        vtkInteractorStyleTrackballCamera::OnMouseMove();
    }
    virtual void OnLeftButtonUp()
    {
        move = false;
        vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
    }

private:

};

vtkStandardNewMacro(MouseInteractorStyle2);

// Execute application.
int main(int argc, char *argv[])
{
    vtkSmartPointer<vtkCylinderSource> planeSource = vtkSmartPointer<vtkCylinderSource>::New();
    planeSource->SetCenter(0, 0, 0);
    planeSource->SetRadius(2);
    planeSource->Update();
    // Create a polydata object
    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
     polydata = planeSource->GetOutput();

    // Create a mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();

#if VTK_MAJOR_VERSION <= 5
    mapper->SetInput(polydata);
#else
    mapper->SetInputData(polydata);
#endif

    // Create an actor
    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    std::cout << "Actor address: " << actor << std::endl;
    vtkSmartPointer<vtkSphereSource> planeSource1 =
            vtkSmartPointer<vtkSphereSource>::New();
    planeSource1->SetCenter(10, 10, 10);
    planeSource1->SetRadius(2);
    planeSource1->Update();
    // Create a polydata object
    vtkPolyData* polydata1 = planeSource1->GetOutput();

    // Create a mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper1 =
            vtkSmartPointer<vtkPolyDataMapper>::New();

#if VTK_MAJOR_VERSION <= 5
    mapper->SetInput(polydata);
#else
    mapper1->SetInputData(polydata1);
#endif

    // Create an actor
    vtkSmartPointer<vtkActor> actor1 =
            vtkSmartPointer<vtkActor>::New();
    actor1->SetMapper(mapper1);
    a = actor1;
    std::cout << "Actor1 address: " << actor1 << std::endl;
    std::cout << "a address: " << a << std::endl;
    // A renderer and render window
    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    // An interactor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Set the custom stype to use for interaction.
    vtkSmartPointer<MouseInteractorStyle2> style =
            vtkSmartPointer<MouseInteractorStyle2>::New();
    style->SetDefaultRenderer(renderer);

    renderWindowInteractor->SetInteractorStyle(style);

    // Add the actors to the scene
    renderer->AddActor(actor);
    renderer->AddActor(actor1);
    renderer->SetBackground(0, 0, 1);

    // Render and interact
    renderWindow->Render();
    renderWindowInteractor->Initialize();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-10 11:40:56  更:2022-05-10 11:41:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 18:33:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码