实现PCL使用Ctrl + 鼠标左键进行框选

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

https://download.csdn.net/download/Lynn_Y_Lin/12140085

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值