简体   繁体   English

固定大小的特征值类型作为参数

[英]Fixed Sized Eigen types as parameters

I am trying to write a function that takes fixed size Eigen Types (but templated on Scalar type eg float/double). 我正在尝试编写一个函数,该函数采用固定大小的特征类型(但以标量类型为模板,例如float / double)。 I have read http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html but I am not able to make it work perfectly. 我已经阅读了http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html,但是我无法使其完美运行。

Here is the function definition: 这是函数定义:

template <typename T>
inline Matrix<T, 3, 3> makeSkewSymmetric(const Matrix<T, 3, 1>& v)
{
  Matrix<T, 3, 3> out;
  out <<     0, -v[2],  v[1],
          v[2],     0, -v[0],
         -v[1],  v[0],     0;

  return out;
}

Now I am using this as following: 现在,我正在按以下方式使用它:

Vector3d a(1,2,3);
Matrix3d ass = makeSkewSymmetric(a); // Compiles
Matrix3d ass = makeSkewSymmetric(a + a); // does NOT compile

I guess, I need to use some sort of MatrixBase<Derived> , but then how do I restrict the size, as the function only makes sense for vectors of length 3. 我猜想,我需要使用某种MatrixBase<Derived> ,但是我该如何限制大小,因为该函数仅对长度为3的向量有意义。

Edit: I redefined the function as following. 编辑:我重新定义了如下功能。 It works, but is there a better way? 它有效,但是有更好的方法吗?

template <typename Derived>
inline Matrix<typename Derived::Scalar, 3, 3> makeSkewSymmetric(const MatrixBase<Derived>& v)
{
    BOOST_STATIC_ASSERT(Derived::RowsAtCompileTime == 3 && Derived::ColsAtCompileTime == 1);
  Matrix<typename Derived::Scalar, 3, 3> out;
  out <<     0, -v[2],  v[1],
          v[2],     0, -v[0],
         -v[1],  v[0],     0;

  return out;
}

I just thought of a good way of checking the way the Eigen developers would want you to solve this problem. 我只是想到了一种检查Eigen开发人员希望您解决此问题的方式的好方法。 Eigen comes with a cross function on MatrixBase , but this function, like yours, is only sensible for 3D vectors - so I dug up the relevant part from the Eigen3 source: (cf Eigen/src/Geometry/OrthoMethods.h ) Eigen在MatrixBase上带有cross函数,但是像您一样,此函数仅对3D向量有意义-因此,我从Eigen3源中挖掘了相关部分:(cf Eigen/src/Geometry/OrthoMethods.h

...
inline typename MatrixBase<Derived>::template cross_product_return_type<OtherDerived>::type
MatrixBase<Derived>::cross(const MatrixBase<OtherDerived>& other) const
{
  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,3)
  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,3)
  ...

and indeed, Eigen itself uses asserts (albeit its own flavor) to check for dimensions in generalized functions. 实际上,Eigen本身使用断言(尽管有其自身的风格)来检查广义函数中的维度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM