Back

Quaternions: A Practical Guide

175 points2 yearsanyleaf.org
bob10292 years ago

In the realm of (real-time) computer graphics, I have recently developed a perspective that Quaternion representation is simply another "space" that you have to spend finite resources transforming to and from. I am not aware of a rasterizer that can operate directly with quaternions, nor a way to apply perspective or orthographic projections to them.

For all of my personal applications, I have found that good old fashioned 4x4 homogeneous matrices are a really good way to deal with all of the transforms I am concerned with. I do recognize the utility of quaternions in dealing with certain specialized transforms, but these are not applicable in my simple DIY/hobby use cases (i.e. Q3A-tier graphics).

From a math perspective, I think quaternions are fantastic and far more elegant way to do certain things. I did recently discover Microsoft has all of this neatly wrapped up in System.Numerics, a SIMD-accelerated library for .NET: https://docs.microsoft.com/en-us/dotnet/api/system.numerics....

eloisius2 years ago

I use quaternions in 3D geometry contexts a lot right now. Manipulating point clouds, describing camera orientations etc. But I kind of agree, a standard 4x4 rotation matrix would most likely be just as useful. I use them because it’s how Colmap represents camera extrinsics. They might use them just because serializing q{xyzw} is more elegant than dumping a matrix to text.

yarg2 years ago

> I am not aware of a rasterizer that can operate directly with quaternions, nor a way to apply perspective or orthographic projections to them.

This reminds me of something (seen here) that I thought somewhat interesting.

Orthographic projects of cones an n-dimensional euclidean space can be used to generate (n - 1)-dimensional voronoi diagrams.

The author of the page that I looked at hadn't generalised to weighted diagrams, but that's trivially implementable by altering the gradients of the cones.

The dimensionality issue is significantly nastier - with higher dimensional forms lacking GPU support, so the page only covered 2D voronois.

It seemed like the idea could make for an interesting voxel engine, if 4D depth clipping were a possibility.

taneq2 years ago

Likewise, I've read all the praises of quaternions and I keep meaning to use them but I always seem to end up just using good ol' matrices. The only thing I've really seen that seems to specifically need them is spherical linear interpolation (SLERP :D ) for smoothly blending orientations, but I've never quite had to use it.

sxp2 years ago

Whenever quaternions come up, I always recommend https://acko.net/blog/how-to-fold-a-julia-fractal/ and https://acko.net/tag/quaternions/

I never fully grokked complex numbers nor quaternions until I read those articles

thunkle2 years ago

I can't thank you enough for the Julie fractal link. It was super enlightening to someone who has enjoyed fractals and seen the formula but has never grasped the mechanics.

kilovoltaire2 years ago

Geometric Algebra provides a compelling alternative to quaternions: https://marctenbosch.com/quaternions/

