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;
}

三、结果展示

版本一结果

版本二结果

相关推荐
zimoyin5 小时前
浅浅了解下0拷贝技术
java·linux·开发语言
AI架构师易筋6 小时前
AIOps 告警归因中的提示工程:从能用到可上生产(4 阶梯)
开发语言·人工智能·llm·aiops·rag
你的冰西瓜6 小时前
C++中的array容器详解
开发语言·c++·stl
淬炼之火6 小时前
笔记:Cross Modal Fusion-Mamba
图像处理·笔记·计算机视觉·多模态·特征融合
2501_943469156 小时前
【无标题】
数据结构·算法
_codemonster6 小时前
计算机视觉入门到实战系列(八)Harris角点检测算法
python·算法·计算机视觉
数说星榆1816 小时前
在线高清泳道图制作工具 无水印 PC
大数据·人工智能·架构·机器人·流程图
Snow_day.6 小时前
有关排列排列组合(1)
数据结构·算法·贪心算法·动态规划·图论
随丶芯6 小时前
IDEA安装leetcode-editor插件
java·开发语言
说私域6 小时前
B站内容生态下的私域流量运营创新:基于AI智能名片链动2+1模式与S2B2C商城小程序的融合实践
人工智能·小程序·流量运营