在进行多传感器融合后,往往会给点云数据赋予更多的信息,类似xyzrgb、法向量、强度等等点云数据,此时就需要我们对pcl库中的点云类型进行自定义操作,以此来满足我们点云数据处理时对点云数据结构的需求。
#define PCL_NO_PRECOMPILE //如果想使用PCL提供的模板类,比如体素下采样,需要添加此行代码
#include <iostream>
#include <pcl/io/pcd_io.h>
using namespace std;
//自定义点云类型
struct PointXYZRGBNormalI
{
PCL_ADD_POINT4D; //添加XYZ,此时相当于 PointXYZ
PCL_ADD_RGB; //添加RGB,此时相当于 PointXYZRGB
PCL_ADD_NORMAL4D; //添加NORMAL,此时相当于 PointXYZRGBNormal
float intensity; //添加intensity,此时相当于 PointXYZRGBNormalintensity
PCL_MAKE_ALIGNED_OPERATOR_NEW //确保new操作符对齐操作
}EIGEN_ALIGN16; //强制SSE内存对齐
//注册点类型宏(要包含所有添加的字段,否则无法正常使用未注册的字段)
POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZRGBNormalI,
(float,x,x)
(float,y,y)
(float,z,z)
(uint8_t,b,b)
(uint8_t,g,g)
(uint8_t,r,r)
(float,rgb,rgb)
(float,normal_x,normal_x)
(float,normal_y,normal_y)
(float,normal_z,normal_z)
(float,intensity,intensity)
);
int main() {
pcl::PointCloud<PointXYZRGBNormalI> cloud; //定义一个自定义的点云数据结构
cloud.width = 2;
cloud.height = 1; //定义该点云为无序点云
cloud.points.resize(cloud.width*cloud.height);
cloud[0].x=cloud[0].y=cloud[0].z = 1;
cloud[1].x=cloud[1].y=cloud[1].z = 2;
cloud[0].r = cloud[0].g =cloud[0].b = 2;
cloud[1].r = cloud[1].g =cloud[1].b = 3;
cloud[0].intensity =cloud[1].intensity = 2;
cloud[0].normal_x= cloud[1].normal_x =cloud[0].normal_y= cloud[1].normal_y=cloud[0].normal_z= cloud[1].normal_z =1;
pcl::io::savePCDFile("saved_points.pcd",cloud); //保存自定义的点云数据文件
cout<<"the size of saved pcd:"<<cloud.points.size()<<endl;
for (size_t i = 0; i < cloud.points.size(); ++i) //逐个输出该点云中的点
{
std::cout << cloud.points[i].x << " "
<< cloud.points[i].y << " " << cloud.points[i].z<<" "<<int(cloud.points[i].r)<<" "<<int(cloud.points[i].g)<<" "<<int(cloud.points[i].b)<<" "<<cloud.points[i].normal_x<<" "<<cloud.points[i].normal_y<<" "<<cloud.points[i].normal_z<<" "<<cloud.points[i].intensity<<std::endl;
}
return 0;
}