Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Visualizing quaternions (2018) (eater.net)
192 points by squashToffee on Oct 27, 2023 | hide | past | favorite | 42 comments


Freya Holmer, of The Continuity of Splines [0] fame, recently gave a great conference talk about how quaternions naturally arise from the desire to define the multiplication of two vectors if you just agree to one new axiom: v^2 = ||v||^2, or "the square of a vector is equal to the square of its norm". It was fascinating and entertaining and I highly recommend anyone who finds this stuff interesting but confusing (like I do) to check it out: https://www.youtube.com/watch?v=htYh-Tq7ZBI

[0] https://www.youtube.com/watch?v=jvPPXbo87ds


Also check out https://youtu.be/60z_hpEAtD8?si=3Klxzo_cj5y39fFe and https://bivector.net/index.html

Some of the perceived complexity of complex numbers and quaternions seems to stem from not knowing the bigger picture.


I actually just watch this talk last night She is an absolutely fantastic explainer for these complex concepts, her video about splines was also hugely illuminating


I didn't understand shaders until I watched a 3 hour video of Freya explaining them from first principles. It was one of those "why didn't anyone say it like this before" moments. She's amazing.


I feel like there are 2 schools of thoughts on learning about quaternions: #1: This article, and the associated 3Bl1Br describe their abstract representations. Visualizations of 4-component imaginary structures etc. They provide a deep dive from a mathematician's perspective. If you're into this sort of thing (You probably are if you watch 3Bl1Br regularly...), it's outstanding.

#2: Quaternions as a powerful tool for representing orientations and rotations. Applicable whenever you're programming something that uses these concepts. Most common are computer graphics and robotics, but you can imagine other uses like biometrics, scientific models of molecules etc.

The visualizations you might use for type 2 are standard 2D and 3D space operations. You don't need to directly map the 4 quaternion parameters. You need these building blocks; find from a library, article, first-principles etc.

  - Rotate a vector using a quaternion
  - Find the quaternion that specifies the shortest rotation from one vector to another
  - Find the quaternion that rotates around a given axis a given amount
  - Rotate an orientation and/or composing rotations
  - Find the quaternion that rotates one orientation to another
Using these building blocks (And maybe one or 2 I left out), you can do any sort of work with rotations and orientations. If you can visualize the rotations, either in your head, or with physical objects or a computer render, you won't forget the principles.

Warning: There are 2 types of quaternions (JPL and Hamilton). Every article/lib etc uses one, but they rarely annotate which! Mixing the 2 will produce incorrect results.


This is, I think, the difference between thinking of a quaternion (or a vector!) as a collection of numbers with rules for manipulating them, and thinking of a quaternion (or a vector) as an abstract geometrical object that exists in a space and has certain properties.

The fascinating thing, to me, is that for so many geometrical objects, you can start with a short list of straightforward properties, and show that they exactly correspond to the collection-of-numbers concept over an arbitrary basis. Oftentimes, if you want to do something to the stuff on your screen, if you can describe it in terms of the abstract objects with no reference to the basis, you can then transliterate that to operations on a bunch of floats in a way that is simpler and more robust than working with the floats from the beginning.


Yeah, I worry that people who just want to use quaternions to make video games will get discouraged that they can't master the content in this kind of webpage and their good old imposter syndrome will kick in hard. When all they really need to do is play around with with the black-box approach to quaternions and just treat them as magical and better "rotation matricies".


As far as I know those two models of quaternions are the same thing. The quaternions in these videos are (an awkward representation of) 3d rotation operators, under the correspondence i = R_yz, j = R_zx, k = R_xy

(although you can assign whichever axis to whichever quaternion you want, and you can arbitrarily swap i<>-i, j<>-j, k<>-k. From googling it looks like the JPL convention is to swap k<>-k, such that ij = -k instead of ij = k).


For simple applications there's no need to learn about quaternions at all. There could be a library type called Rotation3D or something. You should never need to know which representation it uses. The important part is to know Euler angles are bad ;)

But once you get into something more sophisticated like using rotations in a dynamical system, statistical filtering, optimizing a rotation, etc., the abstractions begin to leak.


I was a high school student in 90-s playing with computer graphics. Back then, good information was hard to come by, and all I had was a short book explaining how transformation matrices work in practice. It was like magic, you could combine any kind of transformations together just by multiplying matrices.

