关于VTK默认交互方式和鼠标左键进行交互的两种方式

#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>

int main()
{
    vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    //设置圆柱的高度
    cylinder->SetHeight( 3.0 );
    //设置圆柱的直径
    cylinder->SetRadius( 1.0 );
    //设置圆柱有多少个侧面
    cylinder->SetResolution( 10 ); 

    //===================圆柱的
    vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection( cylinder->GetOutputPort() ); 

    vtkSmartPointer<vtkActor> cylinderActor = 
        vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper( cylinderMapper );
    //设置圆柱的颜色
    cylinderActor->GetProperty()->SetColor(1.0, 0.0, 0.0);


    //==================渲染器
    vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();
    //将圆柱actor添加到渲染器中
    renderer->AddActor( cylinderActor );
    //设置渲染场景的背景颜色 也就是窗口的颜色
    renderer->SetBackground( 1.0, 1.0, 1.0 );

    vtkSmartPointer<vtkRenderWindow> renWin = 
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer( renderer );
    //设置窗口的大小
    renWin->SetSize( 640, 480 );
    renWin->Render();
    //设置窗口的名字
    renWin->SetWindowName("RenderCylinderAndCone");

    //将窗口添加到WindowInterctor中去
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    //默认交互方式是vtkInteractorStyleTrackballCamera
    //现在将其关闭
    iren->SetInteractorStyle(0);

    iren->Initialize();
    iren->Start();

    return EXIT_SUCCESS;
}

鼠标点击左键进行交互的方法

第一种方法:
继承原来的交互样式,对其中进行修改

#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>

class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
  public:
  static MouseInteractorStyle* New();
  vtkTypeMacro(MouseInteractorStyle,vtkInteractorStyleTrackballCamera);
 
  virtual void OnLeftButtonDown(void);

};


vtkStandardNewMacro(MouseInteractorStyle);//必须加!!!!  如果不加,使用vtkSmartPointer会进行报错
void MouseInteractorStyle::OnLeftButtonDown(void)
{
	vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    
    printf("left button down\n");
 
}

//class MouseInteractorStyle : public vtkInteractorStyleTrackballActor
// {
//   public:
//   static MouseInteractorStyle* New();
//   vtkTypeMacro(MouseInteractorStyle,vtkInteractorStyleTrackballActor);
 
//   virtual void OnLeftButtonDown(void);

// };


// vtkStandardNewMacro(MouseInteractorStyle);//必须加!!!!
// void MouseInteractorStyle::OnLeftButtonDown(void)
// {
// 	vtkInteractorStyleTrackballActor::OnLeftButtonDown();
    
//     printf("left button down\n");
 
// }
 




int main()
{
    vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    //设置圆柱的高度
    cylinder->SetHeight( 3.0 );
    //设置圆柱的直径
    cylinder->SetRadius( 1.0 );
    //设置圆柱有多少个侧面
    cylinder->SetResolution( 10 ); 

    //===================圆柱的
    vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection( cylinder->GetOutputPort() ); 

    vtkSmartPointer<vtkActor> cylinderActor = 
        vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper( cylinderMapper );
    //设置圆柱的颜色
    cylinderActor->GetProperty()->SetColor(1.0, 0.0, 0.0);


    //==================渲染器
    vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();
    //将圆柱actor添加到渲染器中
    renderer->AddActor( cylinderActor );
    //设置渲染场景的背景颜色 也就是窗口的颜色
    renderer->SetBackground( 1.0, 1.0, 1.0 );

    vtkSmartPointer<vtkRenderWindow> renWin = 
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer( renderer );
    //设置窗口的大小
    renWin->SetSize( 640, 480 );
    renWin->Render();
    //设置窗口的名字
    renWin->SetWindowName("RenderCylinderAndCone");

    //将窗口添加到WindowInterctor中去
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

  
    vtkSmartPointer<MouseInteractorStyle> style =vtkSmartPointer<MouseInteractorStyle>::New();
    iren->SetInteractorStyle(style);
    //iren->SetInteractorStyle(0);

    iren->Initialize();
    iren->Start();

    return EXIT_SUCCESS;
}

