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

前面对vtk实例中的toturial进行的多次详细讲解,这里最后结束一下vtk中observe和command模式的信息响应机制的实现。

 

1.      Observe和command实现简单的信息输出

Vtk里面的信息响应主要是通过observe和command的模式实现,只要是继承自vtkObject的对象都可以添加多个observe,并且将不同的事件与vtkCommand的子类建立对应关系,从而实现多事件响应机制。

 

#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkCommand.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"

// command的子类,绑定到vtk对应的事件中,即可实现观察者模式的消息响应机制
class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New() 
    { return new vtkMyCallback; }
  //继承函数,实现回调时的响应操作,第一个参数是调用该事件的对象,第二个参数树消息类型,第三个参数为可以传递给
  //command的参数
  virtual void Execute(vtkObject *caller, unsigned long, void*)
    {  
      //将传入参数强制转换成render类型
      vtkRenderer *renderer = reinterpret_cast<vtkRenderer*>(caller);
      //然后输出相机的位置信息
      cout << renderer->GetActiveCamera()->GetPosition()[0] << " "
           << renderer->GetActiveCamera()->GetPosition()[1] << " "
           << renderer->GetActiveCamera()->GetPosition()[2] << "\n";
    }
};

int main()
{
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );

  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );
  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );
  ren1->ResetCamera();

  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );

  //建立mycallback对象,并且将其绑定到render中的start event事件中
  vtkMyCallback *mo1 = vtkMyCallback::New();
  ren1->AddObserver(vtkCommand::StartEvent,mo1);
  mo1->Delete();
  
  int i;
  for (i = 0; i < 360; ++i)
    {
    // render the image
    renWin->Render();
    // rotate the active camera by one degree
    ren1->GetActiveCamera()->Azimuth( 1 );
    }
  
  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  ren1->Delete();
  renWin->Delete();

  return 0;
}
vtk中oberver和command的信息机制详解_第1张图片

2.      Observe和command实现模型的编辑

Vtk中提供了很多widget的类对象,从而可以在可视化中非常方便的对数据进行变形、切片,查看内部模型等处理,因此这里主要讲解实例中boxwidget来操作模型,改变模型大小的例子。


#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"

// command的子类,绑定到vtk对应的事件中,即可实现观察者模式的消息响应机制
class vtkMyCallback : public vtkCommand{public: static vtkMyCallback *New() { return new vtkMyCallback; } virtual void Execute(vtkObject *caller, unsigned long, void*) { vtkTransform *t = vtkTransform::New();


      //将传入参数强制转换成vtkBoxWidget类型
vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);

      //得到wiget的变换矩阵
      widget->GetTransform(t);
      //获得wiget中的actor对象,并且用同样的变换矩阵操作,使其变形
      widget->GetProp3D()->SetUserTransform(t);
      t->Delete();
    }
};

int main()
{
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );
  
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );

  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );
.
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  vtkInteractorStyleTrackballCamera *style = 
    vtkInteractorStyleTrackballCamera::New();
  iren->SetInteractorStyle(style);

 
  //boxwiget操作模型大小
  vtkBoxWidget *boxWidget = vtkBoxWidget::New();
   //设置交互的vtkRenderWindowInteractor对象
  boxWidget->SetInteractor(iren);
  boxWidget->SetPlaceFactor(1.25);

  //设置操作的actor对象
  boxWidget->SetProp3D(coneActor);
  boxWidget->PlaceWidget();
  //绑定事件与回调对象类
  vtkMyCallback *callback = vtkMyCallback::New();
  boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);

  //开始交互
  boxWidget->On();

  iren->Initialize();
  iren->Start();
  
  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  callback->Delete();
  boxWidget->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();
  style->Delete();

  return 0;
}
vtk中oberver和command的信息机制详解_第2张图片vtk中oberver和command的信息机制详解_第3张图片vtk中oberver和command的信息机制详解_第4张图片

你可能感兴趣的:(command,widget,observe,回调机制,VTK)