PCL 生成缺角立方体点云

目录

一、算法原理

使用PCL库生成一个缺角立方体点云。

二、代码实现

版本一

cpp 复制代码
#include <iostream>
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include "time.h"
#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>


int
main(int argc, char* argv[])
{
    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);

    srand(time(NULL));
    for (int i = 0; i <= 800; i++)    // Cube 10K , 40 ------> For more density CUbe 30K , i<= 120     // Or We Can Make the size of the cube 3 times less , the size of the cube is between 0 and 0.3
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = 0.00;
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
   
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = 0.00;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
   
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.00;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
    
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.25 - 0.00)) + 0.00);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = 0.50;
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 400; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.25 - 0.00)) + 0.00);
        basic_point.z = 0.50;
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = 0.50;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.25 - 0.00)) + 0.00);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
  
    for (int i = 0; i <= 400; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.25 - 0.00)) + 0.00);
        basic_point.y = 0.50;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }


    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.50;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.25 - 0.00)) + 0.00);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
   
    for (int i = 0; i <= 400; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.50;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.25 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
   
    for (int i = 0; i <= 400; i++)    // Cube 10K , 40 ------> For more density CUbe 30K , i<= 120     // Or We Can Make the size of the cube 3 times less , the size of the cube is between 0 and 0.3
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.z = 0.25;
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
   
    for (int i = 0; i <= 400; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.y = 0.25;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
   
    for (int i = 0; i <= 400; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.25;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.25)) + 0.25);
        basic_point.r = 255;
        basic_point.g = 255;
        basic_point.b = 255;
        cloud->points.push_back(basic_point);
    }
    
    cloud->width = (int)cloud->points.size();
    cloud->height = 1;

    // Show the cloud
    pcl::visualization::CloudViewer viewer("Sharp Edge");
    viewer.showCloud(cloud);
    while (!viewer.wasStopped())
    {
    }


    return 0;
}

版本二

cpp 复制代码
#include <iostream>
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include "time.h"
#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>


int
main(int argc, char* argv[])
{
    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);

    //1. First Cube
    srand(time(NULL));
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.00;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = 0.00;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        cloud->points.push_back(basic_point);
    }


    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = 0.50;
        cloud->points.push_back(basic_point);
    }

    // First Cube
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.50;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = 0.50;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = 1.00;
        cloud->points.push_back(basic_point);
    }
    // Second Cube
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.50;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.y = 0.00;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = 0.00;
        cloud->points.push_back(basic_point);
    }

    // Second Cube
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 1.00;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.y = 0.50;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.z = 0.50;
        cloud->points.push_back(basic_point);
    }
    // Third Cube

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.00;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = 0.50;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.z = 0.00;
        cloud->points.push_back(basic_point);
    }

    // Third Cube
    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = 0.50;
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = 1.00;
        basic_point.z = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        cloud->points.push_back(basic_point);
    }

    for (int i = 0; i <= 800; i++)
    {
        pcl::PointXYZRGBA basic_point;
        basic_point.x = (((((float)rand()) / (float)RAND_MAX) * (0.50 - 0.00)) + 0.00);
        basic_point.y = (((((float)rand()) / (float)RAND_MAX) * (1.00 - 0.50)) + 0.50);
        basic_point.z = 0.50;
        cloud->points.push_back(basic_point);
    }

    cloud->width = (int)cloud->points.size();
    cloud->height = 1;
    std::cout << "Number of points in the input cloud is:" << cloud->points.size() << std::endl;


    for (size_t i = 0; i < cloud->points.size(); ++i)
    {
        cloud->points[i].r = 255;
        cloud->points[i].g = 255;
        cloud->points[i].b = 255;
    }

    // Show the cloud
    pcl::visualization::CloudViewer viewer("Sharp Edge");
    viewer.showCloud(cloud);
    while (!viewer.wasStopped())
    {
    }


    return 0;
}

三、结果展示

版本一结果

版本二结果

相关推荐
liu****几秒前
20.预处理详解
c语言·开发语言·数据结构·c++·算法
蕨蕨学AI几秒前
【Wolfram语言】8 基本图形对象
开发语言·wolfram
九河云11 分钟前
跨境电商数字化转型:海外仓库存 AI 预警与多平台订单一体化管理实践
大数据·人工智能·数字化转型
HuggingFace12 分钟前
Hugging Face 论文页面功能指南
人工智能
努力学算法的蒟蒻14 分钟前
day26(12.6)——leetcode面试经典150
算法·leetcode·面试
froginwe1117 分钟前
NumPy 数组属性
开发语言
羑悻的小杀马特18 分钟前
突破算力极限:基于openEuler的容器级AI推理优化实战
人工智能
代码游侠18 分钟前
数据结构——哈希表
数据结构·笔记·学习·算法·哈希算法·散列表
林伟_fpga20 分钟前
室联人形机器人居家服务:提高安全性、任务场景降维、工作流程
人工智能·机器人