2/12/12

Quaternions I: Overview

What are quaternions? Let's start with what quaternions once were: an attempt to extend Complex numbers to three dimensions.

Back when the geometric interpretation of complex numbers as a plane was reasonably fresh, Sir William Hamilton became interested in finding a system of algebra that would allow him to express three dimensional space in the same way. To do this, Hamilton needed a way to add and multiply points in 3 dimensional space together.

Addition came easy. Picking some arbitrary origin and axes 1, i, and j to work with, Hamilton just defined (a + bi +cj) + (d + ei +fj ) = ( a + d ) + ( b + e )i + ( c  + f )j.

Multiplication, though, was a problem. Assuming that these new quantities were distributive, Hamilton needed to define ij in such a way that various other properties still held. Despite his best efforts, he couldn't do it.

Still, Hamilton found a way forward. By adding a fourth axis, k, Hamilton could define that i^2 = j^2 = k^2 = ijk = -1 and everything worked nicely. In Hamilton's view, he'd found the best way to represent 3D space. He called the new construct a quaternion and spent the rest of his life promoting their use in physics and chemistry.

Eventually, though, quaternions dropped out of style. They were elegant in some situations, but unintuitive and clumsy in others, and the vector math we have today often suited physicists better. For the better part of a century, quaternions went unused outside of some mathematics.

Clearly, though, quaternions are useful for something, or I wouldn't be writing about them. To understand why they're so neat, though, we need to change tack and talk about something else: gimbals.

Spinning Gimbals, courtesy of Wikipedia
A gimbals are a set of rings attached by pivots. Usually, as in the image above, there are three rings:  an outer gimbal, which can rotate freely around, for instance, vertical with respect to the enviroment, and two inner gimbals, each is mounted at right angles with respect to the axis of the last gimbal.

The purpose of all this is generally to allow some sort of equipment (often a flywheel, whose inertia will keep it level) to rotate freely. If we want to think about the state of the gimbal mathematically, we can assign one angle to each set of joints, for a total of three numbers. This makes sense, of course, because we're trying to capture a three dimensional space (the surface of a sphere accounts for two dimensions, plus we need to be able to rotate around each point we choose).

Mathematically, we also might like to be able to think of rotations as something you can combine, one after another. In order to do this, we have so called Euler Angles: 3x3 matrices which we can combine through matrix multiplication. We also can apply this matrix to a three dimensional vector.
As you'll see later in this article

In most situations, this whole setup works nicely, and we're done. There is one time when it fails, however. Going back to the gimbals, it's possible for two of the axes to line up, constraining the rotation! In other words, while the gimbal usually provides three degrees of freedom, at two specific points, it only provides two. Euler Angles have the same problem.

This is a big enough problem that there is a mechanical solution for actual gimbals: add a fourth constrained ring. As long as you always have at least three non-parallel axes, you can rotate. As long as the fourth gimbal is always in position to take over if two other rings line up, the local space of movements will be three dimensional.

Now if we wanted to represent the gimbals mathematically, we'd have four numbers to worry about, but there would be a constraint on at least one of them. We might like to imagine that there is some constraint will be sufficiently strong to mean that given three of these numbers, we could find the fourth uniquely. In fact there is.

Let's back up for a second. If we're willing to accept that our experiments with gimbals will roughly hold, we've found that in order to properly express rotations, we're going to need more than three just some subset of normal three dimensional space. Specifically, we'll need some kind of three dimensional surface in four dimensional space to properly express rotations in all cases.

Now, where have we seen a four dimensional space before? That's right: quaternions are exactly what we're looking for. Specifically, the unit quaternions, the set of quaternions a + bi + cj + dk which satisfy a^2 + b^2 + c^2 + d^2 = 1, are useful.

Imagine we want to rotate 3-D space about an unit axis v = bi + cj + dk ( b^2 + c^2 + d^2 = 1 ) by an angle θ.  Let q  = cos( θ / 2 ) + sin( θ / 2 )v, and let q^-1 = cos( θ / 2 ) - sin( θ / 2 )v. Then, if we have an arbitrary vector u, the rotation applied to u will give us the vector quq^-1! That's it! The great thing about this method is that everything is uniform. There are no singular points, no weird exceptions, and combining the rotation q followed by the rotation p into a single rotation is as simple as finding pq. Nice, huh?

If you want to understand exactly why that works, you should look here. I'm not going to go into detail because I intend this just to be a functional primer.

Also, once I get it working, I may post some C++ code with python bindings( I'm in the process of porting various things ).

No comments:

Post a Comment