Until I tried to do animations, by linearly interpolating between the "start" and the "end" matrices. It resulted in truly funky transformations that deformed objects in bizarre ways. This in turn led me down the rabbit hole of decomposing matrices into Euler angles, interpolating between them, then struggling with gimbal locks and non-uniformity of polar coordinates, and finally to quaternions.

I wish I had these tutorials back then...


On the topic of matrix multiplication there is an excellent interactive 3D visual explainer from the PyTorch team blog on how it applies to attention heads and weights for ML.

https://pytorch.org/blog/inside-the-matrix/


The interactivity of these videos is really well done. I've never seen anything like it before.


Same. The comments on that page also have some excited discussion and I wonder what (if anything) ever made it to an open-source repo



Quaternions always irk me because they're overspecified. It shouldn't need more than 3 variables, but somehow there are 4. Argh...


Quaternions that represent 3D rotations have exactly 3 parameters, because they are normalized: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotati...

Quaternions that are not normalized do not form a representation of the spin group Spin(3).


I'm assuming Spin(3) is equivalent to SO(3)?


It's a double cover of SO(3) (because q and -q correspond to the same element of SO(3)), and is isomorphic to SU(2)


Oh wow thanks I understand


It’s better than a matrix though. ;) This is like worrying that imaginary numbers are overspecified for representing reals, or that 3D vectors is overspecified for representing normal vectors. Quaternions themselves aren’t overspecified, they are naturally 4-dimensional, but it’s true that 3 components is sufficient for an orientation (unit quat).

Using quats for orientation is useful in part precisely because of the added dimension - it’s what makes interpolating quaternion orientations better than other representations - you naturally get the minimal and most direct motion. Interpolating any of 3 component orientations doesn’t work as well. So don’t let it irk you, instead find the utility and beauty in the extra dimension.


> It shouldn't need more than 3 variables

Why do you say that?

For example, we could represent any point on the sphere {x^2 + y^2 + z^2 = 1} by two numbers, but for most purposes, three numbers is a lot nicer.

And graphics programmers use homogeneous coordinates for everything, mainly because they make translations and perspective projections nicer. (Coincidentally, quaternions are also a kind of homogeneous coordinates, since the space of 3D rotations is (diffeomorphic to) 3D real projective space.)

So there are lots of places where adding extra variables makes things cleaner.


There are four terms: an identity term and three rotations. Q = a + bi + cj + dk = a I + b R_yz + c R_zx + d R_xy = aI + v·R, where v is the axis of rotation.

Basically you have an extra degree of freedom because you can multiply the whole thing by a scalar that just scales whatever vector it multiplies. But if you just want to represent rotations, you want it to have unit magnitude, so you ensure that a = cos θ and |v| = sin θ. (or θ/2 on each if you want to use the two-sided qpq^{-1} rotation).


Well, the way quaternions model rotation is by unit quaternions (the unit vectors in 4-space) acting on pure imaginary quaternions ai + bj + ck. The unit quaternions form a 3 dimensional space called the 3-sphere or SU(2). But 3 variables isn't enough parameters to describe the 3-sphere completely (it will always miss at least 1 point). So there's half a fix to your objection!


>It shouldn't need more than 3 variables But you do. Think about what it means to describe orientation. You need enough degrees of freedom.

When describing rotation of of a sphere projected onto a 2d plane, having two variables (X,Y) isn't enough. Because the sphere could rotate about the axis between the origin and that point. There's an an entire degree of freedom you could describe for a complete rotation about that axis.

In the same way, having 3 variables to describe a quaternion rotation is not enough. You again, have an entire degree of freedom you could rotate the quaternion about.


I think you might have some mathematical gaps, they are not overspecified. It is impossible to fully specify 3d rotations as 3 variables.. if you want a nice algebraic structure. R^3 is not isomorphic to SO(3). All the representations have tradeoffs, quarterions are the simplest algebraically.


It is not over but too specialised. Look at other geometry which is more generalised algebraic geometry. Useful for rotation but then what. See my other comment. Matrix and group and algebraic geometry all have its use. Of course q now use in computer game and that is helpful. But specialised.


Real numbers are also overspecifed, there's too many of them, most of them you'll never be able to compute or even conceptualize(!)

But they are mathematically useful.


It is explained why in the Stereographic projection (3d) interactive video in the linked article, just before the 5 minute mark.


You can always use Euler's angles. Well, maybe not quite _always_...


Euler angles irk me too because latitude rings are parallel to each other but longitude rings aren't, one runs from 0 to 2pi, anther runs from -pi to pi, another runs from 0 to pi, it's a mess. I wish we could find a system with better symmetry and consistency without being overspecified.



