简体   繁体   English

特征::矩阵<double,4,4>到 Eigen::Quaterniond

[英]Eigen::Matrix<double,4,4> to Eigen::Quaterniond

Eigen::Matrix4d transformation_matrix; //assume this is initialized
Eigen::Matrix4d &matrix = transformation_matrix;   
Eigen::Quaterniond quat;
quat(matrix);

I am trying to find a rotation matrix from a transformation matrix.我试图从变换矩阵中找到一个旋转矩阵。 There is already an API in Eigen::Quaterniond library toRotationMatrix .Eigen::QuaterniondtoRotationMatrix已经有一个 API。 But to use this, I need a 4x4 Quaterniond (Please correct, if this is wrong).但是要使用它,我需要一个 4x4 Quaterniond (如果这是错误的,请更正)。

Getting compilation error as获取编译错误为

error: no match for call to ‘(Eigen::Quaterniond {aka Eigen::Quaternion<double>}) (const Matrix4d&)’

Please help.请帮忙。 Followed this link for conversion 按照此链接进行转换

Eigen::Quaterniond quat;
quat(matrix);    

When initializing Quaterniond , I am getting a different error :初始化Quaterniond时,出现不同的错误:

Eigen::internal::quaternionbase_assign_impl<Eigen::Matrix<double, 4, 4>, 4, 4>’ used in nested name specifier
   internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived());

What you are doing here:你在这里做什么:

Eigen::Quaterniond quat;
quat(matrix);

is creating a quaternion and then calling it's operator() with a matrix.正在创建一个四元数,然后用一个矩阵调用它的operator() But as far as I can see, the Quaternion does not even have an operator() for anything: documentation但据我所知,四元数甚至没有任何operator()文档

Take a look at the first answer of ggael in the question you referred to .看看你提到问题中ggael的第一个答案。 You can either use the constructor of the quaternion:您可以使用四元数的构造函数:

Eigen::Quaterniond quat(matrix);

or it's assignment operator:或者它是赋值运算符:

Eigen::Quaterniond quat;
quat = matrix;

Both are defined for a matrix.两者都是为矩阵定义的。 However, I can't tell you, if they work for 4x4 matrices.但是,我不能告诉您,它们是否适用于 4x4 矩阵。 If not extract the 3x3 rotation part of the matrix.如果不提取矩阵的 3x3 旋转部分。

Edit:编辑:

After a quick test on my computer, you can't pass a 4x4 matrix to a quaternion.在我的计算机上进行快速测试后,您无法将 4x4 矩阵传递给四元数。 Either use a Eigen::Matrix3d instead or do something like this:要么使用Eigen::Matrix3d代替,要么执行以下操作:

Eigen::Quaterniond quat(matrix.topLeftCorner<3, 3>());

Edit 2:编辑2:

I am trying to find a rotation matrix from a transformation matrix.我试图从变换矩阵中找到一个旋转矩阵。 There is already an API in Eigen::Quaterniond library toRotationMatrix. Eigen::Quaterniond 库中已经有一个 API toRotationMatrix。 But to use this, I need a 4x4 Quaterniond (Please correct, if this is wrong).但是要使用它,我需要一个 4x4 Quaterniond(如果这是错误的,请更正)。

Well, this isn't really correct.嗯,这真的不正确。 A rotation matrix can be converted into a quaternion and a quaternion into a rotation matrix.旋转矩阵可以转换为四元数,四元数可以转换为旋转矩阵。 In a 4x4 matrix, the rotation part is contained inside the top-left 3x3 submatrix.在 4x4 矩阵中,旋转部分包含在左上角的 3x3 子矩阵内。 However, in a general transformation matrix, this part isn't necessarily only the rotation.但是,在一般的变换矩阵中,这部分不一定只是旋转。 It can also contain other transformations like scaling.它还可以包含其他转换,如缩放。 In this case, I am not sure, if you can transform it directly into a quaternion without extracting the rotational part first.在这种情况下,我不确定是否可以将其直接转换为四元数而无需先提取旋转部分。 I doubt it, but I am not sure about this.我对此表示怀疑,但我不确定这一点。 However, if you have a general transformation matrix, you can test if the upper 3x3 part is a pure rotation by calculating its determinant.然而,如果你有一个通用的变换矩阵,你可以通过计算它的行​​列式来测试上面的 3x3 部分是否是纯旋转。 If it is 1, you have a pure rotation.如果它是 1,你有一个纯旋转。 If this is not the case, take a look at this link to calculate the rotational part.如果不是这种情况,请查看此链接以计算旋转部分。 If your goal is to extract just the rotation of a 4x4 matrix, you do not need quaternions at all.如果您的目标只是提取 4x4 矩阵的旋转,则根本不需要四元数。 Rotation matrices and quaternions are just different representations of the same thing.旋转矩阵和四元数只是同一事物的不同表示。

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

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