#include <gandalf/vision/rotate3D.h>Gandalf has an extensive set of routines for handling 3D rotations. Four different representations are available, defined by the following enumerated type:

/* types of rotation handled by Gandalf */ typedef enum { GAN_ROT3D_QUATERNION, GAN_ROT3D_EXPONENTIAL, GAN_ROT3D_ANGLE_AXIS, GAN_ROT3D_MATRIX } Gan_Rot3D_Type;These representations are now described in turn.

- The
**quaternion**representation uses the following structure:/* quaternion structure */ typedef struct Gan_Quaternion { double q0, q1, q2, q3; } Gan_Quaternion;

The relationship between a quaternion and the equivalent rotation matrix is

Here the quaternion is assumed to have been scaled to unit length, i.e. . - The
**exponential**representation uses a 3-vector related to the equivalent rotation matrix according to

where is the rotation angle, and the cross-product matrix is explained in Section 5.2. - The
**angle/axis**representation is strongly related to a quaternion, according to the formula

where the rotation axis is assumed to be scaled to . The rotation angle is measured in radians. - The
**matrix**representation uses a matrix as above to represent a rotation

is equivalent to the rotation matrix product

You should always use quaternions for combining rotations in this way, because if you use matrices the rounding error will accumulate over repeated matrix multiplications, and cause the matrix to become non-orthogonal. With quaternions the scale will drift slightly, but it is much easier to fix the scale of a quaternion than to correct the matrix representation. The angle/axis form is mainly useful as an intuitive way of defining rotations. It has the problem of having no unique representation of zero rotations, since in this case the axis is arbitrary. The exponential representation is unique in being a

First we describe some routines specific to the quaternion representation.
Then we provide details of how the Gandalf structure `Gan_Rot3D`
is used to create and manipulate rotations of any of the currently
supported representations.