由于不同版本的pcl兼容范围不一样,这里有2个版本的代码,里面的文件路径需要实际情况修改即可,希望对您有所参考或帮助
pcl1.8.1和vs2015版本代码
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
using namespace std;
using namespace pcl;
int main() {
// 读取STL格式模型
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName("Spider_ascii.stl");
reader->Update();
vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
if (!polydata) {
std::cerr << "Failed to load STL file." << std::endl;
return -1;
}
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
pcl::io::vtkPolyDataToPointCloud(polydata, *cloud);
// 创建PCLVisualizer显示STL文件
boost::shared_ptr<pcl::visualization::PCLVisualizer> stl_viewer(new pcl::visualization::PCLVisualizer("STL Viewer"));
stl_viewer->addModelFromPolyData(polydata, "mesh");
// 创建新的线程显示STL窗口
boost::thread stl_thread([&stl_viewer]() {
while (!stl_viewer->wasStopped()) {
stl_viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
});
// 保存PCD文件
pcl::io::savePCDFileASCII("Spider_ascii.stl.pcd", *cloud);
// 加载点云文件
pcl::io::loadPCDFile("Spider_ascii.stl.pcd", *cloud);
std::cout << "Loaded PCD with " << cloud->points.size() << " points." << std::endl;
// 创建PCLVisualizer显示PCD文件
boost::shared_ptr<pcl::visualization::PCLVisualizer> pcd_viewer(new pcl::visualization::PCLVisualizer("PCD Viewer"));
pcd_viewer->addPointCloud(cloud, "cloud");
// 主要线程处理PCD显示
while (!pcd_viewer->wasStopped()) {
pcd_viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
// 等待STL显示线程结束
stl_thread.join();
return 0;
}
以下代码适合pcl1.13.1 vs2022
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkPolyData.h>
using namespace std;
using namespace pcl;
int main() {
// 读取STL格式模型
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName("Wuson.stl");
reader->Update();
vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
if (!polydata) {
std::cerr << "Failed to load STL file." << std::endl;
return -1;
}
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
pcl::io::vtkPolyDataToPointCloud(polydata, *cloud);
// 创建PCLVisualizer显示STL文件
boost::shared_ptr<pcl::visualization::PCLVisualizer> stl_viewer(new pcl::visualization::PCLVisualizer("STL Viewer"));
stl_viewer->addModelFromPolyData(polydata, "mesh");
// 创建新的线程显示STL窗口
boost::thread stl_thread([&stl_viewer]() {
while (!stl_viewer->wasStopped()) {
stl_viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
});
// 保存PCD文件
pcl::io::savePCDFileASCII("Wuson.stl.pcd", *cloud);
// 加载点云文件
pcl::io::loadPCDFile("Wuson.stl.pcd", *cloud);
std::cout << "Loaded PCD with " << cloud->points.size() << " points." << std::endl;
// 创建PCLVisualizer显示PCD文件
boost::shared_ptr<pcl::visualization::PCLVisualizer> pcd_viewer(new pcl::visualization::PCLVisualizer("PCD Viewer"));
pcd_viewer->addPointCloud(cloud, "cloud");
// 主要线程处理PCD显示
while (!pcd_viewer->wasStopped()) {
pcd_viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
// 等待STL显示线程结束
stl_thread.join();
return 0;
}