点云库(PCL)学习——Advanced Usage(一)

添加自定义的PointT类型

这篇文档只适用于PCL0.x和1.x版本。

PCL提供了各种预定义的点类型,从XYZ数据的SSE对齐结构到更复杂的n维直方图表示,如PFH(点特征直方图)。这些类型应该足够支持所有的在PCL中实现的算法以及方法。然而,仍然存在一些情况需要使用者自行定义新的类型。

PS:SSE对齐结构意思应该是说定义结构体时,X、Y、Z的数据类型都是一样的——译者注

1. 为什么使用PointT 类型

PCL的PointT可以追溯到ROS内部开发的一个库的时代。当时存在一个共识,点云是一个复杂的n-D结构,需要能够表示不同类型的信息。但是,用户应该知道并理解需要传递哪些类型的信息,以便使代码更易于调试、考虑优化等。
这里列举了一个例子,对XYZ数据简单操作。对于支持SSE的处理器,最有效的方法是将3个维度存储为浮点,然后使用额外的浮点进行填充:

struct PointXYZ
{
  float x;
  float y;
  float z;
  float padding;
};

然而,作为一个例子,如果用户正在嵌入式平台上编译PCL,添加额外的填充可能会浪费内存。因此,可以使用没有最后一个浮点的更简单的PointXYZ结构。
此外,如果应用程序需要一个包含XYZ 3D数据、RGBA信息(颜色)和每个点估计的曲面法线的PointXYZRGBNormal,那么使用上述所有内容定义结构就很简单了。由于PCL中的所有算法都应该模板化,因此除了结构定义之外,不需要进行其他更改。

2.PCL中有哪些PointT类型

为了囊括所有我们能想到的情况,我们定义了许多point类型。下面仅为一小段代码,完整部分请看point_types.hpp。

  • PointXYZ
    这是最常用的一种数据类型,它表示了三维xyz的信息。用户可以通过例如:points[i].data[0]或points[i].x来获取x坐标。
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
  • PointXYZI
    简单XYZ+强度点类型。在理想情况下,这4个组件将创建一个单一的结构,SSE对齐。但是,由于大多数点操作都会将data[4]数组的最后一个组成部分(从xyz联合体)设置为0或1(用于变换),因此不能将强度设置为同一结构的成员,因为其内容将被覆盖。例如,两点之间的点积将其第4个分量设置为0,否则点积就没有意义,等等。
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
union
{
  struct
  {
    float intensity;
  };
  float data_c[4];
};
  • PointXYZRGBA
    与PointXYZI类似,只不过rgba包含了打包成无符号32位整型数的RGBA信息。根据联合体声明,通过名字单独获取颜色通道也是可以实现的。
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
union
{
  union
  {
    struct
    {
      std::uint8_t b;
      std::uint8_t g;
      std::uint8_t r;
      std::uint8_t a;
    };
    float rgb;
  };
  std::uint32_t rgba;
};
  • PointXYZRGB
    float x, y, z; std::uint32_t rgba;
    与PointXYZRGBA类似
  • PointXY
    float x, y;
    简单的二维point 结构
struct
{
  float x;
  float y;
};
  • InterestPoint
    float x, y, z, strength;
    与PointXYZI类似,只不过strength包含了关键点(keypoint)的强度度量值。
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
union
{
  struct
  {
    float strength;
  };
  float data_c[4];
};
  • Normal
    float normal[3], curvature;
    另外一种常用的数据类型,Normal结构表示在给定点处面的法向量以及曲率的度量值(与曲面面片的特征值之间的关系在同一调用中获得–详情请看NormalEstimation class API)。
    因为在PCL中,对曲面法线的操作非常常见,所以我们用第四个组件填充这3个组件,以实现SSE对齐和计算效率。用户可以通过points[i].data_n[0]或者points[i].normal[0]或者points[i].normal_x来获取法向量的第一个坐标。同样,曲率不能存储在同一个结构中,因为它会被正常数据上的操作覆盖。
