Quaternions
Transcript
going uh everything okay you have an assignment due at 11 5959 on fray Friday right and I'll get to another one of course right after that um so that you can have uh even some more fun um and uh we'll start doing things more with uh General surface types and things like this modeling with General surface types and all as we go through as we go through this um and as you'll find is that this course 10 the the real physics of this course was done in the last lecture when we started discussing how light bounces off surfaces and everything and and basically how we it's too complex and we have to just about hack everything in order to make it look kind of nice that's the physics in this course from now on what what we're going to do is and there's a little bit more of what you might call mathematics I won't but uh you might call mathematics and then we're going to start into the real problems of this of this field are effectively data structures and structure data how are how are data how is the data structures work etc and many of the algorithms are based on data structures here and so uh some people look at at me doing graphics and visualization and say I'm really an advanced data structures guy um if you want to look at it in a very simplistic way that you know we do we deal with uh data structures over and over again and in my group uh we deal with very large data generally data that cannot fit in the memory of your machine and and uh the general problems are how do you structure that data so that you can read it in at the right time and do things interactively uh it's done in the games world all the time um today I'm going to take kind of a sidelight and do something called qu querian and uh querian I first saw them when I was a graduate student in mathematics um and there there a curiosity in mathematics and I'll show you why um but uh they we then ran across them uh again when we started trying to do rotations and they're a very very clever interesting way to do rotations um and so that's what I'll discuss here and I'll kind of show you these caterans they have a long long history um and it's one of those things that somebody invented completely different purpose um many many years ago over 100 years ago um maybe 150 years ago and what happens is all of a sudden they pop up and uh um are starting to be used again the serinsky curve was one of these that it was more or less a curiosity and now we use it all the time to structure our data on disk believe it or not okay we use these space filling curves to structure data on disk and that wasn't the purpose of these things at all but it's one of those things that uh that we do so anyway here go I'm going to give you this lecture on querian and then of course this is going to appear in the next assignment uh for you to do uh everything I do today today there is a nice set of notes out there on querian um on the website there are C++ classes on querian and unit querian which are going to be the the uh the thing that we use uh those things are also out there on the website you're free to use them uh wherever you want you can also download querian programs pretty easy you know querian file type colon uh CPP you can usually get a ton of hits on the website uh these days so um this kind of it's kind of this mystery but I'll show you how it works okay now many of you maybe have noticed that uh rotations are kind of interesting things okay um here's x y z and is anybody counting the number of times I break Chalk in this class um pardon that's five that's five oh okay um the uh um let's take this point up here one0 0 okay and rotate let's rotate it um I'll shorthand let's rotate it uh say uh 90° about the x axis and then rotate it rotate that uh 90° about the y- axis okay let's do this to this point okay and um I'm not going to write the matrices down but you can probably see if you rotate at first 90° around the xaxis it drops down to here okay drops down to there and then if you rotate at 90° around the y- axis it's going to go over here right and it's going to end up at being the point uh let me try this again that's 0 one Z it's going to end up being the 0. 1 z0 when you get done right if you rotate it first around the X and then around the Y but suppose you did it the other way around suppose you did you rotated this 90° around the Y and then you rotated it 90° around the X axis okay if you rotate at first 90° around see what's going to happen okay if you rotate first 90° around the Y it's going to stay right where it is right and then if you rotate it around the X it's going to end up going down here right and so maybe some of you figured out already that rotations are kind of weird and wonderful things that if you apply them in different orders you get different things out okay which is why in these camera transforms you want to make sure to do your rotations in the correct order right if you're looking over here for your points right and you don't do them in the correct order your points all end up over here right you're not going to see anything on your screen and it's very common thing that people apply these things in the wrong in in different order so rotations don't commute right all right commutation property of things is that a * B is equal to B * a right that doesn't happens with rotations you get different things out okay and uh um this is something that that that pops up all the time and actually causes us a lot of problems uh I think I've mentioned this before if you listen to the uh the uh radio from the original Mercury astronauts and all they talk about well we're getting close to gimbal lock and this is one thing that they uh were doing things with rotations and effectively rotations if you pile too much of them on one thing you can get them they'll flip over 180° right all of a sudden that's the one thing you don't want your spacecraft to do is all of a sudden you're driving along and all of a sudden you're upside down okay or backwards or whatever okay so um um this is one of the properties of rotations it's a little irritating it would be nice if they didn't do this but they do okay so here we go I'm going to do now querian and I'm going to get back to that this non-com commutation thing about 20 minutes okay I'll get back to it um caterans were I think invented by a guy named Heisenberg um and what what he was doing was looking for something that extends complex numbers and if you remember complex numbers look like this a * b i where uh we like to say uh i^2 is equal to minus1 right that if you multiply it if you multiply two of these things together like this you get a c minus b d and you get uh um a d + b c * I I think and the reason you get this minus BD is because B * D you get an I squ right and it pops out and you know you can multiply these things you can add them you can do everything everything everybody knows about complex numbers there's classes over in math department you can do for complex numbers and all but what what Heisenberg was trying to do was extend complex numbers for some I haven't looked back at the problem was trying to solve but what he wanted to do was he needed another one of these okay go ahead I squ thank you thank you I square is negative 1 thank you okay he needed another one of these and um and where j^2 is equal also to minus one for example okay and um he he wanted to uh make these this kind of number system work out where you had a second I okay and unfortunately you can't do it because if you just think of it for a minute you get A+ b i + CJ time a plus uh uh uh um a prime plus b Prime I plus C Prime J and when you start multiplying these things well you get B * B * * i^ 2 right that's okay you get C and C Prime j s that's okay because J squ is a minus one but you see you have to you get b i and C Prime J multiplied together so you have to know what I * J is okay and no matter how he tried he couldn't make I * J he couldn't figure out what how to deal with this I * J that was kind of a new thing right and the solution is to this what the the elegant solution that he did here is he said okay if I can't define i i and J I'm going to add another one on okay where k^2 is also equal to minus one all right and he said then I can make it work because I I can say that I * J is equal to K okay I knew things I * J is equal to K and J * K is equal to I and K * I is equal to J and I think uh you can go the other way J * I is equal to minus K things like this okay it it these things go by just put I and J and K in a circle I * Jal k j * k equal I right as long as you go around this thing it's okay but then J * I is minus K right if you go around counterclockwise K * J is minus I and you can Define all these things now so that they work and all of a sudden you get this number system that's now it's a lot more complex than just complex numbers but it comes out as a really nice number system okay and you can add them up one of these plus another one you just add up the the uh components in front right the A's and the B's and the C's you just add them up just like you would do with with complex numbers you can multiply them together and you get this God awful you know 16 things where you have to then reconnect the sums and everything else but you can do it it works knowing it just by knowing these Simple Rules it works okay and you can do all kinds of things with this and he invented these things called quorans okay and they went up and he used them for a bit and then they kind of became this curiosity and I'll show you why they they are curiosity here as we go along okay but you know it's querian you're can to look it up on Wikipedia they have all kinds of theorems and proofs about it and everything else you know these Curiosities kind of get get attacked by the by the abstract mathematicians right until until uh you know you can't prove anything more about these things but there's a what happens is if you look at these if you skew the way you look at them slightly there's a different way to look at them and that is to write them in this form right and I'm going to write them as a and v right okay where a is a right and V here is this Vector BCD okay V is the vector BCD okay and if you look at them in this way they you can make a little bit more sense of this okay and it it kind of works out so let me show you here okay so here's a querian I'm going to call the quarians Q a and v let's do A1 and V1 that's q1 here's Q2 is A2 and V2 now remember V is a vector okay and and I'm hoping that you're going to see what's coming here go ahead where did you put the VOR thank you no a is just a constant okay a is just a constant and and what I want I mean what I'd like you to do is say aha right we got if we do rotate we're going to rotate around an axis and we're going to rotate an angle a certain angle around an axis right there's a vector right which is going to Define our axis and here's an angle okay and maybe uh you know this is going to work it doesn't quite work out that way but close Okay but let me show how it is you can do q1 plus Q2 here and that's just A1 + A2 just add them up right so we can add add these things we can subtract them right they're fine just like we do you know here you're just doing it with vectors okay and you're still getting the same thing as you get over there we can multiply these guys together and this is where it gets fun okay um if we multiply there shouldn't be an arrow over that one either okay if we multiply them together we get A1 * A2 minus V1 dotted with V2 that's dot product okay dot products are scalers out so this is still a scalar number over here we get a vector and well you're going to multiply A1 * [Music] V2 plus A2 * V1 1 plus V1 cross V2 it turns out okay when you multiply these together you get this mess okay now um just to to say something you got to remember if this Vector is zero right these vectors are zero you get back just to the real numbers right A1 and A2 the vectors are all zero right that ends up being zero that ends up being zero you're just multiplying the numbers together okay or adding the numbers together if if these vectors are all b0 0 C and D are always zero right if C and D were always zero you end up with complex numbers in here exactly with the complex numbers okay so so this is an extension of of real numbers and complex numbers okay it's an extension um and uh um you know you can do lots of things with this you can let's see what do I want to do next um um If I multiply let me go over here for a second if I multiply A1 and V1 together with uh um ah let's multiply a and v together with a and a minus V like this um I I'm going to get what I'm going to get a^ 2 minus V Dov and I'm going to get a this times this which is a minus AV V plus a v okay plus v cross V can everybody see this when I multiply it out okay can everybody see this okay um let's see this is equal to A2 minus uh let me write this the length of V ^2 that's what v.v usually is it's the length of v and what's over here uh that's zero right these two go away minus a and plus a and what's v cross V I got zero right so this is zero all right so this is zero so moral isor is for some reason I'm not my numbers aren't right here uh uh I one of these V's is a negative so I get a plus there right and I'll get a plus there now my numbers are right and if I then say Okay suppose I have a and v and I multiply it by a um um all over let me let me see a a over a 2 + b^ 2 and V ided a^ 2 + the norm of v^2 if I do this I'm going to get I think it's not too hard to convince you I'm going to get one zero right and so what you have here is that if I take a querian right and another querian I get out one it's like this Quan if this querian is q then this one is Q inverse right or one over Q right because I get a one out like this so moral the story is each querian has an inverse okay each querian has an inverse that means I can divide right because I can take one Quan q1 divide by the other one Q2 all I have to do is take q1 * Q2 inverse right so here's a so these are kind of cool because I can multip I can add them I can subtract them I can multiply them I can divide them okay I can do everything you would normally want to do with numbers with these querian okay let's see by the way this thing here is typically called the length of the querian and that's normally called the length of the querian square a you [Music] square V uh uh no uh take that off the square root of a^2 + V squared is normally called the length of Q okay that turns out to be the square of the length so these things these things are kind of cool they have you can add them you can subtract them you can multiply that you can divide they have a length right everything about them is really cool and this is why people love them so much right they isn't it supposed to be in the second coordinate prob no it's supposed to be plus b in the second coordinate you mean here no for your right there yeah yeah that should be a negative V right that should be a negative e again there's a complete set of notes out there on the website for quorans that has all of these so if I make a minus sign error in here please uh you know look at those notes um and you know so these things are kind of cool they're just like numbers unfortunately unfortunately here and this is how I multiply them together what happens if I multiply Q2 * q1 okay like that okay suppose I multiply them out well these two are the same right these two are the same dot products commute very nicely uh uh same things here right but when you look at dot products V1 cross V2 is this way right vs2 cross V1 is that way okay it's exactly the opposite and so here's something where you can this is why there are mathematical curiosity because you can do everything with them that you would like to be able to do they have length you can add them you can subtract them you can multiply them you can divide them okay however they don't have they're the one thing that you can do everything with but they don't have this commutative property you have to watch out when you multiply because a * B is not equal to B * a okay that's what's wrong with querian in the world and this is what you know mathematicians love to look at things where um really things that are numbers right look like numbers act like numbers do everything else but one thing's wrong right and then what can we say about this these are the questions that mathematicians would ask suppose we remove this one property from something what what can we say okay how much can we say about these things and uh this is where so this is where querian pops up is that it doesn't have this it has everything you want but it doesn't have this communitive property okay now if I go back to that picture over there that we originally started with when we had two rotations did it one way and the other way and they did not commute right this might clu you into something that you know we have in common here and certainly we do because when we do rotations what we have is something called the unit querian okay ah here's the unit quarians and the unit quarians are simply quarians a and v okay they look like quarians [Music] where the length is one the length of the Quan is one these are the unit Quan okay and if you multiply two unit quarians together right you can go over there and start multiplying if you like but if you multiply two unit quarians together it's not too hard to see you get a unit querian back okay so these things are kind of closed under multiplication If I multiply two unit quarians together I'm going to get a unit querian back okay and most unic quarians look like this okay they look like this where the length where where they have in here a unit Vector right and they have effectively a cosine s relationship between them now can you see really quickly that that if I take the length of this guy h i get something looks like this okay if I take the length it gets something that looks like this if V is a unit Vector that's one right then you got square root of cosine square plus sin Square all right which is also one okay so this is this is a unic querian and it turns out all of them can be written in this way okay now you see where I'm going with this okay you see where the rotations are going to come out because what this rep unit querian represents is a rotation of angle Theta right in the positive cupping direction of my fingers around the vector v okay a rotation in the cupping direction of my fingers around the vector v okay and you'll have to think of the AIS as being at at the origin here but it gives us a rotation right around this vector v now um so okay now I have you have to take me a little bit on faith on this one because otherwise I have to do about um um a three-hour extra lecture in order to show you that this is actually rotation here okay but this is what unit querian do is it gives me this this out and so what happens when I multiply two of these unit querian together I get another unit querian which will give me a rotation around a particular axis okay of a certain angle I can figure out what Theta is by just taking a cosine inverse of the first parameter for example and this enables us to pile up rotations really nicely and actually you can prove you don't get gimbal lock right there is no flipping over here it enables us to pile up these rotations very very very very easily and figure out at the end by multiplying them all together right one rotation around this axis that actually one rotation around a particular axis that actually gets us the same result as piling up all these other rotations together so I can take 500 rotations right different rotations multiply them all together and I can get one rotation out with an axis and an angle and I get the same result when it's when I'm done these are the cool things about unit querian okay the other cool thing is that given any unit querian there is a corresponding 4x4 Matrix that you can write down that affects this same rotation okay and I never remember the matrix it's out there in my code I can't write it down none of us ever remember it we just copy it from you know whatever we've done before it was uh it was shown in the first one was shown in 1982 I think right and everybody else has just copied and it works so everybody else has just copied this code but there is a 4x4 Matrix given any unit querian so I can do things with querian okay here and I can multiply them all up together right and then get one quor and out which affects that rotation and then get the 4x4 Matrix that does it okay and so this is where we all use querian um we use them to fly things around you know like a pilot we because you know we can always guarantee kind of how our things work and uh we use querian a lot here but we use them a lot and then Bango at the end we get back to 4x4 Matrix right we pump that 4X4 four Matrix into model Matrix and open G let it apply to all our points right and it's as if we' rotated all our points all right okay now the place where you normally use querian here so if I confused everybody I've gone a long ways right from Heisenberg 150 years ago to 1982 okay I've gone a long way but where most of us use querian right off the bat is in something called the track ball okay and a track ball looks like this okay we have some some set of polygons that we want a view okay on the screen and what we do is we think in 3D that there's a we think of a sphere sitting around this uh this object okay we think of a sphere sitting around this object and using qt or something else what we do is we pick we select we click on a point here hold the mouse button down click on a point here and we think of that point as being a point on our sphere okay we think of that point as being a point on our sphere and it's not too figure if you have this sphere in here it's not too hard to figure out where on the sphere you actually you actually click okay and then what we do is we pull we pull this point in One Direction or another okay we pull this point in One Direction or another and and it'll uh QT will nicely just keep us given getting these events back and so um what happens is suppose I pull this point down here okay I I want to make this it's actually turns out to be very small things but I actually need to make it very big to show you what's happening what I do is is I think of the center of the sphere think of the sphere in 3D here I think of the center of the sphere and I get this Vector which was the original one and that Vector right and what I do is I take their cross product and I get an Axis okay this will give me an axis through the origin if I take the cross product okay and then if I take the dot product this gives me the Ang the coine of the angle so I can get the angle that I've rotated here okay I can then take this Theta and this Vector here which is V okay and I can make myself a un querian cosine Theta sin Theta V here okay and I can take then the The Matrix that corresponds to this thing okay and pump this this into model Matrix and then draw my object over again and it will rotate okay and then well what do I do well I'm going to drag this point again to another place okay okay and what I do is I then look at this Vector from the center of the sphere out this then is maybe Fe v six times Fe okay I get a I take the cross product of these two get another Vector axis out I can create another one cosine f s f * say V1 if that's V1 here and if I multiply these two together I get a new querian unit querian right of which I can find the axis and the angle in order to rotate my original figure as if I had rotated first here and then there okay and so what we do with this track ball is we click and pull click and pull click and Pull and we can rotate around our objects really fast okay by doing this and all we do is we just keep piling up the quern UN quarians as we go along okay and it's a great way to rotate you guys all have tons of Sliders over here to change angles and everything else we don't do that we just click pull click pull click pull and things like that the other thing people do with these these things is that um suppose I've clicked pulled here right I can you there's a uh if some of you have noticed in QT there's a clock um there's a timer Class A Q timer class what it does is it'll give you a what we what normally we call a tick right it'll give you a tick event every now and then you can say give me an event every uh tenth of a second or something all right throw this event every 10 tenth of a second and what what happens is you can throw you can you can move this thing let up on the mouse and then every tenth of a second you can add that querian on again right and what happens is your object starts animating in front of you right if you use this tick and so you can kind of throw these it's almost as if you throw these things and they they keep rotating in front of you okay and it's something called a track ball everybody uses it with um QT everybody uses it with QT and with uh querian and it's very very elegant it it works very well and it's something that you're going to be doing in the next assignment okay you'll find it's much easier than varying all these sliders and parameters and everything that you have because you can rotate around things very nicely so you can rotate we have we have one button maybe our left button rotates our right button translates um our our wheel Zooms in and out and we all have these things that we can do very quickly with our images that are all handled almost automatically by our system okay and uh one of the things I have when I when I write QT code is I have something called the track ball class okay uh track ball canvas Well normally I have a canvas that I draw on right which has which is derived from qgl window I have a canvas that I dry on I then derive from that a track ball canvas which implements my track ball and I can attach I can you know attach the track ball canvas whenever I want and it just works and so that I can coat up anything I want and I can move things around in 3D okay rotate them around in front of me but we also use these quorans a lot of other places we use it to fly our airplanes around and things like this um and uh it they're very very useful things to have okay now um I'm 15 minutes early is there any questions about this I went a long ways today from like 1840 to today okay but in general with these querian just you know keep in your mind that you know quaternium themselves if they're an extension of the complex numbers right the best way to write them is a and v where you have a and a vector and then you can use product cross product to define the multiplication you can do everything with them you can add subtract multiply divide except they don't commute right in multiplication a * B is not B * a necessarily uh there are cases where it does work but very few okay those are quarians and then the ones that are useful to us are the unit querian because out of these we can pick out an angle Theta and we can pick out a an unit vector and the unit Vector becomes the axis we want to rotate around the Theta becomes the angle and then we can get exactly a perfect translation to a 4x4 Matrix which then we can feed to openg G right and uh and we're off um doing things with these things so it's one of the it's one of the things that uh you know these are Curiosities in the math department uh because of the non-computational and we use them basically on a daily basis to handle things uh now it's a little different here because now all these other things we've done with these 4x4 matrices and now we have this other thing called querian sitting in the middle to do things but we always have to remember that we can get back to a 4x4 Matrix if we want to from all these querian that we use so people have now developed uh how to do curves on these querian spaces and things like this and um they've really uh develop some very very interesting things as we move along so um as we go along in the next assignment we're going to start doing we're going to start viewing things in different ways right using these things and we're going to start going more toward um um U more interesting shapes and all that we pull out of GL okay so all right I'm done for today um I will see you on Friday which one