Point Cloud Joins Point Cloud Field Joins

What is point cloud connection

Point cloud joining refers to merging two or more point cloud data together to form a larger point cloud dataset. In this process, point cloud data are placed together according to their coordinate positions. Specifically, the point cloud join operation sequentially merges the points of two point clouds into a new point cloud object. Eventually, the new point cloud object will contain all the points of both point clouds, and the order of the points remains the same. Doing so allows multiple point cloud data to be combined to form large, more complex point cloud scenes.

pcl::concatenateThe or pcl::concatenatePointsfunction is used to connect the points of multiple point cloud data objects together to form a new point cloud data object. This function is actually a splicing or connection operation of point cloud data objects, which will change the number and position of point clouds.

What is point cloud field join

pcl::concatenateFieldsThe function is used to connect the fields (attributes) of multiple point cloud data objects together, such as combining fields such as XYZ coordinates, RGB color, normal, etc. into a new point cloud data object. This function can combine different fields in different point cloud data objects into a larger point cloud data object, but it will not change the number and position of the point cloud.

Point cloud connection code

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

void generateRandomPointCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, int num_points){
    srand(time(0));
    cloud->resize(num_points);

    for(int i=0; i < num_points; ++i){
        cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
    }
}


int main()
{
    // 定义两个点云对象
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>());
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>());

    // 生成对及点云数据
    int num_points = 30;
    generateRandomPointCloud(cloud1, num_points);
    generateRandomPointCloud(cloud2, num_points);

    // 进行点云连接
    pcl::PointCloud<pcl::PointXYZ>::Ptr combined(new pcl::PointCloud<pcl::PointXYZ>()); // 用于存储连接结果
    pcl::concatenate(*cloud1, *cloud2,*combined);


    std::cout << "Combined Point Cloud size: " << combined->size() << std::endl;

    // 设置点云可视化
    pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");
    viewer.setBackgroundColor(0,0,0);
    viewer.addCoordinateSystem(1.0);
    viewer.initCameraParameters();
    /*设置点云大小*/
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(combined, 0, 255, 0); // 颜色
    viewer.addPointCloud<pcl::PointXYZ>(combined, single_color,"combined_cloud");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, "combined_cloud"); // 大小

    viewer.spin();
    return 0;
}
Each of the two point cloud objects has 5 points, and the total is 10 points

 

Point cloud field connection code

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/features/normal_3d.h>
#include <pcl/common/random.h>
#include <pcl/visualization/pcl_visualizer.h>

int main()
{
    // ------------------------------
    // 随机生成点云数据对象和法线数据对象
    // -----------------------------
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());//点云3D坐标
    pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);// 点云法线信息
    pcl::PointCloud<pcl::PointNormal>::Ptr pointNormals(new pcl::PointCloud<pcl::PointNormal>);//点云3D坐标和法线信息

    pcl::common::UniformGenerator<float> uniformGen(-1.0f, 1.0f); // 在范围(-1.0,1.0)内生成均匀分布的随机生成器
    int numPoints = 5;
    for (int i=0; i<numPoints; ++i){
        pcl::PointXYZ point;
        point.x = uniformGen.run();
        point.y = uniformGen.run();
        point.z = uniformGen.run();
        cloud->push_back(point);

        pcl::Normal normal;
        normal._Normal::normal_x = uniformGen.run();
        normal._Normal::normal_y = uniformGen.run();
        normal._Normal::normal_z = uniformGen.run();
        normals->push_back(normal);
    }

    // ------------------------------------
    // 将XYZ 和 法线子段连接成一个新的点云数据对象
    // -------------------------------------
    pcl::concatenateFields(*cloud, *normals, *pointNormals);




    // ------------------------------------
    // 可视化点云和法线
    // -------------------------------------

    pcl::visualization::PCLVisualizer viewer("Point Cloud with Normals");
    viewer.setBackgroundColor(0,0,0);
    viewer.addCoordinateSystem(1.0);
    viewer.cameraParamsSet();

    // 添加点云和法向量到可视化对象
    viewer.addPointCloud<pcl::PointNormal>(pointNormals, "cloud_normals");

    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud_normals");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 1.0, 0.0, "cloud_normals");

    // 显示法向量
    viewer.addPointCloudNormals<pcl::PointNormal>(pointNormals,
                                                  1, // 每隔一个点进行法线可视化;为了方便可视化,避免过多的法线显示
                                                  0.1,  // 表示法线的长度
                                                  "normals");

    // 可视化循环
    while(!viewer.wasStopped()){
        viewer.spinOnce();
    }
    return 0;
}
3D coordinates and normal vectors are added to a piece

 

 

Guess you like

Origin blog.csdn.net/weixin_45824067/article/details/131451854