【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;
}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值