Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 572
JOYD Avatar asked Oct 18 '25 19:10

JOYD


2 Answers

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.

like image 118
chtz Avatar answered Oct 22 '25 03:10

chtz


As it has been suggested by previous answers and comments, unit quaternions can only represent 3D rotation matrices.

For a matrix to be a rotation matrix, it has to lie in SO(3), the special orthogonal group, which is defined by :

so3

So you need to check if the matrix times its transpose equals identity and if its determinant equals one (and not minus one, or else it only lies in the orthogonal group, and not in its sub-group, the special orthogonal).

At this time, the Eigen function used to create a quaternion from a matrix does not check if the passed matrix is indeed a rotation matrix. A fix or a warning may be needed because this can lead to unexpected behaviours, as you described.

I have found myself in the same position because I tried to form a quaternion from a change of basis matrix (formed by three basis vectors), and it only worked when the said basis was a direct one. If not direct, the transformation from this basis to the standard basis (and vice-versa) is not a rotation.

like image 36
Florian D Avatar answered Oct 22 '25 05:10

Florian D