pcl的显示对少量点运显示,比如一条线,很不友好,需要旋转缩小才能找到,所以这里直接转成vtk显示。
函数直接传入参数,点云的指针即可。
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLine.h>#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);void plot(pcl::PointCloud<pcl::PointXYZ>::Ptr true_tgt_cloudOut)
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();for (int i = 0; i<true_tgt_cloudOut->size(); i++){
vtkIdType pid[1];pid[0] = points->InsertNextPoint(true_tgt_cloudOut->at(i).x, true_tgt_cloudOut->at(i).y, true_tgt_cloudOut->at(i).z);vertices->InsertNextCell(1, pid);}vtkPolyData *polyData = vtkPolyData::New();polyData->SetPoints(points);polyData->SetVerts(vertices);vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();mapper->SetInputData(polyData);vtkActor *actor = vtkActor::New();actor->SetMapper(mapper);actor->GetProperty()->SetColor(1.0, 0.0, 0.0);actor->GetProperty()->SetPointSize(3);vtkRenderer *renderer = vtkRenderer::New();renderer->AddActor(actor);renderer->SetBackground(.0, .0, .0);//set backgroundvtkRenderWindow *renderwind = vtkRenderWindow::New();renderwind->AddRenderer(renderer);vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();vtkRenderWindowInteractor *renderwindIt = vtkRenderWindowInteractor::New();renderwindIt->SetRenderWindow(renderwind);renderwindIt->SetInteractorStyle(style);renderwind->Render();renderwindIt->Start();}
报错处理:
GenericWarning:InC:\Build\3rdParty\x64\VTK8.0.0\Rendering\Core\vtkPolyDataMapper.cxx, line 28Error: no override found for ‘vtkPolyDataMapper’.
修改:代码处添加如下:
然后根据你的OpenGL的版本在代码的最前面添加一下三行代码
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
如果你的OpenGL版本为OpenGL2,那么在代码最前面插入一下代码
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
然后在属性的的链接器——输入——附加依赖项添加opengl32.lib。
再运行就OK了