PCL自带的框选功能需要通过 点击 'x'进入退出,使用起来十分不便.本文实现了使用Ctrl + 鼠标左键进行框选
1.注册PCLVisualizer的键盘事件和框选结束事件,并设置PCL的Picker为AreaPicker
viewer->registerAreaPickingCallback(pp_callback, (void*)this);
viewer->registerKeyboardCallback(KeyDownCallback);
vtkSmartPointer<vtkRenderedAreaPicker> area_picker = vtkSmartPointer<vtkRenderedAreaPicker>::New();
viewer->getRenderWindow()->GetInteractor()->SetPicker(area_picker);
2.使用Style的StartSelect()方法进入选择模式,这样就可以实现Ctrl + 鼠标左键进行框选了,但VTK比较坑,只有StartSelect()却没有EndSelect(),下一步就需要改VTK的源码,当鼠标左键弹起时退出选择模式.
void KeyDownCallback(const visualization::KeyboardEvent& event)
{
if (event.isCtrlPressed())
viewer->getInteractorStyle()->StartSelect();
}
void pp_callback(const pcl::visualization::AreaPickingEvent& event, void* args)
{
ScanCommander* commander = (ScanCommander*)args;
std::vector< int > indices;
if (event.getPointsIndices(indices) == -1)
return;
for (int i = 0; i < indices.size(); ++i)
{
clicked_points_3d->points.push_back(cloud->points.at(indices[i]));
}
visualization::PointCloudColorHandlerCustom<PointXYZ> red(clicked_points_3d, 255, 0, 0);
std::stringstream ss;
std::string cloudName;
ss << num++;
ss >> cloudName;
cloudName += "_cloudName";
viewer->addPointCloud(clicked_points_3d, red, cloudName);
viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 30, cloudName);
commander->centralWidget->update();
}
3.修改VTK源码中的 vtkInteractorStyleRubberBandPick中的OnLeftButtonUp方法,在方法的最后一行添加
this->CurrentMode = VTKISRBP_ORIENT;
修改后如下:
void vtkInteractorStyleRubberBandPick::OnLeftButtonUp()
{
if (this->CurrentMode != VTKISRBP_SELECT)
{
//if not in rubber band mode, let the parent class handle it
this->Superclass::OnLeftButtonUp();
return;
}
if (!this->Interactor || !this->Moving)
{
return;
}
//otherwise record the rubber band end coordinate and then fire off a pick
if ( (this->StartPosition[0] != this->EndPosition[0])
|| (this->StartPosition[1] != this->EndPosition[1]) )
{
this->Pick();
}
this->Moving = 0;
this->CurrentMode = VTKISRBP_ORIENT;
}
重新编译并替换vtkInteractionStyle-8.1.dll即可
链接里是我编译好的dll,包括Debug和Release