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

三、结果展示

版本一结果

版本二结果

相关推荐
OG one.Z25 分钟前
05_逻辑回归
算法·机器学习·逻辑回归
lzptouch29 分钟前
逻辑斯蒂回归(Logistic Regression)算法
算法·数据挖掘·回归
on_pluto_33 分钟前
【基础复习1】ROC 与 AUC:逻辑回归二分类例子
人工智能·机器学习·职场和发展·学习方法·1024程序员节
友友马33 分钟前
『 QT 』QT控件属性全解析 (二)
开发语言·数据库·qt
Yupureki1 小时前
从零开始的C++学习生活 12:AVL树全面解析
c语言·数据结构·c++·学习·visual studio
我是华为OD~HR~栗栗呀1 小时前
华为OD-23届-测试面经
java·前端·c++·python·华为od·华为·面试
我是华为OD~HR~栗栗呀1 小时前
华为od面经-23届-Java面经
java·c语言·c++·python·华为od·华为·面试
逐步前行4 小时前
C标准库--C99--布尔型<stdbool.h>
c语言·开发语言
SunnyKriSmile4 小时前
C语言译码操作
c语言·算法·if语句·译码操作·switch语句
QX_hao4 小时前
【Go】--闭包
开发语言·golang