How do you rotate a vector by a unit quaternion?

Given a 3-variable right-handed vector v that is a translation measured in local space and a unit quaternion representing an orientation from local to world space, how do you use the quaternion to rotate the vector from local space to world space?

For ease of use, the values are:

Vector v = $[1.0, 0.0, 0.0]$

Quaternion $q = [W: 0.7071068, X: 0, Y: 0.7071068, Z: 0]$, which I understand to be a rotation $90^\circ (\frac{\pi}{2})$ around the $Y$-axis and which converts from the local space to the world space. (That is, the resulting vector is $[0.0, 0.0, 1.0]$, and if this was the nose of a spaceship, it'd be pointing to the right in world coordinates)

Thanks.


Solution 1:

To answer the question simply, given:

 P  = [0, p1, p2, p3]  <-- point vector
 R  = [w,  x,  y,  z]  <-- rotation
 R' = [w, -x, -y, -z]

For the example in the question, these are:

 P  = [0, 1, 0, 0]
 R  = [0.707, 0.0,  0.707, 0.0]
 R' = [0.707, 0.0, -0.707, 0.0]

You can calculate the resulting vector using the Hamilton product H(a, b) by:

 P' = RPR'
 P' = H(H(R, P), R')

Performing the calculations:

        H(R, P)      = [0.0, 0.707, 0.0, -0.707]
 P' = H(H(R, P), R') = [0.0, 0.0,   0.0, -1.0  ]

Thus, the example above illustrates a rotation of 90 degrees about the y-axis for the point (1, 0, 0). The result is (0, 0, -1). (Note that the first element of P' will always be 0 and can therefore be discarded.)

For those unfamiliar with quaternions, it's worth noting that the quaternion R may be determined using the formula:

a = angle to rotate
[x, y, z] = axis to rotate around (unit vector)

R = [cos(a/2), sin(a/2)*x, sin(a/2)*y, sin(a/2)*z]

See here for further reference.

Solution 2:

You seem to be having a good deal of trouble with this, over several questions. At the same time, I am confident that you will get no satisfying answers as long as you stick with the terminology you are using. Maybe on the original stack overflow site, aimed at programmers.

Please read this: http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

and this: http://en.wikipedia.org/wiki/Quaternions

To answer not much more than your question, any quaternion is an expression $$ q = w + x \; \mathbf{i} + y \; \mathbf{j} + z \; \mathbf{k}.$$ where the multiplication rules use $$ \mathbf{i}^2 = \mathbf{j}^2 = \mathbf{k}^2 = \mathbf{i} \mathbf{j}\mathbf{k} = -1, $$ and consequences of those, see wikipedia as I said. Any quaternion $ q = w + x \; \mathbf{i} + y \; \mathbf{j} + z \; \mathbf{k}$ has a conjugate, that on wikipedia is written $q^\ast,$ given by $$ q^\ast = w - x \; \mathbf{i} - y \; \mathbf{j} - z \; \mathbf{k}.$$

The "norm" of the quaternion $q$ is exactly $$ \parallel q \parallel^2 = w^2 + x^2 + y^2 + z^2 = q q^\ast = q^\ast q$$

A quaternion $q$ is called a "unit" quaternion when $$ w^2 + x^2 + y^2 + z^2 = 1. $$

A quaternion is called "pure" or a vector in 3-space when $ w = 0,$ so a vector in 3-space is $$ v = v_1 \; \mathbf{i} + v_2 \; \mathbf{j} + v_3 \; \mathbf{k} $$ I have no idea what engineers and programmers call these concepts. You are asking mathematicians.

Given two quaternions, the norm of the product is the product of the norms.

The "real part" (the $w$) of the product of two quaternions $pq$ is the same as the "real part of $qp.$

So, what happens when I take a unit quaternion $q$ and a "pure" quaternion $v,$ and calculate $$ p = q^\ast v q.$$

Well, we have $$\parallel p \parallel = 1 \cdot \parallel v \parallel \cdot 1 = \parallel v \parallel $$

But as to the "real part," we begin with $$ \Re v = 0,$$

then $$ \Re q^\ast (v q) = \Re (v q) q^\ast = \Re v (q q^\ast) = \Re v = 0. $$

So $ p = q^\ast v q$ is another pure quaternion, another "vector," the same length as $v,$ but rotated from where it was.

That's enough for a start.

Solution 3:

The answer above from Doug has been extraordinarily helpful to me. What is difficult with many explanations of Quaternions and their applications is that the notation is difficult to read and understand for learners.

This site from Mathworks explains further how to multiply Quaternions: http://www.mathworks.com/help/aerotbx/ug/quatmultiply.html?requestedDomain=www.mathworks.com

I also tend to think and learn in terms of code. Here is what I was ultimately able to piece together in python:

def quaternion_mult(q,r):
    return [r[0]*q[0]-r[1]*q[1]-r[2]*q[2]-r[3]*q[3],
            r[0]*q[1]+r[1]*q[0]-r[2]*q[3]+r[3]*q[2],
            r[0]*q[2]+r[1]*q[3]+r[2]*q[0]-r[3]*q[1],
            r[0]*q[3]-r[1]*q[2]+r[2]*q[1]+r[3]*q[0]]

def point_rotation_by_quaternion(point,q):
    r = [0]+point
    q_conj = [q[0],-1*q[1],-1*q[2],-1*q[3]]
    return quaternion_mult(quaternion_mult(q,r),q_conj)[1:]

print(point_rotation_by_quaternion([1, 0, 0],[0.7071203316249954, 0.0, 0.7071203316249954, 0.0])

Solution 4:

I can't be sure whether this will be helpful, but it's been helpful to me.

A quaternion rotation does two complex rotations at the same time, in two different complex planes.

Turn your 3-vector into a quaternion by adding a zero in the extra dimension. [0,x,y,z]. Now if you multiply by a new quaternion, the vector part of that quaternion will be the axis of one complex rotation, and the scalar part is like the cosine of the rotation around that axis. This is the part you want, for a 3D rotation.

But the quaternion multiplication also gives you a complex rotation between the fourth dimension and the axis, the same amount of rotation as the rotation you wanted. You don't want this. It gives you a nonzero fourth dimension and joggles up the axis of rotation.

So what you do, is first you multiply by a quaternion that gives you half the rotation you want, and also the extra stuff you don't want. Then you multiply on the other side by a quaternion that gives you the other half of the rotation you want, and cancels away the extra stuff.

Sometimes you don't need the double rotation. If you want to calculate an elliptical orbit, you can use a quaternion set to the axis that makes the ellipse look like a circle. Then you can calculate the position along the orbit at any fraction of a complete cycle. Make the fourth dimension be the cosine, and the sum of the others is the sine. Do one quaternion multiplication and you rotate the circular component just that far around, and the quaternion axis gives you the rest of the location, and the fourth dimension says how far ahead or behind you are in time relative to that fraction of a full orbit. All in one operation.