Quaternions 2 AUI Course Denbigh Starkey 1. Background 2 2. Some Basic Quaternion Math 4 3. The Justification of the Quaternion Rotation Formula 5 4. Interpolation between two Unit Quaternions SLERP vs. LERP 9
1. Background This is a continuation of my previous set of notes, where I defined quaternions and showed how to use them to do rotations around an arbitrary axis through the origin. So I ll assume here that you understand these concepts and that I don t need to reintroduce the notation. Quaternions were originally created by Sir William Rowan Hamilton on October 16, 1843, when he was working on ways to extend the complex plane into a complex cube. 1 He came up with his three complex axes system while he was walking along the Royal Canal to the Royal Irish Academy where he had a Council meeting, and he carved the equation that lies at the heart of quaternion theory i 2 = j 2 = k 2 = ijk = -1 into the Brougham Bridge. Hamilton describes this event twenty years later in a letter to his son, which can be read at http://www.maths.tcd.ie/pub/histmath/people/hamilton/letters/broomebri dge.html. This equation, along with the three equations ij = k jk = i ki = j are now called Hamilton s rules. He published a number of papers on quaternions, many of which are available on the Web including his November 1843 paper On a new Species of Imaginary Quantities connected with a theory of Quaternions which is at http://www.maths.tcd.ie/pub/histmath/people/hamilton/quatern1/quatern1. html. This is a fun paper to read, given the amazing amount of new material that he published there, particularly since he presented it only four weeks after he first though of the idea. I also find it impressive that he requested permission to present this paper on October 16, the day that he first though of the Hamilton rules. 1 He had been trying to add just one extra imaginary axis, but the math hadn t worked out. 2
Quaternions have been used in a number of mathematical disciplines, and so, for example, to algebraists they can be studied as a non-abelian group 2. 2 Weirder mathematicians will remember Martin Gardner s question in Scientific American What s purple and commutes? (http://math.berkeley.edu/~mgsa/halloweenpics/img014.html) although it has nothing to do with graphics. 3
2. Some Basic Quaternion Math If q 1 and q 2 are quaternions with q 1 = (s 1, v 1 ) and q 2 = (s 2, v 2 ), then it is simple to show that q 1 + q 2 = (s 1 + s 2, v 1 + v 2 ) q 1 q 2 = (s 1 s 2 v 1. v 2, s 1 v 2 + s 2 v 1 + v 1! v 2 ) Note that the multiplication is not commutative because of the vector cross product term. If q = (s, v) = (s, v 1, v 2, v 3 ) then the magnitude of q, q, is given by the formula q 2 = (s 2 + v 1 2 + v 2 2 + v 3 2 ) = (s 2 + v 2 ) = (s 2 + v. v) The conjugate of q is (s, -v). The inverse of q, written as usual as q -1, is given by the formula q -1 1 = q 2 (s, -v). (Note: The value (0, 0) is not a valid quaternion.) To confirm this equation, use the formula for quaternion multiplication which gives: qq -1 1 = q 2 (s 2 + v. v, sv - sv + v! -v) The scalar part in the parentheses is q 2, and the vector part is 0, so q q -1 = (1, 0) which is what we want. Note that if q = (s, v) is a unit quaternion, then q -1 is just (s, -v), the conjugate of q, which I assumed in the first set of notes. 4
3. The Justification for the Quaternion Rotation Formula In the first set of notes I stated that to rotate a point P by θ around an arbitrary unit axis, u, through the origin one can use the formula P = qpq -1, where q = (cos( 2! ), sin( 2! )u). Obviously I need to justify this. Before I get into this I ll need a number of equivalencies as I go through the derivation, and don t want to keep breaking off when I need them. I ll list them all here, without proofs. They are for all vectors u, v, and w, and for all angles α. Study them carefully, and ask for proofs in class for any that you cannot prove. (a) sin 2 (α) + cos 2 (α) = 1 (b) 2sin 2 (α) = 1 + sin 2 (α) cos 2 (α) (c) sin(α) = 2cos( 2! )sin( 2! ) (d) cos(α) = cos 2 ( 2! ) sin 2 ( 2! ) (e) (u + v). w = u. w + v. w (f) (u + v)! w = u! w + v! w (g) (u! v) = -(v! u) (h) (u. v) = (v. u) (i) (u! u) = 0 (j) (u. u) = 1 if u is a unit vector (k) (u! v). u = 0 (l) (u! v)! u = v (u. v)u if u is a unit vector I ll start with some basic 3D rotation math, and will then show that the result of this matches the quaternion rotation formula. I ll break it down into 2D pieces. I ll start with the picture below, where I want to rotate the vector r by θ around a unit vector n to get a new vector r R. This is equivalent to rotating the point r around n. 5
n n n n r r r 1 r r 2 r 1 r (n.r)n α (0,0,0) r 2 (0,0,0) r 2 (0,0,0) r-(n.r)n (0,0,0) The left hand diagram shows the plane containing r and n. I then decompose r into two vectors in the plane, r 1 which is parallel to n and r 2 which is perpendicular to n, and show this in the second diagram. If the angle between r and n is α, as shown, then r 1 has length r cos(α), which is (n. r) since n is a unit vector. It is in the direction of n, and so: r 1 = (n. r) n Next, r 2 is the same as the extra vector shown in the third diagram, and so r 1 + r 2 = r, which gives the fourth diagram since: r 2 = r (n. r) n Now instead of a single vector, r, to rotate around n, I have two, r 1 and r 2. However, r 1 is parallel to n, and so rotating it around n leaves it untouched. So I just have to rotate r 2 around n and add that result to r 1 to get the rotated vector. To do the rotation by θ we have the circle shown on the left below, which is a view looking down on n, and so n is vertical at the center of the circle. r 2 θ r 2 θ r 3 r 3 r 4 To define r 3 I ll use another vector, r 4, which lies in the plane and is at right angles to r 2, as shown in the second circle. So r 3 = cos(θ)r 2 + sin(θ)r 4. 6
Going back to the first figures, the length of r 2 is r sin(α), and since r 2, n, and r all lie in the same plane, r 4 is also perpendicular to r. So n! r is a vector in the direction or r 4 which has the same length as r 2. 3 Since r 2 and r 4 have the same length we get the surprisingly simple result that r 4 = n! r. Combining all of this, including that r 2 = r (n. r) n from above, we get: r 3 = (r (n. r) n)cos(θ) + (n! r)sin(θ) Now since, as discussed above, the rotated vector is this plus r 1, we have: r R = (n. r) n + (r (n. r) n)cos(θ) + (n! r)sin(θ) This finishes the general rotation formula. Now I have to show that the usual quaternion multiplication qrq -1 where q = (cos( 2! ), sin( 2! )n), r = (0, r), and q -1 = (cos( 2! ), -sin( 2! )n) gives the same result. We know that for any two quaternions: q 1 q 2 = (s 1 s 2 v 1. v 2, s 1 v 2 + s 2 v 1 + v 1! v 2 ), and so setting s = sin( 2! ) and c = cos( 2! ), to make this readable, we get: qrq -1 = (-sn.r, cr + s(n! r)) (c, -sn) = (-csn.r + csr.n + s 2 (n! r). n, s 2 (n.r)n + c 2 r + cs(n! r) cs(r! n) s 2 (n! r)! n. This might look awful, but I ll take it a piece at a time. First, the scalar part is zero because the first two parts cancel out and the third part is zero. The vector part can be rearranged into: s 2 (n.r)n + c 2 r + 2cs(n! r) s 2 (r (n.r)n) = 2s 2 (n.r)n + (c 2 s 2 )r + 2cs(n! r) = (1 + s 2 c 2 )(n.r)n + (c 2 s 2 )r + 2cs(n! r) = (n.r)n - cos(θ)(n.r)n + cos(θ)r + sin(θ)(n! r) 3 I ll assume until the end of this section that n! r is not the zero vector, because otherwise I ll need a bunch of special cases which will confuse things further. I ll handle that case when I m finished with the normal cases. 7
which matches the equation that I had for a 3D rotation. This just leaves the case that I ignored throughout the preceding derivation, which is when n! r = 0. For this to happen we need r = kn for some k, and I need to show that the rotation quaternion formula works correctly under these circumstances. Since r lies along n, the result of the rotation of kn should be that it isn t affected for any half angle cosine and sine, c and s. (c, sn) (0, kn) (c, -sn) = (-sk(n. n), ckn + sk(n! n)) (c, -sn) = (-sk, ckn) (c, -sn) = (-skc + skc(n. n), s 2 kn + c 2 kn csk(n! n)) = (0, (s 2 + c 2 )kn) = (0, kn) and so, as required, kn has not been affected by the transformation. 8
4. Interpolation Between two Unit Quaternions SLERP vs. LERP A common use of quaternions is to provide a smooth animation by interpolating between two quaternions, q 1 and q 2. There are two basic approaches to doing this interpolation, LERP and SLERP. LERP, which is linear interpolation, has the problem that the animation first speeds up and then slows down (although this could be considered an advantage in many cases) whereas SLERP (spherical linear interpolation) smoothly changes the angle between the two quaternions and so gives a steady interpolation. The descriptions of SLERP in the literature can get a bit scary, because they constantly include comments like the entire group of rotations maps onto the surface of the four-dimensional unit hypersphere. Fortunately much of this is mathematical obfuscation which can be ignored. E.g., since there are four components in a quaternion and rotations are equivalent to unit quaternions, it is obvious that they define the surface of a 4D hypersphere, just as 3D unit vectors define the surface of a 3D sphere and 2D unit vectors define a circle. The basic idea is that if we have two quaternions, q 1 and q 2, as shown in the diagrams below, q 1 q 2 q 1 q 2 then in the left diagram we are interpolating linearly since the points of the quaternions are equally separated, and in the second the angles are equal. As 9
a result in the LERP case (left) the rotation angles are bigger as one gets closer to the middle of the rotation, and so the rotation will speed up until the middle, and then slow down. In the SLERP case (right) the lines are interpolated around the projected circle in the sphere, and so the angular velocity will be constant. As an extra disadvantage the interpolated quaternions in the LERP case will have to be normalized, or they will not represent rotations. To produce a series of quaternions using SLERP, we use the following formula: sin((1 " t)! ) q(t) = q 1 sin(! ) where sin( t! ) + q 2, 0 t 1 sin(! ) q 1. q 2 = cos(θ). This is justified using the interpolation of vectors, and then extending into quaternions. Watt and Policarpo say that this solution is trivial, which is a bit of an exaggeration, but I ll give it as an exercise. Unfortunately, for small t increments, this solution requires many expensive calculations of sin functions, and so work has been done to find faster implementations which give the same intermediate quaternions. A pair of 2004 papers by Barrera, Hast, and Bengtsson, where the most relevant is available at http://www.ep.liu.se/ecp/013/004/ecp01304.pdf, compare six different ways to do incremental SLERPs on quaternions, including a complex number approach via De Moivre s formula, using Chebyshev polynomials, solving a differential equation using the Euler method, and matrix multiplication. They conclude that in general the Chebyshev approach is the most efficient, although any of the six methods are much faster than using the standard trigonometric approach. 10