方法二:使用vtkcommand的方式

#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkCylinderSource.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkCallbackCommand.h>

 
long cntPress = 0;
void MyCallbackFunc(vtkObject*, unsigned long eid, void* clientdata, void* calldata)
{
	std::cout << "You have clicked : " << ++cntPress << " times" << std::endl;
}




int main()
{
    vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    //设置圆柱的高度
    cylinder->SetHeight( 3.0 );
    //设置圆柱的直径
    cylinder->SetRadius( 1.0 );
    //设置圆柱有多少个侧面
    cylinder->SetResolution( 10 ); 

    //===================圆柱的
    vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection( cylinder->GetOutputPort() ); 

    vtkSmartPointer<vtkActor> cylinderActor = 
        vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper( cylinderMapper );
    //设置圆柱的颜色
    cylinderActor->GetProperty()->SetColor(1.0, 0.0, 0.0);


    //==================渲染器
    vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();
    //将圆柱actor添加到渲染器中
    renderer->AddActor( cylinderActor );
    //设置渲染场景的背景颜色 也就是窗口的颜色
    renderer->SetBackground( 1.0, 1.0, 1.0 );

    vtkSmartPointer<vtkRenderWindow> renWin = 
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer( renderer );
    //设置窗口的大小
    renWin->SetSize( 640, 480 );
    renWin->Render();
    //设置窗口的名字
    renWin->SetWindowName("RenderCylinderAndCone");

    //将窗口添加到WindowInterctor中去
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    
    vtkSmartPointer<vtkCallbackCommand> mouseCallback =vtkSmartPointer<vtkCallbackCommand>::New();
	mouseCallback->SetCallback(MyCallbackFunc); //很重要!!!
    iren->AddObserver(vtkCommand::LeftButtonPressEvent, mouseCallback);
 
    //iren->SetInteractorStyle(0); 如果不加上这一句 点一下之后 圆柱体会旋转 之后会打印"You have clicked : " << ++cntPress << " times"
    //所以建议加上iren->SetInteractorStyle(0);这一句

    iren->Initialize();
    iren->Start();

    return EXIT_SUCCESS;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您只想对单个Actor进行鼠标交互,可以使用vtkPropPicker类。该类允许您选择场景中的任何Actor并将其与鼠标事件相关联。 以下是一个简单的示例程序,演示如何使用vtkPropPicker选择一个Actor并将其与鼠标事件相关联: ```python import vtk # 创建一个场景并添加一个Actor sphere = vtk.vtkSphereSource() sphere.SetCenter(0, 0, 0) sphere.SetRadius(1.0) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(sphere.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) renderer = vtk.vtkRenderer() renderer.AddActor(actor) # 创建一个RenderWindow并将Renderer添加到其中 window = vtk.vtkRenderWindow() window.AddRenderer(renderer) # 创建一个Interactor并将其与RenderWindow相关联 interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(window) # 创建一个PropPicker并将其与Renderer相关联 picker = vtk.vtkPropPicker() picker.PickFromListOn() picker.AddPickList(actor) renderer.SetPicker(picker) # 定义鼠标事件处理程序 def onMouseDown(obj, event): # 获取当前鼠标位置 x, y = interactor.GetEventPosition() # 使用PropPicker选择Actor picker.PickProp(x, y, renderer) # 获取选择的Actor actor = picker.GetActor() # 如果选择了Actor,则输出Actor的名称 if actor: print("Selected actor:", actor.GetProperty().GetClassName()) # 将鼠标事件处理程序添加到Interactor interactor.AddObserver("LeftButtonPressEvent", onMouseDown) # 启动交互式窗口 interactor.Initialize() window.Render() interactor.Start() ``` 这个程序创建了一个场景,并在其中添加了一个球体Actor。它还创建了一个PropPicker,并将其与Renderer相关联。在鼠标事件处理程序中,它使用PropPicker选择Actor,并输出所选Actor的名称。 请注意,在这个示例中,我们只将一个Actor添加到PickList中。如果您想选择多个Actor,可以将它们全部添加到PickList中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值