Rotate points around the surface of a sphere, and interpolate between them.
Quaternions are defined by a scalar (real) part, and three imaginary parts collectively called the vector part. Quaternions are often used in graphics programming as a compact representation of the rotation of an object in three dimensions.
The length of a quaternion is the square root of the sum of the squares of its components. For example, consider a quaternion specified by the following code:
The length of the quaternion can be calculated manually with
sqrt(ix*ix + iy*iy + iz*iz + r*r), or more simply accessed through its
length property. Quaternions with a length of one are called unit quaternions and can represent rotations in 3D space. You can easily convert a nonunit quaternion representing a rotation into a unit quaternion by normalizing its axes. The following code shows
q1, which contains rotations around all three axes with a length greater than 1, and
q2, which contains the same rotation but has a length of 1 and is, therefore, suitable for applying a rotation to a 3D coordinate:
Quaternions have some advantages over matrices. For example, they're smaller: A 3 x 3 matrix of floats is 48 bytes, and a single-precision quaternion is 16 bytes. They also can offer better performance: Although a single rotation using a quaternion is a little slower than one using a matrix, when combining actions, quaternions can be up to 30% faster.
The following examples show a few common uses of quaternions.
Rotate a Point Around a Sphere
The following illustration shows a point, defined by
origin, rotated over the surface of a sphere by 60º about the x axis.
To apply this rotation, you define the vector to be rotated and the quaternion that represents the rotation:
The rotation of the vector by a quaternion is known as an action; to apply the rotation to
origin, you call the
rotated is now at the position of the blue point in the illustration above.
Interpolate Between Two Points on a Sphere
One of the advantages quaternions have over matrices when representing rotations is that they simplify interpolation between rotated coordinates.
The following image shows the spherical linear interpolation along the shortest arc between
q1, and along the longest arc between
The following code shows how the three points are defined in the preceding image:
simd function linearly interpolates along the shortest arc between two quaternions. The following code calls
simd with small increments to its
t parameter, adding a line segment at each interpolated value to build the short arc between
q1 shown in the preceding image:
simd function linearly interpolates along the longest arc between two quaternions. The following code calls
simd with small increments to its
t parameter, adding a line segment at each interpolated value to build the long arc between
q2 shown in the preceding image:
Interpolate Between Multiple Points on a Sphere
To interpolate between multiple quaternions that define positions on the surface of a sphere, the simd library provides the
The following image illustrates a series of quaternions that define positions on the surface of a sphere, shown as points, and a line that shows the smooth interpolation between them.
simd accepts the two quaternions to interpolate between, but also requires the surrounding two quaternions. Given an array of quaternions named
rotations, the following code iterates over each element, adding a line segment at each interpolated value to build the smooth line shown in the preceding image: