Rotating a vector by a quaternion

The old Matrix and Quaternion Math FAQ claims that the simplest way to rotate a 3D vector (v) by a rotation quaternion (qr) is

v’ = qr * v * qr[sup]-1[/sup]

Where the multiplication is performed by converting the vector to a quaternion such that its scalar component is 0 and its vector component is the vector. qr[sup]-1[/sup] is the quaternion’s inverse. V’ is gotten by extracting the vector component from the resulting quaternion.

However, I was looking at a few 3D math libraries (most notably the de-facto standard GLM), and the definition of “rotate” function usually is:

v’ = qr * v

Without the inverse component. So which is correct? The latter seems like the intuitively correct one (meaning, if I had to invent a rotate method in a locked cage with no reference materials, it’s what I’d blindly guess), but the matrix and quaternion math FAQ is often treated as sort of a bible on the subject.

I don’t know a lot about quarternions, but it seems to me that if yo just multiplied the 3-D vector by a quarternion your result will not necessarily be a 3-D vector.

A little research tells me that when using quarternions to represent rotations the 3-D vector is represented by a pure imaginary quarternion, therefore the inverse serves the purpose of making sure the rotated vector v’ is also a pure imaginary quarternion.

That was my inkling, that right-multiplying by the inverse/converse would eliminate the scalar component and keep it as a vector/pure imaginary quaternion.

Would your calculation have anything to do with finding the shortest path through the vector’s rotation? In 3D animation, quarternions can be problematic if calculating a rotation over 180°.

I’m aware that without some extra checking that linear interpolation methods with quaternions don’t always guarantee the shortest path (or, as you seem to be alluding to, occasionally take the short way wrongly), but no, I’m rolling a 3D math library for a language that doesn’t have a mature one yet and need to double-check my math – so it’s just a general question for writing a “rotate” function.

Gotchya. Good luck!

Your question is like asking “Which is correct: sine or the natural logarithm?”.

They’re both “correct”; they’re just different functions used for different purposes.

The short story is that quaternions most directly represent certain kinds of rotations in 4-dimensional space, and when you multiply a vector by a quaternion straight-up, you are performing a rotation in 4-dimensional space. That’s what the math libraries you’re looking at are doing. The v’ = qr * v business is using quaternions for their primary purpose: to represent rotations in 4-dimensional space.

However, it turns out that any rotation in 3-dimensional space can be decomposed into two “mirror image” rotations in (3 + 1)-dimensional space. This makes quaternions sometimes convenient for representing rotations in 3-dimensional space as well, which is why they tend to come up even for people only interested in modelling 3d geometry. That’s where the v’ = qr * v * qr[sup]-1[/sup] business comes from. This is how you use quaternions to represent rotations in 3-dimensional space.

So the two functions are both “correct”; they just calculate separate things. You’re probably more interested in the v’ = qr * v * qr[sup]-1[/sup] one, but who knows? You may some day find the other one useful as well.

Rather, I should say, “*scaling and *rotation”. Analogously to how complex numbers represent scaling and rotation in 2-dimensional space.

(Specifically, a quaternion represents a combination of scaling and isoclinic rotations of a particular handedness on 4-dimensional space.

What does that mean? Well, “isoclinic rotation” means a linear operation which preserves magnitude ratios and swings each direction by the same angle.

And “handedness”? Well, in any number of dimensions, to be a combination of scaling and isoclinic rotation is to take the form a + b * i, for scalars a and b and a 90 degree rotation i, where “90 degree rotation” means “a linear operation which preserves magnitudes and swings each direction to a perpendicular one”.

Furthermore, just as, in 2 dimensions, there are two 90 degree rotations which cannot be continuously deformed into each other (while remaining 90 degree rotations throughout the deformation), there are, in 4-dimensions, two kinds of 90-degree rotations modulo continuous deformation. This amounts to the two "handedness"es which an isoclinic rotation can have; fixating on those using a particular handedness gives us the quaternions. [Which are very conveniently closed under addition, though this fact is not obvious on this account, and would fail if we did not limit ourselves to a particular handedness].

Anyway, none of these parenthetical remarks are relevant. Don’t blame me; you’re the one who kept reading past the “(”.)

Is this just clockwise vs counterclockwise or is there something I’m missing? What kind of deformations are you talking about here?

Years ago at work we had to do vector rotations and maxtrix multiplications for some physics/engineering simulations.

I seem to recall one reason to use quaternions was there were certain operations and or values for that could cause problems. I think one was you could end up with a rotation that resulted in a singularity/divide by zero kind of problem. The other was when some matrix values were approached the accuracy of the calculation could become a bit suspect.

In laymans terms I guess you could say when you have a 4 D matrix to do a 3D rotation you always have an extra slot so to speak to keep from “locking up” around one of the 3D rotation axes as well as an extra slot to “store some numerical info” in.

And using quaternions solved both those problems.

My moring coffee hasn’t kicked in so forgive me if this is baloney or somebody already said as much in a fancier way and I’m too slow to figure it out. And it’s been many years so this all may be hogwash.

I assume he’s talking about a mirror operation. You can get that as a 180 degree rotation through a third dimension, but not by a rotation that stays in the plane.

I believe you’re talking about gimbal lock.

It’s just clockwise vs. counterclockwise. The two comprise a 2 point discrete space ({i, -i}, if you like, or {[[0 1] [-1 0]], [[0 -1], [1 0]]}, if you like). There’s no way to get from the one to the other while remaining within that space.

Put another way, if you remove the line of pure scalings (i.e., the “real” line) from the space of scaling-and-rotations-in-2d (i.e., the complex plane), you get 2 topological components (corresponding to clockwise and counterclockwise). There’s no way to cross from above the real line to below it without going through it, in the complex plane.

In 4-dimensions, the space of isoclinic 90 degree rotations doesn’t comprise just 2 pionts, but still comprises 2 disjoint spheres (“sphere” in the sense of the surface of a 3-dimensional ball). Put another way, if you remove the line of pure scalings from the space of scaling-and-isoclinic-rotations-in-4d, you get 2 topological components (splitting your 4 dimensions into 2 + 2, these two components correspond to the two ways you can choose to match up the two rotation directions within the first 2 dimensions with the two rotation directions within the second 2 dimensions).

This phenomenon is rather more important in 4d than in 2d, because while any one of these components, with the real line joined back on, is closed under addition, and indeed comprises a skew field (a field where multiplication isn’t required to be commutative), the two together cannot be closed under addition without producing the 16-dimensional space of all linear operators in 4d, which is very much not a field (as it includes a number of non-invertible operators).

Er, I should say, “closed under addition and multiplication”. The two components (with the real line joined back onto either) are individually closed under addition and multiplication (the latter in consequence of the fact that the two 90-degree rotation spheres are individually closed under multiplication); however, taking both components together and closing under addition and multiplication lets one produce any linear operator in 4d (including non-invertible, non-zero ones like “Project onto this line”).

(This might all be clearer for some people with 4 by 4 matrices written out, but I am a gentleman, and a gentleman never chooses a basis…)

Not that anyone checks me on these things, but this parenthetical should’ve discussed the two spaces of isoclinic rotations, not just the two spheres of 90 degree rotations.

Anyway, whatever. The OP’s question’s answer was just “It’s the difference between rotating 3d vectors with quaternions and rotating 4d vectors with quaternions”.