ch04——4.3.4连接两个点云中的字段或数据形成新点云
程序员文章站
2022-07-14 20:23:32
...
ch04——4.3.4连接两个点云中的字段或数据形成新点云
进行连接前要保证两个数据中的字段类型和维度相同,数目一样。如:点云a是n个点的xyz点,点云b是n个点的rgb点,则形成c点云是n个点xyzrgb类型。
1.代码
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <iostream>
using namespace std;
using namespace pcl;
int main() {
//三个输入:cloud_a, cloud_b,n_cloud_b
//两个输出:cloud_c,p_n_cloud_c
PointCloud<pcl::PointXYZ>cloud_a, cloud_b, cloud_c;
PointCloud<pcl::Normal>n_cloud_b;//存储进行连接时normal点云
PointCloud<pcl::PointNormal>p_n_cloud_c;
//创建点云
cloud_a.width = 3;//点云个数为3
//都设置为无序点云
cloud_a.height = cloud_b.height = n_cloud_b.height = 1;
cloud_a.points.resize(cloud_a.width*cloud_a.height)
cloud_b.width = 2;//点云个数为2
cloud_b.points.resize(cloud_b.width*cloud_b.height);
n_cloud_b.width = 3;//如果是链接xyz与normal则生成3个法线
n_cloud_b.points.resize(n_cloud_b.width*n_cloud_b.height);
//以下循环生成无序点云,填充定义上面两种类型点云对象
for (size_t i = 0; i < cloud_a.points.size(); i++)
{
//cloud_a始终产生3个点
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);
}
for (size_t i = 0; i < cloud_b.points.size(); i++)
{ //cloud_b两个点作为xyz数据
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);
}
for (size_t i = 0; i < n_cloud_b.points.size(); i++)
{
//如果xyz+normal=xyznormal则n_cloud_b用三个点作为normal数据
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 << "cloud_a:" << endl;
for (size_t i = 0; i < cloud_a.points.size(); i++)
{
cout << " " << cloud_a.points[i].x << " " << cloud_a.points[i].y
<< " " << cloud_a.points[i].z << endl;
}
cout << "cloud_b" << endl;
for (size_t i = 0; i < cloud_b.points.size(); i++)
{
cout << " " << cloud_b.points[i].x << " " << cloud_b.points[i].y
<< " " << cloud_b.points[i].z << endl;
}
cout << " n_cloud_b" << endl;
for (size_t i = 0; i < n_cloud_b.points.size(); i++)
{
cout << " " << n_cloud_b.points[i].normal[0] << " " << n_cloud_b.points[i].normal[1]
<< " " << n_cloud_b.points[i].normal[2] << endl;
}
//拷贝点云数据
cloud_c = cloud_a;
cloud_c += cloud_b;
//点云连接
cout << "cloud_c=cloud_a+cloud_b" << endl;
for (size_t i = 0; i < cloud_c.points.size(); i++)
{
cout << " " << cloud_c.points[i].x << " " << cloud_c.points[i].y
<< " " << cloud_c.points[i].z << endl;
}
//点云字段连接
concatenateFields(cloud_a, n_cloud_b, p_n_cloud_c);
cout << "p_n_cloud_c=cloud_a+n_cloud_b" << endl;
for (size_t i = 0; i < p_n_cloud_c.points.size(); i++)
{
cout << " " << 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;
}
return 0;
}
2.显示