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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

 
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;
}

你可能感兴趣的:(VTK,交互,c++,c语言)