union
{
  float data_n[4];
  float normal[3];
  struct
  {
    float normal_x;
    float normal_y;
    float normal_z;
  };
}
union
{
  struct
  {
    float curvature;
  };
  float data_c[4];
};
  • PointNormal
    float x, y, z; float normal[3], curvature;
    包含XYZ数据,以及表面法线和曲率。
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
union
{
  float data_n[4];
  float normal[3];
  struct
  {
    float normal_x;
    float normal_y;
    float normal_z;
  };
};
union
{
  struct
  {
    float curvature;
  };
  float data_c[4];
};
  • PointXYZRGBNormal
    float x, y, z, normal[3], curvature; std::uint32_t rgba;
    包含XYZ数据,RGBA颜色,以及表面法线和曲率。
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
union
{
  float data_n[4];
  float normal[3];
  struct
  {
    float normal_x;
    float normal_y;
    float normal_z;
  };
}
union
{
  struct
  {
    union
    {
      union
      {
        struct
        {
          std::uint8_t b;
          std::uint8_t g;
          std::uint8_t r;
          std::uint8_t a;
        };
        float rgb;
      };
      std::uint32_t rgba;
    };
    float curvature;
  };
  float data_c[4];
};
  • PointXYZINormal
    float x, y, z, intensity, normal[3], curvature;
    包含XYZ,强度值,表面法线和曲率
union
{
  float data[4];
  struct
  {
    float x;
    float y;
    float z;
  };
};
union
{
  float data_n[4];
  float normal[3];
  struct
  {
    float normal_x;
    float normal_y;
    float normal_z;
  };
}
union
{
  struct
  {
    float intensity;
    float curvature;
  };
  float data_c[4];
};

(下面略)

  • PointWithRange
  • PointWithViewpoint
  • MomentInvariants
  • PrincipalRadiiRSD
  • Boundary
  • PrincipalCurvatures
  • PFHSignature125
  • FPFHSignature33
  • VFHSignature308
  • Narf36
  • BorderDescription
  • IntensityGradient
  • Histogram
  • PointWithScale
  • PointSurfel

3.点的类型是如何暴露的?(How are the point types exposed?)

由于PCL模板非常多,并且在一个模板库,在一个源文件中包含许多的PCL算法会降低编译过程的速度。
为了加快包含和链接PCL的用户代码的速度,我们使用显式模板实例化,包括所有可能的组合,在这些组合中,可以使用PCL中已经定义的点类型调用所有算法。这意味着,一旦PCL被编译为库,任何用户代码都不需要编译模板代码,从而加快了编译速度。诀窍包括将模板化实现与前向声明类和方法的头分离,并在链接时解析。下面是一个虚构的例子:

// foo.h

#ifndef PCL_FOO_
#define PCL_FOO_

template <typename PointT>
class Foo
{
  public:
    void
    compute (const pcl::PointCloud<PointT> &input,
             pcl::PointCloud<PointT> &output);
}

#endif // PCL_FOO_

上面这段代码定义了头文件,通常包含在所有用户的代码里。如我们所见,我们正在定义方法和类,但是我们还没有实现任何东西。

// impl/foo.hpp

#ifndef PCL_IMPL_FOO_
#define PCL_IMPL_FOO_

#include "foo.h"

template <typename PointT> void
Foo::compute (const pcl::PointCloud<PointT> &input,
              pcl::PointCloud<PointT> &output)
{
  output = input;
}

#endif // PCL_IMPL_FOO_

上边的代码定义了实际的方法Foo::compute的模板实现。这个一般应该从用户代码中隐藏。

// foo.cpp

#include "pcl/point_types.h"
#include "pcl/impl/instantiate.hpp"
#include "foo.h"
#include "impl/foo.hpp"

// Instantiations of specific point types
PCL_INSTANTIATE(Foo, PCL_XYZ_POINT_TYPES));

最后,上面展示了在PCL中显式实例化的方式。宏“PCL_INSTANTIATE”除了遍历给定类型列表并为每个类型创建显式实例化外,什么都没有。
From pcl/include/pcl/impl/instantiate.hpp:

// PCL_INSTANTIATE: call to instantiate template TEMPLATE for all
// POINT_TYPES

#define PCL_INSTANTIATE_IMPL(r, TEMPLATE, POINT_TYPE) \
  BOOST_PP_CAT(PCL_INSTANTIATE_, TEMPLATE)(POINT_TYPE)

#define PCL_INSTANTIATE(TEMPLATE, POINT_TYPES)        \
  BOOST_PP_SEQ_FOR_EACH(PCL_INSTANTIATE_IMPL, TEMPLATE, POINT_TYPES);

PCL_XYZ_POINT_TYPES is (from pcl/include/pcl/impl/point_types.hpp):

