Pcl读取stl文件,并转换成pcd文件,同时显示stl模型和pcd点云

由于不同版本的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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

养牛大人

感谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值