Quaternions R. J. Renka Department of Computer Science & Engineering University of North Texas 11/09/2015
Definition A quaternion is an element of R 4 with three operations: addition, scalar multiplication, and quaternion multiplication. The first two operations are identical to those on R 4. Multiplication is defined in terms of basis elements, 1, i, j, k, where 1 is the multiplicative identity element: a quaternion can be written as a linear combination q = w + xi + yj + zk, where we have suppressed 1 = (1, 0, 0, 0), and i 2 = j 2 = k 2 = ijk = 1, and multiplication is associative, so that ij = k, jk = i, ki = j, ji = k, kj = i, ik = j. This is a four-dimensional analog of complex numbers. Notice that i, j, and k are unit quaternions that behave like vectors in R 3 with vector cross product, except that cross product is not associative.
Quaternion Operations It is convenient to write a quaternion as a scalar/vector pair Then it is easily verified that q = (w, v), for v = (x, y, z). q 1 q 2 = (w 1 w 2 v 1, v 2, w 1 v 2 + w 2 v 1 + v 1 v 2 ). Note that quaternion multiplication is commutative if and only if the vector parts are linearly dependent. We could have used the above expression to define multiplication, and then derived i 2 = (0, e 1 )(0, e 1 ) = ( 1, 0) = 1, ij = (0, e 1 )(0, e 2 ) = (0, e 3 ) = k, etc. We also have q 1 + q 2 = (w 1, v 1 ) + (w 2, v 2 ) = (w 1 + w 2, v 1 + v 2 ) and α(w, v) = (αw, αv) α R.
Conjugate, Norm, and Reciprocal The conjugate of q = (w,v) is q = (w, v), and the norm of q is q = w 2 + v, v, so that q q = (w 2 + v, v, 0) = w 2 + v, v = q 2. The reciprocal of q is q 1 = q/ q 2. Since qq = q q, q 1 is both a left inverse and right inverse. However, pq 1 q 1 p, and the expression p/q is therefore ambiguous. A unit quaternion is one whose norm is 1. Its reciprocal is its conjugate. It is straightforward to show that q 1 q 2 = q 1 q 2 and (q 1 q 2 ) 1 = q 1 2 q 1 1. The subset of purely imaginary quaternions with zero scalar parts includes the zero quaternion and is closed under linear combinations (addition and scalar multiplication). It is therefore a three-dimensional linear space over the reals, isomorphic to R 3.
Unit Quaternions and Rotation The complex number z = x + iy = ρe iφ = ρ(cos φ + i sin φ) can be rotated through angle θ by multiplying it by the unit complex number e iθ : e iθ z = ρe i(φ+θ) = cos θ x sin θ y + i(sin θ x + cos θ y). Similarly, a unit quaternion represents a rotation about an arbitrary pole (unit vector defining an axis direction) in R 3. Theorem A vector p R 3 can be rotated through angle θ about a pole n, n = 1, by forming the unit quaternion q = (cos θ 2, sin θ 2 n), and computing qpq 1, where p = (0,p) is the purely imaginary quaternion associated with vector p.
Proof of Theorem qp = = qpq 1 = = (cos θ2, sin θ2 ) n (0, p) ( sin θ2 nt p, sin θ2 (n p) + cos θ2 ) p ( sin θ2 nt p, sin θ2 (n p) + cos θ2 p ) (cos θ2, sin θ2 n ) ( sin θ 2 cos θ 2 nt p + sin θ 2 cos θ 2 nt p, sin 2 θ 2 (n p n) cos θ 2 sin θ (p n) 2 + sin 2 θ ( ) n T p n + cos θ 2 2 sin θ 2 (n p) + θ ) cos2 2 p
Proof of Theorem continued qp q = ( 0, sin 2 θ 2 p + 2 θ sin2 2 + cos 2 θ ) 2 p ( ) n T p n + 2 cos θ 2 sin θ 2 = (0, cos θp + (1 cos θ)(n T p)n + sin θ(n p)) = (0, Rp) for rotation matrix R, where we have used the trig identites ( θ cos θ = cos 2 + θ ) = cos 2 θ 2 2 θ sin2 2, 1 cos θ = 2 sin 2 θ 2, and ( θ sin θ = sin 2 + θ ) = 2 cos θ 2 2 sin θ 2. (n p)
Rotation Matrix R for Pole n p (n T p)n n φ 0 p p (n T p)n θ n p + (n T p)n Let p = Rp. Then p (n T p)n = cos θ(p (n T p)n) + sin θ (n p), where n p = p sin φ = p (n T p)n. Hence p = (cos θ)p + (1 cos θ) (n T p)n + sin θ(n p).
Alternative Expression for R Since the vector cross product is bilinear, n p can be computed by applying a linear transformation L to p. Denote the (skew symmetric) matrix representing L by ˆn. Then it is easily shown that ˆn = 0 n 3 n 2 n 3 0 n 1 n 2 n 1 0 We now have the following expression for R:. R = cos θi + (1 cos θ)nn T + sin θˆn. Also, ˆn 2 p = n (n p) = n p n = n 2 p+ (n T p)n = p + nn T p. Hence ˆn 2 = I + nn T, and nn T = I + ˆn 2, so that R = cos θi + (1 cos θ)(i + ˆn 2 ) + sin θˆn = I + sin θˆn + (1 cos θ)ˆn 2.
Properties of Cross Product Operator For u R 3, define Then û = ûv = u v v R 3. û T = û. 0 u 3 u 2 u 3 0 u 1 u 2 u 1 0. αu = αû and û + v = û + ˆv for scalars α and vectors u, v. û (t) = û (t), and u (t) u(t) = û (t)û(t) û(t)û (t). û 3 = û, u(t). û 4 = û 2, and û(t)û (t)û(t) = 0 for unit vectors
R in Terms of the Components of q = (w,x,y,z) In terms of the components of q = (w, v) = (cos(θ/2), sin(θ/2)n), we have where ˆv = so that R = I + 2 cos θ 2 sin θ 2 ˆn + 2 θ sin2 2 ˆn2 = I + 2w sin θ ( 2 ˆn + 2 sin θ ) 2 2 ˆn 0 z y z 0 x y x 0 R = = I + 2wˆv + 2ˆv 2,, ˆv 2 = y 2 z 2 xy xz xy x 2 z 2 yz xz yz x 2 y 2 1 2y 2 2z 2 2xy 2wz 2xz + 2wy 2xy + 2wz 1 2x 2 2z 2 2yz 2wx 2xz 2wy 2yz + 2wx 1 2x 2 2y 2.,
Algebraic Derivation of R Let a, b, and n be orthonormal unit vectors defining a right hand coordinate system, and let c = cos θ, s = sin θ. Then the matrix R that rotates through angle θ (CCW as viewed toward the origin) about n satisfies Ra = ca + sb, Rb = -sa + cb, and Rn = n, or R(a b n) = (a b n) c s 0 s c 0 0 0 1 ; i.e., RP = PR z for rotations P and R z. Hence R = PR z P T = ( a b a T n )R z b T n T = ( a b n ) = a(ca T sb T ) + b(sa T + cb T ) + nn T = c(aa T + bb T ) + s(ba T ab T ) + nn T. ca T sb T sa T + cb T n T
Algebraic Derivation of R continued We will rewrite R in terms of n, ˆn, and ˆn 2 instead of a, b, and n. Any vector p in R 3 may be written p = αa + βb + γn for α = a T p, β = b T p, and γ = n T p. Then ˆnp = n p = αb βa = ba T p ab T p, and hence ˆn = ba T ab T. Similarly, ˆn 2 p = αa βb = aa T p bb T p, and I p =αa + βb + γn = aa T p + bb T p + nn T p giving I = aa T + bb T + nn T, and Thus, we have ˆn 2 = aa T bb T = nn T I. R = cˆn 2 + sˆn + ˆn 2 + I = I + sˆn + (1 c)ˆn 2.
Properties of Unit Quaternions The product of unit quaternions is a unit quaternion. (cos(θ/2), sin(θ/2) n)(cos(φ/2), sin(φ/2)n) = (cos(θ/2 + φ/2), sin(θ/2 +φ/2)n) for n = 1; i.e., rotation through angle φ followed by rotation through angle θ is equivalent to rotation through angle θ + φ. More generally, unit quaternions commute iff the poles coincide. Unit quaternions i, j, and k represent 180-degree axis rotations, and i 2 = -1 = (-1,0) represents a 360-degree rotation (a special case of the following property). In fact every purely imaginary unit quaternion q = (0, n) represents a 180-degree rotation R = 2nn T I and satisfies q 2 = 1. q and -q represent the same rotation: q(0, p)q 1 = q(0, p)( q) 1 for all p. Rotation by q 1 followed by rotation by q 2 is equivalent to rotation by q 2 q 1.
Euler s Theorem Theorem Any orientation defined by a rotation matrix (order-3 orthogonal matrix R with det(r) = 1) can be represented by a rotation about a single Euler pole n; i.e., Rn = n for some nonzero vector n (and R has 1 as an eigenvalue). In particular, any product of rotations in R 3 can be represented by a rotation about a single Euler pole. proof It suffices to prove that 1 is an eigenvalue of R. det(i R) = det((i R) T ) = det(i R T ) = det( R T (I R)) = det(r T ) det(i R) = det(i R), and hence det(i R) = 0.
Euler Angles Any rigid-body orientation can be represented by three Euler angles or, equivalently, by a product of three rotations. An interactive user must be able to incrementally rotate an object about the object axes to change heading (yaw), pitch, or bank (roll), for example, or to smoothly rotate the camera or eye position around a scene. In OpenGL this could be accomplished by left multiplying the modelview matrix M by an axis rotation, but there are two problems with that. First, it would be inefficient because the OpenGL rotate function right multiplies M by the rotation. Second, we would be accumulating roundoff error, requiring periodic re-orthogonalization. The solution is to represent the viewing transformation by a single pole and angle, convert the axis rotation to a pole and angle, and compute a product of unit quaternions to update the viewing transformation.
Viewing Direction Problem: Given an arbitrary viewing direction u = (E O)/ E O, for eye position E and object point O, construct a rotation (pole n and angle θ) that maps u to e 3 so that the viewing direction is along the z axis. Solution: n = u e 3 = (u 2, u 1, 0), θ = cos 1 u, e 3 = cos 1 u 3.
Rigid Body Animation Given key frames associated with different orientations, we need to construct in-between frames for animation by interpolation. If the orientations are defined by pairs of axis rotations or triples of Euler angles, it is not clear how to choose in-between frames. In terms of a single pole and angle, or quaternion, however, we have a one dimensional parameter space, and we simply use a sequence of angles along with the fixed pole. The more general problem is to produce constant-speed motion along a unit-radius arc of a great circle in two or more dimensions. This is referred to as spherical linear interpolation (SLERP). Given unit quaternions (points on the unit hypersphere in R 4 ) representing an initial orientation and a final orientation, we seek to construct a sequence of unit quaternions representing intermediate orientations on the arc of great circle joining the given pair.
Spherical Linear Interpolation (SLERP) Given a pair of unit vectors q 0 and q 1 in R n, n 2, separated by angle θ = cos 1 (q 0 q 1 ) with 0 < θ < π, we wish to construct a constant-speed parameterization {q(t) : t [0, 1]} of the geodesic that joins q 0 and q 1. The solution is characterized as follows. 1 q(t) is a linear combination of q 0 and q 1, 2 q(t) = 1, and 3 the angular distance from q 0 to q(t) is tθ for all t [0, 1]. Let q(t) = α 0 q 0 + α 1 q 1. Then q(t) 2 = α 2 0 + 2α 0 α 1 cos θ + α 2 1 = 1 and q 0 q(t) = α 0 + α 1 cos θ = cos tθ α 0 = cos tθ α 1 cos θ, so that 1 = cos 2 tθ 2α 1 cos θ cos tθ + α1 2 cos 2 θ + 2α 1 cos θ (cos tθ α 1 cos θ) + α1 2 = cos 2 tθ α1 2 cos 2 θ + α1. 2
Spherical Linear Interpolation (SLERP) continued We thus have 1 cos 2 tθ = α 2 1(1 cos 2 θ) sin 2 tθ = α 2 1 sin 2 θ, and hence α 1 = sin tθ/ sin θ = sin tθ/ sin θ since 0 < θ < π. By choosing α 1 0, α 0 and α 1 will be in the interval [0, 1], and q(t) will lie on the shorter of the two great circle arcs defined by q 0 and q 1. We now have sin tθ sin θ cos tθ cos θ sin tθ α 0 = cos tθ cos θ = sin θ sin θ i.e., = sin(θ tθ) ; sin θ q(t) = slerp(t; q 0, q 1 ) = sin[(1 t)θ] sin tθ q 0 + sin θ sin θ q 1. Note the symmetry: slerp(t;q 0, q 1 ) = slerp(1-t;q 1, q 0 ). Also, in the limit as θ 0, slerp(t;q 0, q 1 ) (1 t)q 0 + tq 1.
Spherical Linear Interpolation (SLERP) continued An alternative derivation of the expression for q(t) is based on geometry (and trigonometry). Let q0 denote the 90-degree rotation of q 0 in the direction toward q 1. Then q(t) = (cos tθ)q 0 + (sin tθ)q0, and q 1 = (cos θ)q 0 + (sin θ)q0 implying that q0 = (q 1 (cos θ)q 0 )/ sin θ. Hence ( ) q1 (cos θ)q 0 q(t) = (cos tθ)q 0 + sin tθ sinθ = (sin θ cos tθ cos θ sin tθ)q 0 + (sin tθ)q 1 sin θ sin[θ(1 t)] sin tθ = q 0 + sin θ sin θ q 1. It is a straightforward exercise to show that the derivative satisfies q (t) = θ t, so that q (t)/θ is a unit tangent to the unit hypersphere at q(t) for all t.
Spherical Linear Interpolation (SLERP) continued There remains an additional issue in the application of SLERP to unit quaternions the double coverage of the set of rotations. Each rotation is represented by two unit quaternions, q and q. Thus, slerp(t;q 0, q 1 ) and slerp(t;q 0, q 1 ) both correspond to great circle arcs associated with the same pair of rotations, but only the shorter of the two arcs is the geodesic. We therefore begin by reversing the sign of either q 0 or q 1 if q 0 q 1 < 0. Then we have θ [0, π/2].
Time-varying Quaternion Given a unit quaternion q(t) representing the orientation of a rigid body at time t, we need to evaluate the derivative q (t). We require the following. Lemma: Given a time-dependent angle θ(t) and unit vector u(t) defining a rotation R(t) = I + sin(θ(t))û(t) + (1 cos(θ(t)))û(t) 2 with derivative the angular velocity is R (t) = ˆω(t)R(t), ω(t) = θu + sin(θ) u + (cos(θ) 1)( u u).
Proof of Lemma Differentiating R(t) = I + sin(θ(t))û(t) + (1 cos(θ(t)))û(t) 2, we get Ṙ = θ cos(θ)û + sin(θ) û + (1 cos(θ))(û û + ûû) + θ sin(θ)û 2, and R T = I sin(θ)û + (1 cos(θ))û 2. Using the properties of the cross product operator for unit vectors, and a bit of algebra, we obtain ṘR T = θû + sin(θ) û + (1 cos(θ))(û û ûû) = ˆω.
Derivative of Time-varying Unit Quaternion Theorem: q (t) = (1/2)(0, ω(t))q(t). proof: Let q(t) = γ + σu for γ = cos(θ(t)/2), σ = sin(θ(t)/2), and u = (0, u(t)) for u(t) = 1 for all t. Then Also, Thus q (t) = ( θ/2)σ + σ u + ( θ/2)γu = ( θ/2)(γu σ) + σ u = ( θ/2)uq + σ u = (1/2)( θu + 2σ uq 1 )q. uq 1 = u(γ σu) = γ u σ uu = γ u σ( u T u + (0, u u)) = γ u σ(0, u u). q (t) = (1/2)[ θu + 2σγ u 2σ 2 (0, u u)]q = (1/2)[ θu + sin(θ) u + (cos(θ) 1)(0, u u)]q = (1/2)(0, ω)q.