Alison's New App is now available on iOS and Android! Download Now
Virtual Reality
Prof. Steve Lavalle
Department of Applied Mechanics
Indian Institute of Technology, Madras
Lecture – 4-3
Geometry of Virtual Worlds (converting and multiplying rotations)
And then next time, we can get into homogeneous transformations which combines rotations and translations.
(Refer Slide Time: 00:24)
So, we think about conversions now. So, we have we started with the rotation matrix and I gave you yaw pitch and roll and so, that is the first thing we did, and you can go back and forth between these, but you have to be very careful about the problems I talked about the kinematic singularities, the non uniform representation, and just generally because of non commutative it gets complicated.
So, when you have individual rotations and you are not combining them it is nice, but that is because it is based on three different axis angle representations, that happen to be the coordinate axes right then we went down the path of axis angle representations. So, I mentioned over here you can convert back and forth between quaternions and rotation matrices. So, we can get to quaternions using this q representation, unit quaternions.
So, you may want to convert back and forth between these I am not giving the exact conversion formulas here.
(Refer Slide Time: 01:46)
If you would like to look them up it is very easy to do a search and find those, you can also find them in my planning algorithms book I cover all these things in the context of robotics, but it is the same kind of things. So, it is in my book. So, just you look for that it is online for free, and then it is very simple conversions between this and axis angle representation, which by the Euler rotation theorem as I said it is a very natural way to describe rotations.
So, there is a very easy relationship between axis angle and quaternions, it requires some computational expense to go back and forth between rotation matrices and the others. So, very often people like to work directly in quaternion space, because makes it very natural for access angle kinds of computations and representations as well. And all of this will turn out to be advantageous when we do things like head tracking and we want to avoid kinematic singularities.
So, that is I think just about where I want to finish, it one when we had one last thing here which is the order of operations to pay attention to here and this is just one so, the final reminder about non commutativity and to pay close attention to the order.
(Refer Slide Time: 03:18)
We have a question of if I have rotations ?1, ?2or I have quaternions ?1, ?2 there is the interesting question of which is applied first.
So, for example, let us suppose I have some point p equals x, y, z and I calculate p prime equals R 1 p, and then I calculate p double prime equals R 2 p prime, and now I like to combine both these matrices together. So, I want to figure out what is p double prime is it R 1 R 2 p or p double prime equals R 2, R 1, p.
So, which one of these is it?
Student: (Refer Time: 04:43).
(Refer Slide Time: 04:33)
So, this one. So, a lot of people do not like this, you know like the fact that wait a minute if R 1 came first why is not R 1 appearing first right. So, it is backwards. So, a lot of people call this would say this is counter intuitive, this is the way the algebra works because these matrices are acting from the left side and. So, they come in act you know. So, that if you look at it by putting parentheses, in you can understand it like this right you say first this gets applied and then this gets applied.
So, if you add parentheses and remember associativity, which does not matter for matrix multiplication right in these at this stage, when we were chaining together rotations this does not matter because we have a group of matrices that are being multiplied, but nevertheless if you add the parentheses back then you can keep track of it like that and remember that this is occurring first and then this. So, the order of operations matters because of non commutativity, and just when you see a chain of matrices make sure you are correctly interpreting the order of operations.
So, it is very easy to get that backwards, it is a common source of mistakes and another thing is when you would do inverses as we have talked about just make sure you are applying the proper inverses.
(Refer Slide Time: 05:56)
So, if I take R 1 R 2 R 3 and I invert that just recall that you get R 3 inverse, R 2 inverse, R 1 inverse right. So, that is just a basic property of group theory it applies all over the place, when
you are dealing with algebra that is commutative you do not care, but when you have non commutative algebra which we found ourselves dealing with because of 3 D rotations you have to reverse the order of the matrices when you apply the inverse and distribute it across each of the matrices right.
So, just something to pay attention to. So, if you are going to invert a sequence of quaternions, the same thing is going to happen you can have to reverse the order of the quaternions, and then apply the inverse to each one, which is just these simple sign changes that. I told you about right questions about that alright. So, I am going to finish up for today next time I will talk about homogeneous transform matrices, you can read in chapters believe 6 and 7 of the surely graphics book, if you would like some background on this.
Thanks.
Virtual Reality
Prof. Steve Lavalle
Department of General
University of Illinois
Lecture – 05
Geometry of Virtual Worlds (converting and multiplying rotations, cont’d)
Good morning, let us continue for the next day of lectures. So, one thing I want to remind you of which I finished up last time.
(Refer Slide Time: 00:26)
We were talking about 3 D rotations and we went through four different representations. So, there is the native representation of rotations which is the rotation matrix 3 by 3 matrix for the 3 D case and the first thing we gave was yaw pitch roll representation which we talked about this; these canonical rotations right.
We had yaw being looking like a no gesture we had pitch looking like yes and roll being like this and we also gave you unit quaternions as a better representation and the reason why was better is because it is very closely connected with another very natural representation of rotations, which is axis angle representation due to Euler's rotation theorem that every 3 D rotation can be represented as some amount of twist if you like about an axis through the origin and that is very natural and very helpful when you start doing algebra of rotations you start combining rotations.
This; the algebra unit quaternions will combine the axes very well and. So, the transformation between these 2 was very simple right we had a very simple formula for that not difficult at all it is also computationally efficient. Transformations going back and forth between quaternion yaw pitch roll and rotation matrices are a bit more cumbersome there is a very simple matrix full of quaternion components it ends up being very simple expressions in the matrix. So, it is not too hard to go from quaternions to rotation matrix if you want to go from your pitch roll to matrix you just multiply your yaw pitch and roll matrices together. So, that is not very difficult either, but it may be time consuming or expensive in terms of computation you know try it by hand and you will see yeah.
However if you want to go from R down to these, then you have to do considerable more work, it usually involves some equation solving and then there are sometimes numerical issues because you are essentially finding numerical roots, but it all has closed form expressions there is code for doing that it is not so bad, but you just have to pay attention, if you write such code yourself that there are some subtle numerical issues going from R down to here. So, generally because of the way these workout people very often prefer to remain in this part of the space and not do other conversions right.
So, if we can work directly in there, then there are a lot of advantages and that is one of the reasons why I gave you quaternion multiplication as an alternative to the matrix multiplication. So, that you do not have to convert to matrices you can just stay completely; completely and quaternion land and do your algebraic computations there. In fact, you can go further and do your quaternion transformations there.
So, if you want to just sorry I should say you can do your rotations directly using quaternions. So, I just want to give you a quick expression for that I am not deriving the algebra of that here I would take considerable more time, but suppose you have a point X, Y, Z that you would like to transform I will extend it by one dimension.
(Refer Slide Time: 03:24)
So, I will just write it like this X, Y, Z, 1 and I will pretend that that is another quaternion and now if I want to rotate this point X, Y, Z that is let us say perhaps it is in well just say anywhere on R 3 if I want to rotate this, then I get some p prime that I calculate as q quaternion multiplication with p quaternion multiplication with q inverse and this q is representing the rotation that I want to apply to the point X, Y, Z right that makes sense.
So, in other words, let me summarize give and q some rotation that I would like to apply it does of course, correspond to a rotation matrix, but instead of converting to that rotation matrix I can directly do this algebra and apply q to p using this chain of 2 multiplications in order to get out the results. So, the resulting p prime that I get, I just read off the first 3 components and that will tell me the rotated point. So, so the quaternion algebra has been nicely defined in a way that makes this work. So, that it gives you the same result, but I am not deriving it here you can look that up on your own does that make sense and you remember how easy it is to make you inverse right. (Refer Slide Time: 04:57)
For example, you can just negate the last 3 components speaking of that we had an interesting question last time which I just want to cover we talked about the double representations of every 3 D rotation when you use axis angle and so if you have axis v and some amount of rotation theta then if you take minus v I said that it is equivalent to a rotation of minus theta and then when we look at the formulas for converting from quaternion to axis angle we notice that there was a sign problem well normally if I apply trig functions to some angle ? if I add 2? to it the trig functions yield the same result right.
So, it seems like it should not matter, but it turns out it does matter for this axis angle conversion formula because it has a term that says cosine theta divided by 2. So, the fact that you are dividing by 2 here means that if you do add 2? to it. It will give a different result right because you are dividing by 2 before applying it. So, it is better if you are using this formula to write it as 2?−?.
So, that is where that little bit of confusion, came in last time and it seemed like the sins did not work one more thing I want to say about quaternions and then we will get on to the next bit of topics, remember that we have this 2 different representations there are 2 different unit quaternions for every rotation what could we say about those quaternions where are they located on the unit sphere opposites right are called antipodal points.
(Refer Slide Time: 06:45)
So, one thing that happens is suppose you are rotating your head around and you have some tracking algorithm and it is essentially moving through that space. So, this is let us suppose that sphere that sits in four dimensional space it is a 3 dimensional unit quaternion sphere and then as you move your head around you are essentially moving along the sphere somehow right is that an kind of visualization. So, imagine the quaternions changing. So, as a function of time you are moving around this is transformation moves along this sphere.
Now, I can in doing it as a sphere I can imagine it has an equator. So, that sometimes I am in the northern hemisphere and sometimes I am in the southern hemisphere what does that mean well above I could be saying it is a greater than 0 and below I could be saying it is a less than 0 where a remember a quaternion is a, b, c, d. So, I could go moving around and if I am not careful if I start doing lots of transformations like just going back and forth converting.
I should say lots of conversions if I do lots of conversions between rotations it may be the case that if I convert one way and come back I might all of a sudden jump to the opposite side of the sphere and I might conclude that something absurd has happened with my tracking algorithm right how did I end up on the other side it looks like a huge error, but in terms of rotation it may be exactly right because it just jumped to the opposite point.
So, that causes a lot of trouble and confusion sometimes encoding these algorithms and in dealing with these kind of tracking algorithms and a lot of transformations in these spaces. So, some people might like to say I will just prohibit the lower hemisphere for example,. So, I may just say stay away from here and so, that as soon as you come down the northern hemisphere as soon as a is changing and getting smaller and smaller if it hits 0 you could just jump to the antipodal point and then continue upward does that make sense. So, you can keep it trapped in the in the upper hemisphere the northern hemisphere in this case if you if you view north being a increasing.
So, you can force to change; however, there is still some subtleties right when you come exactly to the equator because it is its not it is not exactly clear what you could do what you should do exactly on the equator because you can still jump to the antipodal point along the equator itself and you may have to protect that and divide that in half and. So, forth as you go all the way down to the final remaining dimension. So, it is a bit complicated at the equator itself, but I just wanted to point that out this 2 to one representation that we have sometimes causes confusion if you make very simple tests like how far apart are these 2 quaternions using simple Euclidean distance.
They may be. In fact, very very close together, but they appear very far apart and attempts to fix that will help, but you have to be very careful about them. So, it helps to visualize it on a sphere I think and just remember that it is one dimension higher questions about that alright. So, I am going to move on to the next topic I am getting up the homogeneous transformation matrices today I want to remind you of a couple of properties of matrices before we get to that which I believe I mentioned last time, but I will quickly mention them again here if I want to for example, rotate some point X, Y, Z.
(Refer Slide Time: 09:55)
So, I put it in vertical column form and then I apply a rotation matrix to it just write it as R this is what I get right. So, I do this multiplication and I get out the result if I want to then apply a second transform a second rotation to the result of that the rotation comes out here correct. So, if I wanted to first apply the rotations together by multiplying the matrices it is just important to understand right.
So, if I were instead were to first do these multiplications and then apply the result it is just important to understand that the first one happening is R 1 getting applied and the second one is R 2. So, it goes in this direction from right to left and since in many languages, we are a very custom reading from left to right it annoys people, right.
So, it looks like it is going backwards. So, it is just something to pay attention to it is easy to get confused by that and remember the other thing is that if you want to make an inverse of a chain of matrices.
(Refer Slide Time: 11:03)
For example, if I want to have R 1, R 2, R 3 inverse just remember and this is going to become important as we start chaining matrices together and if you start digging into the software R 3 inverse R 2 inverse R 1 inverse and this is very easy to see if I just take my R 1, R 2, R 3 and I apply this inverse next alright.
So, that is the next thing I apply. So, a state that goes on this side. So, if I apply this inverse next here I get R 1 inverse R 2 inverse R 3 inverse and then you can just follow all of these cancellations.
So, R 1 times R inverse that should be the identity and then the whole thing collapses together and then these 2 cancel and then it collapses together and then these 2 cancel and you just get the identity right a matrix times its inverse should give you the identity. So, that is what we get right.
So, just remember that just basic property of shows up in group theory which is very general. So, this is the reason why it holds here because of non-commutativity, we have to be very careful, I
will give you a great example of that in just a little bit alright questions probably seen these things before somewhere, but just emphasizing that they are becoming important now there may or may not have been immediately useful to you before.
Virtual Reality
Prof. Steve Lavalle
Department of Applied Mathematics
Indian Institute of Technology, madras
Lecture – 5-1
Geometry of Virtual Worlds (homogenous transformation)
(Refer Slide Time: 00:15)
All right, let us do homogeneous transformation matrices, one of the reasons why I want to jump to this now is I want to combine rotation and translation, because I need to use both of those operations to be able to place a rigid body anywhere I like in the world because remember these are the movable parts of our model the moveable models. So, one way to achieve that is to do rotate rotation followed by translates.
So, in other words, I get my transform point x prime, y prime, z prime by multiplying out this rotation matrix R which I will just again not write all the entries involved just put points here. So, that is a valid rotation matrix, and then X, Y, Z is my 3 D point, where I would like to rotate we agree that that is just simple rotation that we have been doing using the matrix, and now I add to it some translation I do want to do, I was using this notation last time he was ??, ?? ??? ?? for translation. So, if I go ahead and do this,
I should be able to generate any position and orientation for the rigid body, how many degrees of freedom do I have here.
Student: 6.
6, correct. So, I have 3 different parameters here I can apply for translation, and then I have 3 independent parameters I can apply in here after we satisfy all those constraints I gave last time for a rotation right. So, that gives a full 6 degrees of freedom, again we could do this part of the computation we are using quaternions, but I am doing all matrices today at this point because of some of the particular transformations I will beginning into. There are interesting questions and about when to convert the quaternions and why at different various times.
So, one thing to note is that the rotation part is a standard linear transformation, but this addition part comes outside of that because I am not multiplying a matrix here in the front right. So, I am not multiplying and adding matrices, I somehow I do not have a simple equation of the form y equals A x, I have added some extra term to it. So, one simple trick that people like to do is just extend the matrix by one dimension, add another row add another column and then you can get this expression into a form that looks like this or there is just a single matrix. So, it is just a kind of let us say mathematical or algebraic hack, and we end up getting something that is algebraic equivalent.
(Refer Slide Time: 03:17)
So, let me just you know there is no real magic to it other than just recognize that it algebraically computes the same thing. There are some deeper interpretations having to do with projective geometry, but I do not need to go into that for this part. So, we make a 4 by 4 matrix that is algebraically equivalent when applied to a point. So, what I do is I take my x,y,z point over here, when I just applied the rotation and translation in the standard way my point is just x, y, z, over here I am going to extend the vector by one dimension and just put a one at the end. So, I am going to have the point look like this I will put a one at the end, but I will just know that this one is something extra that I am using to perform an algebraic trick.
So, I am not really increasing the dimension of the point itself, and then I am going to calculate some x prime y prime z prime and the results going to be one for that, and now have a 4 by 4 matrix, and the upper the upper left 3 by 3 component is just going to be the rotation matrix. So, this will be the rotation matrix R just copy it exactly from here and place it in, and then I am going to take my translation parts and put them down here ??, ??, ?? and the bottom row is going to look like the a piece of the identity matrix.
So, it would be 0 0 0 1 and then I will apply this matrix to my new 4 dimensional vector x y z 1. So, I go ahead and perform this multiplication now, do we believe that we get the same result as we would from performing this multiplication right.
It should not take too much to see the algebra of that for example, if I just look at the calculation of x prime, I get I get the upper row of the rotation matrix the first row of the rotation matrix applied to this column x, y, z here right. So, that gives me exactly the rotation part, and then because I am carrying this one along here right, I get because I am carrying this one here this one will multiplied with this x t and just add it on right when I take this inner product between this 4 dimensional row, in this 4 dimensional column I add on this ?? that is exactly what I get over here right I am just adding on this ?? for the translation.
So, it is just a very simple hack that converts it into what is called homogeneous form. So, this is a homogeneous linear transformation and if by the way you want to add some extra part to it where there is an offset vector, then this is called an affine transformation. It has very different properties and sometimes it is difficult to work with and you know just algebraically a bit difference. So, if you want to start chaining matrices together, and have there be translations and rotations happening all the time then you do not have to switch operations, you do not have to sometimes add on a vector sometimes multiplied by a matrix. If you use homogeneous transformation matrices then you are always just multiplying matrices to achieve both rotations and translations.
So, that makes it very nice people who do work in computer graphics who want to code algorithms very efficiently and say GPUs, would like to use the same piece of code or the same circuitry let us say over and over again. So, that is another motivation for putting everything into one standard form and just performing the same algebra every time. I have an interesting question for you. Now, what is the inverse of this right. So, this is a homogeneous transformation matrix this 4 by 4, that performs rotation followed by translation. Well before I asked about the inverse completely though before you answer that we should also understand what is the axis of rotation here right so.
So, if I translate first and then rotate is that the same as rotating and then translating, right. So, if I translate first then have effectively moved the axis of rotation. So, you could do that you could consistently go in to find things that way; we are going to do it and be easier to interpret way which is first apply the rotation so, that we when we think about the axis angle representation, we are rotating exactly about that axis in the axis angle representation.
So, you grab the book here. So, if we are if this were the origin you first apply the rotation and then translated somewhere. If you were first to translate it somewhere then the rotation would be offset by quite a bit right depending on how large this displacement was that was induced by the translation. So, it is something to pay attention to alright. So, these operations do not commute.
So, if I take the inverse of this I need to pay attention to that.
(Refer Slide Time: 08:54)
So, just something to warn you about and so, what is T inverse and I have not written exactly what T is; T is this 4 by 4 matrix. So, will say T is the 4 by 4 matrix.
So, this part here that is just the matrix T; so, what is T inverse let me write a candidate for it. So, is T inverse equal to well why do not I just invert the rotation matrix what is the inverse of a rotation matrix.
Student: (Refer Time: 09:37).
Just the transpose right. So, this only happens for what are called orthogonal matrices; and orthogonal matrices are also have might you might want to call them orthonormal matrices, because the columns are have been normalized as well. So, not only are they orthogonal columns, but the lengths are normalized as well the magnitude of the column vectors. And so, if I do that I could just maybe let us say take the 3 by 3 part here and I will just put R transpose in there, because R transpose equals R inverse that does not work for general matrices, but it works here.
So, we can go ahead and do that, and now let us see why do not I just negate the translation. So, I could that is the inverse translation right. If I move forward five meters the inverse of that is moving backwards 5 meters. So, why do not I just negate that. So, I can go minus x t minus y t minus z t and then I complete the rest of this matrix 0 0 0 1 how is that is that the inverse.
Student: No.
Why not?
Student: In case its every (Refer Time: 10:48).
Ok, very good. So, so the operations in the wrong order here. So, if you apply this matrix it is going to first unrotate, and then untranslate right, or first inverse rotate if you like and then inverse translate that seems fine except for that point that I made a little bit earlier when you have non commutative algebra you have to swap the order of operations, when you take an inverse right you have to invert the order of operations. So, this is not going to work. So, the answer is no for that.
So, let us write what the inverse actually is, it is very helpful to observe these kinds of things when you are writing code, when you are doing development for these systems, I found it very helpful myself when doing development and oculus just being very quickly able to find inverses change order of operations fix different kinds of bugs that happen. Very often you can get things working most of the time and then doing something catastrophically bad like maybe the yaw and the pitch will be aligned correctly and then the roll is backwards for some reason.
So, lot of things happening like this, and these are the kinds of tricks I think that are that are helpful the kind of insights let us say, that are helpful that can save you a lot of time in trouble.
(Refer Slide Time: 12:01)
So if we go T inverse; for the first thing I would like to do is undo the translation right because that was the last thing applied. So, to undo the translation I will put it over here, I will make a matrix that undoes the translation. If I want to make a perfect matrix that undoes the translation, what should go inside of here?
The identity rotation, which also just looks like the rest of an identity matrix, so that just undoes the translation that gets applied first, but remember that I am going from right to left that is why I made a big deal out of that as well. So, we are in from right to left that gets applied first and then we come over here, and in this case we undo the rotation. So, I have my R transpose there and then I fix the rest of this matrix here, I had better not do any translations here now, this is a pure rotation. So, I just put zeros and I complete the bottom in the same way always for these I do not need to multiply that out and simplify it you can go ahead and do that there is not you know this is a fairly generic matrix here anyway. So, I like to keep it in that form. So, once again to summarize T is rotation, then translation when applied to a point and T inverse is inverse translation, then inverse rotation.
Questions about that.
Virtual Reality
Prof. Steve Lavalle
Department of Applied Mathematics
Indian Institute of Technology, Madras
Lecture – 5-2
Geometry of Virtual Worlds (the chain of viewing transformations)
The next part of transformations that I want to get to is called viewing transformations. I want to build up enough transformations that. So, that we can place our models into the world, and also define the stationary part of the model in the world. And then figure out how it should look on the screen based on which way we are looking with our eyes.
So, I got to go through a lot of transformations to do, that it is not a computer graphics part yet it is a step towards, that it does not really talk about how to render the pixels let us say, what I just need to know what points in my world should be appearing at one particular spot on my screen. So, how do I apply all those transformations? So, that is the next part I am going to get to, if you have had a computer graphics class before you undoubtedly gone through this chain of transformations before.
(Refer Slide Time: 01:06)
Log in to save your progress and obtain a certificate in Alison’s free Introduction to Virtual Reality online course
Sign up to save your progress and obtain a certificate in Alison’s free Introduction to Virtual Reality online course
Please enter you email address and we will mail you a link to reset your password.