207 points · by imadr · 5y ago · 154 comments
Rotations of 3-dimensional real space form the topological group SO(3). Naive parameterizations of that group do not form a cover [1], but the group of norm-1 quaterinons, Spin(3), does.
The failure of naive parameterizations, like the Euler angles, to be a cover manifests itself as gimbal lock.
It's important to remember that the the group of rotations in 3d space is represented by _unit_ quaternions. This is a subset of the full 4d quaternion space: a spherical shell around the origin, like the skin of a 3d ball but in 4d.
A 3d ball has a 2d, "flat" skin that is "spherically" symmetric.
A 4d ball has a 3d, "volumetric" skin that is, similarly, "spherically" symmetric.
If we are stuck to that 3d skin, it makes perfect sense that it manages to represent rotations in 3d, which have 3 degrees of freedom and spherical symmetry.
The space of rotations that is formed by this "skin" has two special points: the point where all the imaginary coordinates are zero and the real coordinate is one, and it's dual, the negative one, precisely because we are talking about _unit_ quaternions. (Similarly as there is only two "purely x" points in a unit circle: (1, 0) and (-1, 0)).
These points represent the identity rotation, i.e. "don't rotate at all". This makes sense, if you think how complex numbers work: the effect of multiplying "one" is that it keeps everything as-is, whereas every other "unit" complex number has a rotating effect.
Then as you think the three imaginary dimensions as degrees of freedoms you can start traveling into, from this neutral point, you get all kinds of rotations. The geometry of the "round", "skin-shaped" space ensures that the rotations wrap around the correct way, "spherically". Especially that after you have travelled "tau" (2 times pi) units, you are again in purely "real", "not rotated" state. And the spherical symmetry means that this works similarly in _any_ direction you can travel to.
The only gotcha is that the unit quaternion space contains doubly the space of minimal 3d rotations, because of the negative number symmetry. There's some good arguments why this is especially beautiful and true, but they are a bit beyond me.
The mathematical explanation is that SO(3) is doubly connected, whereas the unit quaternions, like any sphere, is simply connected. At any time, each part of your arm has undergone some rotation from the orientation at rest. Halfway through the move, as you travel from the shoulder down to the hand, this rotation starts at the identity, and changes continuously through one rotation, back to the identity once more. But in the unit quaternions, that path takes you from one pole to the opposite pole. This explains why you can’t untwist your arm.
Sorry if that made no sense.
Edit: Have a look at “Your palm is a spinor” [1], and then at this Phillipine* tradtional dance [2] about 40 seconds in. I knew I had written about this before [3].
[1] https://www.youtube.com/watch?v=fTlbVLGBm3Q
[2] https://www.youtube.com/watch?v=mOO_IQznZCQ
[3] https://math.stackexchange.com/a/383549
* I wrote Thai originally.
Euler angles are fugly and annoying. Quarternions are clean and refreshing.
One of the benefits of knowing about it is to know when it doesn't matter to you. In that case, you can just use Euler angles, as you say. If you just need to express a rotation in terms of three angles, or convert back from three angles (e.g. yaw/pitch/roll [2]) to a rotation matrix, then you don't need to know about quartertonians at all.
But yes, otherwise I agree with you.
To truly appreciate groups like SO(3), a course in differential geometry and differential topology is useful.
Edit: This is all assuming you have no background in mathematics (or, alternatively, physics) at all. If you do, a targeted text can teach you these concepts in a few pages.
Also terminology/definitions are vague too, whether a vector has an endpoint or it something unachored in space is itself not clear from many treatments.
The only reason I went through all that pain was because I kept on coming across articles saying that "quaternions fix the gimbal lock issue you encounter with Euler angles" - now I see people saying in this thread that the assertion is false. I no longer know what to believe, but I do know I never want to go crawling down the Euler/quaternion rabbit hole again!
[1] - https://scrawl-v8.rikweb.org.uk/docs/source/factory/quaterni... - just looking at that code makes me wince!
Nevertheless, I think it's still a good and important idea to work things out concretely a few times and for that all you really need is linear algebra.
That said, the concrete version of your statement is as follows:
SO(3) is best defined as the group of all rotations in 3 space. You then show that this is just all 3x3 matrices that are orthogonal (their transpose is the inverse) and have determinant 1.
You can do this by abstract linearity arguments (e.g. the rotation of a vector times a scalar is the scalar times the rotation of the vector) or by directly writing things out with linear algebra.
The first ingredient is to realize that the rotation in the plane by angle t is a linear map of the plane to itself, and can be represented by matrix multiplication and thus a square 2x2 matrix which sends
(1, 0) to (cos(t), sin(t))
and
(0, 1) to (-sin(t), cos(t)).
Thus the matrix is
[cos(t), -sin(t)]
[sin(t), cos(t)]
this matrix clearly has determinant = 1 and you can verify that the transpose is the inverse. But you could have derived this from general principles that rotations are volume and orientation preserving.
Now a rotation in 3 space must fix some line and then is just a planar rotation for the plane perpendicular to the line. So you can pick a new basis in 3 space corresponding to the line, v, and then two orthonormal unit vectors so that the rotation is just the matrix
[1 0 0]
[0 cos(t) -sin(t)]
[0 sin(t), cos(t)]
for some choice of unit vector v and some angle t. Here you should realize that you need an orientation. E.g. the plane perpendicular to v is the same plane as is perpendicular to -v, but you need an orientation on the plane to figure out the direction of rotation.
Already this should tell you that SO(3) is three dimensional and you have a parametrization of (most of) SO(3) as a point on a sphere together with an angle, so it's kinda like S^2xS^1, except the parametrization breaks down when the angle is pi as you get the same rotation if you pick anti-podal directions and when the angle is zero all the points on the sphere map to the same (identity) rotation. So this parametrization is not a diffeomorphism, it's not even 1 to 1, but it is surjective, and knowing exactly how it fails to be 1 to 1 allows you to understand SO(3) completely because you can think of SO(3) as S^2xS^1 with some points identified.
All of the above relies solely the basics of linear algebra such as what you usually get in a multi-variable calculus course. You don't even need stuff like Jordan decomposition or other more advanced linear algebra topics, just the definition of linear maps, the definition of a "rotation" in 3 space, ideas of orthogonality and the determinant being an oriented volume of a linear map. Most of these concepts are taught in multi-variable calculus as you need them to get volume forms as the result of a change of basis when you are doing integrals over surfaces and volumes.
In terms of 'topological group", the set of matrices with determinant 1 that are orthogonal form a group, as is easily verified via the fact that det(A*B) = det(A)det(B) and det(A^t) = det(A). That is all you need to show that this is a group. It is a topological group in the sense that the multiplication operation is continuous in the inherited norm you expect to get on matrices. E.g. if you write out the multiplication of matrices with the entries being variables you just get polynomials in the product of the two matrices so multiplication is a continuous operation.
When you are working at the elementary level, you don't care too much about whether the matrices are topological groups because you are not going to be using the heavy duty Lie theory machinery, you can write everything out in terms of matrices and maps between them explicitly. It's really good to write things out explicitly a few times and then learn all the abstract stuff because it helps you understand what the general results are really saying. Do not be intimated by people using terms like "universal cover", homotopy, classifying spaces, etc, as you don't need any of that to understand the basic properties of quaternions and the orthogonal groups, but these abstractions have shown to be an very useful way of looking at these spaces so they can help explain what is happening in a deeper way than relying on matrix algebra once you get to the point where you are searching for unifying ideas behind these results. The results themselves can always be proved with elementary techniques.
Not sure this is a useful argument. If two structures are isomorphic, there is no way to tell them apart. If you can't tell them apart - maybe they are the same thing.
Real number under addition are isomorphic¹ to positive real number under multiplication. Yet they are not the same objects.
1- The mapping is f(x)=e^x. See https://math.stackexchange.com/questions/573794/prove-that-m... for the proof
I've taken undergrad topology and algebra if you could explain in those terms (I understand what a covering is).
This is a common misconception.
Euler angles can be used to rotate an object exactly the same way as quaternions do with no gimbal lock. Similarly, you can apply quaternions in such a way that gimbal lock will happen (if you wanted to represent a physical system of gimbals with quaternions, where that is a physical property).
I wrote a short article demonstrating and clarifying this, hope it helps: https://omar-shehata.medium.com/how-to-fix-gimbal-lock-in-n-...
With these two things combined - the two most commonly cited reasons about why to use quaternions (using slerp and avoiding gimbal lock) - what are other reasons to use quaternions? I’m aware that there are moderate compute savings in some cases (a matrix obviously has more degrees of freedom than a rigid orientation). Are there other good reasons to deal in quaternions? There are some reasonable ideas about why not to use them. [3]
[1] https://en.wikipedia.org/wiki/Slerp#Geometric_Slerp
[2] https://www.euclideanspace.com/maths/algebra/realNormedAlgeb...
[3] http://number-none.com/product/Understanding%20Slerp,%20Then...
How else would you compute quaternion exponentiation? I don't think there's really a dichotomy here. When you compute quaternion exponentiation, one natural way to do it (as with complex numbers) is to think of the quaternions as a real magnitude multiplied by a phase. For complex numbers, the magnitude grows according to exp(x) and the phase evolves according to cos(x)+isin(x). This just falls out of Euler's formula. If you know that the magnitude is 1, you take the exp(x) term out, and you end up with a point that moves in a circle.
The same thing applies to quaternions.
I'm aware that there are other ways to compute quaternion exponentiation, but this is just a natural way to do it, especially for people who aren't experts in numeric programming.
>To fix gimbal lock, we must avoid modelling this physical gimbal system.
> In 3D, instead of using 3 fixed angles that we multiply together to get the final rotation, we will:
> 1. Construct a quaternion that describes a rotation around whatever axis we want, and the angle to rotate by.
Said another way, are you conflating gimbal lock the physical property, with gimbal lock the common bug of creating irreversible rotations or is the bug still possible?
And just to be 100% sure, is the approach I'm using the right one: storing a quaternion instead of 3 angles, multiplying, overwriting the rotation value?
And the idea is, _that's_ how you avoid gimbal lock. It's that implementation of multiplying, and overwriting, which can be done with any system that describes and applies rotation, including Euler angles, rotation matrices, etc.
http://number-none.com/product/Understanding%20Slerp,%20Then...
And that's funny...I had the exact same experience in a job interview...which gave me the extra motivation I needed to finally write that up!
Would you mind elaborating?
``` rotationAroundX = Quaternion.fromAxisAngle(angle1, Xaxis); rotationAroundY = Quaternion.fromAxisAngle(angle2, Yaxis); rotationAroundZ = Quaternion.fromAxisAngle(angle3, Zaxis);
cubeRotation = rotationAroundX * rotationAroundY * rotationAroundZ; ```
Basically, you store 3 quaternions, representing 3 angles, and combine them to get the final rotation.
You might say "This is just quaternions emulating Euler angles!" and my answer is, sure. You can say the same about rotation matrices. There's nothing inherent about rotation matrices that makes them susceptible to gimbal lock. You can implement them as representing 3 fixed angles, thus gimbal lock, or you can implement them as accumulating rotations, thus no gimbal lock. Same is true of quaternions.
The fact that you can get gimbal lock with quaternions is a feature, not a bug. Quaternions are just one way to describe rotations. Gimbal lock is a natural phenomenon of certain physical rotation systems, and can be described whether you use quaternions, or matrices etc.
I don’t entirely agree with the article’s viewpoint that people do not perfectly understand quaternions and therefore they should not be used, as I get the feeling there are many parts of 3D graphics that are not perfectly understood by developers, and that’s okay.
This is an issue with the GLTF format. They chose quaternions to represent rotations in animation and as such can't easily represent what the artist's intent was.
You might claim you can sample the Euler animation and split it into multiple quaternion slerps but that brings up another issue which is you need support for discontinuous animations in order to handle other situations (another thing the GLTF format apparently didn't consider).
Quaternions are not always right, but they are the right default. If you want Euler angles, you can always translate to-from quaternions. Quaternions are independent of the way you set up the coordinate system and each axis is equal.
Unity, for example, uses quaternions internally. It exposes getters and setters for Euler angles that do the conversion to/from quaternions as a convenience. The editor edits Euler angles but they disappear as soon as you are in-game, and if you open up your scene file in a text editor, you'll see m_LocalRotation with the x/y/z/w of a quaternion. I believe Unreal is the same way.
Honestly, that just makes too much sense. Trying to do a physics simulation with Euler angles is just adding extra steps, because Euler angles are not easily composable. If you want to compose two Euler angles to get a third, the easy way to do it is to convert to quaternions, multiply, and then convert back to euler angles. You can see Euler angles in the editor when you are animating a model, but most of the time you are just dragging stuff around on screen or matching mocap data and quaternions make 100x more sense than Euler angles for representing that stuff.
My sense is that any code which does a lot of trig, when there's an obvious way to write the code that does no trig, should probably be rewritten to eliminate the trig. A little bit of sin/cos/tan is fine but as soon as you are doing round trips with acos/asin/atan, you have to start considering where the branch cuts are.
> However writing a rotation directly in quaternion form isn't really intuitive, what we do instead is convert an Euler angle to a quaternion then use it for rotating.
http://motion.pratt.duke.edu/RoboticSystems/3DRotations.html
If you are not careful, this is what you may end up with: https://www.reddit.com/r/FIFA/comments/9gms3n/most_realistic...
0) Very nice practical introduction to quaternions and their application to rotation.
1) Neat didactic "textbook" implementation, but note that it is not production quality (eg potential overflow in the norm function unnecessarily). That was not the aim, either, but just something to bear in mind.
2) As a supplement, a useful practical reference for rotations in 3D (with good clarifications and basically all formulae you'll ever need, but no implementation) is
Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors by James Diebel
https://www.astro.rug.nl/software/kapteyn-beta/_downloads/at...
The interactive videos alone are quite the technical feat, but after going through it, it's honestly hard to imagine fully understanding this topic with less technology (or with a less incredible teacher!)
The 4 components can be re-written in terms of 3 variables for an orientation vector and 1 variable for rotation about that axis. Basically "point this way and rotate this much". The 4 variables are expressed as two complex numbers.
That helped me understand what the quaternions are actually describing. Incidentally, it also kind of explains why 3 variables isn't enough, and so the regular rotation thing must not be sufficient.
Many chefs are brilliant, but only Jeremiah Tower's book covers will tell you he's brilliant. Many branches of mathematics are of great utility. Geometric Algebra will breathlessly tell you this. I know few fields quite so evangelical.
If you don't know better, you should use quaternions rather than matrices. If you don't know better, stick with quaternions and avoid the generalization presented by Geometric Algebra until the benefit is clear.
This tension is probably why the field is so evangelical.
Quaternions are inevitable. In ten thousand runs of the simulation, sentient beings would come up with quaternions every time. Geometric Algebra is not so inevitable. An aesthetic awareness of the centrality of ideas guides some but not all mathematicians. Like that famous quote about taking an instant dislike to Ted Cruz, it saves time.
The abstract mentions it: “This paper gives one answer by presenting a new kind of spline curve, created on a sphere, suitable for smoothly in-hetweening (i.e. interpolating) sequences of arbitrary rotations.” And the final punch line is section 4.3, then you can work through the details in the earlier sections.
Basically it extends Hermite splines to Quaternion splines using the Lie group operations.
Is it really a vector in the physical sense? People often say vector when they mean N-tuple -- for example we learned in high school that vectors are just N numbers taken together.
For physicists, a vector must satisfy certain transformation laws - it must transform in the correct way if a rotation is applied, and the scalar product must be invariant of the coordinate system, IIRC. I don't have enough intuition of quaternions to say how they behave under transformations, though. I would be surprized if you could have "proper" vectors with four components in three-dimensional space.
If you are dealing with an ellipsoid of revolution, then vector methods can also get tricky.
w + xi + yj + zk
Where w, x, y, and z are real and i^2 = j^2 = k^2 = -1 and i
j = k, ji = -k, jk = i, kj = -i, ki = j, ik = -j.Being u = (x, y, z) = x
i + yj + zk a unitary vector parallel to a rotation axis, it is possible rotate any vector q with a theta arc around u by doing:pqp'
where p = cos(theta/2) + sin(theta/2)u and p' = cos(theta/2) - sin(theta/2)u .
I am telling you Quaternions have HUGE issues. These issues become much more apparent when you deal with physical objects.
Here's the thing Quaternions don't exist in reality. It represents an orientation of rotation but it completely masks the path took to achieve that orientation.
For every gimbal in reality there is an actual YawPitchRoll (YPR) that was executed to achieve that orientation. AS soon as you convert that real YPR into a Quaternion you lose the YPR that was needed to achieve that orienation.
So let's say I need to have one gimbal imitate the position of another gimbal. I take the YPR given to me by gimbal "A" convert the YPR to a Quat, send that Quat over the wire to Gimbal "B" and convert that Quat back to YPR to feed to the gimbal so it can rotate itself to imitate the orientation of gimbal A.
The quat is a higher entropy form of information. Now when converting back to YPR there are MULTIPLE YPRs that yield the same orientation. You can derive a YPR that is out of bounds of the physical gimbal.
Literally you can get a YPR that tells your gimbal to Yaw 190 and pitch all the way back past 90 to 170 degrees and roll 180 degrees until it's right side up. This YPR is identical to a yaw of 10, a pitch of 20 and 0 roll. Quaternions hide the original YPR, you lose information so when you receive a Quaternion it's hard to translate it into a physical realization of the orientation.
The company I work for doesn't realize this. They used Quaternions from day one and we have all kinds of headaches like this when we try to extract the YPR and use these orientations in the real world. Actually I should say only I have these headaches. A lot of people haven't figured out this problem yet.
The only time you should use Quats are if you need to transform an orientation or you're dealing with virtual objects that have no rotational limits. Everybody thinks quats are magic and better. They are not. They have huge downsides. Huge.
Compressing it all into one orientation may work for some use cases, but generally one should not expect that. Similarily one wouldn't try to represent all of the axes of a KuKa arm robot with just one orientation. You need info about the individual axes when you want to control it.
Your company can still use quaternions to represent the rotations of each axis, tho. Might help in convincing them going forward, as they don't have to let go of them (they are good fellas actually).
I don't understand this can you elaborate? My company does indeed use motorized pan tilt units.
As said before you still want to retain the actual angles somewhere.
Depending on your physical configuration one of the Euler angle variants (Tait–Bryan angles is another term to search for) could perfectly describe your case and you could just use these to store the angles. Euler angles can also be converted to quaternion. But you can't recover the original used angles from the quaternion alone because of the 2pi wrapping of angles.
For others still wondering why quaternion or orientation alone won't suffice, here is a different example: Imagine an axis which can rotate more than only one revolution, i.e. can have angles like 4 pi, for example motorized volume knobs. An orientation alone can't represent that as it wraps the angles to 2pi. User manually turns the knob to two full revolutions (4pi) and then increases the value by remote control, which results in motorized knob to rotate. Should the knob turn back to nearly zero revolutions (rotate back 4pi) plus the increase? No, it should rotate to 4pi+increase.
By the way, when you have internally stored the "multi revolution" angle and have a sensor reading in range [0..2pi[ you can recover an multi revolution angle equivalent to the sensor reading with wrapToPiSeq( angle in radians before, sensor reading).
# to equivalent angle in [0,2pi[ range
def wrapTo2Pi(rad):
return rad % (2*pi)
# to equivalent angle in ]-pi,pi] range
def wrapToPi(rad):
return wrapTo2Pi(rad + pi) - pi
# angle difference between rad0 and rad1, in range ]-pi,pi]
def angleDiff(rad0, rad1):
r0 = wrapToPi(rad0)
r1 = wrapToPi(rad1)
return wrapToPi(r1-r0)
# rad1 to equivalent angle so the jump from rad0won't be greater than |PI|
def wrapToPiSeq(rad0, rad1):
r0 = wrapToPi(rad0)
r1 = wrapToPi(rad1)
diff = wrapToPi(r1-r0)
return rad0+diffI would say you obscure it. You can certainly calculate it from the 4 quaternion parameters.
SolveSpace (Free CAD software) can be used to design assemblies and mechanisms from a set of parts with constraints. You can certainly build a gimbal with it by constraining the pieces. If you do it correctly, it will be possible to re-orient the final 3DoF part and the constraint solver will solve for the angles (assuming you built it that way).
Internally we treat all object orientations as quaternions, so this would just be using the algebraic constraint solver to find the angles. In practice there will be closed form solutions - with problems at gimbal lock.
No it is an actual information loss.
There are multiple valid YPR solutions like my example illustrated.
There is NO way to determine which YPR out of the multiple possibilities was the original solution.
Something like solve space can only determine the original YPR with additional assumptions or the original YPR still referenced in memory.
A quick Google search even turns up a Wikipedia entry on this problem: https://en.wikipedia.org/wiki/Conversion_between_quaternions...
If you really need this problem solve I might know someone willing to do paid consulting on it.
However, how the Ypr was picked by the hardware must be explicitly encoded as an assumption that must be part of every conversion from quat to ypr that happens downstream.
I'm in the defense industry as well and guess what? Basically most people don't know any better.