rotmat_to_quaternion

giant.rotations:

giant.rotations.rotmat_to_quaternion(rotation_matrix)[source]

This function converts a rotation matrix into a rotation quaternion of the form discussed in Rotation Representations.

The quaternion is returned as a numpy array and is formed by:

\[\begin{split}q_s = \frac{1}{2}\sqrt{(\text{Tr}(\mathbf{T})+1)}\\ \mathbf{q}_v = \frac{1}{2}\left[\begin{array}{c}\text{copysign}(\sqrt{1+t_{11}-t_{22}-t_{33}}, t_{32}-t_{23})\\ \text{copysign}(\sqrt{1-t_{11}+t_{22}-t_{33}}, t_{13}-t_{31})\\ \text{copysign}(\sqrt{1-t_{11}-t_{22}+t_{33}}, t_{21}-t_{12})\end{array}\right]\end{split}\]

where \(\text{Tr}(\bullet)\) is the trace operator, \(\mathbf{T}\) is the rotation matrix to be converted, \(t_{ij}\) is the \(i, j\) element of \(\mathbf{T}\), and \(\text{copysign}(a, b)\) overwrites the sign of \(a\) with the sign of \(b\).

This function is also vectorized, meaning that you can specify multiple rotation matrices to be converted to quaternions by specifying each matrix along the first axis. Regardless of whether you are converting 1 or many matrices the last two axes must have a length of 3.

This function makes the output have the same number of dimensions as the input. Therefore, if the input is two dimensional, then the output is one dimensional, and if the input is three dimensional then the output will be two dimensional.

Parameters:

rotation_matrix (Sequence[Sequence] | ndarray) – The rotation matrix to convert to a rotation quaternion

Returns:

the rotation quaternion(s) corresponding to the input rotation vector(s)

Return type:

ndarray