## Quaternions and graphics programming

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

### Quaternions and graphics programming

Posting here because I'm asking about the mathematics, moreso than the programming techniques.

As far as I know, a quaternion describes rotation of [imath]w[/imath] radians/degrees (cross out whatever isn't applicable) around an axis [imath](x,y,z)[/imath]. If this is correct, then hooray, shortest thread ever. Otherwise, could you please educate me on what it actually represents and how I would use them to indicate rotation in OpenGL? (where the rotate function is GLRotate(x, y, z, angle) and the result is a rotation of angle around that vector.)

Assuming I do actually understand what quaternions represent, then the euler rotations would be represented by the quaternions <(u, 1, 0, 0), (v, 0, 1, 0), (w, 0, 0, 1)>, and a rotation of 90 degrees around the x axis, then 180 degrees around the y axis could be represented by the quaternions [imath](\pi/2, 1, 0, 0)(\pi, 0, 1, 0)[/imath] or [imath](3\pi/4, \pi, \pi/2, 1)[/imath], correct?

Going off the wiki entry for quaternion multiplication:
$(a1,b1,c1,d1)(a2,b2,c2,d2) = a1a2 − b1b2 − c1c2 − d1d2, a1b2 + b1a2 + c1d2 − d1c2, a1c2 − b1d2 + c1a2 + d1b2, a1d2 + b1c2 − c1b2 + d1a2$

If I'm completely off, could someone explain in fairly simple English what quaternions are actually all about? From my googling and wiki-ing, I'm just coming up with the above. The wiki article in particular is incredibly confusing.
I'm not lazy, I'm just getting in early for Christmas is all...

PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

### Re: Quaternions and graphics programming

Sorry, I can't offer much help: to transform 3D points I use 4D homogeneous coordinates, rather than quaternions. But this page may help: http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

jestingrabbit
Factoids are just Datas that haven't grown up yet
Posts: 5967
Joined: Tue Nov 28, 2006 9:50 pm UTC
Location: Sydney

### Re: Quaternions and graphics programming

ameretrifle wrote:Magic space feudalism is therefore a viable idea.

Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

### Re: Quaternions and graphics programming

Proof of the quaternion rotation identity in particular.

cos alpha/2 + u sin alpha/2 is the quaternion that represents a rotation (clockwise) by angle alpha around axis u.

I'm using the scalar+vector notation for quaternions. Ie, the quaternion (a,b,c,d) is represented as a + (b,c,d). (I hadn't seen it before reading wiki, but I sort of like it). I'm also using bold face to indicate single letters that represent vectors.

In this notation, if we have a + u times b + v, we do the following:

ab + av + bu + u v
but where we use a special multiplication for the vector multiplication:
u v = u x v - < u, v >
where < , > is the dot product and x is the cross product. There are a myriad different ways to define the quaternion multiplication, I just agree with the wiki article that the above is pretty.

Some of what you describe in the OP is fundamentally wrong. Quaternions that are used to represent rotations are unit quaternions. They contain no components that scale up to pi.

A 90 degree rotation around the (1,0,0) axis would look like:
(1/rt(2), 1/rt(2), 0, 0) in 4-vector notation, or
1/rt(2) + (1/rt(2), 0, 0) in scalar+vector notation.

squaring it (180 degree rotation) gives us:
1/2 + (1/2,0,0) + (1/2,0,0) + (1/rt(2),0,0)x(1/rt(2),0,0) - 1/2
= 0 + (1,0,0)
squaring this (a 360 degree rotation) gives us:
0 + 0 + 0 + (1,0,0)x(1,0,0) - 1
= -1, (0,0,0)
Notice the singularity! Here is the other one:
and squaring that (a 720 degree rotation) gives us:
1+ 0 + 0 + 0 x 0 - 0
= 1 + (0,0,0)

Make any sense?

So to extract the axis and angle from a quaternion, first calculate the inverse cos of the first component. Then double. That is the angle of rotation. Now rescale the last 3 components by 1/sqrt(1-first component squared), or by 1/sin(alpha/2) (same difference). Or you could just take the unit vector along the direction of the last 3 components. Note that the singularity (where sin(alpha/2) is 0) that exists here corresponds to no rotation at all. You'll have to be careful when programming this, because of the numerical instability around rotations that are really small could screw you up. (ie, if you take the 1000th root of a quaternion (!) then scale it back up by ^1000, things can get wonkey...)

