This example reviews concepts in three-dimensional rotations and how quaternions are used to describe orientation and rotations. Quaternions are a skew field of hypercomplex numbers. They have found applications in aerospace, computer graphics, and virtual reality. In MATLAB®, quaternion mathematics can be represented by manipulating the
HelperDrawRotation class is used to illustrate several portions of this example.
dr = HelperDrawRotation;
All rotations in 3-D can be defined by an axis of rotation and an angle of rotation about that axis. Consider the 3-D image of a teapot in the leftmost plot. The teapot is rotated by 45 degrees around the Z-axis in the second plot. A more complex rotation of 15 degrees around the axis [1 0 1] is shown in the third plot. Quaternions encapsulate the axis and angle of rotation and have an algebra for manipulating these rotations. The
quaternion class, and this example, use the "right-hand rule" convention to define rotations. That is, positive rotations are clockwise around the axis of rotation when viewed from the origin.
The vertices of the teapot were rotated about the axis of rotation in the reference frame. Consider a point (0.7, 0.5) rotated 30 degrees about the Z-axis.
Frame rotation is, in some sense, the opposite of point rotation. In frame rotation, the points of the object stay fixed, but the frame of reference is rotated. Again, consider the point (0.7, 0.5). Now the reference frame is rotated by 30 degrees around the Z-axis. Note that while the point (0.7, 0.5) stays fixed, it has different coordinates in the new, rotated frame of reference.
Orientation refers to the angular displacement of an object relative to a frame of reference. Typically, orientation is described by the rotation that causes this angular displacement from a starting orientation. In this example, orientation is defined as the rotation that takes a quantity in a parent reference frame to a child reference frame. Orientation is usually given as a quaternion, rotation matrix, set of Euler angles, or rotation vector. It is useful to think about orientation as a frame rotation: the child reference frame is rotated relative to the parent frame.
Consider an example where the child reference frame is rotated 30 degrees around the vector [1/3 2/3 2/3].
figure; dr.draw3DOrientation(gca, [1/3 2/3 2/3], 30);
Quaternions are numbers of the form
and and are real numbers. In the rest of this example, the four numbers and are referred to as the parts of the quaternion.
The axis and the angle of rotation are encapsulated in the quaternion parts. For a unit vector axis of rotation [ x, y, z], and rotation angle , the quaternion describing this rotation is
Note that to describe a rotation using a quaternion, the quaternion must be a unit quaternion. A unit quaternion has a norm of 1, where the norm is defined as
There are a variety of ways to construct a quaternion in MATLAB, for example:
q1 = quaternion(1,2,3,4)
q1 = quaternion 1 + 2i + 3j + 4k
Arrays of quaternions can be made in the same way:
quaternion([1 10; -1 1], [2 20; -2 2], [3 30; -3 3], [4 40; -4 4])
ans = 2x2 quaternion array 1 + 2i + 3j + 4k 10 + 20i + 30j + 40k -1 - 2i - 3j - 4k 1 + 2i + 3j + 4k
Arrays with four columns can also be used to construct quaternions, with each column representing a quaternion part:
qmgk = quaternion(magic(4))
qmgk = 4x1 quaternion array 16 + 2i + 3j + 13k 5 + 11i + 10j + 8k 9 + 7i + 6j + 12k 4 + 14i + 15j + 1k
Quaternions can be indexed and manipulated just like any other array:
ans = quaternion 9 + 7i + 6j + 12k
ans = 2x2 quaternion array 16 + 2i + 3j + 13k 9 + 7i + 6j + 12k 5 + 11i + 10j + 8k 4 + 14i + 15j + 1k
ans = 2x1 quaternion array 1 + 2i + 3j + 4k 1 + 2i + 3j + 4k
Quaternions have well-defined arithmetic operations. Addition and subtraction are similar to complex numbers: parts are added/subtracted independently. Multiplication is more complicated because of the earlier equation:
This means that multiplication of quaternions is not commutative. That is, for quaternions and . However, every quaternion has a multiplicative inverse, so quaternions can be divided. Arrays of the
quaternion class can be added, subtracted, multiplied, and divided in MATLAB.
q = quaternion(1,2,3,4); p = quaternion(-5,6,-7,8);
p + q
ans = quaternion -4 + 8i - 4j + 12k
p - q
ans = quaternion -6 + 4i - 10j + 4k
ans = quaternion -28 - 56i - 30j + 20k
Multiplication in the reverse order (note the different result)
ans = quaternion -28 + 48i - 14j - 44k
Right division of
q is equivalent to .
ans = quaternion 0.6 + 2.2667i + 0.53333j - 0.13333k
Left division of
p is equivalent to .
ans = quaternion 0.10345 + 0.2069i + 0j - 0.34483k
The conjugate of a quaternion is formed by negating each of the non-real parts, similar to conjugation for a complex number:
ans = quaternion -5 - 6i + 7j - 8k
Quaternions can be normalized in MATLAB:
pnormed = normalize(p)
pnormed = quaternion -0.37905 + 0.45486i - 0.53067j + 0.60648k
ans = 1
Quaternions can be used to rotate points in a static frame of reference, or to rotate the frame of reference itself. The
rotatepoint function rotates a point using a quaternion q through the following equation:
and indicates quaternion conjugation. Note the above quaternion multiplication results in a quaternion with the real part, , equal to 0. The , , and parts of the result form the rotated point (, , ).
Consider the example of point rotation from above. The point (0.7, 0.5) was rotated 30 degrees around the Z-axis. In three dimensions this point has a 0 Z-coordinate. Using the axis-angle formulation, a quaternion can be constructed using [0 0 1] as the axis of rotation.
ang = deg2rad(30); q = quaternion(cos(ang/2), 0, 0, sin(ang/2)); pt = [0.7, 0.5, 0]; % Z-coordinate is 0 in the X-Y plane ptrot = rotatepoint(q, pt)
ptrot = 0.3562 0.7830 0
rotateframe function takes a quaternion and point to compute
Again the above quaternion multiplication results in a quaternion with 0 real part. The (, , ) parts of the result form the coordinate of the point in the new, rotated reference frame. Using the
ptframerot = rotateframe(q, pt)
ptframerot = 0.8562 0.0830 0
A quaternion and its conjugate have opposite effects because of the symmetry in the point and frame rotation equations. Rotating by the conjugate "undoes" the rotation.
ans = 0.7000 0.5000 0
Because of the symmetry of the equations, this code performs the same rotation.
ans = 0.7000 0.5000 0
Often rotations and orientations are described using alternate means: Euler angles, rotation matrices, and/or rotation vectors. All of these interoperate with quaternions in MATLAB.
Euler angles are frequently used because they are easy to interpret. Consider a frame of reference rotated by 30 degrees around the Z-axis, then 20 degrees around the Y-axis, and then -50 degrees around the X-axis. Note here, and throughout, the rotations around each axis are intrinsic: each subsequent rotation is around the newly created set of axes. In other words, the second rotation is around the "new" Y-axis created by the first rotation, not around the original Y-axis.
figure; euld = [30 20 -50]; dr.drawEulerRotation(gca, euld);
To build a quaternion from these Euler angles for the purpose of frame rotation, use the
quaternion constructor. Since the order of rotations is around the Z-axis first, then around the new Y-axis, and finally around the new X-axis, use the
qeul = quaternion(deg2rad(euld), 'euler', 'ZYX', 'frame')
qeul = quaternion 0.84313 - 0.44275i + 0.044296j + 0.30189k
'euler' flag indicates that the first argument is in radians. If the argument is in degrees, use the
qeuld = quaternion(euld, 'eulerd', 'ZYX', 'frame')
qeuld = quaternion 0.84313 - 0.44275i + 0.044296j + 0.30189k
To convert back to Euler angles:
rad2deg(euler(qeul, 'ZYX', 'frame'))
ans = 30.0000 20.0000 -50.0000
eulerd method can be used.
eulerd(qeul, 'ZYX', 'frame')
ans = 30.0000 20.0000 -50.0000
Alternatively, this same rotation can be represented as a rotation matrix:
rmat = rotmat(qeul, 'frame')
rmat = 0.8138 0.4698 -0.3420 -0.5483 0.4257 -0.7198 -0.1926 0.7733 0.6040
The conversion back to quaternions is similar:
quaternion(rmat, 'rotmat', 'frame')
ans = quaternion 0.84313 - 0.44275i + 0.044296j + 0.30189k
Just as a quaternion can be used for either point or frame rotation, it can be converted to a rotation matrix (or set of Euler angles) specifically for point or frame rotation. The rotation matrix for point rotation is the transpose of the matrix for frame rotation. To convert between rotation representations, it is necessary to specify
The rotation matrix for the point rotation section of this example is:
rotmatPoint = rotmat(q, 'point')
rotmatPoint = 0.8660 -0.5000 0 0.5000 0.8660 0 0 0 1.0000
To find the location of the rotated point, right-multiply
rotmatPoint by the transposed array
rotmatPoint * (pt')
ans = 0.3562 0.7830 0
The rotation matrix for the frame rotation section of this example is:
rotmatFrame = rotmat(q, 'frame')
rotmatFrame = 0.8660 0.5000 0 -0.5000 0.8660 0 0 0 1.0000
To find the location of the point in the rotated reference frame, right-multiply
rotmatFrame by the transposed array
rotmatFrame * (pt')
ans = 0.8562 0.0830 0
A rotation vector is an alternate, compact rotation encapsulation. A rotation vector is simply a three-element vector that represents the unit length axis of rotation scaled-up by the angle of rotation in radians. There is no frame-ness or point-ness associated with a rotation vector. To convert to a rotation vector:
rv = rotvec(qeul)
rv = -0.9349 0.0935 0.6375
To convert to a quaternion:
ans = quaternion 0.84313 - 0.44275i + 0.044296j + 0.30189k
One advantage of quaternions over Euler angles is the lack of discontinuities. Euler angles have discontinuities that vary depending on the convention being used. The
dist function compares the effect of rotation by two different quaternions. The result is a number in the range of 0 to
pi. Consider two quaternions constructed from Euler angles:
eul1 = [0, 10, 0]; eul2 = [0, 15, 0]; qdist1 = quaternion(deg2rad(eul1), 'euler', 'ZYX', 'frame'); qdist2 = quaternion(deg2rad(eul2), 'euler', 'ZYX', 'frame');
Subtracting the Euler angles, you can see there is no rotation around the Z-axis or X-axis.
eul2 - eul1
ans = 0 5 0
The difference between these two rotations is five degrees around the Y-axis. The
dist shows the difference as well.
ans = 5.0000
For Euler angles such as
eul2, computing angular distance is trivial. A more complex example, which spans an Euler angle discontinuity, is:
eul3 = [0, 89, 0]; eul4 = [180, 89, 180]; qdist3 = quaternion(deg2rad(eul3), 'euler', 'ZYX', 'frame'); qdist4 = quaternion(deg2rad(eul4), 'euler', 'ZYX', 'frame');
eul4 represent nearly the same orientation, simple Euler angle subtraction gives the impression that these two orientations are very far apart.
euldiff = eul4 - eul3
euldiff = 180 0 180
dist function on the quaternions shows that there is only a two-degree difference in these rotations:
euldist = rad2deg(dist(qdist3, qdist4))
euldist = 2.0000
A quaternion and its negative represent the same rotation. This is not obvious from subtracting quaternions, but the
dist function makes it clear.
qpos = quaternion(-cos(pi/4), 0 ,0, sin(pi/4))
qpos = quaternion -0.70711 + 0i + 0j + 0.70711k
qneg = -qpos
qneg = quaternion 0.70711 + 0i + 0j - 0.70711k
qdiff = qpos - qneg
qdiff = quaternion -1.4142 + 0i + 0j + 1.4142k
ans = 0
quaternion class lets you effectively describe rotations and orientations in MATLAB. The full list of quaternion-supported functions can be found with the
Methods for class quaternion: angvel ismatrix prod cat isnan quaternion classUnderlying isrow rdivide compact isscalar reshape conj isvector rotateframe ctranspose ldivide rotatepoint disp length rotmat dist log rotvec double meanrot rotvecd eq minus single euler mtimes size eulerd ndims slerp exp ne times horzcat norm transpose iscolumn normalize uminus isempty numel validateattributes isequal parts vertcat isequaln permute isfinite plus isinf power Static methods: ones zeros