# Quaternions

Eric Bainville - Mar 2007

## Quaternion from rotation matrix

We have seen in the previous chapter how to obtain the 3×3 rotation matrix corresponding to unit quaternion q. We see here how to obtain q=(s,ux,uy,uz), one of the two opposite unit quaternions corresponding to a given rotation matrix M:

 Mxx Mxy Mxz Myx Myy Myz Mzx Mzy Mzz

We first compute the four values:

• 4.s2 = 1+Mxx+Myy+Mzz,
• 4.ux2 = 1+Mxx-Myy-Mzz,
• 4.uy2 = 1-Mxx+Myy-Mzz,
• 4.uz2 = 1-Mxx-Myy+Mzz.

Comparing these values (all between 0 and 4), we can identify the largest component of q in magnitude. Then we pick the three products involving this component in the following list to build a vector proportional to q. q is then obtained by normalizing this vector.

• 4.s.ux = Mzy - Myz,
• 4.s.uy = Mxz - Mzx,
• 4.s.uz = Myx - Mxy,
• 4.ux.uy = Mxy + Myx,
• 4.uy.uz = Myz + Mzy,
• 4.uz.ux = Mzx + Mxz.

For example, suppose the largest component is ux, we should normalize the vector (4.s.ux,4.ux2,4.ux.uy,4.ux.uz). This guarantees the best stability in all cases, and the unit norm of the result.