VTK Learning Eight
引言
VTK
事件回调演示。第一种回调函数模式;第二种继承并实现vtkCommand
。
回调函数模式
#include <vtkCallbackCommand.h>
long cntPress = 0;
void MyCallbackFunc(vtkObject* object, unsigned long eid, void* clientdata, void* calldata)
{
std::cout << "You have clicked : " << ++cntPress << " times" << std::endl;
std::cout << "Object: " << object->GetClassName() << std::endl;
std::cout << "EventID: " << eid <<":"<< vtkCommand::GetStringFromEventId(eid)<< std::endl;
std::cout << "ClientData: " <<*((double*)clientdata )<< std::endl;
std::cout << "calldata" << calldata << std::endl;
}
#include <vtkSmartPointer.h>
#include <vtkPNGReader.h>
#include <vtkImageViewer2.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
int main()
{
vtkSmartPointer<vtkPNGReader> reader =
vtkSmartPointer<vtkPNGReader>::New();
reader->SetFileName("D:\\VTK\\VTK_Build\\ExternalData\\Testing\\Data\\vtk.png");
reader->Update();
vtkSmartPointer<vtkImageViewer2> viewer =
vtkSmartPointer<vtkImageViewer2>::New();
viewer->SetInputData(reader->GetOutput());
viewer->GetRenderer()->SetBackground(0, 0, 0);
viewer->SetSize(480, 320);
viewer->GetRenderWindow()->SetWindowName("Observer-Callback");
vtkSmartPointer<vtkRenderWindowInteractor> rwi =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
viewer->SetupInteractor(rwi);
viewer->Render();
/*************************************************************/
//Step1:设置事件回调函数
vtkSmartPointer<vtkCallbackCommand> mouseCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
double data = 0.01;
mouseCallback->SetCallback(MyCallbackFunc);
mouseCallback->SetClientData(&data);
//Step2:将vtkCallbackCommand对象添加到观察者列表。
rwi->SetRenderWindow(viewer->GetRenderWindow());
rwi->AddObserver(vtkCommand::LeftButtonPressEvent, mouseCallback);
rwi->Initialize();
rwi->Start();
return 0;
}
vtkCommand
子类的方式
class vtkMyCommand : public vtkCommand{
public:
static vtkMyCommand *New() { return new vtkMyCommand; }
virtual void Execute(vtkObject *object, unsigned long event, void * clientdata) {
std::cout << "You have clicked : " << ++cntPress << " times" << std::endl;
std::cout << "Object: " << object->GetClassName() << std::endl;
std::cout << "EventID: " << event << ":" << vtkCommand::GetStringFromEventId(event) << std::endl;
}
};
//
rwi->AddObserver(vtkCommand::LeftButtonPressEvent, vtkMyCommand::New());