Eigen: matrix to quaternion and back have different result

三世轮回 提交于 2021-02-08 04:10:52

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/43896041/eigen-matrix-to-quaternion-and-back-have-different-result

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