# SO(3): 3D Rotations¶

The group of all rotations in the 3D Cartesian space is called (SO: special orthogonal group). It is typically represented by 3D rotations matrices. The minimum number of components that are required to describe any rotation from is 3. However, there is no representation that is non-redundant, continuous, and free of singularities. We will now take a closer look at competing representations of rotations and the orientations they can describe.

Here is an overview of the representations and the conversions between them that are available in pytransform3d.

## Rotation Matrix¶

The most practical representation of orientation is a rotation matrix

Note that

this is a non-minimal representation for orientations because we have 9 values but only 3 degrees of freedom

must be orthonormal

pytransform3d uses a numpy array of shape (3, 3) to represent rotation matrices and typically we use the variable name R for a rotation matrix.

Warning

There are two conventions on how to interpret rotations: active
or passive rotation. The standard in pytransform3d is an active rotation.
Note that there are some functions to generate rotation matrices that
generate passive rotations as well: `matrix_from_angle()`

,
`matrix_from_euler_xyz()`

, and
`matrix_from_euler_zyx()`

. These are kept
for backward compatibility. When in doubt, read the docstring, which
clearly states that a passive convention is used here.

We can use a rotation matrix to transform a point from frame to frame .

Warning

There are two different conventions on how to use rotation matrices to
apply a rotation to a vector. We can either (pre-)multiply the rotation
matrix to a column vector from the left side or we can (post-)multiply it
to a row vector from the right side.
We will use the **pre-multiplication** convention.

This means that we rotate a point by

This is called **linear map**.

We can see that *each column* of such a rotation matrix is a basis vector
of frame with respect to frame .

We can plot the basis vectors of an orientation to visualize it.

Note

When plotting basis vectors it is a convention to use red for the x-axis, green for the y-axis and blue for the z-axis (RGB for xyz).

Here, we can see orientation represented by the rotation matrix

```
from pytransform3d.rotations import plot_basis
plot_basis()
```

(Source code, png, hires.png, pdf)

We can easily chain multiple rotations: we can apply the rotation defined by after the rotation by applying the rotation

Warning

There are two different conventions on how to concatenate rotation matrices. Suppose we have a rotation matrix and another matrix and we want to first rotate by and then by . If we want to apply both rotations in global coordinates, we have to concatenate them with . We can also express the second rotation in terms of a local, body-fixed coordinates by , which means defines new coordinates in which is applied. Note that this applies to both passive and active rotation matrices.

**Pros**

It is easy to apply rotations on point vectors by matrix-vector multiplication

Concatenation of rotations is trivial through matrix multiplication

You can directly read the basis vectors from the columns

No singularities

**Cons**

We use 9 values for 3 degrees of freedom.

Not every 3x3 matrix is a valid rotation matrix, which means for example that we cannot simply apply an optimization algorithm to rotation matrices or interpolate between rotation matrices. Renormalization is computationally expensive in comparison to quaternions.

## Axis-Angle¶

(Source code, png, hires.png, pdf)

Each rotation can be represented by a single rotation around one axis. The axis can be represented as a three-dimensional unit vector and the angle by a scalar:

pytransform3d uses a numpy array of shape (4,) for the axis-angle representation of a rotation, where the first 3 entries correspond to the unit axis of rotation and the fourth entry to the rotation angle in radians, and typically we use the variable name a.

It is possible to write this in a more compact way as a rotation vector:

pytransform3d uses a numpy array of shape (3,) for the compact axis-angle representation of a rotation and typically we use the variable name a.

We can also refer to this representation as **exponential coordinates of
rotation**. We can easily represent angular velocity as
and angular acceleration as
so that we can easily do
component-wise integration and differentiation with this representation.
In addition, we can represent by
the cross-product matrix

where is the matrix logarithm of a rotation matrix and is the Lie algebra of the Lie group .

**Pros**

Minimal representation (as rotation vector, also referred to as compact axis-angle in the code)

It is easy to interpret the representation (as axis and angle)

Can also represent angular velocity and acceleration when we replace by or respectively, which makes numerical integration and differentiation easy.

**Cons**

There might be discontinuities during interpolation as an angle of 0 and any multiple of represent the same orientation. This has to be considered.

Concatenation involves conversion to another representation

## Quaternions¶

Quaternions are represented by a scalar / real part and an vector / imaginary part .

Warning

There are two different quaternion conventions: Hamilton’s convention defines and the Shuster or JPL convention (from NASA’s Jet Propulsion Laboratory, JPL) defines . These two conventions result in different multiplication operations and conversions to other representations. We use Hamilton’s convention.

Read this paper for details about the two conventions and why Hamilton’s convention should be used. Section VI A gives further useful hints to identify which convention is used.

The unit quaternion space can be used to represent orientations. To do that, we use an encoding based on the rotation axis and angle. A rotation quaternion is a four-dimensional unit vector (versor) . The following equation describes its relation to axis-axis notation.

pytransform3d uses a numpy array of shape (4,) for quaternions and typically we use the variable name q.

Warning

The scalar component of a quaternion is sometimes the first element and sometimes the last element of the versor. We will use the first element to store the scalar component.

Warning

The unit quaternions and represent exactly the same rotation.

**Pros**

More compact than the matrix representation and less susceptible to round-off errors

The quaternion elements vary continuously over the unit sphere in as the orientation changes, avoiding discontinuous jumps (inherent to three-dimensional parameterizations)

Expression of the rotation matrix in terms of quaternion parameters involves no trigonometric functions

Concatenation is simple and computationally cheaper with the quaternion product than with rotation matrices

No singularities

Renormalization is cheap in comparison to rotation matrices: we only have to divide by the norm of the quaternion.

**Cons**

The representation is not straightforward to interpret

There are always two unit quaternions that represent exactly the same rotation

## Euler Angles¶

A complete rotation can be split into three rotations around basis vectors. pytransform3d uses a numpy array of shape (3,) for Euler angles, where each entry corresponds to a rotation angle in radians around one basis vector. The basis vector that will be used and the order of rotation is defined by the convention that we use. See Euler Angles for more information.

Warning

There are 24 different conventions for defining euler angles. There are 12 different valid ways to sequence rotation axes that can be interpreted as extrinsic or intrinsic rotations: XZX, XYX, YXY, YZY, ZYZ, ZXZ, XZY, XYZ, YXZ, YZX, ZYX, and ZXY.

**Pros**

Minimal representation

**Cons**

24 different conventions

Singularities (gimbal lock)

Concatenation and transformation of vectors requires conversion to rotation matrix or quaternion

## References¶

Why and How to Avoid the Flipped Quaternion Multiplication: https://arxiv.org/pdf/1801.07478.pdf

Kindr cheat sheet: https://docs.leggedrobotics.com/kindr/cheatsheet_latest.pdf