固定大小的特征矩阵作为模板函数中的参数 [英] Fixed size Eigen matrix as parameters in template function

查看:147
本文介绍了固定大小的特征矩阵作为模板函数中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使用矩阵大小上的模板的固定大小矩阵的函数.我已阅读 http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html 但我无法使其完美运行.我无法在函数内部的固定大小矩阵上使用固定大小矩阵块操作. (TutorialBlockOperations.html> http://eigen.tuxfamily.org/dox/group_TutorialBlockOperations.html)

I am trying to write a function that takes fixed size matrix using template on the matrix size. I have read http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html but I am not able to make it works perfectly. I could not use fixed size matrix block operations on the fixed size matrix inside my function. (TutorialBlockOperations.html">http://eigen.tuxfamily.org/dox/group_TutorialBlockOperations.html)

我试图通过两种方式来做到这一点,但是两者都没有用.

I tried to do it in two ways but both of them did not work.

这是函数定义A:

template <int N>
Matrix<double, 3, N> foo(const Matrix<double, 3, N>& v)
{
  Matrix<double, 3, N> ret;

  Vector3d a = v.leftCols<1>();  // error: expected primary-expression before ')' token

  return ret;
}

这是函数定义B:

template<typename Derived>
Eigen::MatrixBase<Derived> bar(const Eigen::MatrixBase<Derived>& v)
{
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived);
  EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime == 3,
                  THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE);

  Eigen::Matrix<double,
      Derived::RowsAtCompileTime,
      Derived::ColsAtCompileTime> ret;

  Vector3d a = v.leftCols<1>();  // error: expected primary-expression before ')' token

  return ret;
}

有什么想法吗?

推荐答案

版本B中的参数正确,但b!t不是应为Derived::PlainObject的返回类型.您还需要template disambiguate关键字来访问模板代码中的模板成员:

The argument in version B is correct, b!t not the return type which should be Derived::PlainObject. You also need the template disambiguate keyword to access template member within templated code:

template<typename Derived>
typename Derived::PlainObject bar(const Eigen::MatrixBase<Derived>& v)
{
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived);
  EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime == 3,
                      THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE);

  typename Derived::PlainObject ret;

  Vector3d a = v.template leftCols<1>();

  return ret;
}

这篇关于固定大小的特征矩阵作为模板函数中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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