// Define all point types that include XYZ data
#define PCL_XYZ_POINT_TYPES   \
  (pcl::PointXYZ)             \
  (pcl::PointXYZI)            \
  (pcl::PointXYZRGBA)         \
  (pcl::PointXYZRGB)          \
  (pcl::InterestPoint)        \
  (pcl::PointNormal)          \
  (pcl::PointXYZRGBNormal)    \
  (pcl::PointXYZINormal)      \
  (pcl::PointWithRange)       \
  (pcl::PointWithViewpoint)   \
  (pcl::PointWithScale)

基本上,如果只想显式实例化pcl::PointXYZ的Foo,则不需要使用宏,如下所示:

// foo.cpp

#include "pcl/point_types.h"
#include "pcl/impl/instantiate.hpp"
#include "foo.h"
#include "impl/foo.hpp"

template class Foo<pcl::PointXYZ>;

4.如何添加一个新的PointT类型

要添加一个新的point type,首先应该定义。比如:

struct MyPointType
{
  float test;
};

接下来,你需要确定你的代码包含了在PCL中你希望你的新point type‘MyPointType’所使用的模板头文件的实现。(Then, you need to make sure your code includes the template header implementation of the specific class/algorithm in PCL that you want your new point type MyPointType to work with.)
例如,你想使用pcl::PassThrough。你所需要做得全部就是:

#define PCL_NO_PRECOMPILE
#include <pcl/filters/passthrough.h>
#include <pcl/filters/impl/passthrough.hpp>

// the rest of the code goes here

如果您的代码是库的一部分,而库被其他人使用,那么对于您公开的任何类(从PCL到我们的外部PCL),尝试对MyPointType类型使用显式实例化也是有意义的。

5 Example

下面的代码段示例创建了一个新的点类型,该点类型包含XYZ数据(SSE填充)和一个测试浮点。

#define PCL_NO_PRECOMPILE
#include <pcl/memory.h>
#include <pcl/pcl_macros.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>

struct MyPointType
{
  PCL_ADD_POINT4D;                  // preferred way of adding a XYZ+padding
  float test;
  PCL_MAKE_ALIGNED_OPERATOR_NEW     // make sure our new allocators are aligned
} EIGEN_ALIGN16;                    // enforce SSE padding for correct memory alignment

POINT_CLOUD_REGISTER_POINT_STRUCT (MyPointType,           // here we assume a XYZ + "test" (as fields)
                                   (float, x, x)
                                   (float, y, y)
                                   (float, z, z)
                                   (float, test, test)
)


int
main (int argc, char** argv)
{
  pcl::PointCloud<MyPointType> cloud;
  cloud.points.resize (2);
  cloud.width = 2;
  cloud.height = 1;

  cloud[0].test = 1;
  cloud[1].test = 2;
  cloud[0].x = cloud[0].y = cloud[0].z = 0;
  cloud[1].x = cloud[1].y = cloud[1].z = 3;

  pcl::io::savePCDFile ("test.pcd", cloud);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PCL学习教程是关于点云Point Cloud Library)的教程,该可以用于处理和分析来自传感器的三维点云数据。学习PCL的教程通常包括以下内容: 1. 安装PCL:首先,你需要安装PCL及其依赖项。具体的安装方法可以参考PCL官方网站上的文档。 2. 点云数据的读取和可视化:学习如何读取和可视化点云数据是PCL学习的第一步。使用PCL提供的函数和类,你可以读取来自传感器的点云数据,并将其可视化以便观察和分析。 3. 点云滤波:PCL提供了各种滤波器,用于去除点云中的噪声、采样和下采样,以及提取感兴趣的特征。 4. 特征提取:学习如何从点云中提取表面特征,例如平面、曲率、法线等。 5. 点云配准:点云配准是将多个点云对齐到一个共同的坐标系中的过程。PCL提供了各种配准算法,包括ICP(迭代最近点)和SAC-IA(随机一致性),用于实现点云的配准。 6. 点云分割:点云分割是将点云分成多个不同的部分或对象的过程。PCL提供了各种分割算法,例如基于颜色、法线、平面模型等的分割算法。 7. 点云配准和分割的应用:学习如何将点云配准和分割应用于实际问题,例如机器人导航、三维重建和目标检测等。 在学习PCL时,你可以通过阅读PCL官方文档、实践示例代码和参加相关培训课程等方式来深入了解和掌握PCL的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值