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

三、结果展示

版本一结果

版本二结果

相关推荐
vincent&lin2 小时前
vLLM - GPUModelRunner
人工智能·vllm
9毫米的幻想3 小时前
【Linux系统】—— 程序地址空间
java·linux·c语言·jvm·c++·学习
用户5191495848453 小时前
在AI技术唾手可得的时代,挖掘JavaScript学习资源的新需求成为关键
人工智能·aigc
天选之女wow3 小时前
【代码随想录算法训练营——Day27(Day26休息)】贪心算法——455.分发饼干、376.摆动序列、53.最大子数组和
算法·leetcode·贪心算法
北邮刘老师3 小时前
【未来】智能体互联时代的商业模式变化和挑战:从HOM到AOM
人工智能·大模型·智能体·智能体互联网
东方芷兰3 小时前
LLM 笔记 —— 03 大语言模型安全性评定
人工智能·笔记·python·语言模型·自然语言处理·nlp·gpt-3
小树苗1933 小时前
Berachain稳定币使用指南:HONEY与跨链稳定币的协同之道
大数据·人工智能·区块链
MediaTea3 小时前
Python 库手册:keyword 关键字查询
开发语言·python
不枯石3 小时前
Matlab通过GUI实现点云的Loss配准
图像处理·算法·计算机视觉·matlab