Search code examples
matrixeigenquaternions

Eigen: matrix to quaternion and back have different result


I use Eigen library to covert matrix to quaternion, but when I turn one of the matrix to quaternion and burn it back, it turn out to be another matrix which is identity matrix. The rotation matrix I use was decomposed from a transform matrix.

    Eigen::Matrix3f R3d = R.topLeftCorner<3,3>();
    *Rquat = R3d;

    R3d = (*Rquat).normalized().toRotationMatrix();

What may cause this problem? This is the matrix before change to quaternion

and This is the matrix when I turn it back form the quaternion


Solution

  • Just checked the implementation of Eigen's matrix to quaternion conversion. It is based on "Quaternion Calculus and Fast Animation", by Ken Shoemake.

    And as one can see when analyzing the source, this assumes that the matrix is indeed a rotation matrix (or close to one). In fact all symmetric matrices with M.trace()>0 will result in a (scaled) identity quaternion. If you expect anything else for invalid rotation matrices, you need to implement your own conversion method.