vtk回调函数

格式1:

1. 定义回调函数,

void MyCallbackFunc( vtkObject* caller, long unsigned int eventId, void* clientData, void* callData )
{
    //输出鼠标点击的次数
    std::cout<<"You have clicked: "<<++pressCounts<<" times."<<std::endl;
}

 caller是调用该回调函数的对象,一般是renderWindowInteractor/其他VTKWidget类;eventId是处理的事件代号;clientData是传递到该函数中的数据;callData是随着触发的event一起传递的数据;

2. 创建一个vtkCallbackCommand对象

vtkSmartPointer<vtkCallbackCommand> mouseCallback = 
        vtkSmartPointer<vtkCallbackCommand>::New();
    mouseCallback->SetCallback (MyCallbackFunc );

 3. 观察者列表

interactor->AddObserver(vtkCommand::LeftButtonPressEvent, mouseCallback);

 格式2:(派生法)

从vtkCommand派生出子类,接着实现vtkCommand::Execute()虚函数;(在普通的虚函数后面加上"=0"这样就声明了一个pure virtual function)

class vtkImageViewerMyCallback : public vtkCommand
{
public:
    static vtkImageViewerMyCallback *New() { return new vtkImageViewerMyCallback; }
void Execute(vtkObject *caller, unsigned long event, void *vtkNotUsed(callData))
{
//把要实现的功能放在这里
}

//调用方法与上面的方法类似

int main()
{
vtkImageViewerMyCallback *cbk = vtkImageViewerMyCallback::New();
//AddObserver是为了把事件,响应函数和VTK联系在一起
this->InteractorStyleMy->AddObserver(vtkCommand::MouseMoveEvent, cbk);
cbk->Delete();
}

 格式3:(交互器法)

继承已有的默认interactor style对希望设置的事件做出相应的响应,该方法可看作对若干个event的集合,但缺点是只能对renderWindowInteractor进行设置。

class MyStyle : public vtkInteractorStyleImage
{
  public:
    static MyStyle* New();
    vtkTypeMacro(MyStyle, vtkInteractorStyleImage);
 
    virtual void OnLeftButtonDown() 
    {
      std::cout << "Pressed left mouse button." << std::endl;
      // Forward events
      vtkInteractorStyleImage::OnLeftButtonDown();
    }
 
	virtual void OnRightButtonDown()
	{
		std::cout << "Pressed right mouse button." << std::endl;
	}
 
	virtual void OnRightButtonUp()
	{
		std::cout << "Release right mouse button." << std::endl;
	}
 
	virtual void OnMouseMove()
	{
		int *pos = this->GetInteractor()->GetEventPosition();
		std::cout << pos[0] << "	" << pos[1] << "	" << pos[2] << std::endl;
	}
 
	virtual void OnLeftButtonUp()
	{
		std::cout << "Release right mouse button." << std::endl;
	}
 
};

 

posted @ 2022-06-13 09:14  二先生-  阅读(522)  评论(0)    收藏  举报