(HN discussion 8 months ago https://news.ycombinator.com/item?id=29512302 )

ogogmad2 years ago

> Geometric Algebra provides a compelling alternative to quaternions

No it doesn't. Please stop repeating this. Quaternions are literally a part of geometric algebra. His page uses the algebra Cl_{3,0}, whose even subalgebra is Cl_{0,2}, which is the quaternions.

Just to clarify what he's actually saying, because he said it poorly: He's not criticising the quaternions at all. He's criticising a philosophy of the quaternions that goes back to Hamilton: That they are formally scalars plus vectors. He's instead promoting a view that they're formally scalars plus bivectors. You can go from the author's POV to Hamilton's POV by replacing the word "bivector" with "vector" and "exterior product" with "cross product". Mathematically, this replacement constitutes an isomorphism, which shows that the author's algebra is isomorphic to - which for mathematicians means the same as - Hamilton's quaternions. The author's philosophy has the advantage that it shows that the quaternions are a subalgebra of Geometric Algebra. But this is only a different way of talking about the quaternions from Hamilton's. And Hamilton's view isn't even wrong - it leads to the octonions, which the GA approach doesn't - and it doesn't need you to know what a bivector is.

hgomersall2 years ago

The GA view is IMO much easier to understand if you think visually. The fact you can subsume complex numbers and quaternions into a common framework is incredibly neat.

jacobolus2 years ago

> Hamilton's view isn't even wrong

Hamilton’s view (and later the view of Gibbs, Heaviside, et al.) is wrong insofar as in models of the 3-dimensional physical world we have two kinds of "vectors" which behave fundamentally differently under reflection (because one type are actually bivectors called by the wrong name). Physics textbooks usually consider these both “vectors” but call one type “pseudovectors” (or call them “axial” and “polar” vectors), and (ideally) keep careful track of which vector is of each type. If you take the cross product of two regular vectors (yielding a pseudovector), it is fundamentally meaningless to add that to another ordinary vector, even though that seems like an entirely reasonable thing to do, algebraically.

Hamilton got there by trying to generalize “complex numbers” – mathematicians had been eliding the difference between points in the plane, vectors in the plane, and quotients of vectors in the plane (“complex numbers”) for a century or so by his time, and didn’t yet know the territory well enough to understand which points they were confused about.

Even in the plane, separating vectors from complex numbers is very powerful and useful. Nearly any time you see something like z̄w between complex numbers you can profitably consider z and w to be vectors u and v and use the geometric product instead (by comparison, when you see zw that implies a scalar+bivector was the kind of object you wanted already). Being able to use the dot and wedge products of planar vectors u·v and uv is clearer than the version mathematicians use, Re(z̄w) = ½(z̄w + zw̄) and Im(z̄w)i = ½(z̄w – zw̄), and makes the other applicable vector identities a lot easier to learn, recognize, and use. Bonus: they keep working in higher dimensional spaces or spaces of other signature.

The “cross product” is sort of interesting as a historical curio or abstract puzzle, but it is an abomination of a tool to teach to students, because it (1) creates widespread conceptual confusion, and leads people to make wrong inferences about the world, and (2) completely fails to generalize to the obvious analogous situations like the Euclidean plane or 4-dimensional spacetime (so people end up needing to learn a new notation for each new context they find). Replacing the cross product with the wedge product (as the anti-commuting part of a geometric product) clears up the confusion and builds up a whole shed full of effective general-purpose tools.

ajross2 years ago

Quaternions are one of those subjects where the community sees a steady stream of tutorial content, which really doesn't seem to be adding much to the discourse to my eyes. It's like a "yet another git tutorial" kind of thing.

In fact Ken Shoemake, who was responsible for originally popularizing quaternions as a robust rotation/orientation representation[1], wrote his own tutorial of this stuff back in the late 80's sometime, and it's really, really great: https://www.ljll.math.upmc.fr/~frey/papers/scientific%20visu...

Folks should start with that one before trying to rewrite it, IMHO.

[1] Here's a copy of the 1985 paper: https://www.cs.cmu.edu/~kiranb/animation/p245-shoemake.pdf

westoncb2 years ago

Quaternions are the monads of graphics programming.

boppo12 years ago

Monads?

westoncb2 years ago

It’s a mathematical abstraction a lot of people feel they need to understand (‘cause of its connection to some programming language theory stuff, and practical use within Haskell notably), so there are a million tutorials where someone is purportedly, finally, going to make the concept clear.

eternityforest2 years ago

I think somewhere I actually did find one that made it clear. But I don't remember anything about it at all.

I just vaguely remember them as something super abstract, that you can work through with about the same effort as a sudoku puzzle, but you have to go actually learn Haskell to understand why you'd want one.

Then they say something about function composition, but it seems like have to already be a functional expert to know why you would care about composing functions in a consistent way instead of just calling them from a new function.

eloisius2 years ago

Monoids in the category of endofunctors, what are you daft?

boppo12 years ago

Hahaha apparently so.

Akronymus2 years ago

https://www.youtube.com/watch?v=ENo_B8CZNRQ I think this is a pretty good, concise explanation.

jacobolus2 years ago
JadeNB2 years ago

It is well for non-specialists to be aware, however, that, while geometric algebra is a real thing, Hestenes's tireless promotion of it as the One True Language of physics is about as appropriate as identifying and anointing the One True Language of programming. One can and should have at one's command as many languages as possible; singling out geometric algebra as the one right language and forgetting the others is as bad as not being aware of geometric algebra at all.

jacobolus2 years ago

Hestenes’s tireless promotion is necessary because most everyone else, having grown up with a different formalism, is too entrenched / busy to try to make a basic change to what we teach and use, even when that change would provide tremendous benefits.

In physics there was some uptake where appropriate, but mathematicians for the most part scoff (“this is just a different notation for structures that were studied in the 19th century, there’s nothing new here” kind of thing).

But being able to multiply and divide vectors is a big deal, and GA is packed with incredibly useful identities. Working coordinate-free in a rich enough language of geometric relationships makes it (a) a lot easier to solve gnarly geometric problems, and (b) a whole heck of a lot easier to demonstrate and explain those solutions.

The change of perspective has been most impactful in fields where the details can’t be hand-waved away because they are getting interpreted by a computer, so streamlining them helps a lot. In particular, computer graphics, computer vision, robotics, physical simulation, and the like.

+1
JadeNB2 years ago
hgomersall2 years ago

Your response seems an overreaction to a suggestion that that article should be read first. GA is IMO without a doubt a very helpful way of understanding quaternions, which the OP doesn't really try to do.

JadeNB2 years ago

> Your response seems an overreaction to a suggestion that that article should be read first. GA is IMO without a doubt a very helpful way of understanding quaternions, which the OP doesn't really try to do.

I was indeed specifically responding to the suggestion that the speech be read first. If it's the first thing one reads on the subject, then one has no context in which to take Hestenes's remarks, and it is easy to take very literally his claims about the primacy and universality of geometric algebra—so I wanted to give some of that context first.

derjames2 years ago

Ken Shoemake also has an article in Graphics Gems IV. Section III.1 page 175. Theory and Sample program included. This is the only thing I needed to read to understand quaternions.

keepquestioning2 years ago

Add to the list

- Hidden Markov Models

- Kalman filters

Things which have tutorials that continually fail to explain the tao of the thing.

pklausler2 years ago

Don't forget monad tutorials!

eternityforest2 years ago

Kalman filters are so interesting. As far as I can tell they are one of the very simplest things one can do that requires a real understanding of math.

It doesn't seem like you can just drop in a library, in the general case, or if you can, it doesn't seem to be common practice.

Usually things that need an actual understanding are either totally novel, or solved problems with existing tools.

Kalman filters are where you find out that you really did need math all along, if you're like me and don't really see math in everyday life.

I suspect there's still a way to fake your way through if you need to and have a day or two to study, but the first time you see it it really shows what you don't know.

It's like when you see non physicists talking about physics and feel amazed at how they managed to seemingly understand maxwells equations just for fun.

wwweston2 years ago

Are there any tutorials successfully convey the tao of those things?

keepquestioning2 years ago

No

the__alchemist2 years ago

Those look like outstanding resources, especially given their history; will dive in later today; added links in the article overview and references.

the__alchemist2 years ago

I wrote this article over the past few weeks, while building a structural chemistry model. I found most sources available either dive into quaternion mathematics and reasoning without building intuition useful in applications, or are documentation for specific computer graphics libraries. I'm using this article as a personal reference; perhaps others will find it useful too.

KboPAacDA32 years ago

Thank you for writing this article and especially skipping the unneeded proofs that a programmer usually has to step over to apply quaternions.

Grustaf2 years ago

Are the long variable names intentional or some kind of Latex artifact? I find it hard to read.

the__alchemist2 years ago

Intentional, but I don't know how I feel about them. I often find math computation terse, to the point where it's difficult to understand how to implement it in code. I took a hybrid approach where I used descriptive names and individual steps; will likely to revise, perhaps by splitting into pure math/latex, and code.

dang2 years ago

I'm sure this is a fine article, but an article is not a valid Show HN. Please see the rules: https://news.ycombinator.com/showhn.html.

We've taken "Show HN" out of the title now.

ogogmad2 years ago

Haven't dual quaternions taken over for their ability to represent all rigid body motions?

Quaternions are still important for building intuition for dual quaternions.

mrjet2 years ago

I can only answer for robotics:

Dual quaternions haven’t really taken over. While there’s no universal way that people end up handling rigid body transforms, SO(3) + R3 for rigid body transforms are the norm in robotics.

Some robotics orgs choose a single representation for SO(3) — usually quaternions or matrices — and use it everywhere. Note that quaternions are more space efficient and easier to normalize, but require 2x more operations to rotate a vector [1]. So you might see a quaternion shop use matrices in a hot loop for transforming 3D points. And a matrix shop might use quaternions in a hot loop for composing many transformations. Usually you have many fewer rigid body links than points in an e.g. pointcloud so it is performance favorable to use matrices.

A final comment: Most users in a codebase should interact with rotations through a rotation library like Sophus or Manif. That library should implement left/right lerp, splines, “rotate a vector” and “compose a transformation”. There is basically never a need for a developer to know what rotation representation is being used to compute the rotation.

If such a library is correctly implemented, you can even use Euler Angles as the underlying representation and never experience gimbal lock.

[1] https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotati...

ajkjk2 years ago

I don't think so? Most game engines for instance implement quaternions for ie slerp, but wouldn't mention dual quaternions, which are much more niche (used for translation + rotation at the same time).

ajkjk2 years ago

* ie -> eg

meheleventyone2 years ago

Mostly in jointed hierarchies like robotics and skeletal animation in my experience. Otherwise quaternions dominate for representing rotation and translation and scale are kept separately. Even further in an editor you might represent the rotation as Euler angles.

bodhiandpysics12 years ago

In graphics, dual quaternions are used in skinning, but rarely for orientation and translation.

percentcer2 years ago

As you pointed out, you still need regular ol' quaternions if you're dealing with non-rigid transforms (e.g. in animation where there's some scaling transform involved as well)

bodhiandpysics12 years ago

which is very very very common (squatch and stretch)

gfxgirl2 years ago

geometric algebra is replacing quaternions because GA handles tons of edge cases that quaternions don't?

Const-me2 years ago

> You can construct the orthonormal vector by taking the cross product between our first bond, and an arbitrary unit vector.

Arbitrary not gonna work. The cross product will sometimes be zero vector, and the algorithm will fail. The cross product will sometimes be a vector of very small length, not long enough to give accurate direction, and the algorithm will glitch.

One reliable way is finding index of the smallest absolute coordinate of the input vector, and using that unit vector.

Another minor thing, I think most libraries are storing these 4 numbers in xyzw order, while this article uses wxyz order for the formulae.

ngvrnd2 years ago

Want to read "Sedenions: a Practical Guide" ("Trigintaduonions", anyone?)

ajkjk2 years ago

They're not practical, no point! As this article alludes to, `bivectors` are the useful abstraction of rotation to higher dimensions, not octonions/sedenions.

ngvrnd2 years ago

Yes, that was the joke.

ajkjk2 years ago

oh cool

...you never know with this stuff