Then again, taking roots of quaternions gets interesting: there is a fundamental instability problem, because the nth root of a quaternion is any quaternion that, when raised to the power of n, gives the original value.

If you know your complex nth roots of unity, you'll see the problem here. The same situation falls out here. Maybe worse? I cannot recall.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

Cleverbeans
Posts: 1378
Joined: Wed Mar 26, 2008 1:16 pm UTC

### Re: Quaternions and graphics programming

Ephphatha wrote:If I'm completely off, could someone explain in fairly simple English what quaternions are actually all about?

Well lets talk about the motivation. The main issues with Euler angle representation is Gimbal lock, and quaternions solve this problem. Another nice feature is that their representation in memory is smaller, you don't need a full matrix to describe them. Of course, you can still use a matrix to represent them.

Geometrically it's best to start with a lower dimensionally analogy. Lets say we're looking at rotations in the plane. Given a vector v we could build a 2x2 rotation matrix and use four numbers to represent the rotation. Completely legitimate way to go. However there is another way to do this, by considering v as a point in the complex plane, and using the geometric interpretation of complex multiplication to rotate it. Notice that if we multiply v by a point on the unit circle, no scaling occurs, but the rotation does. Now we have a way to represent all the rotations in the plane with just two numbers which are easily identified. Historically this was the motivation for quaternions, Hamilton was looking for algebraic means of extending complex number operations work in three dimensions.

So with that in mind, quaternions end up with certain algebraic properties similar to complex numbers, however some properties like commutativity break down. Also the quaternions are 4-dimensional points on the unit hypersphere - which is a 3-dimensional space. It's hard to visualize because the hypersphere is an entire three dimensional space, but if rely on the lower dimensional analogies you can interpret them from this perspective. Notice that rather than moving to a three dimensional space they jumped to four, and that's for algebraic reasons which are likely beyond the scope of your inquiry. This is a common idea, consider that you can represent n-dimensional vector addition as matrix multiplication in an n+1 dimensional space acting on an n-dimensional plane (you might have come across this before in your studies).
"Labor is prior to, and independent of, capital. Capital is only the fruit of labor, and could never have existed if labor had not first existed. Labor is the superior of capital, and deserves much the higher consideration." - Abraham Lincoln

Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

### Re: Quaternions and graphics programming

Thanks, I had read most of the wiki stuff but hadn't really been able to gather much off it. This makes much more sense now. So the euler rotations of 90 degrees about each axis would be represented with the quaternions [imath](cos(\pi/2) + sin(\pi/2)(1, 0, 0), cos(\pi/2) + sin(\pi/2)(0, 1, 0), cos(\pi/2) + sin(\pi/2)(0, 0, 1))[/imath] (which in this case is [imath](0 + u)[/imath]).

And if I have a quaternion [imath]a + u[/imath] that I want to use in the GLRotate function, I'd need to call it like:

Code: Select all

Quaternion orientation; //assuming it is set with some value
float angle = 2 * acos(orientation.scalar); //will be the rotation amount in radians, acos() is the inverse cosine function.
Vector3 axis = Quaternion.vector;
axis.Normalise(); //Making axis a unit vector
GLRotate(axis.x, axis.y, axis.z, angle);

Then to multiply two quaternions, I would implement this?
The scalar part of the resulting Quaternion is the product of the scalar parts of the two quaternions, minus the dot product of the vector parts.
And the vector part is the sum of both vector parts scaled by the other operands scalar part, plus the cross product of the lhs vector part and the rhs vector part.

Code: Select all

mul_quaternion(Quaternion lhs, Quaternion rhs)
{
Quaternion result;
result.scalar = lhs.scalar * rhs.scalar - dot(lhs.vector, rhs.vector);
result.vector = lhs.vector * rhs.scalar + rhs.vector * lhs.scalar + cross(lhs.vector, rhs.vector);
return result;
}
I'm not lazy, I'm just getting in early for Christmas is all...

Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

### Re: Quaternions and graphics programming

Ephphatha wrote:Thanks, I had read most of the wiki stuff but hadn't really been able to gather much off it. This makes much more sense now. So the euler rotations of 90 degrees about each axis would be represented with the quaternions [imath](cos(\pi/2) + sin(\pi/2)(1, 0, 0), cos(\pi/2) + sin(\pi/2)(0, 1, 0), cos(\pi/2) + sin(\pi/2)(0, 0, 1))[/imath] (which in this case is [imath](0 + u)[/imath]).

