Prof. Steve Lavalle
Department of Applied Mechanics
Indian Institute of Technology, Madras
Lecture – 4-1
Geometry of Virtual Worlds (axis-angle represent)
So, I want to fix this problem I want to pick a different parameterization or a different expression of 3 D rotations that might be a little less intuitive at first, but it fixes all of these problems and once you become comfortable with it, it ends up being very very natural. (Refer Slide Time: 00:36)
So, and is it being the way to do things one first step to that is Euler’s rotation theorem, which is that all 3 D rotations have what is called an axis angle representation.
So, what do I mean by that, if I consider the orientation of a rigid body in space, no matter what orientation it ends up in I can describe that as a single rotation about some axis through the origin. That axis might not be aligned with any of the coordinate axes, just some arbitrary axis that goes through the origin I give it a twist by some amount theta, let us say and that describes the orientation of the body. So, that is Euler’s observation.
Interestingly that is what let us you get away from Euler angles, and into a better representation.
So, Euler’s name is all over in mathematics, but in this case the representation theorem establishes this very important observation about orientation so; that means, that when I look at loops when I look at sorry draw a little bit wrong here, again put up my three coordinate axes. So, when I look at the orientation of an object or a body, then all I need to do is find some access through the origin let us say that is an axis v, and then there is some amount of rotation about that axis or use counterclockwise is the standard convention.
So, some amount of rotation theta about that axis, and that describes the orientation of the body and. So, if we do it in that way and we make an entire algebra, that uses as the basic elements a representation that is access an angle and as I start combining rotations I always keep figuring out what the axis and angle is we end up with something very nice very consistent, and we avoid these strange problems of kinematic singularities that come from combining these yaw pitches and rolls, which seem very intuitive by themselves, but when you combine them you end up with some kind of trouble.
So, it is better to make an algebra that consistently reasons about or represents the axis and the angle. So, I make some kind of sense. So, that is what we are going to do and. So, so generally I have some axis.
(Refer Slide Time: 03:24)
Now, and it is an axis through the origin that is very important always through the origin. So, this vector V we write as ?1, ?2, ?3has three components the length of the vector is not critical here. So, we may as well just use a unit length vector, we could normalize it is really the direction that matters and then there is some amount theta.
So, you could say that the norm of this vector is equal to 1 sum of squares of the compound is equal to 1. There is one fundamental observation I should make at this point which is that this could be the axis. So, if I have this axis of rotation, and I say that the orientation of the body is some amount of rotation about this axis. I could just as easily turn the axis in the opposite direction, and rotate by a negative amount and I will get the same result that make sense.
So, that is something that is arbitrary just by the basic fact that, when I look at this vector it appears to be pointing in some direction. What if I pointed in the opposite direction that should work also. So, that is one unfortunate let us say ambiguity and it eventually goes and causes trouble somewhere and there is nothing that can be done about it, it is just the basic fact about 3 D rotations is that, they all have an axis angle representation they in fact, have two of them and they correspond to opposites.
So, it s like a mirror image problem, but not exactly right. So, just um they correspond to opposites. So, there is a double representation so; that means, that even though I have this I also have this corresponding case where I have minus V, and then I rotate by either minus theta or we could say 2 pi minus theta depending on what do you want to keep positive angles or not. So, that will end up corresponding to the same orientation and I will show you where this comes up in a little bit, think about representing rotations like maybe I just have V and theta.
(Refer Slide Time: 05:25)
That is enough to represent the orientation or the amount of rotation applied to a rigid body and I would like to have some kind of algebra like maybe I take it and I combined it with another one; maybe I combined versus V prime and theta prime, and I get something right I would like to get the resulting axis and the resulting angle, I would like to make an algebra it just keeps doing that.
So, there is different ways to encode these things, should I be worried about the fact that the 4 parameters here? But there is only three degrees of freedom right is this is ?1, ?2, ?3and this is an extra scalar there is four parameters, but note that the length here does not matter right. So, this is removing a degree of freedom. So, we still have really three independent parameters, there we are just trying to make sure that the length is equal to one because the length of this does not matter. At all is providing is an axis that we use to orient alright you know there s different possibilities for encoding these things. In fact, here s one representation which is used in open C V the popular computer vision library, some of you may have spend some time with we can do the following we could say just take theta and multiply it by the V s to scalar by a vector multiplication, and I just get three coordinates like this assuming that V has already been normalized.
So, assume that at the normal v equals 1 at the length of v equals 1, I could just represent rotations like this does that seem reasonable has a nice intuitive feel to it the smaller the rotation in other words, the closest the closer the rotation is to the identity rotation the smaller this vector gets right because theta is getting close to 0. So, the length of that vector has to do with how much are rotating, and the direction of that vector corresponds to the axis.
So, that is a very nice representation this is called exponential coordinates, and you can find these used quite a bit in optimization control theory literature mechanics, still not going to be exactly what we want here because it does not have that property that I wanted which is when you do a small variation of the parameters or you vary the parameters by some let us say fixed amount, then the corresponding rotation change is consistent, it is the same amount of change in rotation that is what I want to maintain this kind of property. So, kind of uniformity.
So, this is one candidate this is embed very easy to take this v and theta and convert it to this form, I am going to do something that is a little more complicated of a conversion, but it will have this nice kind of uniformity property that I want with the parameterization; you know almost feel as good as using theta to represent points on the circle for the 2D rotation case.
Prof. Steve Lavalle
Department of General
Indian Institute of Technology, Madras
Lecture – 4-2
Geometry of Virtual Worlds (quaternions)
(Refer Slide Time: 00:19)
So, what I am going to do is I am going to use something called unit quaternions as a better or perhaps the best representation of 3D rotation. So, I will use q for quaternion, it will look like a 4 dimensional vector. I will give it 4 very simple coordinates a, b, c, d; not going to use x, y, z just to avoid confusion with the ordinary Euclidean space that we are in, but nevertheless you can consider this as an element of R 4.
So, there is nothing strange going on with each of the components. They are just 4 real numbers 4
real parameters and I am going to impose a constraint, they are called unit quaternions which is going to mean that a squared plus b squared plus c squared plus D squared equals 1, hey that’s not bad that gives us how many degrees of freedom?
(Refer Time: 01:33).
So, that is good. So, I just want to make sure that is a sanity check, right, just make sure, we are always keeping the degrees of freedom, right. what is this geometrically? What kind of shape is this?
It is the equation of a.
Student: hypersphere (Refer Time: 01:47).
Yeah, hypersphereor let us just say a sphere; hypersphere that is right. So, it is one dimension higher than the 3D sphere. It is a 3 dimensional surface, right, the ordinary sphere is a 2 dimensional surface that sits in R 3. This is a 3 dimensional surface that sits in R 4, all right. So, you know; you do not have to completely visualize that. So, the set of all q that were allowed to have here the ones that are unit, let me even say it, I will say unit quaternions is a hypersphere in mathematics, it is called S 3 if you are curious.
(Refer Slide Time: 02:20)
So, yes, you can do it in higher dimensions, you can have S n in n dimensions, it is 3 because it is a 3 dimensional surface.
So, the standard sphere that you know is S 2 and a circle is S 1. I should point out that you know, I am going to use this a, b, c, d representation and in unity 3 D and in dimensions, they tend to use a different representation which is x, y, z and w. So, even without being sorted alphabetically correctly and in terms of my representation which I am going to use the one that is consistent with mathematics and most of engineering as well which is b c d a. So, if you want to go back and forth between what I am doing in class and what you might see in unity and other game engines, then you have to take the first element and put it at the end.
I do not want to tell how many bugs I had in my code because of this extra change. So, it is very easy to make mistakes with this. So, so to be very careful; very commonly you will see quaternions especially in math written like this instead of writing it as a 4D vector you will have a plus b i plus c j plus d k perhaps with little hats on these, but not necessarily; so, maybe with or without the hats.
In fact, I am going to take the hats off, let me; I am sorry. So, very often you will see it written like that, if I look at it with just this part a plus b i; that reminds you of what? Complex numbers, right maybe you put a j there, if you are an electrical engineer because somebody used i for currents, right. So, very often people say that there is a real part of the quaternion which is the a and then 3 imaginary parts which are which are i, j and k and then this leads to lots of fear and confusion among students right and so when you deal with complex numbers and you have the real and imaginary parts, those were invented because people wanted to find roots of polynomials, right.
The polynomials have real coefficients and if you want to find all their roots, you need to invent complex numbers to have a kind of algebraic closure. This does not correspond to the algebraic closure of some kind of polynomials. So, there are some reasons for referring to these as imaginary parts because algebraically, they behave similar to complex numbers and become a kind of generalization, but they do not correspond to roots of polynomials and we are not going to do things like that.
So, in this class, I prefer not to make a big deal out of real and imaginary. I do not think that is important here. It is important for you to just think of it as 4 parameters. It is a 4 D vector and we are going to normalize them. So, that they are on a sphere. So, I would say, avoid this kind of you know; let us say fear of something perhaps, nightmarish and algebraic that is happening here, if you find these things interesting go and read about it and learn about it, it is quite fine, I find it interesting, but we do not need to go down that path and so, it is important here just to keep in mind that there are 4 parameters a, b, c, d and if you treat it as a 4 D vector the length is 1.
The next thing I want to tell you is how to encode a 3 D rotation using our a, b, c, d parameters. (Refer Slide Time: 06:05)
So, here I gave you an example of how to encode rotation using exponential coordinates, I just took the theta and multiplied it by the v. I am going to do some other transformation or operation to do the encoding as a quaternion. So, I am going to have V and theta and convert it to a quaternion.
(Refer Slide Time: 06:30)
So, here is the thing, again suppose, I get V equals ?1, ?2, ?3 and I have ?, right.
So, I have 4 parameters that I am using. So, I want to go between V and ? and I am going to have this representation cosine theta over 2 that is the a part I am just going to be a 4 D vector here that is the a part, the b part is going to be V 1 times sin of theta over 2. So, that is the b, V 2 sin theta over 2 that’s the c, V 3 sin theta over 2 that is the d, all right.
So, all I did was write cosine theta over 2 here and then for the remaining 3 components I just took the V vector and did a scalar multiplication times sin theta over 2 correct that is all I did if I square all of these and add them up what do I get right. So, I get cosine square theta over 2 and then these 3 well I get a bunch of sin squares to add up, but I know the length is going to be one because the Vs when I square those and add them up they all add up to one. So, the entire thing is going to when I square all of these is going to add up to be sin squared theta over 2 by the simple trig identity I get one.
So, this is a parameterization that puts points on this unit sphere in 4 dimensional space, right. So, what I want you to get comfortable with is just being able to move back and forth between these 2. So, that when you see this a, b, c, d vector somewhere in the code maybe you are looking at unity or you are reading the code except remember that it x, y, z, w there, but whenever you whenever you see it you should be able to look at it directly and understand how to extract the angle theta and the axis V.
So, it should be very natural for you do not have fear about imaginary parts and other kinds of strange things going on and visualization of 4 dimensional spheres and the spheres in R 4 and things like that. So, 3 dimensional sphere and R 4 do not worry about that just worry about understanding that there is an axis at an angle that is encoded in your and it is provided by this simple formula.
(Refer Slide Time: 09:23)
So, if you understand that let us try to go over some simple examples.
So, these are some useful examples. So, what does that quaternion do if I apply this formula; what does it correspond to?
Student: (Refer Time: 09:46).
What is that; yes. So, this is this should do this should be the identity rotation right it should not do anything if theta equals 0 then all of these components become 0 b c and d and this component becomes one correct. So, this one means the identity rotation it is very interesting that in that case I cannot even tell what the axis was anymore right because they all became 0, does that matter? No, it is the perfect case, it did not rotate anyway. So, there is no axis. So, it seems like the axis just vanishes exactly in the place where there was no rotation occurring.
So, very nice, feels good, does not it; let us see, well; let us try some other very obvious patterns here what does that correspond to. So, that is right, 180 degree rotation about the x axis, right because the axis here for this case must be V equals 1 0 0, right, for this case. So, this is pitch, am I still being consistent without my definitions pitch is rotation about x correct. So, pitch by pi 180 degrees, well, if you know that case should be able to fill in these, this one is yaw by pi, right because it is rotation about the y axis, see how you can look at the last 3 components and if it is simple enough you know the axis of rotation.
In fact, this is just the axis of rotation, but not correctly normalized, right, if you just take the last 3 components and renormalize them, you will get the axis you can you normalize it in this case, but that is because there is no rotation anyway. So, the axis does not matter, but the other cases, if you test for being non-zero, then you can figure out the axis very easily with your eyes not even writing code all right the last one is roll by pi right it needs a very simple example.
So, there really should not be fear of quaternions, I see a lot of fear of quaternion is this it is quite simple once you understand what to look for the first coordinate is the amount of rotation which is inverted right to smaller the number the bigger of the rotation and then the last 3 coordinates are giving information about the axis of rotation let us try another one; 1 over square root 2, 1 over square root 2 0 0.
(Refer Slide Time: 12:43)
So, suppose I see that what is the axis of rotation?
So, again that that is a perfect case where now I have to renormalize it, but it looks like it is about the x axis how far am I rotated yeah very good. So, that is good. So, you are doing you are doing great here. So, 45 or 90.
90 because in the formula here, we divide by 2 so that is how some of you got fooled, right, I make the mistake a lot myself. So, it is no problem. So, pitch by pi over 2. So, I should be able to very quickly go with the other cases one over square root 2, 0, 1 over square root 2, 0. 1 over square root 2, 0, 0, 1 over square root 2.
So, this is very nice if you are debugging your code, you just want to make some simple transformations and try them out it is very nice to be able to just immediately specify some simple transformations like this or if you are debugging and you are not sure you got the code correct. So, it is just very nice to be able to read these quickly.
So, this one is yaw by pi over 2 and roll by pi over 2 questions about that I am going to give a little more information as we as we go along here, but I want to make sure that is clear right. So, now, questions see their means it is very clear or very confusing.
(Refer Slide Time: 14:34)
So, here is something else to think about inverses and multiple representations in other words non uniqueness multiple representations.
So, if I have a quaternion a, b, c, d. Now unit quaternion that is being used to represent a rotation I should probably write back the conversion formula.
(Refer Slide Time: 15:28)
Let me remind you of this cosine theta over 2, V 1 sin theta over 2, V 2 sin theta over 2, V 3 sin theta over 2, all right. So, remember that that is how we got the a, b, c, d components. So, what if I have minus a minus b minus c minus d can anyone tell me how the 2 of these are related.
So, if a, b, c, d corresponds to some 3 D rotation; what is minus a minus b minus c minus d going to correspond to is that the inverse rotation, it is the same rotation is it not that amazing, it is the same rotation because if I take the V and I twist it in the opposite direction, right, I just flip it around I will negate the b, c, d components and then if I negate the theta as well that will negate the a, but it will not force another negation here.
So, that will negate all 4 components so; that means that these 2 are equivalent. So, that is something very interesting to pay attention to so; that means, that when we travel on this sphere right it is a 3 dimensional sphere in 4 dimensions if we go to the opposite side of it, right, it is called the antipodal point right the opposite point what anybody know where the opposite point on the earth is from here.
Maybe it is a good guess I do not know no it has to be in the southern hemisphere. So, you have to change hemispheres in all cases right. So, you know you go to the opposite point on the earth and that corresponds to the antipodal point. So, on this quaternion sphere this unit sphere of quaternions unit hyper sphere of quaternions the opposite point is always equivalent is not that interesting well just to add an extra layer of confusion, all right.
(Refer Slide Time: 17:43)
Let us think about what this one does; what if I have a, but I decide to negate the b, c and d parts.
So, I keep a the same binding gate these 3, what does that correspond to?
Student: I just (Refer Time: 17:55).
Yeah, I decided not to know;
Student: (Refer Time: 17:58).
Student: (Refer Time: 17:59).
If I take, what do I get if I do cosine minus theta. It negates, right or it stays the same well he should say the same ok I see. So, if I move the axis backwards; let us see. So, these should stay the same, right.
(Refer Slide Time: 18:31)
So, let me think about this, if let us go back here, say I have V, I do rotation by theta, I want to now do minus V and the question is let us see; I want to make this correspond to the exact same rotation see from going in the opposite direction yeah that.
Student: I think (Refer Time: 18:59).
Yeah, it is a difficult problem of orientation. You have to visualize it. We have to remain going counterclockwise, right. So, if I want to continue going counterclockwise and apply the exact same rotation; is it going to be theta I minus theta?
Student: (Refer Time: 19:17).
What is that?
Student: (Refer Time: 19:19).
2 pi minus theta, thank you. So, it’s 2 pi minus theta. So, we go in the opposite direction it is 2 pi minus theta, all right. So, if I go 2 pi minus theta, what sign do I get over here? Negative a, all right, good.
So, let us keep that we keep the trig going, all right. So, we are fine. Now what if I decided to do it this way instead and I did not negate the a how are these 2 related. So, if I say by Euler’s 3 D representation theorem that every rotation corresponds to a single rotation about some axis, then if I want to undo that rotation I just twist it back the other way right. So, that corresponds to the inverse does that make sense.
So, if I have some axis V and I rotate it by some amount theta if I now rotate by minus theta it will just undo that rotation. So, in this case this should change the sign and I leave oh yeah this should change this sign. So, I end up getting the opposite right. So, if I keep the axis the same, but I just negate the theta then I will negate these 3 components, but the a will end up being the same.
So, here if I relate these to its inverses is that fine. So, if you are looking at a quaternion a, b, c, d and you want to figure out what its inverse is what do you do just negate the last 3 components is that interesting it is that easy to compute the inverse rotation if you want to do it with a matrix, what do you do? Do you have to compute a full matrix inverse maybe except if it is a rotation matrix? It is just a transpose turns out. So, it is easier it is also reasonably easy in that case.
If this is the case here, then we also can have another case where I just negate the first coordinate I negate a and I leave b, c and d alone and these 2 should be equivalent, right, does that make sense? The same rule is up here if I negate all 4 components, I should end up with these being equivalent and these 2 being inverses that seem, all right.
(Refer Slide Time: 21:42)
So, again just a simplest to summarize if I negate all 4 components it is the same rotation if I decide to negate only the first component or I decide to negate the second third and fourth components together, but not the first one either one of those corresponds to an inverse.
Then nice, so that is one very quick way many times in programming in these kinds of systems. It is easy to make a mistake where you should have the inverse rotation and you instead calculated the forward rotation; in that case you just perform some simple sign changes you should of course, go back and check all of your math carefully and make sure you understand what you are trying to do, but for debugging purposes, it is very helpful to know, you can very quickly spot that you are off by inverse if the signs are wrong. So, that is why I am telling you this trying to give you this extra information all right any questions about that.
There is a direct formula for multiplication of quaternions that do not that does not require you to convert back to rotation matrices I am not going to make a big deal out of it.
(Refer Slide Time: 23:07)
But I at least want to quickly give it to you. So, you know multiplication; right multiplication.
So, let us suppose that again, we have a, b, c, d for quaternions and I will say let p equal just b, c, d part. So, I can treat it as a 3 D vector. Let us just suppose that is a 3 D vector it called p and now if I want to calculate some quaternion q 1 multiplied by some quaternion q 2 to get some third quaternion q 3, there is a very simple formula just using 3 D standard vector calculus I say q 1 multiplied by q 2 is equal to a 1, a 2 that is the scalar part.
The first components of each 1 minus p 1 dot product p 2 so that gives me the first component, now this is actually a 3 that we are calculating.
(Refer Slide Time: 24:33)
And then the remaining 3 components are given by p 1 standard cross product p 2 plus scalar a 1 multiplied by p 2 plus scalar a 2 multiplied by vector p 1. So, this gives me the b 3 c 3 and d 3
I am not deriving this at all there is a whole algebra of quaternions, there is a reason for it coming out this way, but what I want to say that is very interesting about a couple of things one of them is that if you go through this you will find out that it is not commutative this product is that a good thing it is a good thing to guess if you are using this to represent 3 D rotations and 3 D rotations are not commutative it would make sense that this algebra of multiplying quaternions also ends up being non commutative as does matrix multiplication of rotations encoded that way the reason why I will put this and you usually, you will find this inside of a quaternion library you know that you do not have to really worry about the details of it, but one of the reasons why I put this is. So, that you know that you can do the following.
(Refer Slide Time: 25:43)
You could have rotation matrices R 1 and R 2 and you might want to multiply them to get some third matrix R 3.
The thing you could do is you could represent the first rotation as a quaternion q 1 you can represent the second rotation as a quaternion q 2 and if you do this and you apply this product that I have given here and you get a result q 3 the interesting thing is that these 2 results here end up being the same if I go and convert back. So, the arrows here correspond to the standard conversion formula to convert rotations. So, if I go and I convert from rotation matrix to quaternion another rotation matrix to quaternion I do the quaternion multiplication and then I convert that back to a rotation matrix, I will get the same result and you will actually get it with fewer algebraic operations.
So, people tend to prefer to just stay in quaternion land the whole time let us say right. So, you do not ever have to convert back to matrices if you do not want to if you are using a library that forces you to in your code then. So, be it, but I just wanted to point out that because of this nice algebra you can stay entirely inside of quaternion representation. So, it is another reason why people like using them in computer graphics and in virtual reality.
In addition to this property that I mentioned, but I did not prove formally in any way which is that small changes in your in your quaternion parameters correspond to the same small changes in rotation of the rigid body regardless of where that rotations occurring at in the space of rotations and that that is very important another way to say it is that if I were to pick a an orientation uniformly at random what would that mean if it is a 2 D orientation you just pick a number between 0 and 2 pi had random and you would be fine what would I pick at random for 3 D rotation.
If you pick Euler’s angles the all pitch and roll at random you will not end up with a uniform covering of orientations if you pick a unit quaternion at random a random point on the sphere you will. In fact, have perfect uniform coverage. So, so if you do probabilities over transformations as well, it is very important to have this if you are interested in a lot further detail on this subject it is called Haar measure, if you want to study the mathematics of these things.