连接两个不同的点云为一个点云。两种情况:
- 同种类型点云拼接:
- 同样N个点的xyz和rgb拼接:或者xyz与Normal的拼接。
注意不同点云类型的转换。
第一种:同种类型点云直接拼接。
#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;
cerr << "Cloud A:" << endl;
cloud_a.width = 5;
cloud_a.height = cloud_b.height = 1;
cloud_a.points.resize(cloud_a.width * cloud_a.height);
for (size_t i = 0; i < cloud_a.size(); i++)
{
cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
cerr << " " << cloud_a.points[i].x << " " << cloud_a.points[i].y << " " << cloud_a.points[i].z << endl;
}
cerr << "Cloud B:" << endl;
cloud_b.width = 3;
cloud_b.points.resize(cloud_b.width * cloud_b.height);
for (size_t i = 0; i < cloud_b.size(); i++)
{
cloud_b.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_b.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_b.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
cerr << " " << cloud_b.points[i].x << " " << cloud_b.points[i].y << " " << cloud_b.points[i].z << endl;
}
cout << endl;
cerr << "拼接之后:Cloud C:" << endl;
cloud_c = cloud_a;
cloud_c += cloud_b; // 简单粗暴。
for (size_t i = 0; i < cloud_c.size(); i++)
{
cerr << " " << cloud_c.points[i].x << " " << cloud_c.points[i].y << " " << cloud_c.points[i].z << endl;
}
}
输出:
Cloud A:
1.28125 577.094 197.938
828.125 599.031 491.375
358.688 917.438 842.563
764.5 178.281 879.531
727.531 525.844 311.281
Cloud B:
15.3438 93.5938 373.188
150.844 169.875 1012.22
456.375 121.938 4.78125
拼接之后:Cloud C:
1.28125 577.094 197.938
828.125 599.031 491.375
358.688 917.438 842.563
764.5 178.281 879.531
727.531 525.844 311.281
15.3438 93.5938 373.188
150.844 169.875 1012.22
456.375 121.938 4.78125
缺少去重,排异,对齐等操作。
第二种:同样N个点的xyz和rgb拼接:或者xyz与Normal的拼接。
#include<iostream>
#include<pcl/io/pcd_io.h>
#include<pcl/point_types.h>
using namespace std;
int main()
{
pcl::PointCloud<pcl::PointXYZ>cloud_a;
pcl::PointCloud<pcl::Normal>n_cloud_b;
pcl::PointCloud<pcl::PointNormal> p_n_cloud_c;
cerr << "Cloud A:" << endl;
cloud_a.width = 5;
cloud_a.height = n_cloud_b.height = 1;
cloud_a.points.resize(cloud_a.width * cloud_a.height);
for (size_t i = 0; i < cloud_a.size(); i++)
{
cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
cerr << " " << cloud_a.points[i].x << " " << cloud_a.points[i].y << " " << cloud_a.points[i].z << endl;
}
cerr << "Cloud B:" << endl;
n_cloud_b.width = 5;
n_cloud_b.points.resize(n_cloud_b.width * n_cloud_b.height);
for (size_t i = 0; i < n_cloud_b.size(); i++)
{
n_cloud_b.points[i].normal[0] = 1024 * rand() / (RAND_MAX + 1.0f);
n_cloud_b.points[i].normal[1] = 1024 * rand() / (RAND_MAX + 1.0f);
n_cloud_b.points[i].normal[2] = 1024 * rand() / (RAND_MAX + 1.0f);
cout << " " << n_cloud_b.points[i].normal[0] << " " << n_cloud_b.points[i].normal[1] << n_cloud_b.points[i].normal[2] << endl;
}
cout << endl;
cout << "Cloud C:" << endl;
pcl::concatenateFields(cloud_a, n_cloud_b, p_n_cloud_c);
for (size_t i = 0; i < p_n_cloud_c.size(); i++)
{
cerr << " " << p_n_cloud_c.points[i].x << " " << p_n_cloud_c.points[i].y << " " << p_n_cloud_c.points[i].z
<< " " << p_n_cloud_c.points[i].normal[0] << " " << p_n_cloud_c.points[i].normal[1]
<< " " << p_n_cloud_c.points[i].normal[2] << endl;
}
}
cloud_b可以看作法线或者RGB等信息。
Cloud A:
1.28125 577.094 197.938
828.125 599.031 491.375
358.688 917.438 842.563
764.5 178.281 879.531
727.531 525.844 311.281
Cloud B:
15.3438 93.5938373.188
150.844 169.8751012.22
456.375 121.9384.78125
9.125 386.938544.406
584.875 616.188621.719
Cloud C:
1.28125 577.094 197.938 15.3438 93.5938 373.188
828.125 599.031 491.375 150.844 169.875 1012.22
358.688 917.438 842.563 456.375 121.938 4.78125
764.5 178.281 879.531 9.125 386.938 544.406
727.531 525.844 311.281 584.875 616.188 621.719