以多个特征对象为参数的函数的最佳实践 [英] Best practices for functions with multiple Eigen objects as parameters

查看:165
本文介绍了以多个特征对象为参数的函数的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以Eigen对象作为参数的函数设计很麻烦.尽管本征文档中的信息很有帮助,但它建议您使用尴尬的模板参数方法.假设我们要编写一个几何例程,例如线平面相交.一种简单透明的方法是:

I find the design of functions with Eigen objects as parameters cumbersome. While the information in the Eigen Documentation is helpful, it suggests an awkward approach for the template arguments. Suppose, we want to write a geometric routine like a line-plane-intersection. A simple and transparent approach would be:

template<typename _Tp> 
bool planeLineIntersect(const Eigen::Matrix<_Tp, 3, 1>& planePoint, 
                        const Eigen::Matrix<_Tp, 3, 1>& planeNormal, 
                        const Eigen::Matrix<_Tp, 3, 1>& linePoint, 
                        const Eigen::Matrix<_Tp, 3, 1>& lineDir,
                        Eigen::Matrix<_Tp, 3, 1>& intersectionPoint)

这看起来比较令人愉快,并且研究此方法的人可以了解到,每个参数都应该是相同类型的3D向量.但是,直接地,这不允许任何形式的Eigen表达式(我们必须为我们使用的每个表达式调用Eigen :: Matrix构造函数).因此,如果将表达式与此配合使用,我们需要创建不必要的临时对象.

This looks relatively pleasing and someone that looks at this can learn that every parameter shall be a 3D vector of the same type. However, directly, this does not allow for Eigen expressions of any kind (we would have to call Eigen::Matrix constructors for every expression, that we use). So if expressions are used with this, we need to create unnecessary temporaries.

建议的解决方案是:

template<typename Derived1, typename Derived2, typename Derived3, typename Derived4, typename Derived5> 
bool planeLineIntersect(const Eigen::MatrixBase<Derived1>& planePoint, 
                        const Eigen::MatrixBase<Derived2>& planeNormal, 
                        const Eigen::MatrixBase<Derived3>& linePoint, 
                        const Eigen::MatrixBase<Derived4>& lineDir,
                        const Eigen::MatrixBase<Derived5>& intersectionPoint)

这没有显示有关期望矩阵的任何内容,也没有显示用于输入和输出的参数,因为我们必须constcast交叉点以允许在输出参数中使用表达式.据我了解,这是在所有函数参数中允许本征表达式的唯一方法.尽管表达的表达毫不含糊,但第一个摘录对我来说还是更讨人喜欢.

This does not reveal anything about the matrices that are expected, nor which parameters are used for input and output, as we have to const-cast the intersectionPoint to allow for expressions in output parameters. As I understand it, this is the only way to allow for Eigen expressions in all function parameters. Despite the unelegant expression support, the first snippet still seems more likable to me.

我的问题:

  1. 您认为第二个代码段是此示例的最佳解决方案吗?
  2. 您是否曾经将const-cast解决方案用于输出参数,或者您认为不值得在透明度方面有所损失?
  3. 您如何使用本征函数编写哪些准则/最佳实践?

推荐答案

对于如此小的固定大小的对象,我不会花太多时间去寻求第一个解决方案.

For such small fixed size objects, I'd not bother much and go with the first solution.

拥有输出函数参数很少是一个好方法.在您的特殊情况下,一种方法是创建一个PlaneLineIntersection类,该类的ctor会获取一个平面和一条线,存储相交的结果,然后提供访问器来查询计算结果(没有相交,是一个点,一行).

It's rarely a good approach to have output function parameters. In you particular case, one approach would be to create a PlaneLineIntersection class whose ctor would take a plane and a line, stores the result of the intersection and then provides accessors to query the result of the computation (no intersection, is it a point, a line).

顺便说一句,您是否注意到本征/几何模块的HyperPlane和ParametrizedLine类? ParametrizedLine类具有一个HyperPlane的交点成员(尽管它受到限制,因为它假定交点确实存在并且是一个点).

BTW, have you noticed the HyperPlane and ParametrizedLine class of the Eigen/Geometry module? The ParametrizedLine class has an intersectionPoint member with an HyperPlane (though it's limited because it assumes the intersection does exist and it is a point).

这篇关于以多个特征对象为参数的函数的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