VTK中oberver 和 command 的信息机制详解

来源:互联网 发布:局域网共享 网络密码 编辑:程序博客网 时间:2024/05/17 09:38

前言

看了东灵出版的书的第八章,有点迷糊,这里简单做些笔记。


正文

观察者/命令模式(Observer / Command)

目的: 实现VTK的交互功能

定义: 一个vtkObject (包括它的子类) 可以拥有多个 Observer。  就是观察者在对象的状态发生改变时,调用相应的函数或者说是执行预定的动作。


实现

  • 事件回调函数
示例
//第一步,定义回调函数。注意回调函数的签名,不能更改。void CallbackFunc(vtkObject*, unsigned long eid, void* clientdata, void *calldata){     std::cout<<" ZYH "<<std::endl;}//第二步,将回调函数和 vtkCallbackCommand 联系起来vtkSmartPointer<vtkCallbackCommand> mouseCallback = vtkSmartPointer<vtkCallbackCommand>::New();mouseCallback->SetCallback ( CallbackFunc );//第三步,将vtkCallbackCommand对象添加到观察者列表。interactor->AddObserver(vtkCommand::LeftButtonPressEvent, mouseCallback);

步骤就是上面所示的三步,需要注意的是:

① 灰调函数除了函数名可以自己重新定义,其他的是固定格式的

void func(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)

 

② AddObserver函数是将事件、回调函数、VTK对象联系在一起


  • 从vtkCommand派生出具体的子类
command的子类,绑定到vtk对应的事件中,即可实现观察者模式的消息响应机制 

示例  (种子点)
步骤一:
派生子类, 实现vtkCommand::Execute()
class vtkSeedCallback : public vtkCommand{public:static vtkSeedCallback *New(){ return new vtkSeedCallback; }vtkSeedCallback() {}        //继承函数,实现回调时的响应操作,第一个参数是调用该事件的对象,第二个参数树消息类型,第三个参数为可以传递给 command 的参数  virtual void Execute(vtkObject*, unsigned long event, void *calldata){if (event == vtkCommand::PlacePointEvent){std::cout << "Placing point..." << std::endl;std::cout << "There are now " << this->SeedRepresentation->GetNumberOfSeeds() << " seeds." << std::endl;for(unsigned int seedId = 0; seedId < this->SeedRepresentation->GetNumberOfSeeds(); seedId++){double pos[3];this->SeedRepresentation->GetSeedDisplayPosition(seedId, pos);std::cout << "Seed " << seedId << " : (" << pos[0] << " " << pos[1] << " " << pos[2] << ")" << std::endl;}return;}if (event == vtkCommand::InteractionEvent){std::cout << "Interaction..." << std::endl;if (calldata){double pos[3];this->SeedRepresentation->GetSeedDisplayPosition(0, pos);std::cout << "Moved to (" << pos[0] << " " << pos[1] << " " << pos[2] << ")" << std::endl;}return;}}void SetRepresentation(vtkSmartPointer<vtkSeedRepresentation> rep){this->SeedRepresentation = rep;}void SetWidget(vtkSmartPointer<vtkSeedWidget> widget) {this->SeedWidget = widget;}private:vtkSeedRepresentation* SeedRepresentation;vtkSeedWidget* SeedWidget;};

实现种子点的交互【个人觉得这个InteractorEvent这个有点麻烦,只要focus到这个窗口也响应,移动种子点也响应
需要注意的是种子点的坐标是窗口坐标。
程序需要实现的功能一般都在 Execute() 函数中。

步骤二:
在主程序中实例化一个子类对象以及调用相关的方法
// Create the representation  设置点的颜色vtkSmartPointer<vtkPointHandleRepresentation2D> handle = vtkSmartPointer<vtkPointHandleRepresentation2D>::New();handle->GetProperty()->SetColor(1,0,0);vtkSmartPointer<vtkSeedRepresentation> rep = vtkSmartPointer<vtkSeedRepresentation>::New();rep->SetHandleRepresentation(handle);// Seed widgetvtkSmartPointer<vtkSeedWidget> seedWidget = vtkSmartPointer<vtkSeedWidget>::New();seedWidget->SetInteractor(renderWindowInteractor);seedWidget->SetRepresentation(rep);<strong>vtkSmartPointer<vtkSeedCallback> seedCallback = vtkSmartPointer<vtkSeedCallback>::New();</strong>seedCallback->SetRepresentation(rep);seedCallback->SetWidget(seedWidget);seedWidget->AddObserver(vtkCommand::PlacePointEvent,seedCallback);seedWidget->AddObserver(vtkCommand::InteractionEvent,seedCallback);renderWindow->Render();renderWindowInteractor->Initialize();renderWindow->Render();seedWidget->On();

AddObserver 监听感兴趣的事件

这个示例是VTK官网上的示例,参见。


0 0
原创粉丝点击