Eigen: convert Matrix3d rotation to Quaternion

混江龙づ霸主 提交于 2020-07-03 05:46:25

问题


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

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?


回答1:


The constructors from an AngleAxis or Matrix are explicit meaning you have to write the conversion as follow:

Matrix3f mat;
Quaternionf q(mat);

or

Quaternionf q;
q = mat;

Same for AngleAxis.



来源:https://stackoverflow.com/questions/21761909/eigen-convert-matrix3d-rotation-to-quaternion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!