4

在使用不同的库时,我总是发现每个库都有相同的“真实单词实体”的自定义类型。

假设我有一个使用 3 维点的项目,我只使用来自 OpenCv 和 PCL(点云库)的算法。我发现自己有这些类型:

  • OpenCv 的 Point3_
  • 用于 PCL 的 PointXYZ
  • Point3d 我的自定义类型

现在我有了为我的 Point3d 编写的算法,但我也想使用这些库中的算法。来回将大集合中的每个点从一种类型转换为另一种类型需要内存和时间。

对此进行某种抽象的最佳方法是什么?

4

1 回答 1

1

你可以做这样的事情

template<class T>
    struct Getter{

};

template<class T>
struct Getter<Point3_<T>>{
     typedef T& type;
     static type getx(Point3_<T>& p){
         return p.x;
     }
};
template<>
struct Getter<PointXYZ>{
     typedef float& type;
     static type getx(PointXYZ& p){
         return p.x;
     }
};

template <class T>
point_x(T& p) -> Getter<T>::type{
      return Getter<T>::getx(p);

}

对 y 和 z 执行相同操作然后修改您的算法以采用模板而不是使用 px = ... 使用

getx(p) = ..
auto x = getx(p)
于 2012-11-29T15:36:12.667 回答