不用pcl的该窗口 pcl::visualization::PCLVisualizer viewer ,直接用VTK来显示 pcl::PointCloudpcl::PointXYZ::Ptr 类型点云
时间: 2023-07-02 17:09:45 浏览: 214
可以使用VTK来显示pcl::PointCloud<pcl::PointXYZ>::Ptr类型的点云。以下是一个简单的示例代码:
```cpp
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindowInteractor.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("your_point_cloud.pcd", *cloud);
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
for (size_t i = 0; i < cloud->points.size(); ++i) {
points->InsertNextPoint(cloud->points[i].x, cloud->points[i].y, cloud->points[i].z);
}
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return 0;
}
```
在这个例子中,我们使用vtkSmartPointer来管理VTK对象的生命周期。首先,我们加载了一个pcl::PointCloud<pcl::PointXYZ>::Ptr类型的点云。然后,我们将点云数据复制到VTK的vtkPoints对象中。接下来,我们使用vtkPolyDataMapper和vtkActor对象将vtkPoints对象中的点云数据可视化。最后,我们创建了一个vtkRenderWindowInteractor对象,它允许用户与渲染窗口进行交互,并启动了渲染窗口的事件循环。
阅读全文