pcl-设置多线段宽度和颜色
阅读原文时间:2023年07月16日阅读:1

显示点云有使用vtk的,有使用 ros 中riz ?库的,使用pcl显示点云数据比较方便,但是对于一些模型形状只能固定特定的效果,比如说直线段,只能绘制点到点两点之间的线段。但是项目需要绘制点1到点2到…点n多条线段的连接,并且绘制设置线段宽度。

步骤:

1、把

vtkRenderWindowInteractorFix.cpp

vtkRenderWindowInteractorFix.h

pcl_visualizer.h

pcl_visualizer.cpp

shapes.h

shapes.cpp
这几个文件加入到工程P里,修改其依赖的头文件路径;屏蔽之前项目中使用的这三个头文件;添加编译这三个文件所依赖的vtk库。
2、代码修改

/*****自定义多点连接线段,可以设置线段宽度*****/

pcl_visualizer.cpp

/*****自定义多点连接线段,可以设置线段宽度*****/
bool pcl::visualization::PCLVisualizer::addMultyLine(vtkSmartPointer points,float width,double r,double g,double b,const std::string &id, int viewport)
{
// Check to see if this ID entry already exists (has it been already added to the visualizer?)
ShapeActorMap::iterator am_it = shape_actor_map_->find (id);
if (am_it != shape_actor_map_->end ())
{
pcl::console::print_warn (stderr, "[addMultyLine] A shape with id <%s> already exists! Please choose a different id and retry.\n", id.c_str ());
return (false);
}

if (points->GetNumberOfPoints() < )  
{  
  PCL\_WARN ("\[addMultyLine\] point size less 2.\\n");  
  return (false);  
}

vtkSmartPointer<vtkDataSet> data = createLine (points);

// Create an Actor  
vtkSmartPointer<vtkLODActor> actor;  
createActorFromVTKDataSet (data, actor);  
actor->GetProperty ()->SetRepresentationToSurface ();  
actor->GetProperty()->SetLineWidth(width);  
actor->GetProperty()->SetColor(r,g,b);  
addActorToRenderer (actor, viewport);

// Save the pointer/ID pair to the global actor map  
(\*shape\_actor\_map\_)\[id\] = actor;  
return (true);  

}

shape.cpp

vtkSmartPointer
pcl::visualization::createLine (vtkSmartPointer points)
{
vtkSmartPointer lineSource = vtkSmartPointer::New();
lineSource->SetPoints(points);
lineSource->Update();
return (lineSource->GetOutput());
}

例子

vtkSmartPointer points = vtkSmartPointer::New();

double origin[] = {0.0, 0.0, 0.0};
double p0[] = {1.0, 0.0, 0.0};
double p1[] = {2.0, 0.0, 0.0};
double p2[] = {3.0, 0.0, 0.0};
double p3[] = {4.0, 0.0, 0.0};

points->InsertNextPoint(origin);
points->InsertNextPoint(p0);
points->InsertNextPoint(p1);
points->InsertNextPoint(p2);
points->InsertNextPoint(p3);
m_viewerOrg->addMultyLine(points,,,,,"multiline",);

效果