QT+VTK+PCL实现交互式点云平面切割

PCL及QT配置请移步点击打开链接

前言

恭喜你已经入了qt和pcl的坑,这篇文章帮助你实现在qvtkWidget中点云平面切割以及保存切割数据。

前提是你已经能成功读取pcd文件,并且将qvtkWidget配置好(代码中省略读取pcd部分)

先放效果图,如果是你想要的就继续读下去。

ps:初学,so只是将功能实现而已,若代码风格以及完成功能方法不尽如人意请尽管提出建议~么么哒

QT+VTK+PCL实现交互式点云平面切割_第1张图片QT+VTK+PCL实现交互式点云平面切割_第2张图片

class vtkIPWCallback : public vtkCommand
{
public:
	static vtkIPWCallback *New()
	{
		return new vtkIPWCallback;
	}
	virtual void Execute(vtkObject *caller, unsigned long, void*)
	{
		vtkImplicitPlaneWidget2 *planeWidget =
			reinterpret_cast(caller);
		vtkImplicitPlaneRepresentation *rep =
			reinterpret_cast(planeWidget->GetRepresentation());
		rep->GetPlane(this->Plane);
	}

	vtkIPWCallback() :Plane(0), Actor(0) {}
	vtkPlane *Plane;
	vtkActor *Actor;

};

    //!!!!!!!!!!!!!!!!!!!!超重要的两句,设置viewer的(试了很久才出来的)
    ui.qvtkWidget->SetRenderWindow(renderWindow);
    viewer->setupInteractor(renderWindowInteractor, renderWindow);

void initial()
{
        //添加cloud和viewer
     pcl::PointCloud::Ptr cloud;
     boost::shared_ptr viewer;
        //初始化
       cloud.reset(new pcl::PointCloud);
	viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
	viewer->addPointCloud(cloud, "cloud");
        //初始化vtk
	ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
	viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());
	viewer->setCameraPosition(0, 0, -2, 0, -1, 0, 0);
	ui.qvtkWidget->update();
}
int planeCut()
{
	//input data
	points = vtkSmartPointer::New();//添加点集
	sourcePolyData = vtkSmartPointer::New();//添加数据
	glyphFilter = vtkSmartPointer::New();//过滤器
	plane = vtkSmartPointer::New();//切割平面
	clipper = vtkSmartPointer::New();//
	mapper = vtkSmartPointer::New(); //映射器,可以根据点、线、面构造图形
	actor = vtkSmartPointer::New();//
	backFaces = vtkSmartPointer::New();
	renderer = vtkSmartPointer::New();//绘制器,能够绘制物体
	renderWindow = vtkSmartPointer::New();//绘制窗,提供绘制的平面
	renderWindowInteractor = vtkSmartPointer::New();//交互器

	//!!!!!!!!!!!!!!!!!!!!超重要的两句,设置viewer的
	ui.qvtkWidget->SetRenderWindow(renderWindow);
	viewer->setupInteractor(renderWindowInteractor, renderWindow);
        //点云数据输入
	for (size_t i = 0; i < cloud->points.size(); i++)
	{
		points->InsertNextPoint(cloud->points[i].x, cloud->points[i].y, cloud->points[i].z);
	}	
	sourcePolyData->SetPoints(points);

#if VTK_MAJOR_VERSION <= 5  
	glyphFilter->SetInputConnection(polyData->GetProducerPort());
#else  
	glyphFilter->SetInputData(sourcePolyData); //完成点的传递
#endif  
	glyphFilter->Update();

       //裁剪设置
	clipper->SetClipFunction(plane);
	clipper->InsideOutOn();
	clipper->SetInputConnection(glyphFilter->GetOutputPort());
	clipper->Update();

	// 映射和动作
	mapper->SetInputConnection(clipper->GetOutputPort());
	actor->SetMapper(mapper);
	backFaces->SetDiffuseColor(0.8275, 0.8275, 0.8275);
	actor->SetBackfaceProperty(backFaces);

	// 绘制窗口
	renderWindow->AddRenderer(renderer);
	renderer->AddActor(actor);

	//交互器
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindow->Render();

	// 回调
	vtkSmartPointer myCallback =
		vtkSmartPointer::New();
	myCallback->Plane = plane;
	myCallback->Actor = actor;

        //提前于planewidget设置
	vtkSmartPointer rep =
		vtkSmartPointer::New();
	rep->SetPlaceFactor(1.25); 
	rep->PlaceWidget(actor->GetBounds());
	rep->SetNormal(plane->GetNormal());
	rep->SetOrigin(0, 0, 0); 

	vtkSmartPointer planeWidget =
		vtkSmartPointer::New();
	planeWidget->SetInteractor(renderWindowInteractor);
	planeWidget->SetRepresentation(rep);
	planeWidget->AddObserver(vtkCommand::InteractionEvent, myCallback); //对观察到的事件响应

	// 绘制
	renderWindowInteractor->Initialize();
	renderWindow->Render();
	planeWidget->On();

	// 鼠标交互
	renderWindowInteractor->Start();	

        //过滤被裁剪部分
    ////////////////////////////////////////
    vtkSmartPointer cellIdFilter =
        vtkSmartPointer::New();
    cellIdFilter->SetInputConnection(clipper->GetOutputPort());
    cellIdFilter->SetCellIds(true);
    cellIdFilter->SetPointIds(false);
    cellIdFilter->SetIdsArrayName("CellIds");
    cellIdFilter->Update();

    //将过滤部分保存ply
    //////////////////////////////////////////////////////////////////
    vtkSmartPointer writer = vtkSmartPointer::New();
    std::string filename = "temp/mesh.ply";
#if VTK_MAJOR_VERSION <= 5
    writer->SetInput(dataSet);
#else
    writer->SetInputData(cellIdFilter->GetOutput());
#endif
    writer->SetFileName(filename.c_str());
    writer->Write();
    
       //格式转换,先将ply转为vtk,再转vtk为pcd
     pcl::PolygonMesh mesh; 
  
    vtkSmartPointer poly = vtkSmartPointer::New();
    pcl::io::loadPolygonFilePLY("temp/mesh.ply", mesh);
    pcl::io::mesh2vtk(mesh, poly);
    pcl::io::vtkPolyDataToPointCloud(poly, *cloud);
    viewer->updatePointCloud(cloud, "cloud");
    ui.qvtkWidget->update();

    //更新上一步剪切的数据为新的input
    glyphFilter->SetInputData(poly);
    glyphFilter->Update();return EXIT_SUCCESS;}
 
  
 
 
  


你可能感兴趣的:(Qt,PCL)