我的代码主要时将单个和多个点云使用VTK和QT显示,运用QVTKWidget控件。
显示单个点云
//单个点云可视化
void Visualize(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud2){vtkSmartPointer<vtkPoints> m_points = vtkSmartPointer<vtkPoints>::New();for(int i = 0;i < cloud2->points.size();i++){m_points->InsertNextPoint(cloud2->points[i].x,cloud2->points[i].y,cloud2->points[i].z);}m_points2->DeepCopy(m_points);std::cout << "可视化点云数量cloud2: " << cloud2->points.size() << std::endl;vtkSmartPointer<vtkPolyData> m_data = vtkSmartPointer<vtkPolyData>::New();m_data->SetPoints(m_points);vtkSmartPointer<vtkVertexGlyphFilter> m_vertexGlyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();m_vertexGlyphFilter->SetInputData(m_data);m_vertexGlyphFilter->Update();//数据类型转换物体vtkSmartPointer<vtkPolyDataMapper> m_dataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();m_dataMapper->SetInputData(m_vertexGlyphFilter->GetOutput());m_dataMapper->Update();//演员vtkSmartPointer<vtkActor> m_actor = vtkSmartPointer<vtkActor>::New();//演员手持物体m_actor->SetMapper(m_dataMapper);if(color==1){m_actor->GetProperty()->SetColor(0, 0, 1);}else if(color==0){m_actor->GetProperty()->SetColor(0, 1, 0);}else if (color==2){m_actor->GetProperty()->SetColor(1, 0, 0);}m_actor->GetProperty()->SetAmbient(0.5);m_actor->GetProperty()->SetPointSize(2);//舞台vtkSmartPointer<vtkRenderer> m_renderer = vtkRenderer::New();// m_renderer->RemoveAllViewProps();//舞台添加演员m_renderer->AddActor(m_actor);m_renderer->SetBackground(0, 0, 0);//设置背景色// 相机m_renderer->GetActiveCamera()->SetPosition(0, 0, 1);m_renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);m_renderer->GetActiveCamera()->SetViewUp(0, -1, 0);m_renderer->ResetCamera();//窗口vtkSmartPointer<vtkRenderWindow> m_renderWindow = vtkSmartPointer<vtkRenderWindow>::New();//窗口添加舞台m_renderWindow->AddRenderer(m_renderer);//设置交互vtkSmartPointer<vtkRenderWindowInteractor> m_renderWindowInteractor = pui->qvtkwidget->GetRenderWindow()->GetInteractor();std::cout << "可视化点云数量cloud2: " << cloud2->points.size() << std::endl;vtkSmartPointer<vtkAreaPicker> areaPicker = vtkSmartPointer<vtkAreaPicker>::New();m_renderWindowInteractor->SetPicker(areaPicker);vtkSmartPointer<HighlightInteractorStyle> style = vtkSmartPointer<HighlightInteractorStyle>::New();style->SetPolyData(m_data);style->SetCurrentRenderer(m_renderer);m_renderWindowInteractor->SetInteractorStyle(style);m_renderWindowInteractor->Initialize();m_renderWindowInteractor->SetRenderWindow(m_renderWindow);m_renderWindow->SetInteractor(pui->qvtkwidget->GetInteractor());//放入qtwidgetpui->qvtkwidget->SetRenderWindow(m_renderWindow);pui->qvtkwidget->GetRenderWindow()->Render();m_renderWindowInteractor->Start();}
显示多个点云
//可视化多个点云
void MultVisualize(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1,pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud2){vtkSmartPointer<vtkPoints> result_points = vtkSmartPointer<vtkPoints>::New();vtkSmartPointer<vtkPoints> source_points = vtkSmartPointer<vtkPoints>::New();for(int i = 0;i < cloud1->points.size();i++){result_points->InsertNextPoint(cloud1->points[i].x,cloud1->points[i].y,cloud1->points[i].z);}for(int i = 0;i < cloud2->points.size();i++){source_points->InsertNextPoint(cloud2->points[i].x,cloud2->points[i].y,cloud2->points[i].z);}std::cout << "点云配准数量cloud_result: " << cloud1->points.size() << std::endl;std::cout << "背景点云数量cloud: " << cloud2->points.size() << std::endl;//设置数据1vtkSmartPointer<vtkPolyData> m_data1 = vtkSmartPointer<vtkPolyData>::New();m_data1->SetPoints(result_points);vtkSmartPointer<vtkVertexGlyphFilter> m_vertexGlyphFilter1 = vtkSmartPointer<vtkVertexGlyphFilter>::New();m_vertexGlyphFilter1->SetInputData(m_data1);m_vertexGlyphFilter1->Update();//设置数据2vtkSmartPointer<vtkPolyData> m_data2 = vtkSmartPointer<vtkPolyData>::New();m_data2->SetPoints(source_points);vtkSmartPointer<vtkVertexGlyphFilter> m_vertexGlyphFilter2 = vtkSmartPointer<vtkVertexGlyphFilter>::New();m_vertexGlyphFilter2->SetInputData(m_data2);m_vertexGlyphFilter2->Update();//数据类型转换物体1vtkSmartPointer<vtkPolyDataMapper> m_dataMapper1 = vtkSmartPointer<vtkPolyDataMapper>::New();m_dataMapper1->SetInputData(m_vertexGlyphFilter1->GetOutput());m_dataMapper1->Update();//数据类型转换物体2vtkSmartPointer<vtkPolyDataMapper> m_dataMapper2 = vtkSmartPointer<vtkPolyDataMapper>::New();m_dataMapper2->SetInputData(m_vertexGlyphFilter2->GetOutput());m_dataMapper2->Update();//演员1vtkSmartPointer<vtkActor> m_actor1 = vtkSmartPointer<vtkActor>::New();//演员1vtkSmartPointer<vtkActor> m_actor2 = vtkSmartPointer<vtkActor>::New();//演员1手持物体1m_actor1->SetMapper(m_dataMapper1);m_actor1->GetProperty()->SetColor(0, 1, 0);m_actor1->GetProperty()->SetAmbient(0.5);m_actor1->GetProperty()->SetPointSize(3);//演员2手持物体2m_actor2->SetMapper(m_dataMapper2);m_actor2->GetProperty()->SetColor(0, 0, 1);m_actor2->GetProperty()->SetAmbient(0.5);m_actor2->GetProperty()->SetPointSize(2);//舞台vtkSmartPointer<vtkRenderer> m_renderer = vtkRenderer::New();// m_renderer->RemoveAllViewProps();//舞台添加演员m_renderer->AddActor(m_actor1);m_renderer->AddActor(m_actor2);m_renderer->SetBackground(0, 0, 0);//设置背景色// // 相机// m_renderer->GetActiveCamera()->SetPosition(0, 0, 1);// m_renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);// m_renderer->GetActiveCamera()->SetViewUp(0, -1, 0);// m_renderer->ResetCamera();//窗口vtkSmartPointer<vtkRenderWindow> m_renderWindow = vtkSmartPointer<vtkRenderWindow>::New();//窗口添加舞台m_renderWindow->AddRenderer(m_renderer);m_renderWindow->SetInteractor(pui->qvtkwidget->GetInteractor());//放入qtwidgetpui->qvtkwidget->SetRenderWindow(m_renderWindow);pui->qvtkwidget->GetRenderWindow()->Render();
}