pcl1.12.1生成点云外包围盒

原理

xx


代码

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
#include <pcl/visualization/pcl_visualizer.h>

int main()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cube(new pcl::PointCloud<pcl::PointXYZ>);

    // 读取点云文件
    if (pcl::io::loadPCDFile<pcl::PointXYZ>("E:\\File\\gradPaper\\data\\station\\4Clip.pcd", *cloud) == -1)
    {
        PCL_ERROR("Couldn't read file test_pcd.pcd \n");
        return (-1);
    }

    // 获取最小包围盒
    Eigen::Vector4f min_pt, max_pt;
    pcl::getMinMax3D(*cloud, min_pt, max_pt);

    // 生成均匀分布的点云
    float x_range = max_pt[0] - min_pt[0];
    float y_range = max_pt[1] - min_pt[1];
    float z_range = max_pt[2] - min_pt[2];
    for (size_t i = 0; i < 10000; ++i)
    {
        pcl::PointXYZ point;
        int face = rand() % 6;
        switch (face)
        {
        case 0: // x = x_min
            point.x = min_pt[0];
            point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
            point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
            break;
        case 1: // x = x_max
            point.x = max_pt[0];
            point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
            point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
            break;
        case 2: // y = y_min
            point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
            point.y = min_pt[1];
            point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
            break;
        case 3: // y = y_max
            point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
            point.y = max_pt[1];
            point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
            break;
        case 4: // z = z_min
            point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
            point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
            point.z = min_pt[2];
            break;
        case 5: // z = z_max
            point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
            point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
            point.z = max_pt[2];
            break;
        }
        cloud_cube->points.push_back(point);
    }

    // 设置点云的宽度和高度
    cloud_cube->width = cloud_cube->points.size();
    cloud_cube->height = 1;

    // 保存点云
    pcl::io::savePCDFileASCII("E:\\File\\gradPaper\\data\\station\\4ClipCube.pcd", *cloud_cube);

    // 可视化
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
    viewer->addCube(min_pt[0], max_pt[0], min_pt[1], max_pt[1], min_pt[2], max_pt[2], 1.0, 1.0, 0.0, "cube");
    viewer->spin();

    return (0);
}

效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Qt, C++

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值