Quaternions and 3D Rotations

Jeanette Schofield

April 7, 2011 Contents

1 : Definition and Basic Properties 3 1.1 Equality ...... 4 1.2 ...... 4 1.2.1 Closed Under Addition ...... 4 1.2.2 Additive Identity ...... 4 1.2.3 ...... 5 1.2.4 Commutative Under Addition ...... 5 1.2.5 Associative Under Addition ...... 5 1.3 ...... 6 1.3.1 Scalar Multiplication ...... 6 1.3.2 Multiplication of Two Quaternions ...... 6 1.3.3 Associative Under Multiplication ...... 6 1.3.4 Implications of i2 = j2 = k2 = ijk = −1...... 7 1.3.5 Multiplication of Two Quaternions Revisited ...... 8 1.3.6 Closed Under Multiplication ...... 9 1.3.7 Multiplicative Identity ...... 9 1.3.8 Conjugate ...... 10 1.3.9 Norm ...... 10 1.3.10 Multiplicative Inverse ...... 10 1.4 Multiplication is Distributive over Addition ...... 11 1.5 Remarks on Multiplication and Commutativity ...... 12 1.6 Summary of Algebraic Properties ...... 13

2 Quaternions and Rotations 14 2.1 Rotations in Three-Space ...... 14 2.2 Proof: w = qvq∗ ...... 15 2.3 Expanding w = qvq∗ ...... 20 2.4 ...... 21 2.5 Examples ...... 22 2.5.1 Example 1 ...... 22 2.5.2 Example 2 ...... 23

3 Rotating a Coordinate Frame S into Another Coordinate Frame S0 26 3.1 Generalized Two Axis-Angle Rotation Method ...... 26 3.2 Two Axis-Angle Rotation Method Using Quaternions ...... 27

1 3.2.1 Example ...... 28

A Some Useful Equations 31 A.1 The Dot and Cross Product of Two Vectors ...... 31 A.1.1 The Dot Product ...... 31 A.1.2 The Cross Product ...... 31 A.2 Useful Relationships ...... 31 A.2.1 Proof: If v is ⊥ to w, kv + wk2 = kvk2 + kwk2 ...... 32

B Source Code for the Two Axis-Angle Frame Rotation Method 33 B.1 C++ Code Listing ...... 33

2 Chapter 1

Quaternions: Definition and Basic Properties

Quaternions may be thought of as 4-tuples of real : q = (q0, q1, q2, q3). Equivalently, quaternions may also be written in the form

q = q0 + q1i + q2j + q3k (1.0.0.1) where

i = (0, 1, 0, 0) j = (0, 0, 1, 0) (1.0.0.2) k = (0, 0, 0, 1) are themselves 4-tuples which satisfy the following relations

i2 = j2 = k2 = ijk = −1. (1.0.0.3)

3 In R , a vector is defined as v = vxi + vyj + vzk where i, j, and k are vectors of the form

i = (1, 0, 0) j = (0, 1, 0) k = (0, 0, 1).

3 4 Any vector in R can be mapped to R by (vx, vy, vz) 7→ (0, vx, vy, vz) This mapping is often represented by v = 0 + v = v. By convention, the i, j, and k components of a quaternion are usually written as vectors. Thus, any quaternion q may be written as

q = q0 + q = q0 + q1i + q2j + q3k (1.0.0.4) where i2 = j2 = k2 = ijk = −1. (1.0.0.5)

Here, q0 is referred as the scalar part of the quaternion and q = q1i + q2j + q3k is referred to

3 as the vector part of the quaternion. The quaternions form a non-commutative . This means that quaternions have all the properties of a field except that quaternions are not commutative under multi- plication. A discussion of the various properties of quaternions follows.

1.1 Equality

Two quaternions, p = p0 + p1i + p2j + p3k and q = q0 + q1i + q2j + q3k are considered equal if and only if their corresponding scalar components are equal:

p0 = q0

p1 = q1

p2 = q2

p3 = q3.

1.2 Addition

Two quaternions, p = p0 + p1i + p2j + p3k and q = q0 + q1i + q2j + q3k, can be added together by combining their corresponding scalar and vector components:

p + q = (p0 + p1i + p2j + p3k) + (q0 + q1i + q2j + q3k)

= (p0 + q0) + (p1 + q1)i + (p2 + q2)j + (p3 + q3)k.

The following subsections discuss various properties of the quaternions under addition.

1.2.1 Closed Under Addition

The addition of two quaternions, p = p0 + p1i + p2j + p3k and q = q0 + q1i + q2j + q3k, will result in a third quaternion, r = r0 + r1i + r2j + r3k:

p + q = p0 + p1i + p2j + p3k + q0 + q1i + q2j + q3k

= (p0 + q0) + (p1 + q1)i + (p2 + q2)j + (p3 + q3)k

= r0 + r1i + r2j + r3k = r.

1.2.2 Additive Identity The additive identity for quaternions is the zero quaternion, 0 = 0 + 0i + 0j + 0k:

q + 0 = q0 + q1i + q2j + q3k + 0 + 0i + 0j + 0k

= (q0 + 0) + (q1 + 0)i + (q2 + 0)j + (q3 + 0)k

= q0 + q1i + q2j + q3k = q.

4 1.2.3 Additive Inverse

The additive inverse for a quaternion q = q0 + q1i + q2j + q3k is given by −q = −q0 − q1i − q2j − q3k:

q + (−q) = (q0 + q1i + q2j + q3k) + (−q0 − q1i − q2j − q3k)

= (q0 − q0) + (q1 − q1)i + (q2 − q2)j + (q3 − q3)k = 0.

1.2.4 Commutative Under Addition Commutativity means that the order in which two quaternions are added together does not matter. Thus, for any two quaternions, p = p0 + p1i + p2j + p3k and q = q0 + q1i + q2j + q3k,

p + q = (p0 + p1i + p2j + p3k) + (q0 + q1i + q2j + q3k)

= (p0 + q0) + (p1 + q1)i + (p2 + q2)j + (p3 + q3)k

= (q0 + p0) + (q1 + p1)i + (q2 + p2)j + (q3 + p3)k

= (q0 + q1i + q2j + q3k) + (p0 + p1i + p2j + p3k) = q + p.

1.2.5 Associative Under Addition Associativity means given any three quaternions under addition, the order in which they are grouped together does not matter. For any three quaternions, p = p0 + p1i + p2j + p3k, q = q0 + q1i + q2j + q3k, and r = r0 + r1i + r2j + r3k, observe that   (p + q) + r = (p0 + q0) + (p1 + q1)i + (p2 + q2)j + (p3 + q3)k   + r0 + r1i + r2j + r3k       = (p0 + q0) + r0 + (p1 + q1) + r1 i + (p2 + q2) + r2 j   + (p3 + q3) + r3 k.

Because the real numbers are associative under addition, the scalar componenets can be manipulated. Thus,       (p + q) + r = p0 + (q0 + r0) + p1 + (q1 + r1) i + p2 + (q2 + r2) j   + p3 + (q3 + r3) k    = p0 + p1i + p2j + p3k + (q0 + r0) + (q1 + r1)i + (q2 + r2)j  +(q3 + r3)k = p + (q + r).

To summarize, the associativity of quaternions under addition means that the order in which three or more quaternions are combined does not matte; i.e. (p + q) + r = p + (q + r).

5 1.3 Multiplication

For quaternions, there are two types of multiplication to discuss: scalar multiplication and the muiltiplication of two quaternions. Scalar multiplication involves the multiplication of a quaternion q = q0 +q1i+q2j+q3k by any scalar s ∈ R. The multiplication of two quaternions involves multiplying two quaternions p = p0 + p1i + p2j + p3k and q = q0 + q1i + q2j + q3k. Various properties of quaternions that deal with multiplication will also be discussed in this section.

1.3.1 Scalar Multiplication

Multiplication of a quaternion q = q0 + q1i + q2j + q3k by a scalar s is defined as

sq = sq0 + sq = sq0 + sq1i + sq2j + sq3k.

1.3.2 Multiplication of Two Quaternions

The multiplication of two quaternions, p = p0 + p and q = q0 + q, is defined as

pq = (p0 + p)(q0 + q) = p0q0 + p0q + q0p + pq. (1.3.2.1)

At this point is in unclear how to expand the term ”pq.” This will be revisted in a later section.

