https://blog.youkuaiyun.com/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(

这篇博客展示了如何使用VTK(Visualization Toolkit)创建一个交互式的3D场景,通过自定义的鼠标事件处理类MouseInteractorStyle2实现在3D空间中选择和移动对象。程序创建了一个圆柱和平面,并在点击圆柱时生成一个小球,用户可以拖动小球来移动圆柱。主要涉及的技术包括VTK的Actor、Mapper、PolyData、RenderWindowInteractor等组件以及自定义交互风格的实现。
最低0.47元/天 解锁文章
1420

被折叠的 条评论
为什么被折叠?



