c++ - Best practices for functions with multiple Eigen objects as parameters -
i find design of functions eigen objects parameters cumbersome. while information in eigen documentation helpful, suggests awkward approach template arguments. suppose, want write geometric routine line-plane-intersection. simple , transparent approach 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)
this looks relatively pleasing , looks @ can learn every parameter shall 3d vector of same type. however, directly, not allow eigen expressions of kind (we have call eigen::matrix constructors every expression, use). if expressions used this, need create unnecessary temporaries.
the suggested solution is:
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)
this not reveal matrices expected, nor parameters used input , output, have const-cast intersectionpoint allow expressions in output parameters. understand it, way allow eigen expressions in function parameters. despite unelegant expression support, first snippet still seems more likable me.
my questions:
- would consider second code snippet best solution example?
- do ever use const-cast solution output parameters or think not worth loss in transparency?
- what guidelines/best practices use eigen function writing?
for such small fixed size objects, i'd not bother , go first solution.
it's approach have output function parameters. in particular case, 1 approach create planelineintersection class ctor take plane , line, stores result of intersection , provides accessors query result of computation (no intersection, point, line).
btw, have noticed hyperplane , parametrizedline class of eigen/geometry module? parametrizedline class has intersectionpoint member hyperplane (though it's limited because assumes intersection exist , point).
Comments
Post a Comment