自定义PCL PointXYZIR点类型无法使用PCL函数与运算符的问题
我自定义了PCL的PointXYZIR点类型,代码如下:
#define PCL_NO_PRECOMPILE #include <pcl/pcl_macros.h> #include <pcl/point_types.h> #include <pcl/point_cloud.h> #include <Eigen/Geometry> #include <Eigen/Dense> #include <Eigen/Core> namespace pcl{ struct PointXYZIR { PCL_ADD_POINT4D; // Macro quad-word XYZ float intensity; // Laser intensity uint16_t ring; // Laser ring number EIGEN_MAKE_ALIGNED_OPERATOR_NEW // Ensure proper alignment } EIGEN_ALIGN16; } POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZIR, (float, x, x) (float, y, y) (float, z, z) (float, intensity, intensity) (uint16_t, ring, ring) ) typedef pcl::PointCloud<pcl::PointXYZIR>::Ptr pcXYZIRPtr; typedef pcl::PointCloud<pcl::PointXYZIR> pcXYZIR;
随后编写测试代码如下:
pcXYZIRPtr test1(new pcXYZIR); pcXYZIRPtr test2(new pcXYZIR); pcl::PointXYZIR test_p(1.0f,1.0f,1.0f,1.0f,1); test1->push_back(test_p); test2->push_back(test_p); test1 += test2;
运行后出现两个错误:
error: no matching function for call to ‘pcl::PointXYZIR::PointXYZIR(float, float, float, float, int)’
error: no match for ‘operator+=’ (operand types are ‘pcXYZIRPtr’ {aka ‘boost::shared_ptr<pcl::PointCloudpcl::PointXYZIR >’} and ‘pcXYZIRPtr’ {aka ‘boost::shared_ptr<pcl::PointCloudpcl::PointXYZIR >’})
92 | test1 += test2;
错误1:自定义点构造函数缺失
PCL的PCL_ADD_POINT4D宏仅生成默认构造函数和带XYZ参数的构造函数,未包含intensity和ring的重载构造函数,因此直接传入5个参数初始化会报错。
解决方法:在PointXYZIR结构体中手动添加对应构造函数:
namespace pcl{ struct PointXYZIR { PCL_ADD_POINT4D; // Macro quad-word XYZ float intensity; // Laser intensity uint16_t ring; // Laser ring number EIGEN_MAKE_ALIGNED_OPERATOR_NEW // Ensure proper alignment // 自定义构造函数 PointXYZIR() : intensity(0.0f), ring(0) {} PointXYZIR(float x_, float y_, float z_, float intensity_, uint16_t ring_) : intensity(intensity_), ring(ring_) { x = x_; y = y_; z = z_; data[3] = 1.0f; // PCL_ADD_POINT4D包含w分量,必须设为1.0以符合点云规范 } } EIGEN_ALIGN16; }
错误2:智能指针无法直接使用+=运算符
pcXYZIRPtr是boost::shared_ptr<pcl::PointCloud<pcl::PointXYZIR>>类型,智能指针本身未重载+=运算符,该运算符属于pcl::PointCloud类的成员,需要作用在指针指向的实际对象上。
解决方法:将代码修改为对指针解引用后使用运算符:
*test1 += *test2;
或者使用insert方法批量添加点云数据,效果一致:
test1->insert(test1->end(), test2->begin(), test2->end());
内容的提问来源于stack exchange,提问作者Kataran

