顯示點雲有使用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<vtkPoints> 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() < 2) { 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<vtkDataSet> pcl::visualization::createLine (vtkSmartPointer<vtkPoints> points) { vtkSmartPointer<vtkLineSource> lineSource = vtkSmartPointer<vtkLineSource>::New(); lineSource->SetPoints(points); lineSource->Update(); return (lineSource->GetOutput()); }
例子
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); double origin[3] = {0.0, 0.0, 0.0}; double p0[3] = {1.0, 0.0, 0.0}; double p1[3] = {2.0, 0.0, 0.0}; double p2[3] = {3.0, 0.0, 0.0}; double p3[3] = {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,100,255,0,0,"multiline",0);
效果