I mean, one could create an "east pole" and a "west pole" on the Earth and have latitudes behave like longitudes ...


It took me a minute to understand what you mean, but think for a minute about the disadvantages and inconsistencies of your idea, because this seems like it would be worse in every possible way for Earth coordinates and navigation, aside from the imagined symmetry. Smart people have already considered this idea, and it hasn’t caught on. Why?

- First of all, this is minor, but you cannot call them east and west poles, because the east one is only to the east from one side. One the other side, the “east pole” would be to the west of the “west” pole.

- Bigger problem but related: traveling along latitudes is no longer traveling north or south, and no longer traveling in great arcs. When you stay in one latitude in your system, you have to know where you are in order to know what compass heading you’re traveling (or vice versa - if you start with a heading, you don’t know whether your longitude or latitude is either increasing or decreasing), it cycles through all direction on the compass without passing the north or south pole.

- That means that latitudinal navigation by the stars is no longer an option, and latitudinal travel by the compass (!) is no longer an option either. Your east-west-pole idea would require GPS for sailing. (Might be technically fine today, but should be pretty obvious by now why earth coordinates could never have evolved this way and would be extra bad for pre-computer navigation.)

- You lose the property that longitude lines and latitude lines are perpendicular to each other on the surface - except at the two non-poles - the two spots on the axis in between the 4 “poles”. Longitude and latitude in this system are no longer orthogonal, and in some places you can specify ways to travel from one longitude to another via either longitude or latitude, in other places you can’t.

- You add two more singularities to your coordinate system. And now you have a 3-dimensional coordinate system, but only 2 axes. Why have 4 poles and not 6? That feels asymmetric somehow. How do you pick the location of these east & west poles (and what would you call them instead of east & west?)

- Finally, think about how you’d draw a map. If you get rid of the north-south axis, then which way is up on the map? How do you flatten your coordinate system and put it on a 2d rectangle? Do you want the lat/long lines to look straight and perpendicular, or faithfully represent the local surface while letting lat/long lines curve all over the map? Could you actually do either of those things with this system? The existing solutions aren’t great, but it seems like this would be even worse. There are very good reasons, for example, that nobody uses a 4-pole system for texture-mapping spheres.


You couuld also have an East pole and a West pole and have great circles for both latitude and longitude. It's not clear eitherr of those would be preferable to the system we have now.


Check out the exponential representation of rotations in geometric algebra.


Once you accept that imaginary number is not about a straight real number but a number about rotation. And you have two number that is not the same. One about straight line and one about rotation I.e.

X0 ops X1(another number system ) where X0, X1 is real

You can have complex number or a+ bi where i^2 = -1 And you can have split complex number a+bj where j^2 = 1 etc

The next move may be then not to q but to try

x0 ops x1n1 ops x2n2 ops … xi is all real but ni is a different number system represent things not on the real number. Of course you can do q here. But ..

The natural course is to cliff geometry, where it dimension meant something else.

Or even more other systems.


Geometric algebra is the only intuitive explanations of quaternions that I have seen, since geometric algebra actually explains what quaternions are much better.

You can then see that rotations using rotors (from geometric algebra aka "quaternions") are then basicaly two consequitive mirror operations across two vectors.


Coincidentally, this conference talk[1] released yesterday regarding vector multiplication. In it, it describes how quaternions can be seen as an extension of 3d vector spaces, like how complex numbers can be seen as an extension to 2d vector spaces. More specifically, quaternions are a 4d vector with the basis of {1,yz,xz,xy} where those "bivectors" result from 3d vector multiplication, e.g. (ax+by+cz)(dx+ey+fz)

[1]: https://youtu.be/htYh-Tq7ZBI


im reading a book on quaternions by vince jones at the moment that basically says we are using quaternions now because they "just happen" to encode an axis-angle rotation in their algebra.

thats it! theres no more magic than that.

just like complex numbers encode a rotation in 2d due to the way the algebra is structured.

olinde rodrigues wrote about axis-angle rotations before hamilton discovered quaternions, and i think euler and gauss may have been involved too. anyway, its a fascinating story about the meeting of algebra and geometry.


im an idiot, authors name is john vince, im sorry, dyslexia is hard


Isn't geometric algebra supposed to replace these ?


Isn't geometric algebra simply a more general theory that actually encompasses complexes and quaternions?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: