0 前言
VTK可以借助vtkPointPicker或者vtkCellPicker进行三维坐标拾取,其官方例程序参见annotatePick.py或QtVTKRenderWindows.cxx。
QtVTKRenderWindows.cxx例子中说明,如果将vtkCellPicker与vtkImagePlaneWidget结合使用时,只需要使用vtkImagePlaneWidget的SetPicker()将它们两者绑定即可实现坐标点的拾取和显示;然而,如果想在三维重建时,实现目标的三维坐标拾取,可不那么简单。下面是VTK6.2.0的使用文档中对含有SetPicker()函数的类的总结:
SetPicker() : vtkRenderWindowInteractor , vtkBalloonWidget , vtkImagePlaneWidget
由上面的总结可见,只有3个类是含有SetPicker()函数的,也就是说只有3个类可以与vtkCellPicker绑定。在三维重建效果显示时,通常使用的是第1个:vtkRenderWindowInteractor。
将vtkCellPicker与vtkRenderWindowInteractor通过SetPicker()绑定之后,并非就万事大吉。这时候还不能触发三维坐标点的采集。此外,就算继续设定vtkCellPicker的事件处理函数,也还不行。
那么,要怎样才能触发vtkCellPicker的采集呢?答案是必须调用其Pick()函数,而且是调用一次,就触发一次!下面将用一个实例说明。
1 vtkCellPicker的创建并且与Interactor绑定
// Cell Picker
m_pvtkCellPicker =
vtkSmartPointer< vtkCellPicker >::New();
m_pvtkCellPicker->SetTolerance( 0.005 );
vtkSmartPointer < vtkCallbackCommand > callback_picker = vtkSmartPointer < vtkCallbackCommand >::New();
callback_picker->SetCallback( handle_cell_picker );
callback_picker->SetClientData( this );
m_pvtkCellPicker->AddObserver( vtkCommand::EndPickEvent, callback_picker );
this->ui->qvtkWidget3D->GetInteractor()->SetPicker( m_pvtkCellPicker );
2 vtkCellPicker回调函数
static void handle_cell_picker(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata)
{
QString hints;
double selPt[3];
double normal[3];
//float pixelValue = 0;
RSAMeasurement3D *parent= (RSAMeasurement3D *)clientdata;
if ( !parent )
return;
if ( !parent->m_bHasRendered3D ) {
parent->m_pvtkTextActor->VisibilityOff();
return;
}
if ( Qt::Unchecked == parent->ui->enablePickerCheckBox->checkState() ) {
// parent->m_pvtkTextActor->VisibilityOff();
return;
}
if ( parent->m_pvtkCellPicker->GetCellId() < 0)
return;
parent->m_pvtkCellPicker->GetPickPosition( selPt );
parent->m_pvtkTextActor->VisibilityOn();
parent->m_pvtkConeActor->SetPosition( selPt );
}
3 触发坐标点采集
我们的设想是,点击以下鼠标,采集一次坐标。首先要设置Interactor的单击处理回调函数:
vtkSmartPointer < vtkCallbackCommand > callback_interactor =
vtkSmartPointer < vtkCallbackCommand >::New();
callback_interactor->SetCallback( handle_interactor_event );
callback_interactor->SetClientData( this );
this->ui->qvtkWidget3D->GetInteractor()->AddObserver(
vtkCommand::LeftButtonPressEvent, callback_interactor );
在回调函数里面,触发坐标采集
static void handle_interactor_event(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata)
{
//qDebug() << "left button release";
RSAMeasurement3D *parent= (RSAMeasurement3D *)clientdata;
if ( !parent )
return;
int *pick = parent->ui->qvtkWidget3D->GetInteractor()->GetEventPosition();
parent->m_pvtkCellPicker->Pick(
(double)pick[0],
(double)pick[1],
0.0,
parent->m_pvtkRenderer3D );
}
参考资料
[2]vtk捡选实例
[3]vtk坐标系统
[4]VTK拾取相关的类(转)
[6][vtkusers] vtkCellPicker failing to pick vtkPolyLine (using Python and wx)