1.3.3 Associative Under Multiplication Quaternions are associative under multiplication. This means that for any three quaternions, p = p0 + p1i + p2j + p3k, q = q0 + q1i + q2j + q3k, and r = r0 + r1i + r2j + r3k, the order in which the quaternions are grouped together under multiplication does not matter:

(pq)r = (p0q0 + p0q + q0p + pq)(r0 + r)

= p0q0r0 + p0q0r + p0r0q + p0qr + q0r0p + q0pr + r0pq + pqr.

Factoring p0 out of the first four terms and p out of the last four terms,

(pq)r = p0(q0r0 + q0r + r0q + qr) + p(q0r0 + q0r + r0q + qr)

= (p0 + p)(q0r0 + q0r + r0q + qr).

By definition, qr = q0r0 + q0r + r0q + qr. Thus,

(pq)r = p(qr) (1.3.3.1) and it has been shown that quaternions are associative under multiplication.

6 1.3.4 Implications of i2 = j2 = k2 = ijk = −1 The relationships i2 = j2 = k2 = ijk = −1 must always hold; some important relationships that follow from these statements will now be derived. Since i2 = ii = ijk = −1, this implies that

i = jk. (1.3.4.1)

Similarly, since k2 = kk = ijk = −1, then

k = ij. (1.3.4.2)

Using equation (1.3.4.2) and multiplying by j on the right,

k(j) = ij(j)

kj = i(jj) = i(j2) = i(−1) = −i so i = −kj. (1.3.4.3) From equations (1.3.4.1) and (1.3.4.3), the following relationships can be found for i:

i = jk = −kj.

Using equation (1.3.4.1) and multiplying on the left by j,

(j)i = (j)jk

ji = (jj)k = (j2)k = (−1)k = −k so k = −ji. (1.3.4.4) From equation (1.3.4.2) and (1.3.4.4), the following relationships are found for k:

k = ij = −ji.

Using equation (1.3.4.4) and multiplying on the right by i,

k(i) = −ji(i)

ki = −j(ii) = −j(i2) = −j(−1) = j. Thus, j = ki. (1.3.4.5) Using equation (1.3.4.2) and multiplying on the left by i,

(i)k = (i)ij

7 ik = (ii)j = (i2)j = (−1)j = −j. Hence, j = −ik. (1.3.4.6) From equations (1.3.4.5) and (1.3.4.6), the following relationships are found for j:

j = ki = −ik.

To summarize, when multiplying two quaternions together, the relationships i2 = j2 = k2 = ijk = −1 must always hold. These relationships imply the following:

ij = −ji = k jk = −kj = i (1.3.4.7) ki = −ik = j

The above relationships are helpful when multiplying two quaternions together.

1.3.5 Multiplication of Two Quaternions Revisited

Earlier the multiplication of two quaternions, p = p0 + p and q = q0 + q, was defined as

pq = (p0 + p)(q0 + q) = p0q0 + p0q + q0p + pq.

The last term, pq, is the product of two vectors. This term can now be expanded to become

pq = (p1i + p2j + p3k)(q1i + q2j + q3k)

= p1i(q1i + q2j + q3k) + p2j(q1i + q2j + q3k) + p3k(q1i + q2j + q3k)

= p1q1ii + p1q2ij + p1q3ik + p2q1ji + p2q2jj + p2q3jk

+p3q1ki + p3q2kj + p3q3kk.

The relationships developed in the previous subsection (1.3.4.7), and the fact that i2 = j2 = k2 = −1, the above equation can be further simplified as

pq = p1q1(−1) + p1q2k + p1q3(−j) + p2q1(−k) + p2q2(−1) + p2q3i

+p3q1j + p3q2(−i) + p3q3(−1)

= −(p1q1 + p2q2 + p3q3)

+[(p2q3 − p3q2)i + (p3q1 − p1q3)j + (p1q2 − p2q1)k].

In the above equation, the term (p1q1 +p2q2 +p3q3) is the familiar dot product of two vectors. The term [(p2q3 − p3q2)i + (p3q1 − p1q3)j + (p1q2 − p2q1)k] is the cross product of two vectors. Thus, pq = −(p · q) + p × q. (1.3.5.1) The above equation says that the product of two vectors in R4 results in a scalar (the dot product) and another vector (the cross product).

8 Plugging pq = −(p · q) + p × q into equation (1.3.2.1), the multiplication of two quater- nions, p = p0 + p and q = q0 + q, is

pq = p0q0 + p0q + q0p + pq

= p0q0 + p0q + q0p + [−(p · q) + p × q].

The terms p0q0 and −(p·q) are both scalars. The other three terms are all vectors. Grouping scalar terms together and vector terms together, the equation for the multiplication of two quaternions becomes

pq = p0q0 − (p · q) + p0q + q0p + p × q. (1.3.5.2)

1.3.6 Closed Under Multiplication

The multiplication of any two quaternions, p = p0 + p and q = q0 + q, will result in a third quaternion, r = r0 + r:

pq = p0q0 − (p · q) + p0q + q0p + p × q

= (p0q0 − p1q1 − p2q2 − p3q2) + (p0q1 + p1q0 + p2q3 − p3q2)i

+(p0q2 − p1q3 + p2q0 + p3q1)j + (p0q3 + p1q2 − p2q1 + p3q0)k

= r0 + r1i + r2j + r3k = r.

1.3.7 Multiplicative Identity

The product of a quaternion q = q0 + q and the multiplicative idenitity should return the quaternion q. For quaternions, the multiplicative identity is the quaternion with a scalar part of 1 and zero vector part:

q(1) = (q0 + q)(1 + 0)

= q0(1) − (q · 0) + q0(0) + (1)q + q × 0

= q0 + q = q.

If the order of multiplication is reversed:

(1)q = (1 + 0)(q0 + q)

= (1)q0 − (0 · q) + (1)q + q0(0) + 0 × q

= q0 + q = q.

Since q(1) = (1)q = q, 1 is the multplicative identity for R4.

9 1.3.8 Conjugate

The of a quaternion q = q0 + q = q0 + q1i + q2j + q3k is defined as ∗ q = q0 − q = q0 − q1i − q2j − q3k. Note that multiplication of a quaternion and its complex conjugate always results in a scalar product and is commutative. Multiplying q by q∗,

∗ qq = (q0 + q)(q0 − q) 2 = q0 − (q · −q) + q0q − q0q + q × (−q).

The cross product of q and −q can be simplified by pulling the negative out: q × −q = −(q × q). Recalling that the cross product of any vector with itself is always equal to zero (q × q = 0), the above equation now becomes

∗ 2 qq = q0 + q · q 2 2 2 2 = q0 + q1 + q2 + q3.

Switching the order of multiplication and multiplying q∗ by q,

∗ q q = (q0 − q)(q0 + q) 2 = q0 − (−q · q) + q0q − q0q + (−q) × q.

Again, the negative can be pulled out of the cross product: (−q) × q = −(q × q) = 0. The above equation can now be simplified as

∗ 2 q q = q0 + q · q 2 2 2 2 = q0 + q1 + q2 + q3.

It has now been shown that the product of a quaternion and its complex conjugate is com- mutative and is equal to ∗ ∗ 2 2 2 2 qq = q q = q0 + q1 + q2 + q3. (1.3.8.1)

1.3.9 Norm

The norm of a quaternion q = q0 + q = q0 + q1i + q2j + q3k is defined as √ N(q) = q∗q q 2 2 2 2 = q0 + q1 + q2 + q3 (1.3.9.1) = kqk.

1.3.10 Multiplicative Inverse

Let q = q0 + q = q0 + q1i + q2j + q3k be any quaternion. The multiplicative inverse of q is given by the quaternion q−1 which will return the multplicative identity when it multiplies q; i.e. q−1 is the quaternion that satisfies the equations qq−1 = 1 and q−1q = 1.

10 To find q−1, multiply qq−1 = 1 on the right by q∗:

q∗(qq−1) = q∗(1) (q∗q)(q−1) = q∗ kqk2q−1 = q∗ q∗ q−1 = . kqk2

Now, q−1q = 1 must be multiplied by q∗ on the left:

(q−1q)q∗ = (1)q∗ (q−1)(q∗q) = q∗ q−1kqk2 = q∗ q∗ q−1 = . kqk2

Thus, the multiplicative inverse for quaternions is given by q∗ q−1 = . kqk2

1.4 Multiplication is Distributive over Addition

Quaternion multiplication is distributive over addition; i.e. p(q +r) = pq +pr. For any three quaternions, p = p0 + p, q = q0 + q, and r = r0 + r,   p(q + r) = (p0 + p) (q0 + r0) + (q + r)

