#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPropPicker.h>
#include <vtkCommand.h>
#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkDistanceWidget.h>
#include <vtkDistanceRepresentation.h>
#include <vtkAngleWidget.h>
#include <vtkProperty2D.h>
#include <vtkLeaderActor2D.h>
#include <vtkAngleRepresentation2D.h>
#include <vtkBiDimensionalWidget.h>
#include <vtkBiDimensionalRepresentation2D.h>
#include <vtkLineRepresentation.h>
#include "vtkLineWidget2.h"
#include <vtkProperty.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include "vtkWidgetEventTranslator.h"
#include "vtkWidgetCallbackMapper.h"
#include "vtkWidgetEvent.h"
#include <vtkHandleRepresentation.h>
//#include <vtkDistanceRepresentation3D.h>
#include <vtkDistanceRepresentation2D.h>
#include <vtkPointHandleRepresentation2D.h>
#include "vtkNamedColors.h"
//如果出现错误请加上下面的声明
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
//VTK_MODULE_INIT(vtkRenderingvolumeOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
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;
if (picker->GetActor())
{
m_pSelectActor = picker->GetActor();
m_bStatus = true;
return;
}
//this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetDefaultRenderer()->AddActor(actor);
//this->GetDefaultRenderer()->AddActor(actor);
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
virtual void OnMouseMove()
{
if (m_pSelectActor == nullptr)
return;
vtkRenderWindowInteractor* rwi = this->Interactor;
if (m_bStatus == true)
{
double* obj_center = m_pSelectActor->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 (m_pSelectActor->GetUserMatrix() != NULL)
{
vtkTransform* t = vtkTransform::New();
t->PostMultiply();
t->SetMatrix(m_pSelectActor->GetUserMatrix());
t->Translate(motion_vector[0], motion_vector[1], motion_vector[2]);
m_pSelectActor->GetUserMatrix()->DeepCopy(t->GetMatrix());
t->Delete();
}
else
{
//在当前位置增加偏移量
m_pSelectActor->AddPosition(motion_vector[0],
motion_vector[1],
motion_vector[2]);
}
rwi->Render();
}
vtkInteractorStyleTrackballCamera::OnMouseMove();
}
virtual void OnLeftButtonUp()
{
m_bStatus = false;
vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
}
private:
vtkSmartPointer<vtkActor> m_pSelectActor = nullptr;
bool m_bStatus = false;
};
vtkStandardNewMacro(MouseInteractorStyle2);
#include <vtkPlaneSource.h>
int main(int, char[])
{
vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();
planeSource->SetCenter(0, 0, 0);
//planeSource->SetRadius(2);
planeSource->Update();
// Create a polydata object
vtkPolyData *polydata = planeSource->GetOutput();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polydata);
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();
mapper1->SetInputData(polydata1);
// Create an actor
vtkSmartPointer<vtkActor> actor1 =
vtkSmartPointer<vtkActor>::New();
actor1->SetMapper(mapper1);
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);
//style->setNeedSelectActor(actor1);
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;
}
vtk中鼠标拖vtkActor在Render移动
最新推荐文章于 2024-06-24 15:26:26 发布
该博客展示了如何使用VTK库创建一个交互式3D场景,包括选择和移动3D对象。通过自定义MouseInteractorStyle2类,实现了鼠标左键点击选择对象并进行平移操作的功能。示例中创建了平面和球体,并设置了交互风格,允许用户在渲染窗口中直接操作3D元素。
摘要由CSDN通过智能技术生成