C++ 本征:将 Matrix3d 旋转转换为四元数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21761909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:48:33  来源:igfitidea点击:

Eigen: convert Matrix3d rotation to Quaternion

c++matrixeigenquaternions

提问by Esparver

I'm trying to convert a Matrix3drotation to a Quaternion<double>, but I got only weird compiler errors so far. The code I'm using is:

我正在尝试将Matrix3d旋转转换为Quaternion<double>,但到目前为止我只遇到了奇怪的编译器错误。我正在使用的代码是:

Quaternion<double> getQuaternionFromRotationMatrix(const Matrix3d& mat)
{
    AngleAxisd aa; 
    aa = mat;
    Quaternion<double> q = aa;// conversion error

    return q;
}

And the compiler errors:

和编译器错误:

path/src/Utils.cpp: In function ‘Eigen::Quaternion<double> Utils::getQuaternionFromRotationMatrix(const Matrix3d&)':
path/src/Utils.cpp:55:26: error: conversion from ‘Eigen::AngleAxisd {aka Eigen::AngleAxis<double>}' to non-scalar type ‘Eigen::Quaternion<double>' requested
In file included from /usr/local/include/eigen3/Eigen/Core:283:0,
                 from /usr/local/include/eigen3/Eigen/Dense:1,
                 from path/include/Utils.h:4,
                 from path/src/Utils.cpp:1:
/usr/local/include/eigen3/Eigen/src/Core/Assign.h: In member function ‘Derived& Eigen::DenseBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::Matrix<double, 3, 1>, Derived = Eigen::Block<Eigen::Matrix<double, 4, 4>, 4, -0x00000000000000001, true, true>]':
/usr/local/include/eigen3/Eigen/src/Core/Assign.h:534:123:   instantiated from ‘static Derived& Eigen::internal::assign_selector<Derived, OtherDerived, false, false>::run(Derived&, const OtherDerived&) [with Derived = Eigen::Block<Eigen::Matrix<double, 4, 4>, 4, -0x00000000000000001, true, true>, OtherDerived = Eigen::Matrix<double, 3, 1>]'
/usr/local/include/eigen3/Eigen/src/Core/Assign.h:574:89:   instantiated from ‘Derived& Eigen::MatrixBase<Derived>::operator=(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::Matrix<double, 3, 1>, Derived = Eigen::Block<Eigen::Matrix<double, 4, 4>, 4, -0x00000000000000001, true, true>]'
path/src/Utils.cpp:34:20:   instantiated from here
/usr/local/include/eigen3/Eigen/src/Core/Assign.h:504:3: error: static assertion failed: "YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES"

Does someone know how to convert between both representations?

有人知道如何在两种表示之间进行转换吗?

回答by ggael

The constructors from an AngleAxisor Matrixare explicitmeaning you have to write the conversion as follow:

来自AngleAxisor的构造函数Matrix明确的意思,您必须按如下方式编写转换:

Matrix3f mat;
Quaternionf q(mat);

or

或者

Quaternionf q;
q = mat;

Same for AngleAxis.

对于AngleAxis.