Almost. The quaternions need to be normalized (that is, have absolute value 1). If you change your 1s to [imath]1/\sqrt 2[/imath] you've got it right. So [imath]cos(\pi/2) + sin(\pi/2)(1/\sqrt 2, 0, 0)[/imath] would be a rotation around the x-axis (if you multiply a vector from the left or right by this, you'll rotate it clockwise or anti-clockwise (which one is which I can't remember)).

Edit: That was wrong. Disregard!
Last edited by Torn Apart By Dingos on Sun May 09, 2010 5:52 pm UTC, edited 1 time in total.

Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

### Re: Quaternions and graphics programming

Torn Apart By Dingos wrote:
Ephphatha wrote:Thanks, I had read most of the wiki stuff but hadn't really been able to gather much off it. This makes much more sense now. So the euler rotations of 90 degrees about each axis would be represented with the quaternions [imath](cos(\pi/2) + sin(\pi/2)(1, 0, 0), cos(\pi/2) + sin(\pi/2)(0, 1, 0), cos(\pi/2) + sin(\pi/2)(0, 0, 1))[/imath] (which in this case is [imath](0 + u)[/imath]).

Almost. The quaternions need to be normalized (that is, have absolute value 1). If you change your 1s to [imath]1/\sqrt 2[/imath] you've got it right. So [imath]cos(\pi/2) + sin(\pi/2)(1/\sqrt 2, 0, 0)[/imath] would be a rotation around the x-axis (if you multiply a vector from the left or right by this, you'll rotate it clockwise or anti-clockwise (which one is which I can't remember)).

To normalise a quaternion you divide the scalar and vector parts by the magnitude of the quaternion, and you calculate the magnitude by taking the square root of the sum of the square of each component, right?

So taking the first example, [imath]cos(\pi/2) + sin(\pi/2)(1, 0, 0) = 0 + 1(1, 0, 0) = 0 + (1, 0, 0)[/imath] and that quaternion normalised would be [imath]n = \sqrt{0^2 + 1^2 + 0^2 + 0^2}, 0/n + (1/n, 0/n, 0/n)[/imath] and since [imath]n = 1[/imath] you get [imath]0 + (1, 0, 0)[/imath] not [imath]0 + (1/\sqrt{2}, 0, 0)[/imath].

But I'm going off this website and some of the examples contain obvious errors, so I could be mistaken in how you calculate the magnitude of a quaternion.

(In other news, the \sqrt symbol really fucks up post formatting on my computer.)
I'm not lazy, I'm just getting in early for Christmas is all...

Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

### Re: Quaternions and graphics programming

Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

### Re: Quaternions and graphics programming

Just as a warning: it has been a while since I poked around with Quaternions in code, but I think there might be quadrant issues in corner cases.

In particular, cos( pi/2+x ) = cos( pi/2 -x )

On the other hand, a rotation of 170 degrees, and another by 190 degrees around the opposite axis, are identical. Hmm. Maybe the quadrant problems only show up when you are doing many-multiples of a given rotation (ie, when you have a rotation by 95 degrees repeated twice, then reconstituted as a rotation of -85 degrees repeated twice).

But I cannot recall exactly what the gotchas where -- I just remember running into gotchas at least once.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

### Re: Quaternions and graphics programming

From the looks of it I won't be using them in this current project anyway, both the physics library and the graphics library I'm using work on 4x4 matrices for position/orientation so it's gonna be easier to just use that system for the time being. But I look forward to the day when I get to give them a go
I'm not lazy, I'm just getting in early for Christmas is all...

Cleverbeans
Posts: 1378
Joined: Wed Mar 26, 2008 1:16 pm UTC

### Re: Quaternions and graphics programming

Ephphatha wrote:From the looks of it I won't be using them in this current project anyway, both the physics library and the graphics library I'm using work on 4x4 matrices for position/orientation so it's gonna be easier to just use that system for the time being. But I look forward to the day when I get to give them a go

Their matrix representation is 4X4 as well, so you can use them right away for exploratory purposes.
"Labor is prior to, and independent of, capital. Capital is only the fruit of labor, and could never have existed if labor had not first existed. Labor is the superior of capital, and deserves much the higher consideration." - Abraham Lincoln