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

三、结果展示

版本一结果

版本二结果

相关推荐
老胡说科技1 小时前
美砺科技谢秀鹏:让“看见”走在“相信”之前,AI驱动下的数字化范式革命,从“技术长征”到“生态协同”
人工智能·科技
endcy20164 小时前
基于Spring AI的RAG和智能体应用实践
人工智能·ai·系统架构
Blossom.1184 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
FPGA小迷弟4 小时前
ChatGPT回答用AI怎么怎么赚钱
大数据·人工智能
轻微的风格艾丝凡4 小时前
卷积的直观理解
人工智能·深度学习·神经网络·算法·计算机视觉·matlab·cnn
月下倩影时5 小时前
视觉进阶篇——机器学习训练过程(手写数字识别,量大管饱需要耐心)
人工智能·学习·机器学习
PixelMind5 小时前
【超分辨率专题】HYPIR:扩散模型先验与 GAN 对抗训练相结合的新型图像复原框架
人工智能·生成对抗网络·扩散模型·图像复原
说私域5 小时前
从裂变能力竞争到技术水平竞争:开源AI智能名片链动2+1模式S2B2C商城小程序对微商企业竞争格局的重塑
人工智能·小程序·开源
xybDIY5 小时前
基于 Tuya.AI 开源的大模型构建智能聊天机器人
人工智能·机器人·开源
这张生成的图像能检测吗5 小时前
(论文速读)基于DCP-MobileViT网络的焊接缺陷识别
图像处理·深度学习·计算机视觉·可视化·缺陷识别·焊缝缺陷