Quaternions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good everything okay you have an assignment due at 11:59 59 on Friday right and I'll get to another one of course right after that so that you can have even some more fun and we'll start doing things more with general surface types and things like this modeling with general surface types and all as we go through as we go through this and as you'll find is that this course tent 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 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 we're going to do is and there's a little bit more of what you might call mathematics I won't but 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 structuring data our our data how is the data structures work etc and many of the algorithms are based on data structures here and so some people look at at me doing graphics and visualization and say I'm really an advanced data structures guy if you want to look at it in a very simplistic way that you know we do we deal with the data structures over and over again and in my group we deal with very large data generally data that cannot fit in the memory of your machine and 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 now it's done in the games world all the time today I'm going to take kind of a sidelight and do something called quaternions and quaternions on I first saw them when I was a graduate student in mathematics and their their curiosity in mathematics and I'll show you why but they we then ran across them again when we started trying to do rotations and they're very very clever interesting way to do rotations and so that's what I'll discuss here and I'll kind of show you these quarter nians they have a long long history and it's one of those things that somebody invented completely different purpose many many years ago over a hundred years ago maybe 150 years ago and what happens is all of a sudden they pop up and are starting to be used again as the sierpinski 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 the space-filling curves to structure data on disk and that it wasn't the purpose of these things at all but it's one of those things that that we do so anyway here go I'm going to give you this lecture on quaternions and then of course is going to appear in the next assignment for you to do everything I do today there is a nice set of notes out there on quaternions on the website there are C++ classes on quaternion x' and unit quaternions which are going to be the the the thing that we use those things are also out there on the website you're free to use them wherever you want you can also download quaternion programs pretty easy you know a quaternion file type : CPP you can usually get a ton of hits on the website these days so 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 rotations are kind of interesting things huh here's X Y Z and anybody counting the number of times I break chalk in this class pardon that's 500k the let's take this point up here one zero zero okay and rotate let's rotate it well just sort hand let's rotate it say ninety degrees about the x axis and then rotate it rotate that ninety degrees about the y axis okay let's do this to this point okay and I'm not going to write the matrices down but you can probably see if you rotate it first 90 degrees around the x-axis it drops down here okay drops down to there and then if you rotate it 90 degrees around the y-axis that's going to go over here right and it's going to end up but being the point let me try this again that's zero one zero it's going to end up being the point one zero zero 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 degrees to around the Y and then you rotate it at 90 degrees around the x axis okay if you rotate it first 90 degree around see what's going to happen okay you're okay the first 90 degree around the y let's go stay right where it is right and then if you rotate it around the X it's going to end up going down here alright 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 all 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 times B is equal to B times a right that doesn't happens with rotations you get different things out okay and this is something that pops up all the time and actually causes us a lot of problems I think I've mentioned this before if you listen to the the radio from the original mercury astronauts and all they talk about what we're getting close to gimbal lock and this is one thing that they 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 degrees 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 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 quaternion x' and I'm going to get back to that this non commute commutation thing about 20 minutes okay I'll get back to it um quaternion sir I think invented by a guy named Heisenberg and what what he was doing was looking for something that extends complex numbers and if you remember complex numbers look like this a times bi where we like to say I squared is equal to minus 1 right that if you multiply it if you multiply two of these things together like this you get a C - B D and you get a D plus B C times I I think and the reason you get this - B D is because B times D you get nice squared 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 summer i haven't looked back at the problem he was trying to solve but what he wanted to do was he needed another one of these okay go ahead I squared thank you thank you I squared is negative one thank you okay he needed another one of these and um and where J squared is equal to also two minus one for example okay and um he he wanted to make these this kind of number system work out where you had a second eye okay and unfortunately he can't do it because if you just think of it for a minute you get a + b i+ CJ times a plus a plus b prime i plus c prime j and when you start multiplying these things well you get B times B times times I squared right that's okay you get C and C prime J squared that's okay because J squared 2 minus 1 but you see you have to you get B I and C prime J multiplied together so you have to know what I times J is okay and no matter how he tried he couldn't make hi times J he couldn't figure out what how to deal with this I times J that was kind of a new thing right and the solution is to this what the LG solution that he did here is he said okay if i can't define i inj i'm going to add another one on okay where k squared is also equal to minus one alright and he said then i can make it work because i can say that i times j is equal to k okay I knew things I times J is equal to K and J times K is equal to I and K times I is equal to J and I think you can go the other way J times I is equal to minus K things like this okay if these things go by just put I on J and K in a circle I times J equal KJ times K equals I might as long as you go around this thing it's okay but then J times I is minus K if you go around counterclockwise K times 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 I'm 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 components in front right the A's and the B's in 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 quaternions 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 are they are curiosity here as we go along okay but you know it's quarter nians you can 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 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 kind of works out so let me show you here okay so here's a quaternion I'm going to call the quaternion skew a and V let's do a 1 and V 1 that's Q 1 here's Q 2 is a 2 and V 2 now remember V is a vector okay and and I'm hoping that you're gonna see what's coming here go ahead thank you know it's just a constant okay it's just a constant and in what I want I mean what I'd like you to do is say aha right we've 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 find our axis and here's an angle okay and maybe you know this is going to work it doesn't quite work out that way but close okay but let me show you how this you can do to 1 plus Q 2 here and that's just a 1 plus a 2 just add them up right so we can add add these things we can subtract them right there fine just like we do you know here you're 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 if we multiply there shouldn't be an arrow with that one either okay and we multiply them together we get a 1 times a 2 minus V 1 dotted with V 2 that's dot product okay dot products are scalars out so this is still a scalar number now over here we get a vector and well you're going to multiply a 1 times V 2 plus a 2 times V 1 plus V 1 cross V 2 it turns out okay when you multiply these together you get this mess okay now just to say something you got to remember if this vector is 0 right these vectors are 0 you get back to the real numbers 9 a 1 and a 2 the vectors are all 0 right that ends up being 0 that ends up being 0 you're just multiplying the numbers together ok or adding the numbers together if if these vectors are all be 0 0 C and D are always 0 right if C and D you are always 0 you can end up with complex numbers in here exactly with the complex numbers okay so so this is an extension of real numbers and complex numbers okay it's an extension and you know you can do lots of things with this you can let's see what do I want to do next if I multiply let me go over here for a second if I multiply a 1 and V 1 together with ah let's multiply a and V together with a and the minus V like this I get I'm going to get what I'm going to get a squared minus V V and I'm going to get a this times this which is minus AV plus AV okay plus V cross V can everybody see this when I multiply it out okay everybody see this okay let's see this is equal to a squared minus let me write this the length of V squared that's what V dot V usually is is two like the V and what's so over here that's zero right these two go away - AV and plus AV and what's V cross V I got zero right so this is zero all right so this is zero so moral of story is for some reason I'm not my numbers aren't right here I one of these visas the 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 + V and I multiply it by a all over let me say a over a squared plus B squared and V divided by a squared plus the norm of V squared 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 quaternion right and another quarter and I get out one it's like this quaternion if this quaternion is Q then this one is Q inverse right or 1 over Q right because I get the one out like this so moral of the story is each quaternion has an inverse okay each quaternion has an inverse that means I can divide right because I can take one quaternion q one divided by the other one Q two all I have to do is take Q 1 times Q 2 inverse right so here's a so these are kind of cool because I can multiply I can add them I can subtract them I can multiply them I can divide them ok I can do everything you would normally want to do with numbers with these quaternion x' ok let's see by the way this thing here is typically called the length of the quaternion and that's normally called the length of the quaternion square a u square B no take that off the square root of a squared plus B squared is normally called the length of Q ok 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 they can divide they have a length right everything about them is really cool and this is why people love them so much right there Probab no it's supposed to be plus fee in the second coordinate you mean here 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 quaternion that has all of these so if I make a minus sign error in here please you know look at those notes 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 Q 2 times Q 1 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 the same things here right but when you look at dot products V one cross V true is this way right V two cross V one 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 times B is not equal to B times 8 okay that's what's wrong with quaternions in the world and this is what you know mathematicians love to look at things where they're really things that are numbers 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 right what can we say okay how much can we say about these things and this is where so this is where quaternions pops up is that it doesn't have this it has everything you want but it doesn't have this community property right 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 clue Li 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 quaternions okay here's the unit quaternions and yuna quarter nians are simply quaternions a and V okay it looked like quaternions where the length is 1 the length of the quaternion is 1 these are the unit quaternions okay and if you multiply two unit quaternions together right you can go over there and start multiplying if you like but if you multiply two a unit quaternions together it's not too hard to see you get a unit quaternion back okay so these things are kind of closed in under multiplication if I multiply two unit quaternion together I'm going to get a unit quaternion back okay and most unit quaternions look like this okay they look like this where the length at where they have in here a unit vector right and they have effectively a cosine sine relationship between them now can you see really quickly that that if I take the length of this guy I get something that 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 and then you get square root of cosine squared plus sine squared right which is also one okay so this is this is a unit quaternion 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 repertory and represents is a rotation of angle theta right in the positive cupping direction of my fingers around the vector V okay a rotation and in the cupping direction of my fingers around the vector V okay and you'll have to think of the axis as being at the origin here but it gives us a rotation right around this vector V now so okay now I am you have to take me a little bit on faith on this one because otherwise I have to do about a three-hour extra lecture in order to show you that this is actually rotation here okay but this is what unit quaternions do is it gives me this this out and so what happens when I multiply two of these unit quaternions together I get another unit quaternion 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 it 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 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 I 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 you two quaternions okay the other cool thing is that given any unit quaternion there is a corresponding 4x4 matrix that you can write down that affects this russain 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 it was shown in the first one was shown in 1982 I think right and everybody else is just copied and it works so everybody else has just copied this code but there is a four by four matrix given any unit quaternion so I can do things with quaternions okay here and I can multiply them all up together right and then get one quaternion out which affects that rotation and then get the four by four matrix that does it okay and so this is where we all use quaternions we use them to fly things around you know like a pilot we because you know we can always guarantee kind of how are things work and we use quaternions a lot here but we use them a lot and then Bango at the end we get back to four by four matrix right we pumped that for program or for matrix into model matrix in opengl let it apply to all our points writing this as if we rotate it all our points all right okay now the place where you normally use quaternions here so if i confused everybody i've gone a long ways right from Heisenberg 150 years ago to 1982 okay i've gotten a long way but where most of us use quaternions right off the bat is in something called a trackball okay and a trackball looks like this okay we have some some set of polygons that we want to view okay on the screen and what we do is we think in 3d that there say we think of a sphere sitting around this 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 to figure if you have this fear in here it's not too hard to figure out where on the sphere you actually you actually click OK 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 QTL nicely just keep us given getting these events back and so um what happens is suppose I pull this point down here okay 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 is 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 access through the origin if I take their cross product okay and then if I take the dot product this gives me the a of the cosine 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 unit quaternion cosine theta sine 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 rotate okay and then well what do I do well I'm going to drag this point again to another place okay and what I do is I then look at this vector from the center of the sphere out this then is maybe fee fee six times fee okay I get it I'll take the cross-product for these to get another vector access out I can create another one cosine fee sign fee times say v1 if that's v1 here and if I multiply these two together I get a new quaternion unit quaternion 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 trackball 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 what we do is we just keep piling up - quad turn unit quaternion 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 suppose I've clicked pulled here right I can you there's a some of you noticed in QT there's a clock there's a timer class a queue 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 ticket every now and then you can say give me an event every tenth of a second or something right throw this event every tenth of a second and 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 quaternion on again right and what happens is your object starts animating in front of you and if you use this tick and so you can kind of throw these it's almost as if you throw these things and they keep rotating in front of you okay and it's something called a trackball everybody uses it with Qt everybody uses it with Qt and with quaternions and it's very very elegant 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 one button maybe our left button rotates our right button translates 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 one of the things i have when i when i write QT code is i have something called the trackball class okay trackball canvas well normally i have a canvas that i've drawn right which has which is derived from qgl window i have a canvas that I draw on I then derive from that a trackball canvas which implements my trackball and I can attack I can you know attach the trackball canvas whenever I want and it just works and so that I can code 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 quaternion a lot of other places we use it to fly our airplanes around and things like this and it they're very very useful things to have okay now my 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 quaternions just you know keep in your mind that you know quaternion is 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 dot 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 times B is not B times a necessarily there are cases where it does work but very few okay those are quaternions and then the ones that are useful to us are the unit quaternions because out of these we can pick out an angle theta and we can pick out a 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 the opengl right and and we're off doing things with these things so it's one of the it's one of the things that you know these are curiosities in the math department because of the non commutation but for us they're actually really useful and we use them basically on a daily basis to handle things 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 quaternion sitting in the middle to do things but we always have to remember that we can get back to a four by four matrix if we want to from all these quaternion that we used so people have now developed how dukers on these quaternions faces and things like this and they've really developed some very very interesting things as we move along so as we go along in the next assignment we're going to start doing it we're going to start viewing things in different ways right using these things and we're going to start going more toward more interesting shapes and all that we pull out a GL okay so all right I'm done for today I will see you on Friday noun 1
Info
Channel: UC Davis Academics
Views: 130,047
Rating: 4.87432 out of 5
Keywords:
Id: mHVwd8gYLnI
Channel Id: undefined
Length: 39min 7sec (2347 seconds)
Published: Fri Dec 19 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.