本文包括pcl官方实现的字段拼接和数据拼接以及自实现两种拼接的代码。
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
using namespace std;
int main()
{
pcl::PointCloud<pcl::PointXYZ> cloud_a,cloud_b,cloud_c,cloud_c_op;
pcl::PointCloud<pcl::Normal> n_cloud_b;
pcl::PointCloud<pcl::PointNormal> n_cloud_b_normal,n_cloud_b_normal_op;
for(size_t i = 0;i<3;i++)
{
pcl::PointXYZ tmp;
tmp.x = 1024*rand()/(RAND_MAX+1.0f);
tmp.y = 1024*rand()/(RAND_MAX+1.0f);
tmp.z = 1024*rand()/(RAND_MAX+1.0f);
cloud_a.push_back(tmp);
}
for(size_t i = 0;i<cloud_a.points.size();i++)
{
cout<<"cloud_a:x="<<cloud_a.points[i].x<<",y="<<cloud_a.points[i].y<<",z="<<cloud_a.points[i].z<<endl;
}
for(size_t i = 0;i<3;i++)
{
pcl::PointXYZ tmp;
tmp.x = 1024*rand()/(RAND_MAX+1.0f);
tmp.y = 1024*rand()/(RAND_MAX+1.0f);
tmp.z = 1024*rand()/(RAND_MAX+1.0f);
cloud_b.push_back(tmp);
}
for(size_t i = 0;i<cloud_b.points.size();i++)
{
cout<<"cloud_b:x="<<cloud_b.points[i].x<<",y="<<cloud_b.points[i].y<<",z="<<cloud_b.points[i].z<<endl;
}
for(size_t i = 0;i<3;i++)
{
pcl::Normal tmp_normal;
tmp_normal.normal_x = 1024*rand()/(RAND_MAX+1.0f);
tmp_normal.normal_y = 1024*rand()/(RAND_MAX+1.0f);
tmp_normal.normal_z = 1024*rand()/(RAND_MAX+1.0f);
n_cloud_b.push_back(tmp_normal);
}
for(size_t i = 0;i<n_cloud_b.points.size();i++)
{
cout<<"n_cloud_b:normal_x="<<n_cloud_b.points[i].normal_x<<",normal_y="<<n_cloud_b.points[i].normal_y<<",normal_z="<<n_cloud_b.points[i].normal_z<<endl;
}
//数据连接-----------------
//pcl官方操作
cloud_c =cloud_a;
cloud_c += cloud_b;
// for(size_t i = 0;i<cloud_c.points.size();i++)
// {
// cout<<"cloud_c:x="<<cloud_c.points[i].x<<",y="<<cloud_c.points[i].y<<",z="<<cloud_c.points[i].z<<endl;
// }
//自定义操作
for(size_t i = 0; i<cloud_a.points.size()+cloud_b.points.size();i++)
{
if(i<cloud_a.points.size())
{
pcl::PointXYZ tmp;
tmp.x = cloud_a.points[i].x;
tmp.y = cloud_a.points[i].y;
tmp.z = cloud_a.points[i].z;
cloud_c_op.push_back(tmp);
}
else
{
pcl::PointXYZ tmp;
tmp.x = cloud_b.points[i-cloud_a.points.size()].x;
tmp.y = cloud_b.points[i-cloud_a.points.size()].y;
tmp.z = cloud_b.points[i-cloud_a.points.size()].z;
cloud_c_op.push_back(tmp);
}
}
// for(size_t i = 0;i<cloud_c_op.points.size();i++)
// {
// cout<<"cloud_c_op:x="<<cloud_c_op.points[i].x<<",y="<<cloud_c_op.points[i].y<<",z="<<cloud_c_op.points[i].z<<endl;
// }
//-----------------
//字段拼接---------------
//pcl官方操作
pcl::concatenateFields(cloud_b,n_cloud_b,n_cloud_b_normal);
for(size_t i = 0;i<n_cloud_b_normal.points.size();i++)
{
cout<<"n_cloud_b_normal:x="<<n_cloud_b_normal.points[i].x<<",y="<<n_cloud_b_normal.points[i].y<<",z="<<n_cloud_b_normal.points[i].z<<",normal_x:"<<n_cloud_b_normal.points[i].normal_x<<",normal_y"<<n_cloud_b_normal.points[i].normal_y<<",normal_z"<<n_cloud_b_normal.points[i].normal_z<<endl;
}
//自定义操作
for(size_t i = 0; i<cloud_b.points.size();i++)
{
pcl::PointNormal tmp;
tmp.x = cloud_b.points[i].x;
tmp.y = cloud_b.points[i].y;
tmp.z = cloud_b.points[i].z;
tmp.normal_x = n_cloud_b.points[i].normal_x;
tmp.normal_y = n_cloud_b.points[i].normal_y;
tmp.normal_z = n_cloud_b.points[i].normal_z;
n_cloud_b_normal_op.push_back(tmp);
}
for(size_t i = 0;i<n_cloud_b_normal_op.points.size();i++)
{
cout<<"n_cloud_b_normal_op:x="<<n_cloud_b_normal_op.points[i].x<<",y="<<n_cloud_b_normal_op.points[i].y<<",z="<<n_cloud_b_normal_op.points[i].z<<",normal_x:"<<n_cloud_b_normal_op.points[i].normal_x<<",normal_y"<<n_cloud_b_normal_op.points[i].normal_y<<",normal_z"<<n_cloud_b_normal_op.points[i].normal_z<<endl;
}
return 0;
}