在pcl查看器中绘制样条线

时间:2019-01-09 13:37:02

标签: c++ vtk point-cloud-library spline

我正在处理形成曲线的点云,并且使用PCL查看器将其可视化(图b)。

我想将各个样条线与点添加到同一查看器中,但是到目前为止,我仅找到了绘制线条的方法,而没有找到样条线的方法。但是,我已经基于此link(图a)在Vtk中绘制了样条曲线。

enter image description here

这是我对以上情节的代码:

 // Load the point cloud 
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_curve(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("line.pcd", *cloud_curve) == -1) 
{
    PCL_ERROR("Couldn't read the pcd file.\n");
    exit(1); 
}
// Visualisze curve points in pcl Viewer
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer( "Simple Cloud Viewer"));
viewer->setBackgroundColor(0, 0, 0);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> handler(cloud_curve, 255 , 0, 0);
viewer->addPointCloud(cloud_curve, handler, "cloud");
viewer->initCameraParameters();

while (!viewer->wasStopped()) {
    viewer->spinOnce(100);
    boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
// Visualisze splines in vtk Viewer

double p0[3] = {-5.91152, 4.65316, 28063.7};
double p1[3] = { -6.2126, 3.8805, 28034.9};
double p2[3] = {-6.54915, 4.6517, 28007};
// Create a vtkPoints object and store the points in it
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(p0);
points->InsertNextPoint(p1);
points->InsertNextPoint(p2);
// Setup render window, renderer, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(1000,2000); //(width, height)
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkParametricSpline> spline = vtkSmartPointer<vtkParametricSpline>::New();
spline->SetPoints(points);
vtkSmartPointer<vtkParametricFunctionSource> functionSource = vtkSmartPointer<vtkParametricFunctionSource>::New();
functionSource->SetParametricFunction(spline);
functionSource->Update();

// Setup actor and mapper
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(functionSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(1,0,0);
renderWindow->AddRenderer(renderer);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderWindow->Render();
vtkSmartPointer<vtkInteractorStyleSwitch> style = vtkSmartPointer<vtkInteractorStyleSwitch>::New();
style->SetCurrentStyleToTrackballCamera();
renderWindowInteractor->SetInteractorStyle( style );
renderWindowInteractor->Start();

关于如何在PCL查看器中同时绘制点和样条的任何建议?

1 个答案:

答案 0 :(得分:0)

通过在pcl查看器中添加角色,我设法将样条曲线与点一起绘制:

viewer->getRenderWindow ()->GetRenderers()->GetFirstRenderer()->AddActor(actor);

我在此discussion

中找到了答案