nighthawk454 2 days ago

The easiest way for me to conceptualize it is to think of it as orientation + rotation. 3 dims for the orientation vector to get the object facing/pointing the right way, then a further 1 dim/var for rotation about that axis. For a total of 4 variables/dimensions

3blue1brown and Ben Eater did a series of interactive videos on the subject that can be explored:

https://eater.net/quaternions

My favorite demo on this point is this one: https://eater.net/quaternions/video/rotation

Where they have a toggle to go between `a + bi + cj + dk` quaternion notation and an equivalent formulation in terms of 3d orientation vector plus rotation angle as `cos(θ) + sin(θ)*(ai + bj + ck)`

  • tsarakoye_selo 2 days ago

    What happens if rotation is zero? Won't (ai +bj+ ck) get multiplied by zero then, and the orientation information is lost?

    • nighthawk454 2 days ago

      If you click through to the interactive example, you can try it! You'll see that the if the above equations are `q`, then full formula to rotate a vector `v` is `v' = q * v * q^-1`

      • tsarakoye_selo 2 days ago

        Sorry, what you have stated is not very clear. If you use cos(theta) + sin(theta)*(ai + bj + ck) representation as you mentioned, what does happen to the orientation information when sin(theta) becomes zero

        • moefh 2 days ago

          When theta=0, you have

            q = cos(0) + sin(0)*(ai+bj+ck) = 1 + 0 = 1
          
          That means applying the "rotation" to the vector gives

            v' = q * v * q^-1 = 1 * v * 1^-1 = v
          
          So the output ("rotated") vector is the same as the input, as you would expect for a rotation by 0.
  • thaumasiotes 2 days ago

    > The easiest way for me to conceptualize it is to think of it as orientation + rotation. 3 dims for the orientation vector to get the object facing/pointing the right way, then a further 1 dim/var for rotation about that axis. For a total of 4 variables/dimensions

    Something's missing. Orientation in 3D space is a two-dimensional quantity; you would never need three dimensions to express it. The third dimension has to be providing some additional information, like a magnitude.

    • xeonmc 2 days ago

      Direction in 3D has two degrees of freedom, orientation has three.

    • vrighter 18 hours ago

      I think a better word than orientation is "axis". They are conceptually similar to axis-angle, with some added sugar to make an algebra

    • nighthawk454 2 days ago

      Only if you’re describing orientation as two orthogonal rotations. I’m saying think of it like a ‘pointing’ vector that defines the axis of rotation. And such a vector does require 3 components in 3d space

      • creativenolo 2 days ago

        It’s possible to point in 3D space with two rotation components. For example, the first two components of a UV texture.

        But I agree it is helpful to think of quaternions as direction and spin.

      • thaumasiotes 2 days ago

        Yes, a three-dimensional vector is a combination of a 3D orientation and a magnitude. An orientation by itself doesn't have three dimensions. The surface of a sphere is a two-dimensional space.

        > Only if you’re describing orientation as two orthogonal rotations.

        No, the space has the dimensionality it has. You may choose to describe a 3D orientation with more than two numbers, but you won't stop it from being a two-dimensional quantity that way. If you use more than two numbers, those numbers will fail to be independent of each other.

        • oasisaimlessly a day ago

          Orientation would conventionally be a member of SO(3), which is a 3-dimensional manifold.

          Your comment is essentially correct if you replace the word "orientation" with "direction", though.

matmann2001 2 days ago

Not detracting from this post, but has anyone else noticed there's a front page post about Quaternions or Kalman filters on about a monthly cadence? Wonder why that is?

  • Jarmsy 2 days ago

    I have noticed too that HN has an enduring fascination with quaternions, and they seem to reach front page surprisingly frequently (considering that there's not a huge amount of discussion of other geometry topics). I'm certainly not complaining though - I love quaternions too!

  • mettamage 2 days ago

    Maybe because it intersects deeply with game dev?

    • lawlessone 2 days ago

      yes, that's the module i learnt about them in during college.

      IMHO I think for me they're interesting because it felt like mathematically they straddle the line of being nearly impossible for me.

      I could just barely do them and felt very accomplished when I could. Anything else was either impossible or easy by comparison.

      assuming my maths skills are average here then most people have similar experiences with them.

      • xanderlewis 2 days ago

        What in particular did you find difficult about dealing with them?

        • lawlessone a day ago

          There was something called a slerp that allowed you to get an object to rotate from on orientation to another, similar to a linear interpolation.

          This was fine for some things. but if you wanted something to rotate but only in a certain way it was annoying as sometimes it would get from A to B, but go through Z during its rotation.

          Getting the order or multiplications right too.

          • ahazred8ta 4 hours ago

            Spherical linear interpolation, slerp https://en.wikipedia.org/wiki/Slerp (which can sometimes wrap around the wrong way)

            For levity, see Acts 12:4 And when he had apprehended him, he put him in prison, and delivered him to four quaternions of soldiers to keep him.

  • esafak 2 days ago

    Maybe it's aspirational: things that people don't know, and would like to learn, and perhaps use one day.

  • ofalkaed 2 days ago

    I never noticed it until I read Pynchon's Against the Day, I assume I just ignored quaternion posts before then. Great book, enjoyed the way he used quaternions and vectors towards literary ends.

    Edit: just noticed we also have a thread on bifurcation, another big topic in Against the Day.

  • orangea 2 days ago

    At least this is a topic that the average HN reader is likely to be able to understand and which is closely related to software. Several years ago there was a period where there were periodic posts about things like homotopy type theory and research-level algebraic geometry which inevitably spark only inane misunderstandings and uninformed speculation in the comments. I can only attribute it to some kind of fetish for the frontiers of pure math.

  • mhh__ 2 days ago

    They're both simple to motivate and complicated enough to have an allure.

  • hassleblad23 2 days ago

    Because quaternions are awesome? :) it definitely feels like a discovery when you learn about them first.

    • gamedever 2 days ago

      Are they?

      [Let's remove Quaternions from every 3D Engine] https://marctenbosch.com/quaternions/

      • xeonmc 2 days ago

        I feel like there should be a followup post "Dear Sir, you have reimplemented quaternions" noting how implementing GA and using it only in 3D gets you precisely right back to quaternions-but-with-a-different-name since they're isomorphic when specialized for 3D.

  • mbonnet a day ago

    Others have mentioned game dev, but they're also essential for guidance, navigation, and control - spacecraft, aircraft, robotics, etc.

  • defrost 2 days ago

    HN's going to need a windscreen cleaner and a bucket if 3blue1brown and Terence Tao ever collaborate on using adaptive Kalman filters for optimal paths in Quaternion spaces.

  • gcr 2 days ago

    no worries, quaternions just make the usual rotations. It’s cyclic.

  • aap_ 2 days ago

    Because quaternions are so cool yet under-appreciated. Unfortunately not many octonion posts.

  • adalacelove 2 days ago

    It's because the quaternion is part of the state of the Kalman filter.

    • ballooney 2 days ago

      Not in any intrinsic way, it’s just a mildly better way of representing attitude if your state vector includes attitude.

  • ballooney 2 days ago

    They’re both quite basic building blocks in state estimation and as SV’s focus shifts from web apps to drone warfare it will only increase in frequency.

  • TaurenHunter 2 days ago

    They are posted by the Quaterminions.

LegionMammal978 2 days ago

While they might be theoretically pleasing, I've had trouble seeing the appeal of quaternions for 3D graphics. Recently I was working on some 3D-rendering code from scratch for a project, and I looked into using quaternions for rotation, only to scratch my head at how fiddly they were to apply to vectors. (Also, many resources talking about them focus on their abstract properties at the expense of actual examples, which is annoying when I'm just trying to implement them.)

I had a much simpler time just using rotation matrices for everything. They're not much more difficult to compose, they're trivial to apply to vectors, and they can be easily understood in terms of their row and column vectors. (For my project in particular, I really enjoyed the property of easily knowing which octants the basis vectors are mapped to.)

Where are the practical areas where quaternions shine? Are they just useful for the slerp operations that everyone points at, or are there other situations where they're better than rotation matrices?

  • xeonmc 2 days ago

    Quaternions are automatically orthogonal, whereas matrices can shear and therefore may accumulate floating point distortions under repeated opreations.[0]

    Think of matrices as computational instructions, which are straightforward but lossy, while quaternions are the canonical "lossless representations".

    The sweet spot for using quaternions is to use them as intermediate representations of rotation operations, then "compile" them down to a matrix once you are gonna apply it to a vector.

        ijkl_as_matrix = {
            (ll+ii)-(jj+kk), (ij+ji)-(lk+kl), (ki+ik)+(lj+jl),
            (ij+ji)+(lk+kl), (ll+jj)-(kk+ii), (jk+kj)-(li+il),
            (ki+ik)-(lj+jl), (jk+kj)+(li+il), (ll+kk)-(ii+jj),
        };
    
    (Keep in mind that unnormalized quaternion has uniform scaling, so if your quats are unnormalized then your matrix will also apply a scale factor of dot(q,q), so you should divide your final vertex coords by that factor.)

    ---

    [0] For an example, see https://old.reddit.com/gdd8op

    • LegionMammal978 2 days ago

      Does leaving them unnormalized affect their numerical accuracy? A few sources [0] [1] suggest renormalizing often, but I'm not sure whether it's just dogmatic or if it's actually necessary. It definitely seems less involved than re-orthogonalizing matrices, in any case.

      (Luckily, for my project, I'm not particularly worried about error, since the only thing being rotated frequently is the camera, and microscopic scaling and shearing won't affect the result much. I measured the error in the basis vectors over time to make sure, and it just seems to be O(sqrt(t)) random-walk noise, not anything that compounds on itself.)

      [0] https://www.tobynorris.com/work/prog/csharp/quatview/help/or...

      [1] https://stackoverflow.com/a/12934750

      • xeonmc 2 days ago

        As I mentioned, unnormalized quaternions just adds an extra uniform scaling, so it’s just a matter of dividing the final vertex by qq* to remove the scaling.

        EDIT: I guess if you use a shitty overzealously reduced version of the quaternion formula then you absolutely need to normalize it constantly, because the reduced formula assumed three degrees of freedom (completely defeating the purpose of using quats in the first place), normalization is then to solve a problem that you caused. But if you use a proper formula then my recommendation is actually that you never normalize your quats, instead only un-scale your vertex at the end.

    • dahart 2 days ago

      I get what you mean, but it seems misleading to cite floating point issues with matrices and call quats lossless. Matrices are not inherently imprecise, they have floating point error when you use floating point numbers to represent them, and the same is absolutely true for quats too.

      A better word than lossless is perhaps ‘overspecified’ when referring to a 3x3 matrix being used to represent a rotation or orientation. A 3x3 matrix has redundant information in that case (however a matrix is more general and more powerful than a quat). But axis-angle is 4D like a quat too, and more intuitive than a quaternion. Actually normalized-axis-angle (with no scaling) can beat normalized quats, because axis-angle can be a 3D value and quats cannot. Same goes for Euler angles too. In general, if your quats are implemented with floats then applying quat transforms will introduce unwanted floating point scaling that may accumulate under repeated operations (and if you compile to a matrix first then you also have the matrix problem you mentioned).

      • xeonmc 2 days ago

        1. In this application, floating point drift affect matrices far more than quats.

        Drifts in quats results in a drifted value of rotation + uniform scaling, but will never introduce deformation.

        Drifts in matrices may result in total mutilation of your coordinates.

        The overdetermined nature of matrices with respect to orthogonal transforms means that you lose information about which values constitute the authoritative state, whereas it is by definition impossible for a quat not to be orthogonal even when perturbed with significant error.

        As an analogy, think of matrices as retained-mode GUI while quats are immediate-mode.

        2. Axis angle is just the logarithm of [unit]quats (non-unit quat adds an additional scalar to the axis-angle components).

        If you want to compose rotations sequentially, you'll still need to take the exponential of axis-angle to turn it into quats.

        You can author initial state in axis angles as an authoritative declaration of what you meant for the orientation to be, but composing them still invariably requires you to un-logarithm them back to quats, hence what I said about "intermediate representation"

        3. I said compile to a matrix at the very end when transforming the final vertices, entirely sidestepping the problem of repeated operations since you're only "baking" it for the final transformation onto vertices.

    • MortyWaves 2 days ago

      As someone not at all really familiar with maths/3D stuff, your comment gives me some relief that should I want to do any for hobby projects, I can use quaternions to avoid accumulating floating point errors.

  • jfantl 2 days ago

    Quaternions can be useful in robotics when you're trying to perform trajectory planning or any kind of rotation control. Quaternions provide a continuous space where every point represents a rotation, unlike rotation matrices, which exist in a much harder space to explore since most matrices do not represent pure rotations. If you have algorithms for example trying slerp between rotations, or find a path from one rotation to another under some constraint, or sample rotations near the current rotation, then the space of quaternions is a much more practical space to work in.

  • chefandy 2 days ago

    Houdini is all quaternions under the hood— purportedly to avoid gimbal lock. Houdini’s thing generally is doing things the hard way if there’s any possibility it could lead to a better outcome. Unfortunately, without a (fortunately open source at the free-level) plugin, doing things like rotating a bunch of objects on their own local axes means wrangling the quaternions directly in code.

    If you’re interested in seeing their approach, here’s the repo:

    https://github.com/toadstorm/MOPS

  • sampullman 2 days ago

    I found quaternions much more convenient to work with when writing some 3d graphics software for a college course a long time ago.

    It's mostly for the same reasons everyone else mentioned, simplifying interpolation and avoiding gimbal lock. I also found the actual operations much easier to implement. I never developed a good mental model of what they actually are, but tried not to let that bother me too much.

  • cjbgkagh 2 days ago

    For 3D graphics I think the main issue quaternions helps with is for interpolations. Depending on your use case that may or may not be important to you.

  • hi_hi 2 days ago

    Imagine you are creating a 3D flight or space simulator. You would need to use the quaternion system to avoid your camera/craft from experiencing gimbal lock.

    It can have other uses for things like inverse kinematics too.

    • fc417fc802 2 days ago

      Never having done anything aviation simulation related I've never properly understood this.

      At least for a simple camera I've done that with something that resembled a spherical coordinate system (although maybe not following the usual conventions) and it didn't experience gimbal lock. Effectively the axes rotate with you. It's also very intuitive to work with the code, although probably not very efficient compared to other approaches (but hey it's a camera it's not like there's 10k of them). Once you have the camera orientation you can derive a corresponding rotation matrix to apply to the scene geometry.

      I guess what I'm trying to ask is if there's any reason other than efficiency not to do something like that when modeling craft and the like?

      • cwillu a day ago

        How do you represent the viewports roll angle?

        • fc417fc802 a day ago

          Suppose unit X is viewport direction and unit Y is viewport horizontal. Since the axes follow you with each rotation the viewport direction is always the X unit vector even after rotation. So the viewport roll angle is a rotation about X.

          Future user control inputs (ie a 4th rotation step) happen in this rotated space, and then you normalize the result so you only ever need 3 instead of 4 rotations.

          It's been several years since I did this so hopefully I didn't mess up the explanation. Again I realize it isn't computationally efficient but there's only the one camera and it let me have code that was minimal and easy to reason about. Presumably if I were simulating 10k airplanes or whatever this wouldn't be a good idea.

aithrowawaycomm 2 days ago

> These discontinuities are not just an artifact of poor implementation; it can be proven that any representation of 3D rotations using only three values must contain discontinuities.

This is a bit pedantic - and the blog post actually does clarify this - but the problem isn't that a 3D representation of representations has "discontinuities" as such, it's that it's not orientable in Euclidean 3D space. It is similar to the Klein bottle - the mathematical description is continuous, but any Klein bottle made of real-world glass has to intersect itself. Or likewise that a Mobius strip can be demonstrated in 3D but can't be built in Flatland without a 3D entity doing the copy-pasting. Reality just has the wrong topology to represent the full group of rotations. Hence the discussion about projective space later in the blog post.

So adding a fourth gimbal is really tantamount to a correctly-oriented embedding of 3D rotations onto a 4-torus (that is, [0,2pi]^4).

Gimbal lock also relates to another issue of continuity, related to the "plate dance."[1] Rotations themselves have a sense of continuity (infinitesimal differences in either the angle or axis of rotation, aka they are Lie groups), but Euler angles fail to respect the equivalent fundamental theorem of calculus: adding a bunch of infinitesimal changes might say you are at the identity rotation according to Euler angles, but in reality you have flipped the meaning of the right-hand rule and the overall state of the system is not at the identity. In a robotics context, the robot's hand might have done a complete rotation, but its arm is twisted without the robot "knowing." I believe robotic arms used to have a serious problem with this, either overrotating and breaking the arm, or swinging dangerously fast in the opposite direction. Using quaternions / a fourth gimbal / etc. there would be a measurable phase or pole indicating the true state of the system, and letting the robot know how to rotate its arm without malfunctioning. So, like the Apollo mission, the need for a fourth dimension to keep track of that stuff - and even the quaternion multiplication structure - comes about pretty naturally without ever thinking about abstract math.

[1] https://en.m.wikipedia.org/wiki/Plate_trick

  • tagrun 2 days ago

    That statement is also incorrect.

    1. "any representation of 3D rotations using only three values"

    That is not representation, that is parametrization. Euler-angle parametrization sometimes fails because it is not a correct parameterization of SO(3) in general by construction, this is why it sometimes fails (essentially, the three consecutive rotations can sometimes effectively collapse into two for certain set of angles, regardless of how you choose your 3 axes, in which case you can't relate 2 independent parameters back to the 3 independent axis-angle parameters). The correct parametrization of SO(3) is the axis-angle parametrization, which can be represented using quaternions or 3D reals matrices.

    The "representation", on the other hand, would typically be unit quaternions or 3D orthogonal matrices.

    2."it can be proven that any representation of 3D rotations using only three values must contain discontinuities." where is that proof and what discontinuity are you talking about? It sound like he misunderstood what "SO(3) is not simply connected" means. Lie groups are differentiable.

    3 parameters are sufficient to represent any 3D rotation. The natural parametrization of all Lie groups, including SO(3), is the axis-angle parametrization, and their elements have the form exp(i θ n.J) where n is a unit vector defining the axis of rotation, θ determines the amount of rotation, and J is a vector of the generators of the corresponding Lie algebra. The "regular" 3D matrix representation in the axis-angle parameterization is obtained with so(3) generators L_x, L_y, L_z in their fundamental representation. Basis quaternions i, j, k (which can be represented by Pauli matrices) obey the same Lie algebra as L_x, L_y, L_z, but the group that it corresponds to (which is SU(2)) is a double cover of SO(3) (up to a sign), so they can still be used for implementing 3D rotations once you pick a sign.

r1chardnl 2 days ago

If I had to describe Quaternions to someone I would first try to explain a Plane (Ax + By + Cz + D = 0) to them. ABC being a (normal) direction that the Plane is pointed towards and D being the distance from the origin.

A Quaternion from what I believe is just the same but instead of distance it just encodes the rotation around that direction as a fixed axis. (Instead the angle stored is half etc).

Feel free to correct me if I'm wrong, I'm not a math-heavy person.

  • adamhartenz 2 days ago

    If you a explaining this to the average American, you are going to have to start a bit further back than a "Plane".

    • grg0 2 days ago

      Think of a fraction as a pizza. The bottom number is how many slices the pizza has, and the top number is the slices that you are going to eat. Sally will eat the rest, so you need to leave some for her.

    • raincole 2 days ago

      I know it's a tradition to bash Americans, but I'm quite sure in any country, Ax + By + Cz + D = 0 is a plane isn't common sense.

      • renox 2 days ago

        I learned it in the mandatory part of school (I'm French) so while I'm sure lots of people have forgotten it, most did learn it at school..

    • ge96 2 days ago

      In the beginning... there was nothing...

    • xanderlewis 2 days ago

      A plane is just a burrito.

      • kmstout 2 days ago

        And monads are like burritos. Therefore, monads are like planes. How shall we represent diverted luggage?

  • sundarurfriend 2 days ago

    > A Quaternion from what I believe is just the same but instead of distance it just encodes the rotation around that direction as a fixed axis. (Instead the angle stored is half etc).

    The first part kinda makes sense, but I have no idea what the paranthetical is intended to mean.

agnishom 2 days ago

I also recently came across "Geometric Algebra", which seems like an idea of equipping a vector space with a formal anticommutative product. There seems to be a subset of people on the internet who swear by it.

  • Sharlin 2 days ago

    GA gives rise to the bivector representation of 3D rotations, mentioned in passing by the author of TFA.

cantalopes 2 days ago

I was hoping thered be more of actual quaternions