= p0(q0 + r0) − p · (q + r) + p0(q + r) + (q0 + r0)p + p × (q + r). (1.4.0.1)

Both the dot product and cross product are distributive over addition, so the terms −p·(q+r) and p × (q + r) become −p · (q + r) = −p · q − p · r p × (q + r) = p × q + p × r. Equation (1.4.0.1) now becomes

p(q + r) = p0(q0 + r0) − p · (q + r) + p0(q + r) + (q0 + r0)p + p × (q + r)

= p0q0 + p0r0 − p · q − p · r + p0q + p0r + q0p + q0p + p × q + p × r

= (p0q0 − p · q + p0q + q0p + p × q) + (p0r0 − p · r + p0r + r0p + p × r) = pq + pr.

Since p(q + r) = pq + pr, quaternion multiplication is distributive over addition.

11 1.5 Remarks on Quaternion Multiplication and Com- mutativity

When quaternion multiplication was introduced, it was stated that quaternions are not commutative under multiplication. In equation form, this means that

pq 6= qp.

A closer look at the equation for multiplication,

pq = p0q0 − (p · q) + p0q + q0p + p × q, reveals that term p × q leads quaternion multiplication to not be commutative. This is because the cross product of two vectors is itself not commutative. If the term p × q = 0, then, in this special case, quaternion multiplication will be commutative. The following example shows a case where quaternion multiplication is not commutative.

Example 1.5.1.

Let p = 1 − 3j + 2k and q = 5 + 2i − j. Here p0 = 1, q0 = 5, p = −3j + 2k, and q = 2i − j. First, pq = p0q0 − (p · q) + p0q + q0p + p × q will be computed. The dot and cross product are p · q = (0)(2) + (−3)(−1) + (2)(0) = 3

p × q = (−3)(0) − (2)(−1)i + (2)(2) − (0(0)j + (0)(−1) − (−3)(2)k = 2i + 4j + 6k so

pq = p0q0 − (p · q) + p0q + q0p + p × q = 1(5) − 3 + 1(2i − j) + 5(−3j + 2k) + 2i + 4j + 6k = 2 + 4i − 12j + 16k.

To find qp, the dot and cross product are

q · p = (2)(0) + (−1)(−3) + (0)(2) = 3

q × p = (−1)(2) − (0)(−3)i + (0(0) − (2)(2)j + (2)(−3) − (−1)(0)k = −2i − 4j − 6k

12 so

qp = q0p0 − (q · p) + q0p + p0q + q × p = 5(1) − 3 + 5(−3j + 2k) + 1(2i − j) − 2i − 4j − 6k = 2 − 20j + 4k.

Thus, pq = 2 + 4i − 12j + 16k and qp = 2 − 20j + 4k. As expected pq 6= qp.

1.6 Summary of Algebraic Properties

Various properties of quaternions have now been discussed. A summary of these properties is shown in Table 1.1. The addition and multiplication of two quaternions, how to find the conjugate of a quaternion, and how to computer the norm of a quatenrion have also been discussed; Table 1.2 lists these useful quatenrion operations.

Table 1.1: List of Quaternion Properties Closed under addition and multiplication. Commutative under addition: p + q = q + p Associative under addition: (p + q) + r = p + (q + r) Associative under multiplication: (pq)r = p(qr) An identity exists for addition: q + 0 = q Ad identity exists for multiplication: q(1) = q Inverse exists for addition: −q = −q0 − q1i − q2j − q3k −1 q∗ Inverse exists for multiplication: q = kqk2 Multiplication is distributive over addition: p(q + r) = pq + pr

Quaternions are NOT commutative under multiplication: pq 6= qp

Table 1.2: Summary of Quaternion Operations Addition: p + q = (p0 + q0) + (p1 + q1)i + (p2 + q2)j + (p3 + q3)k Multiplication: pq = p0q0 − (p · q) + p0q + q0p + p × q Conjugate of q = q + q: q∗ = q − q √ 0 0 2 ∗ p 2 2 2 2 Norm: kqk = q q = q0 + q1 + q2 + q3

The most important thing to remember when working with quaternions is that, in general, multiplication is not commutative:

pq 6= qp.

Because quaternion multiplication is not commutative, one must always be careful with the order in which two or more quaternions are multiplied together.

13 Chapter 2

Quaternions and Rotations

In his youth, Sir William Hamilton spent a lot of time developing complex theory. During his work with complex numbers, he realized that by multiplying a of the form z = x + iy by a number of the form u = cos θ + i sin θ, he could rotate the vector z in R2. Hamilton’s work with complex numbers led him to believe that since a vector could be rotated by multiplying two numbers together in two-dimensions, there should also be a way to rotate vectors in three-dimensions by multiplying two numbers together. For more than a decade of his life, Hamilton became obsessed with developing the theory of ”triples.” Triples were numbers of the form w = w0 + w1i + w2j where i and j are of the form (0, 1, 0) and (0, 0, 1). No matter how he tried, Hamilton could not find a way to multiply two triples together. The reason for this is simple: multiplication is not closed under R3 – what Hamilton had been trying to do was impossible in R3. One day in 1843, while walking across the Royal Canal in Dublin, Hamilton had a flash of insight and realized that he could rotate a vector in R4 by multiplying three numbers of 2 2 2 the form q = q0 + q1i + q2j + q3k together where the relationships i = j = k = ijk = −1 must be satisfied. Hamilton was so afraid of forgetting this equation, that he carved it into the Broom Bridge. While Hamilton’s original carving has long since faded, a plaque now marks the spot where Hamilton famously discovered quaternions. A discussion of how to rotate a vector in three-dimensions by multiplying three quater- nions together follows.

2.1 Rotations in Three-Space

In this section, a way to rotate the vector v = vxi + vyj + vxk through an angle 2θ about some vector u will be presented. The vector u must be a vector and both v and u must pass through the origin. The vector w = wxi + wyj + wzk is the image of v after rotation. Both v and w are vectors in R3 and must be mapped to R4 by introducing a zero scalar part: v = 0 + v w = 0 + w. By convention, the quaternions v = 0 + v and w = 0 + w are usually represented by the

14 z

w ¡¡ ¡ ¡  u ¡ ¡ ¡2θ ¡ ¨¨*v ¡¨ ¨ ¡¨ y

x

Figure 2.1: The vector v will be rotated clockwise about u through an angle of 2θ.

vectors v and w. Both v and w will be used here. Pre-multiplying v by a quaternion of the form q = cos θ + u sin θ and post-multiplying it by a quaternion of the form q∗ = cos θ − u sin θ, will result in a vector w that is the image of v rotated about u through an angle 2θ:

w = qvq∗ = (cos θ + u sin θ)v(cos θ − u sin θ). (2.1.1)

The following proof will show that equation (2.1.1) does indeed produce a vector w that is the image of v rotated about u through an angle 2θ.

2.2 Proof: w = qvq∗

∗ It will now be shown that the equation w = qvq results in a vector w = wxi + wyj + wzk that is the image of v = vxi + vyj + vzk rotated about u = uxi + uyj + uzk through an angle 2θ. Here, u is a unit vector (kuk = 1) and both u and v pass through the origin. The quaternions q and q∗ are have the form

q = cos θ + u sin θ q∗ = cos θ − u sin θ.

The vector v will now be split into two components: vk, the portion of v to u and v⊥, the portion of v perpendicular to u. Now,

w = qvq∗ = (cos θ + u sin θ)v(cos θ − u sin θ)

= (cos θ + u sin θ)(vk + v⊥)(cos θ − u sin θ).

15 Quaternion multiplication is distributive over addition which allows the last equation to be split up into two terms:

w = (cos θ + u sin θ)vk(cos θ − u sin θ) + (cos θ + u sin θ)v⊥(cos θ − u sin θ). (2.2.1)

It is easier to evaluate each term of the above equation individually.

Showing that vk is unchanged under rotation

Since vk is parallel to u, it should be unchanged under rotation. It will now be shown that this is indeed the case. Expanding the term (cos θ + u sin θ)vk(cos θ − u sin θ),   (cos θ + u sin θ)vk(cos θ − u sin θ) = cos θvk + (uvk) sin θ (cos θ − u sin θ).

Note that the term uvk, is the product of two vectors. By equation (1.3.5.1), uvk = u×vk −u·vk. However, u and vk are parallel to one another so u×vk = 0 and uvk = −u·vk. Plugging this into the above equation,     cos θvk + (uvk) sin θ (cos θ − u sin θ) = cos θvk − (u · vk) sin θ (cos θ − u sin θ).

Expanding the right side,

  2 cos θvk − (u · vk) sin θ (cos θ − u sin θ) = cos θvk − vku cos θ sin θ − (u · vk) cos θ sin θ 2 +u(u · vk) sin θ.

The term vku can also be expanded using equation (1.3.5.1): vku = vk × u − vk · u. Again, since u and vk are parallel to one another, u × vk = 0 so vku = −vk · u. Plugging this into the above equation,

2 2 cos θvk − vku cos θ sin θ − (u · vk) cos θ sin θ + u(u · vk) sin θ 2 2 = cos θvk + (vk · u) cos θ sin θ − (u · vk) cos θ sin θ + u(u · vk) sin θ 2   2 = cos θvk + (vk · u) − (u · vk) cos θ sin θ + u(u · vk) sin θ.

The dot product of two vectors is commutative. This means that (vk · u) = (u · vk) so the term (vk · u) − (u · vk) = (u · vk) − (u · vk) = 0. The above equation now becomes

2   2 2 2 cos θvk + (vk · u) − (u · vk) cos θ sin θ + u(u · vk) sin θ = cos θvk + u(u · vk) sin θ. (2.2.2)

The product u(u·vk) will now be simplified. By definition, the dot product of two vectors is defined as a·b = kakkbk cos φ where φ is the angle between a and b. Since vk was defined ◦ ◦ as being parallel to u, φ is either 0 (u and vk are pointing in the same direction) or 180 ◦ (u and vk are pointing in opposite directions). If φ = 0 ,

◦ u(u · vk) = u(kukkvkk cos 0 ).

16 Since cos 0◦ = 1 and kuk = 1 (recall that we defined u to be a unit vector),

u(u · vk) = kvkku.

◦ Because the angle between u and vk is 0 and because u and vk both pass through the origin, ◦ vk = kvkku. Thus, u(u · vk) = kvkku = vk when φ = 0 . Now, if φ = 180◦, ◦ u(u · vk) = u(kukkvkk cos 180 ). Here cos 180◦ = −1 and kuk = 1, so

u(u · vk) = −kvkku.

◦ Since the angle between u and vk is 180 and since u and vk both pass through the origin, ◦ vk = −kvkku. Thus, u(u · vk) = −kvkku = vk when φ = 180 . ◦ It has now been shown that u(u · vk) = vk whether the angle between u and vk is 0 or 180◦. Plugging this into equation (2.2.2),

2 2 2 2 cos θvk + u(u · vk) sin θ = cos θvk + vk sin θ 2 2 = (cos θ + sin θ)vk.

From basic trig, cos2 θ + sin2 θ = 1, so

2 2 (cos θ + sin θ)vk = vk.

It has now been shown that (cos θ + u sin θ)vk(cos θ − u sin θ) = vk, which is the first term in equation (2.2.1). As expected, vk is not changed when v is rotated about u.

Showing that v⊥ is rotated through an angle 2θ upon rotation

Here, it will be shown that v⊥ will be rotated about u through an angle of 2θ. Expanding the second term in equation (2.2.2),

(cos θ + u sin θ)v⊥(cos θ − u sin θ) = (cos θv⊥ + uv⊥ sin θ)(cos θ − u sin θ).

Here uv⊥ is the product of two vectors, so it can be expanded using equation (1.3.5.1): uv⊥ = u × v⊥ − u · v⊥. Since u is perpendicular to v⊥, the dot product of u and v⊥ is 0. This means that uv⊥ simplifies as uv⊥ = u × v⊥. The above equation now becomes   (cos θv⊥ + uv⊥ sin θ)(cos θ − u sin θ) = cos θv⊥ + (u × v⊥) sin θ (cos θ − u sin θ).

Expanding the right side,

  2 cos θv⊥ + (u × v⊥) sin θ (cos θ − u sin θ) = cos θv⊥ − v⊥u cos θ sin θ + (u × v⊥) cos θ sin θ 2 −(u × v⊥)u sin θ.

The term v⊥u is the multiplication of two vectors. Using equation (1.3.5.1), v⊥u =

17 v⊥ × u − v⊥ · u. Because v⊥ is perpendicular to u, the dot product of v⊥ and u is 0. Thus, v⊥u = v⊥ × u and the above equation becomes

2 2 cos θv⊥ − v⊥u cos θ sin θ + (u × v⊥) cos θ sin θ − (u × v⊥)u sin θ 2 2 = cos θv⊥ − (v⊥ × u) cos θ sin θ + (u × v⊥) cos θ sin θ − (u × v⊥)u sin θ 2   2 = cos θv⊥ + − (v⊥ × u) + (u × v⊥) cos θ sin θ − (u × v⊥)u sin θ.

The cross product is anti-commutative. This means that −(v⊥ × u) = u × v⊥. The term   −(v⊥ ×u)+(u×v⊥) now becomes (u×v⊥)+(u×v⊥) = 2(u×v⊥) so the above equation can now be written as

2   2 cos θv⊥ + − (v⊥ × u) + (u × v⊥) cos θ sin θ − (u × v⊥)u sin θ 2 2 = cos θv⊥ + (u × v⊥)(2 cos θ sin θ) − (u × v⊥)u sin θ 2 2 = cos θv⊥ − (u × v⊥)u sin θ + (u × v⊥)(2 cos θ sin θ). (2.2.3)

The product (u×v⊥)u can be simplified in the above equation. First, note that (u×v⊥) produces some vector that is perpendicular to both u and v⊥. Since (u × v⊥) is a vector, the product (u × v⊥)u can be expanded using equation (1.3.5.1): (u × v⊥)u = (u × v⊥) × u − (u × v⊥) · u. Because the vector produced by (u × v⊥) is perpendicular to u, then (u × v⊥) · u = 0. Thus, (u × v⊥)u = (u × v⊥) × u. Equation (2.2.3) becomes

2 2 cos θv⊥ − (u × v⊥)u sin θ + (u × v⊥)(2 cos θ sin θ) 2   2 = cos θv⊥ − (u × v⊥) × u sin θ + (u × v⊥)(2 cos θ sin θ). (2.2.4)   Using the identity (a × b) × c = −a(b · c) + b(a · c), the term (u × v⊥) × u can be further expanded:   (u × v⊥) × u = −u(v⊥ · u) + v⊥(u · u).

Since v⊥ is perpendicular to u, v⊥ · u = 0. Using the definition of the dot product, u · u 2 ◦ 2   becomes kuk cos 0 = 1 = 1. Thus, (u × v⊥) × u = v⊥ so equation (2.2.4) becomes

2   2 cos θv⊥ − (u × v⊥) × u sin θ + (u × v⊥)(2 cos θ sin θ) 2 2 = cos θv⊥ − v⊥ sin θ + (u × v⊥)(2 cos θ sin θ) 2 2 = (cos θ − sin θ)v⊥ + (u × v⊥)(2 cos θ sin θ).

Plugging in the identities cos2 θ−sin2 θ = cos 2θ and 2 cos θ sin θ = sin 2θ, the above equation becomes

2 2 (cos θ − sin θ)v⊥ + (u × v⊥)(2 cos θ sin θ) = cos 2θv⊥ + (u × v⊥) sin 2θ

= cos 2θv⊥ + sin 2θ(u × v⊥). (2.2.5)

It has just been shown that (cos θ + u sin θ)v⊥(cos θ − u sin θ) = cos 2θv⊥ + sin 2θ(u × v⊥), which is the second term in equation (2.2.1). Earlier it was shown that (cos θ +

18 u sin θ)vk(cos θ − u sin θ) = vk. Thus,

w = qvq∗ = (cos θ + u sin θ)v(cos θ − u sin θ)

= (cos θ + u sin θ)(vk + v⊥)(cos θ − u sin θ)

= (cos θ + u sin θ)vk(cos θ − u sin θ) + (cos θ + u sin θ)v⊥(cos θ − u sin θ)

= vk + cos 2θv⊥ + sin 2θ(u × v⊥). (2.2.6)

Showing that kwk = kvk w is supposed to be the image of the vector v after it has been rotated about u through an angle 2θ, so the length of w should be the same as the length of v. Squaring the norm of equation (2.2.6),

kwk2 = kqvq∗k2 2 = kvk + cos 2θv⊥ + sin 2θ(u × v⊥)k .

Since vk is perpendicular to v⊥ and u × v⊥, equation (A.1) (see Appendix) can be used to 2 split kvk + cos 2θv⊥ + sin 2θ(u × v⊥)k into three terms:

2 2 kwk = kvk + cos 2θv⊥ + sin 2θ(u × v⊥)k 2 2 2 = kvkk + k cos 2θv⊥k + k sin 2θ(u × v⊥)k 2 2 2 2 2 = kvkk + kv⊥k cos 2θ + k(u × v⊥)k sin 2θ.

◦ The of the cross product of u and v⊥ is given by (u×v⊥) = kukkv⊥k sin 90 = kv⊥k so

2 2 2 2 2 2 kwk = kvkk + kv⊥k cos 2θ + k(u × v⊥)k sin 2θ 2 2 2 2 2 = kvkk + kv⊥k cos 2θ + kkv⊥kk sin 2θ 2 2 2 2 2 = kvkk + kv⊥k cos 2θ + kv⊥k sin 2θ 2 2 2 2 = kvkk + kv⊥k (cos 2θ + sin 2θ).

Since cos2 2θ + sin2 2θ = 1, 2 2 2 kwk = kvkk + kv⊥k . Using equation (A.1) again,

2 2 2 kwk = kvkk + kv⊥k 2 = kvk + v⊥k = kvk2.

Taking the squareroot of both sides of the above equation,

kwk = kvk.

19 Thus, is has been shown that the length of w is the same as the length of v. In summary, the product qvq∗, where q = cos θ +u sin θ and q∗ = cos θ −u sin θ, produces a vector w that is the image of v rotated about some axis u through an angle of 2θ where both v and u pass through the origin.

2.3 Expanding w = qvq∗

Instead of multiplying qvq∗ = (cosθ + u)v(cosθ − u) out every time, it is easiest to multiply it out once. After that, the vectors v and u and the angle θ can be plugged in and w can be easily computed. Expanding w = qvq∗,

w = qvq∗ = (cosθ + u sin θ)v(cosθ − u sin θ) = (v cos θ + uv sin θ)(cosθ − u sin θ) = v cos2 θ − vu cos θ sin θ + uv cos θ sin θ − uvu sin2 θ = v cos2 θ +  − vu + uv cos θ sin θ − uvu sin2 θ. (2.3.1)

Using equation (1.3.5.1) to expand the product of two vectors, the term −vu + uv becomes

−vu + uv = −v × u − v · u + u × v − u · v = −u · v + v · u + u × v − v × u.

Since the dot product is commutative (v·u = u·v) and the cross product is anti-commutative (v × u = −u × v), the above equation becomes

−u · v + v · u + u × v − v × u = −u · v + u · v + u × v + u × v = 2(u × v).

Plugging −vu + uv = 2(u × v) into equation (2.3.1),

w = qvq∗ = v cos2 θ +  − vu + uv cos θ sin θ − uvu sin2 θ = v cos2 θ + 2(u × v) cos θ sin θ − uvu sin2 θ. (2.3.2)

Using equation (1.3.5.1) to expand the product of uvu,

uvu = (uv)u = (u × v − u · v)u = (u × v)u − (u · v)u = (u × v) × u − (u × v) · u − (u · v)u.

Note that the vector u × v is perpendicular to u, so the term (u × v) · u = 0. Using the identity (a × b) × c = −a(b · c) + b(a · c), (u × v) × u = −u(v · u) + v(u · u). The above

20 equation can now be simplied as

uvu = (u × v) × u − (u × v) · u − (u · v)u = −u(v · u) + v(u · u) − (u · v)u.

Recalling once again that the dot product is commutative (v · u = u · v) and since u · u = kuk2 cos 0◦ = 1,

uvu = −u(v · u) + v(u · u) − (u · v)u = −u(u · v) + v(1) − (u · v)u = −2(u · v)u + v.

Plugging uvu = −2(u · v)u + v into equation (2.3.2),

w = qvq∗ = v cos2 θ + 2(u × v) cos θ sin θ − uvu sin2 θ = v cos2 θ + 2(u × v) cos θ sin θ − (−2(u · v)u + v) sin2 θ = v cos2 θ + 2(u × v) cos θ sin θ + 2(u · v) sin2 θu − v sin2 θ = v(cos2 θ − sin2 θ) + 2(u × v) cos θ sin θ + 2(u · v) sin2 θu.

Using the identities cos 2θ = cos2 θ − sin2 θ and sin 2θ = 2 cos θ sin θ, the above equation can be simplied as

w = qvq∗ = v cos 2θ + (u × v) sin 2θ + 2(u · v)u sin2 θ. (2.3.3)

2.4 Rotation Algorithm

In order to rotate a vector v = vxi + vyj + vzk about an axis u = uxi + uyj + uzk through an angle 2θ where v and u pass through the origin, the following steps should be taken:

3 4 1. The vector v = vxi + vyj + vzk must be mapped from R to R :

v = 0 + v.

2. The axis of rotation must have length 1. If the desired axis of rotation, m = mxi + myj + mzk, does not have length 1, then m must be first normalized: m i + m j + m k m = x y z . p 2 2 2 mx + my + mz

3. Plug v, u, and 2θ into equation (2.3.3):

w = v cos 2θ + (u × v) sin 2θ + 2(u · v)u sin2 θ.

21 4. The resulting quaternion w = 0 + w should have a zero scalar part. The vector part of w is the image of v after it has been rotated about u through an angle of 2θ.

2.5 Examples

The following examples show how to rotate a vector v about some axis u through an angle of 2θ. These examples make use of equation (2.3.3) to find the rotated vector w. However, w could also be found directly by multiplying qvq* out using quaternion multiplication.

2.5.1 Example 1 Rotate the vector v = i about the j-axis through an angle of 120◦. By equation (2.3.3), the rotated vector w will be

w = v cos 2θ + (u × v) sin 2θ + 2(u · v)u sin2 θ where 1 cos 2θ = cos 120◦= − √2 3 sin 2θ = sin 120◦ = − √2  32 3 sin2 θ = sin2 60◦ = = . 2 4 It is easiest to calculate the dot and cross product of u = j and v = i first and then plug their corresponding values into w. The dot product of u and v is given by

u · v = j · i = 0(1) + 1(0) + 0(0) = 0 and the cross product of u and v is

u × v = j × i = [1(0) − 0(0)]i + [0(1) − 0(0)]j + [0(0) − 1(1)]k = −k.

Plugging the corresponding dot and cross products into w,

w = v cos 2θ + (u × v) sin 2θ + 2(u · v)u sin2 θ √  1   3   32 = i − + − k − + 2 0 j 2 √ 2 4 1 3 = − i + k. 2 2

22 ◦ As shown in Figure√ 2.2, the vector v = i has been rotated 120 about the j-axis into the 1 3 vector w = − 2 i + 2 k. z

√ 1 3 w = (− 2 , 0, 2 ) 7     ◦  120 - y u = (0, 1, 0)

©v = (1, 0, 0)

x

√ ◦ 1 3 Figure 2.2: The vector v = i is rotated 120 clockwise about j into w = − 2 i + 2 k.

2.5.2 Example 2 Rotate the vector v = 3i − 5j + 2k 90◦ about the axis (i + j). The first step is to normalize the axis of rotation: 1 1 0 u = √ i + √ i + √ k 12 + 12 + 02 12 + 12 + 02 12 + 12 + 02 1 1 = √ i + √ j. 2 2 Now, equation (2.3.3) can be used to find the rotated vector, w,

w = v cos 2θ + (u × v) sin 2θ + 2(u · v)u sin2 θ

where

cos 2θ = cos 90◦ = 0 sin 2θ = sin 90◦ = 1  1 2 1 sin2 θ = sin2 45◦= √ = . 2 2

23 Plugging the above trig identities in,

w = v cos 90◦ + (u × v) sin 90◦ + 2(u · v)u sin2 45◦ 1 = v(0) + (u × v)(1) + 2(u · v)u 2 = (u × v) + (u · v)u.

Again, it is easiest to calculate the dot and cross products of u = √1 i+ √1 j and v = 3i−5j+2k 2 2 seperately and then plug them in to find w. First, the dot product of u and v is  1 1    u · v = √ i + √ j · 3i − 5j + 2k 2 2 1   1     = √ 3 + √ − 5 + 0 2 2 2 3 − 5 = √ 2 −2 = √ √2 = − 2.

The cross product of u and v is  1 1    u × v = √ i + √ j × 3i − 5j + 2k 2 2 h 1    i h   1  i h 1   1  i = √ 2 − 0 5 i + 0 3 − √ 2 j + √ − 5 − √ 3 k 2 2 2 2 2 2 −5 − 3 = √ i − √ j + √ k √2 √ 2 √ 2 = 2i − 2j − 4 2k.

The dot and cross product can now be plugged in to solve for w,

w = (u × v) + (u · v)u √ √ √   √  1 1  = 2i − 2j − 4 2k + − 2 √ i + √ j √ √ √ 2 2 = ( 2i − 2j − 4 2k) + (−i − j) √ √ √ = (−1 + 2)i − (1 + 2)j − 4 2k.

◦ As shown in Figure 2.3, the√ vector v = 3√i − 5j + 2√k has been rotated 90 about the axis i + j into the vector w = (−1 + 2)i − (1 + 2)j − 4 2k.

24 z

X QXX y C Q XX C Q XXz u = (1, 1, 0) Q C Q C Q C Qs v = (3, −5, 2) C x C C C C C √ √ √ CW w = (−1 + 2, −1 − 2, −4 2)

◦ Figure 2.3:√ The vector v√= 3i − 5√j + 2k is rotated 90 clockwise about the axis (i + j) into w = (−1 + 2)i − (1 + 2)j − 4 2k.

25 Chapter 3

Rotating a Coordinate Frame S into Another Coordinate Frame S0

There are many real world situations where it is necessary to know how two coordinate frames, S and S0, are related. Once the relationships between any two coordinate frames are known, any vector v in frame S can be rotated to its corresponding vector v0 in S0. In this chapter, a method for rotating a vector v in S into v0 in S’ will be presented. The method involves rotating a vector through two different axes and two different angles. Initially, this method will be presented without the use of quaternions. An algorithm that specially uses quaternions to carry out the two rotations will then be presented. Finally, an example using quaternions will be presented.

3.1 Generalized Two Axis-Angle Rotation Method

In order to use this method, the vectors v = vxi + vyi + vzk and s = sxi + syi + szk must 0 0 0 0 be known and defined in the S frame. Their corresponding vectors, v = vxi + vyi + vzk and 0 0 0 0 0 0 0 s = sxi + syi + szk, in the S frame must also be known. The vectors v and s must be defined with respect to the S frame.

1. Find the midpoint vector of v and v0:

0 0 0 v + v  vy + v  v + v  m = x x i + y j + z z k. 2 2 2

◦ ∗ ∗ ∗ ∗ 2. Rotate s 180 about m into s = sxi + syj + szk. 3. Find the angle of φ that will take s∗ into s0 when s is rotated about v0.

0 0 0 0 4. Any vector r = rxi + ryj + rzk can now be rotated into r = rxi + ryj + rzk by first rotating r 180◦ degrees about m followed by a rotation φ degrees about v0.

26 3.2 Two Axis-Angle Rotation Method Using Quater- nions

1. Define v = vxi + vyi + vzk and s = sxi + syi + szk to be any two vectors in the S frame. 0 0 0 0 0 0 0 0 Let v = vxi + vyi + vzk and s = sxi + syi + szk be the corresponding vectors in the S0 frame.

2. Find the midpoint vector of v and v0:

0 0 0 v + v  vy + v  v + v  m = x x i + y j + z z k. 2 2 2 Since m is the axis of the first rotation, it must be normalized: m i + m j + m k m = x y z . p 2 2 2 mx + my + mz

◦ ∗ ∗ ∗ ∗ 3. Equation (2.3.3) can be used to rotate s 180 about m into s = sxi + syj + szk:

s∗ = s cos 2θ + (m × s) sin 2θ + 2(m · s)m sin2 θ = s cos 180◦ + (m × s) sin 180◦ + 2(m · s)m sin2(90◦).

Plugging in cos 180◦ = −1, sin 180◦ = 0, and sin 90◦ = 1, the above equation becomes

s∗ = s(−1) + (m × s)(0) + 2(m · s)m(1)2 = −s + 2(m · s)m. (3.2.1)

When rotating s 180◦ about m, it is easiest to use the equation above.

4. Before the second rotation about v0 can be performed, v0 must first be normalized:

0 0 0 0 vxi + vyj + vzk v = q . 0 2 0 2 0 2 vx + vy + vz

5. Finding the angle φ that will take s into s0 when s is rotated about v0 can be quite difficult. The code provided in Appendix B uses a brute force method to estimate the angle. First, the code sets the angle to some minimum value and rotates the angle s∗ about v0 through that angle. It then compares each component of the rotated s∗ and s0 to see whether or not they are equal. If they are not equal, the angle is increasing by a amount and the comparison is performed again. This test is repeated until some φ is found that rotates s∗ into s0 when rotated about v0.

0 0 0 0 6. Any vector r = rxi + ryj + rzk can now be rotated into r = rxi + ryj + rzk by first rotating r 180◦ degrees about m followed by a rotation φ degrees about v0:

27 (a) r∗ = −r + 2(m · r)m 0 ∗ 0 ∗ 0 ∗ 0 2 φ (b) r = r cos φ + (v × r ) sin φ + 2(v · r )v sin 2

3.2.1 Example Let v =< 1, 1, 1 > and s =< 0, 1, 0 > in the S frame. The corresponding vectors in the S0 frame are v0 =< −1, 1, 1 > and s0 =< −1, 0, 0 >. The midpoint vector, m is found by adding the corresponding components of v and v0 together and dividing by 2: 1 − 1 1 + 1 1 + 1 m = < , , > 2 2 2 = < 0, 1, 1 > .

Since m and v0 are the axes of rotation, both must be normalized: 1 1 m = < 0, √ , √ > 02 + 12 + 12 02 + 12 + 12 1 1 = < 0, √ , √ > 2 2

−1 1 1 v0 = < , , > p(−1)2 + 12 + 22 p(−1)2 + 12 + 22 p(−1)2 + 12 + 22 1 1 1 = < −√ , √ , √ > . 3 3 3 Now, s =< 0, 1, 0 > can be rotated 180◦ about m using equation (3.2.1) to find that

s∗ = −s + 2(m · s)m  1 1  1 1 = − < 0, 1, 0 > +2 < 0, √ , √ > · < 0, 0, 1 > < 0, √ , √ > 2 2 2 2  1 1  1 1 = − < 0, 1, 0 > +2 0 ∗ 0 + √ ∗ 0 + √ ∗ 1 < 0, √ , √ > 2 2 2 2  1  1 1 = − < 0, 1, 0 > +2 √ < 0, √ , √ > 2 2 2 = − < 0, 1, 0 > + < 0, 1, 1 > = < 0, 0, 1 > .

At this point, the angle 2φ must be found that will rotate s∗ into s0 about the normalized v0. Using the code provided in Appendix B, 2φ is found to be 240◦. It will now be shown that 2φ does indeed rotate s∗ into s0: φ s0 = s∗ cos φ + (v0 × s∗) sin φ + 2(v0 · s∗)v0 sin2 2 = s∗ cos 240◦ + (v0 × s∗) sin 240◦ + 2(v0 · s∗)v0 sin2 120◦

28 The cross product of v0 and s∗ is 1 1 1 v0 × s∗ = < −√ , √ , √ > × < 0, 0, 1 > 3 3 3  1    1    1    1    1    1   = < √ 1 − √ 0 , √ 0 − − √ 1 , − √ 0 − √ 0 > 3 3 3 3 3 3 1 1 = < √ , √ , 0 > . 3 3 The dot product of v0 and s∗ is 1 1 1 v0 · s∗ = < −√ , √ , √ > · < 0, 0, 1 > 3 3 3  1    1    1   = − √ 0 + √ 0 + √ 1 3 3 3 1 = √ . 3 √ Plugging in v0 × s∗ =< √1 , √1 , 0 >, v0 · s∗ = √1 , cos 240◦ = − 1 , sin 240◦ = − 3 , and 3 3 3 2 2 √ 2 2 ◦  3  3 0 sin 120 = − 2 = 4 , the equation for s becomes

s0 = s∗ cos 240◦ + (v0 × s∗) sin 240◦ + 2(v0 · s∗)v0 sin2 120◦ √  1 1 1  3  1  1 1 1 3 = < 0, 0, 1 > − + < √ , √ , 0 > − + 2 √ < −√ , √ , √ > 2 3 3 2 3 3 3 3 4 1 1 1 3 1 1 1 = < 0, 0, − > + < − , − , 0 > + < − , , > 2 2 2 2 3 3 3 1 1 1 1 1 1 = < − , − , − > + < − , , > 2 2 2 2 2 2 = < −1, 0, 0 > .

Rotating s∗ =< 0, 0, 1 > 240◦ about v0 =< − √1 , √1 , √1 > does indeed rotate s∗ into 3 3 3 s0 =< −1, 0, 0 >. Any vector r in the S frame can now be rotated into the S0 frame by first rotating r 180◦ degrees about m =< 0, √1 , √1 > into r∗ and then rotating r∗ 240◦ about v0 =< 2 2 − √1 , √1 , √1 >. 3 3 3 The vector r =< 2, 2, 0 > will now be rotated into the S0 frame. First, r∗ is found to be

r∗ = −r + 2(m · r)m  1 1  1 1 = − < 2, 2, 0 > +2 < 0, √ , √ > · < 2, 2, 0 > < 0, √ , √ > 2 2 2 2  2  1 1 = < −2, −2, 0 > +2 √ < 0, √ , √ > 2 2 2 = < −2, −2, 0 > + < 0, 2, 2 > = < −2, 0, 2 > .

29 The vector r∗ =< −2, 0, 2 > must now be rotated 240◦ about v0 =< − √1 , √1 , √1 > to 3 3 3 find r0. Thus, φ r0 = r∗ cos φ + (v0 × r∗) sin φ + 2(v0 · r∗)v0 sin2 2 = r∗ cos 240◦ + (v0 × r∗) sin 240◦ + 2(v0 · s∗)v0 sin2 120◦

The cross product of v0 and r∗ is 1 1 1 v0 × r∗ = < −√ , √ , √ > × < −2, 0, 2 > 3 3 3  1    1    1    1    1    1   = < √ 2 − √ 0 , √ − 2 − − √ 2 , − √ 0 − √ − 2 > 3 3 3 3 3 3 2 2 2 2 = < √ , −√ + √ , √ > 3 3 3 3 2 2 = < √ , 0, √ > . 3 3 The dot product of v0 and r∗ is 1 1 1 v0 · s∗ = < −√ , √ , √ > · < −2, 0, 2 > 3 3 3  1    1    1   = − √ − 2 + √ 0 + √ 2 3 3 3 2 2 = √ + √ 3 3 4 = √ . 3 √ Plugging v0 × r∗ =< √2 , 0, √2 >, v0 · s∗ = √4 , cos 240◦ = − 1 , sin 240◦ = − 3 , and 3 3 3 2 2 √ 2 2 ◦  3  3 0 sin 120 = − 2 = 4 , the equation for r becomes

r0 = r∗ cos 240◦ + (v0 × r∗) sin 240◦ + 2(v0 · s∗)v0 sin2 120◦ √  1 2 2  3  4  1 1 1 3 = < −2, 0, 2 > − + < √ , 0, √ > − + 2 √ < −√ , √ , √ > 2 3 3 2 3 3 3 3 4 = < 1, 0, −1 > + < −1, 0, −1 > + < −2, 2, 2 > = < 0, 0, −2 > + < −2, 2, 2 > = < −2, 2, 0 > .

The vector r =< 2, 2, 0 > in the S frame has now been rotated into r0 =< −2, 2, 0 > in the S0 frame.

30 Appendix A

Some Useful Equations

A.1 The Dot and Cross Product of Two Vectors

The dot and cross product of two vectors are two basic operations of . The definition of each is given below.

A.1.1 The Dot Product

For any two vectors v = vxi + vyj + vzk and w = wxi + wyj + wzk, the dot product is defined as v · w = vxwx + vywy + vzwz. (A.1) Note that the dot product of two vectors results in a scalar. The dot product is also commutative; i.e. v · w = w · v.

A.1.2 The Cross Product

For any two vectors v = vxi + vyj + vzk and w = wxi + wyj + wzk, the cross product is defined as v × w = (vywz − vzwy)i + (vzwx − vxwz)j + (vxwy − vywx)k. (A.1) The cross product of two vectors always results in vector. The dot product of two vectors is anticommutative; i.e. (v × w) = −(w × v).

A.2 Useful Relationships

Some of the proofs in my thesis reference equations but do not bother to explain why the given equation is true. In these situations, how the equation was derived was not pertinent to the proof at hand. This section shows how some of these equations were derived.

31 A.2.1 Proof: If v is ⊥ to w, kv + wk2 = kvk2 + kwk2

Let v = vxi + vyj + vzk be a vector that is perpendicular to the vector w = wxi + wyj + wzk. By definition, q 2 2 2 kv + wk = (vx + wx) + (vy + wy) + (vz + wz) q 2 2 2 2 2 2 = (vx + wx) + 2vxwx + (vy + wy) + 2vywy + (vz + wz ) + 2vzwz q 2 2 2 2 2 2 = (vx + wx + vy + wy + vz + wz ) + 2(vxwx + vywy + vzwz).

By definition, v · w = vxwx + vywy + vzwz. Since v is perpendicular to w, v · w = 0. The above equation now becomes q 2 2 2 2 2 2 kv + wk = (vx + wx + vy + wy + vz + wz ) + 2(vxwx + vywy + vzwz) q 2 2 2 2 2 2 = (vx + vy + vz ) + (wx + wy + wz ).

2 2 2 2 2 2 2 2 Note that kvk = (vx + vy + vz ) and kwk = (wx + wy + wz ) so q 2 2 2 2 2 2 kv + wk = (vx + vy + vz ) + (wx + wy + wz ) = pkvk2 + kwk2.

Squaring both sides of the equation, it is found that

kv + wk2 = kvk2 + kwk2. (A.1)

Equation (A.1) hold for any vectors v = vxi + vyj + vzk and w = wxi + wyj + wzk that are perpendicular to one another.

32 Appendix B

Source Code for the Two Axis-Angle Frame Rotation Method

Below the code is listed for the the axis-angle frame rotation method described in Chapter 3. This code was written using Microsoft Visual Studio 2008 Professional Edition. The WildMagic5.4 engine was also used to write code. WildMagic is available for free online at http://www.geometrictools.com. The following program will rotate any vector r in a frame S into r0 in the S0 frame. The program requires the user to know the coordinates of two different vectors in both the S and S0 frames. Once these coordiantes are known, the program will find an r0 that corresponds to each r the user inputs.

B.1 C++ Code Listing

1 /∗ TwoAxisAngleRotationMethod.cpp 2 ∗ 3 ∗ Created by: Jeanette Schofield 4 ∗ itzjeanette@yahoo .com 5 ∗ Advisor: Dr. Wayne Keith, McMurry University 6 ∗ k e i t h . wayne@mcm . edu 7 ∗ Date Created: 4/8/2011 8 ∗ 9 ∗ Program Description: 10 ∗ Once two different vectors, v and s, are known in both the S and S’ frame, any vector r 11 ∗ can be rotated from frame S to S’. 12 ∗ 13 ∗ This method details a way to rotate between the S and S’ frame using only two rotations. 14 ∗ 15 ∗ Vectors in the S Frame: 16 ∗ v − any arbitary vector in the S frame 17 ∗ vPRIME − the vector that corresponds to v in the S’ frame defined with respect to the S frame. 18 ∗ For example, if v=(0,1,0) and the S frame is related to the S’ frame by a 120 degree rotation about the

33 19 ∗ x−axis, then v’=(0,0,1). 20 ∗ m − midpoint vector of v and v’ (m=(1/2) ∗( v+v ’) 21 ∗ s − a second arbitrary vector in the S frame 22 ∗ sPRIME − the vector that corresponds to s in the S’ frame defined with respect to the S frame. 23 ∗ r − any vector in the S frame 24 ∗ rPRIME − the vector that corresponds to r in the S’ frame defined with respect to the S frame. 25 ∗/ 26 27 #include ” s t d a f x . h” 28 #include /∗ Needed for various math (sin, cos, pow , . . . ) . ∗/ 29 #include /∗ Used to print output to the screen and to get input from the user. ∗/ 30 #include /∗ Used to format output to the screen. ∗/ 31 /∗ Files from WildMagic5.4 engine. Allow for the use of Vector3 and Quaternion objects. ∗/ 32 #include ”Wm5Matrix3. h” 33 #include ”Wm5Quaternion.h” 34 35 /∗ Constants ∗/ 36 #define PI 3.14159265 37 38 /∗ Functions ∗/ 39 double degToRad ( double degree ) ; 40 double radToDeg ( double radian ) ; 41 double getAngle(Wm5:: Vector3 rotationAxis , Wm5:: Vector3 vecStart , Wm5:: Vector3 vecFinish); 42 void setVector(Wm5:: Vector3 ∗ vec ) ; 43 void setMidpointVector(Wm5:: Vector3 ∗ midpointVec , Wm5:: Vector3 < double> vector1 , Wm5:: Vector3 vector2 ) ; 44 void normalizeVector(Wm5:: Vector3 ∗ vector ) ; 45 Wm5: : Vector3 rotate180aboutm(Wm5:: Vector3 rotAxis , Wm5:: Vector3 v e c s ) ; 46 void printVector(Wm5:: Vector3 vector ) ; 47 48 int tmain ( int argc , TCHAR∗ argv [ ] ) 49 { 50 /∗ Creates a char that will be used later for loop control. ∗/ 51 char YorN ; 52 53 /∗ Creates all the vectors that will be used in the program. Each vector is initialized to <0,0,0>.∗/ 54 Wm5: : Vector3 vec v(0,0,0), vec vPRIME(0,0,0) , vec m ( 0 , 0 , 0 ) ; 55 Wm5: : Vector3 v e c s(0,0,0), vec sPRIME(0,0,0) ; 56 Wm5: : Vector3 v e c r(0,0,0), vec rPRIME(0,0,0) ; 57 58 /∗ Sets the number of decimal places that will be output. ∗/ 59 std : : cout << std : : f i x e d << std:: setprecision(3); 60 61 std : : cout << ”Enter the coordinates for vector v.” << std : : endl ; 62 setVector (& vec v ) ; 63 std : : cout << ”Enter the coordinates for vector v’.” << std : : endl ;

34 64 setVector (& vec vPRIME ) ; 65 std : : cout << ”Enter the coordinates for vector s.” << std : : endl ; 66 setVector (& v e c s ) ; 67 std : : cout << ”Enter the coordinates for vector s ’.” << std : : endl ; 68 setVector (& vec sPRIME ) ; 69 70 /∗ Create a midpoint vector. ∗/ 71 setMidpointVector(& vec m , vec v , vec vPRIME ) ; 72 73 /∗ Normalize m and v’. ∗/ 74 normalizeVector(& vec m ) ; 75 normalizeVector(& vec vPRIME ) ; 76 77 /∗ Rotate s 180 degrees about m. ∗/ 78 v e c s = rotate180aboutm(vec m , v e c s ) ; 79 80 /∗ Calculate the anglePHI that will rotate s into s PRIME when s is rotated about vPRIME. ∗/ 81 double anglePHI = getAngle(vec vPRIME , vec s , vec sPRIME ) ; 82 83 /∗ Create a quaternion with an axis of rotation of vPRIME and rotate through anglePHI. ∗/ 84 Wm5: : Quaternion axis vPRIME anglePHI(vec vPRIME, anglePHI); 85 86 /∗ 87 ∗ Start main loop that allows a user to input any vector in the S frame and find out its 88 ∗ coordinates with respect to the S’ frame. 89 ∗/ 90 do { 91 /∗ Get r . ∗/ 92 std : : cout << ”Enter the coordinates of any vector r in the S frame . ” << std : : endl ; 93 setVector (& v e c r ) ; 94 95 /∗ Rotate r 180 degrees about m. ∗/ 96 v e c r = rotate180aboutm(vec m , v e c r ) ; 97 /∗ Rotate r PHI degrees about v’. ∗/ 98 vec rPRIME = axis vPRIME anglePHI.Rotate(vec r ) ; 99 100 /∗ Prints the coordinates of r’ to the screen. ∗/ 101 std : : cout << ” r ’ : ” ; 102 printVector ( vec rPRIME ) ; 103 std : : cout << std : : endl ; 104 105 std : : cout << std : : endl << ”Do you want to input another vector ? (Y/N) : ” ; 106 std : : c i n >> YorN ; 107 std : : c i n . i g n o r e (1000 , ’ \n ’ ) ; 108 std : : cout << std : : endl ; 109 110 /∗ If the user has entered a lowercase ’y’ or ’n,’ converts it to ’Y’ or ’N’. ∗/ 111 YorN = toupper (YorN) ;

35 112 113 /∗ This while loop checks to make the input from the user is of the correct form. ∗/ 114 while ( YorN != ’Y’ && YorN != ’N’) 115 { 116 std : : cout << ”Please enter a Y or N: ”; 117 std : : c i n >> YorN ; 118 std : : c i n . i g n o r e (1000 , ’ \n ’ ) ; 119 120 YorN = toupper (YorN) ; 121 } 122 123 } while (YorN == ’Y’); 124 125 /∗ Clear output buffer (allows user to just press ”Enter” once to exit program ) . ∗/ 126 std : : cout << std : : endl << ”Press Enter to exit program.” << std : : endl ; 127 std : : c i n . i g n o r e (1000 , ’ \n ’ ) ; 128 getchar ( ) ; 129 130 return 0 ; 131 } 132 133 /∗ Converts degrees to radians. ∗/ 134 double degToRad ( double degree ) 135 { 136 return degree ∗( PI /180) ; 137 } 138 139 /∗ Converts radians to degrees ∗/ 140 double radToDeg ( double radian ) 141 { 142 return radian ∗(180/ PI ) ; 143 } 144 145 /∗ Figures out the angle necessary to rotate vecStart into vecFinish about rotationAxis. ∗/ 146 double getAngle(Wm5:: Vector3 rotationAxis , Wm5:: Vector3 vecStart , Wm5:: Vector3 vecFinish ) 147 { 148 const double step = 0.0001; 149 const double delta = 0.001; 150 double angle = step; 151 152 Wm5: : Vector3 newVec(vecStart); 153 154 while ((abs(vecFinish.X() − newVec.X() ) > d e l t a ) | | (abs(vecFinish.Y() − newVec.Y() ) > d e l t a ) | | (abs(vecFinish.Z() − newVec.Z()) > d e l t a )) 155 { 156 Wm5: : Quaternion q qConj(rotationAxis , angle); 157 newVec = q qConj.Rotate(vecStart); 158 159 angle += step ;

36 160 } 161 162 return angle ; 163 } 164 165 /∗ Gets x, y, and z components from the user for a given vector. ∗/ 166 void setVector(Wm5:: Vector3 ∗ vec ) 167 { 168 double x , y , z ; 169 170 /∗ Begin entering data. ∗/ 171 std : : cout << ”x : ” ; 172 std : : c i n >> x ; 173 std : : cout << ”y : ” ; 174 std : : c i n >> y ; 175 std : : cout << ”z : ” ; 176 std : : c i n >> z ; 177 std : : cout << std : : endl ; 178 179 vec−>X( ) = x ; 180 vec−>Y( ) = y ; 181 vec−>Z ( ) = z ; 182 } 183 184 /∗ Sets midpointVec to be a vector that is halfway in between vector 1 and v e c t o r 2. ∗/ 185 void setMidpointVector(Wm5:: Vector3 ∗ midpointVec , Wm5:: Vector3 < double> vector1 , Wm5:: Vector3 vector2 ) 186 { 187 midpointVec−>X() = ( vector1.X() + vector2.X() ) / 2; 188 midpointVec−>Y() = ( vector1.Y() + vector2.Y() ) / 2; 189 midpointVec−>Z() = ( vector1.Z() + vector2.Z() ) / 2; 190 } 191 192 /∗ Normalizes a vector by taking each component of a vector v= and diviing it by the squareroot of 193 ∗ ( v x ) ˆ2+( v y ) ˆ2+( v z ) ˆ2. 194 ∗/ 195 void normalizeVector(Wm5:: Vector3 ∗ vector ) 196 { 197 double length = sqrt(pow(vector −>X(), 2) + pow(vector −>Y(), 2) + pow( vector −>Z ( ) , 2) ) ; 198 199 vector −>X() /= length; 200 vector −>Y() /= length; 201 vector −>Z() /= length; 202 } 203 204 /∗ This function returns vec v after it has been rotated 180 degrees about rotAxis . ∗/ 205 Wm5: : Vector3 rotate180aboutm(Wm5:: Vector3 rotAxis , Wm5:: Vector3 vec v ) 206 { 207 return −vec v +(2∗ rotAxis.Dot(vec v ) ) ∗ rotAxis ;

37 208 } 209 210 /∗ Prints an vector out in the form ” vector ) 212 { 213 std : : cout << ”<” << vector .X( ) << ”,” << vector .Y( ) << ”,” << vector .Z() << ”>”; 214 }

38