Вопрос

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.

Here is the function definition 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;
}

Here is the function definition 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;
}

Any ideas?

Это было полезно?

Решение

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top