Lecture 3: Coordinate Systems And Transformations - Forsiden

Transcription

Lecture 3:Coordinate Systems and TransformationsTopics:1.2.3.4.5.Coordinate systems and framesChange of framesAffine transformationsRotation, translation, scaling, and shearRotation about an arbitrary axisChapter 4, Sections 4.3, 4.5, 4.6, 4.7, 4.8, 4.9.1

Coordinate systems and framesRecall that a vector v lR3 can be represented as a linear combination ofthree linearly independent basis vectors v1 , v2 , v3 ,v α 1 v1 α 2 v2 α 3 v3 .The scalars α1 , α2 , α3 are the coordinates of v. We typically choosev1 (1, 0, 0), v2 (0, 1, 0), v3 (0, 0, 1) .v2v α1v 1 α2v 2 α3v 3α1v32v1

Suppose we want to (linearly) change the basis vectors v 1 , v2 , v3 to u1 , u2 ,u3 . We express the new basis vectors as combinations of the old ones,u1 a11 v1 a12 v2 a13 v3 ,u2 a21 v1 a22 v2 a23 v3 ,u3 a31 v1 a32 v2 a33 v3 ,and thus obtain a 3 3 ‘change of basis’ matrix a11M a21a31a12a22a32 a13a23 .a33If the two representations of a given vector v are v1v a T v2 ,v3 where a ( α1α2 u1and v bT u2 ,u3 Tα3 ) and b ( β1β2Tβ3 ) , then v1u1v1T TTav2 v b u2 b M v2 ,v3u3v3which implies thata M T b and b (M T ) 1 a.3

This 3D coordinate system is not, however, rich enough for use in computergraphics. Though the matrix M could be used to rotate and scale vectors,it cannot deal with points, and we want to be able to translate points(and objects).In fact an arbitary affine transformation can be achieved by multiplicationby a 3 3 matrix and shift by a vector. However, in computer graphics weprefer to use frames to achieve the same thing.A frame is a richer coordinate system in which we have a reference pointP0 in addition to three linearly independent basis vectors v 1 , v2 , v3 , and werepresent vectors v and points P , differently, asv α 1 v1 α 2 v2 α 3 v3 ,P P 0 α 1 v1 α 2 v2 α 3 v3 .We can use vector and matrix notation and re-express the vector v andpoint P asv ( α1α2α3 v1 v 0) 2 ,v3P0 and P ( α1α2α3 v1 v 1) 2 .v3P0 The coefficients α1 , α2 , α3 , 0 and α1 , α2 , α3 , 1 are the homogeneous coordinates of v and P respectively.4

Change of framesSuppose we want to change from the frame (v1 , v2 , v3 , P0 ) to a new frame(u1 , u2 , u3 , Q0 ). We express the new basis vectors and reference point interms of the old ones,u1 a11 v1 a12 v2 a13 v3 ,u2 a21 v1 a22 v2 a23 v3 ,u3 a31 v1 a32 v2 a33 v3 ,Q0 a41 v1 a42 v2 a43 v3 P0 ,and thus obtain a 4 4 matrix a11 aM 21a31a41a12a22a32a42a13a23a33a43 00 .01Similar to 3D vector coordinates, we suppose now that a and b are thehomogeneous representations of the same point or vector with respect tothe two frames. Then v1u1v1 v u v aT 2 b T 2 b T M 2 ,v3u3v3P0Q0P0which implies thata M T b and b (M T ) 1 a.5

Affine transformationsThe transposed matrixMTa11 a 12a130 a21a22a230a31a32a330 a41a42 ,a431simply represents an arbitrary affine transformation, having 12 degreesof freedom. These degrees of freedom can be viewed as the nine elementsof a 3 3 matrix plus the three components of a vector shift.The most important affine transformations are rotations, scalings,and translations, and in fact all affine transformations can be expressedas combinaitons of these three.Affine transformations preserve line segments. If a line segmentP (α) (1 α)P0 αP1is expressed in homogeneous coordinates asp(α) (1 α)p0 αp1 ,with respect to some frame, then an affine transformation matrix M sendsthe line segment P into the new one,M p(α) (1 α)M p0 αM p1 .Similarly, affine transformations map triangles to triangles and tetrahedrato tetrahedra. Thus many objects in OpenGL can be transformed by transforming their vertices only.6

Rotation, translation, scaling, and shearTranslation is an operation that displaces points by a fixed distance in agiven direction. If the displacement vector is d then the point P will bemoved toP 0 P d.We can write this equation in homeogeneous coordinates asp0 p d,where x y p ,z1 x0 y0 0p 0 ,z1 αx α d y .αz0 so thatx0 x α x ,y 0 y αy ,z 0 z αz .So the transformation matrix T which gives p0 T p is clearly1 0T T (αx , αy , αz ) 00 0100 0 αx0 αy ,1 αz0 1called the translation matrix. One can check that the inverse isT 1 (αx , αy , αz ) T ( αx , αy , αz ).7

Rotation depends on an axis of rotation and the angle turned through.Consider first rotation in the plane, about the origin. If a point (x, y) withcoordinatesx ρ cos φ,y ρ sin φ,is rotated through an angle θ, then the new position is (x 0 , y 0 ), wherex0 ρ cos(φ θ),y 0 ρ sin(φ θ).Expanding these latter expressions, we findx0 x cos θ y sin θ,y 0 x sin θ y cos θ,or x0y0 cos θsin θ sin θcos θy x.yyxx8

Thus the three rotation matrices corresponding to rotation about the z, x,and y axes in lR3 are:cos θ sin θRz Rz (θ) 00 sin θcos θ0010 0 cos θRx Rx (θ) 0 sin θ00 cos θ 0Ry Ry (θ) sin θ0 0010 00 ,010 sin θcos θ0 00 ,01 00 .010 sin θ100 cos θ00All three give positive rotations for positive θ with respect to the righthand rule for the axes x, y, z. If R R(θ) denotes any of these matrices,its inverse is clearlyR 1 (θ) R( θ) RT (θ).Translations and rotations are examples of solid-body transformations: transformations which do not alter the size or shape of an object.9

Scaling can be applied in any of the three axes independently. If we senda point (x, y, z) to the new pointx0 βx x,y 0 βy y,z 0 βz z,then the corresponding matrix becomesβx 0S S(βx , βy , βz ) 00 0βy0000βz0 00 ,01with inverseS 1 (βx , βy , βz ) S(1/βx , 1/βy , 1/βz ).If the scaling factors βx , βy , βz are equal then the scaling is uniform:objects retain their shape but alter their size. Otherwise the scaling isnon-uniform and the object is deformed.10

Shears can be constructed from translations, rotations, and scalings, butare sometimes of independent interest.yyxxzzA shear in the x direction is defined byx0 x (cot θ)y,y 0 y,z 0 z,for some scalar a. The corresponding shearing matrix is therefore1 cot θ1 0Hx (θ) 0000 0010with inverseHx 1 (θ) Hx ( θ).11 00 ,01

Rotation about an arbitrary axisHow do we find the matrix which rotates an object about an arbitrary pointp0 and around a direction u p2 p1 , through an angle θ?yp2up1p0xzThe answer is to concatenate some of the matrices we have alreadydeveloped. We will assume that u has length 1. The first step is to usetranslation to reduce the problem to that of rotation about the origin:M T (p0 ) R T ( p0 ).To find the rotation matrix R for rotation around the vector u, we firstalign u with the z axis using two rotations θx and θy . Then we can applya rotation of θ around the z-axis and afterwards undo the alignments, thusR Rx ( θx )Ry ( θy )Rz (θ)Ry (θy )Rx (θx ).12

It remains to calculate θx and θy from u (αx , αy , αz ). The first rotationRx (θx ) will rotate the vector u around the x axis until it lies in the y 0plane. Using simple trigonometry, we find thatcos θx αz /d,where d qsin θx αy /d,αy2 αz2 , so, without needing θx explicitly, we find100 0 αz /d αy /dRx (θx ) 0 αy /d αz /d000 00 .01In the second alignment we findcos θy d,and sosin θy αx ,d 0Ry (θy ) αx0 130 αx1 00 d0 0 00 .01

Example in OpenGLThe following OpenGL sequence sets the model-view matrix to represent a45-degree rotation about the line through the origin and the point (1, 2, 3)with a fixed point of (4, 5, 6):void myinit(void){glMatrixMode(GL MODELVIEW);glLoadIdentity();glTranslatef(4.0, 5.0, 6.0);glRotatef(45.0, 1.0, 2.0, 3.0);glTranslatef(-4.0, -5.0, -6.0);}OpenGL concatenates the three matrices into the single matrixC T (4, 5, 6) R(45, 1, 2, 3) T ( 4, 5, 6).Each vertex p specified after this code will be multiplied by C to yield q,q Cp.14

Spinning the CubeThe following program rotates a cube, using the three buttons of the mouse.There are three incube);glutMouseFunc(mouse);The display callback sets the model-view matrix with three angles determined by the mouse callback, and then draws the cube (see Section 4.4 ofthe book).void display(){glClear(GL COLOR BUFFER BITglLoadIdentity();glRotatef(theta[0], 1.0, 0.0,glRotatef(theta[1], 0.0, 1.0,glRotatef(theta[2], 0.0, 0.0,colorcube();glutSwapBuffers();}15GL DEPTH BUFFER BIT);0.0);0.0);1.0);

The mouse callback selects the axis of rotation.void mouse(int btn, int state, int x, int y){if(btn GLUT LEFT BUTTON && state GLUT DOWN) axis 0;if(btn GLUT MIDDLE BUTTON && state GLUT DOWN) axis 1;if(btn GLUT RIGHT BUTTON && state GLUT DOWN) axis 2;}The idle callback increments the angle of the chosen axis by 2 degrees.void spincube(){theta[axis] 2.0;if(theta[axis] 360.0) theta[axis] - 360.0;glutPostRedisplay();}16

A little bit about quarternionsQuarternions offer an alternative way of describing rotations, and have beenused in hardware implementations.In the complex plane, rotation through an angle φ can be expressed asmultiplication by the complex numbereiφ cos φ i sin φ.This rotates a given point reiθ in the complex plane to the new pointreiθ eiφ rei(θ φ) .Analogously, quarternions can be used to elegantly describe rotations inthree dimensions. A quarternion consists of a scalar and a vector,a (q0 , q1 , q2 , q3 ) (q0 , q).We can write the vector part q asq q1 i q2 j q3 k,where i, j, k play a similar role to that of the unit vectors in lR 3 , and obeythe propertiesi2 j2 k2 1,andij k ji,jk i kj,17ki j ik.

These properties imply that the sum and multiple of two quarternions a (q0 , q) and b (p0 , p) area b (q0 p0 , q p),andab (q0 p0 q · p, q0 p p0 q q p).The magnitude and inverse of a areq a q02 q · q,a 1 1(q0 , q). a Rotation. Suppose now that v is a unit vector in lR3 , and let p be anarbitrary point. Denote by p0 the rotation of p about the axis v, placed atthe origin. To find p0 using quarternions, we let r be the quarterionr (cos(θ/2), sin(θ/2)v),of unit length, whose inverse is clearlyr 1 (cos(θ/2), sin(θ/2)v).Then if p and p0 are the quarternionsp0 (0, p0 ),p (0, p),we claim thatp0 r 1 pr.18

Let’s check that this is correct! Following the rule for multiplication, a littlecomputation shows that p0 does indeed have the form (0, p0 ), and thatθθθθθp0 cos2 p sin2 (v · p)v 2 sin cos (v p) sin2 v (v p).22222But using the identityp (v · p)v v (v p),(1)and the multiple-angle formulas for cos and sin, this simplifies top0 (v · p)v cos θ(v p) v sin θ(v p).This formula can easily be verified from a geometric interpretation. It isa linear combination of three orthogonal vectors. The first term (v · p)vis the projection of p onto the axis of rotation, and the second and thirdterms describe a rotation in the plane perpendicular to v.Notice also that the formula clearly represents an affine transformation;all terms are linear in p. The formula can be used to find the coefficientsof the corresponding transformation matrix directly.19

All three give positive rotations for positive with respect to the right hand rule for the axes x;y;z. If R R( ) denotes any of these matrices, its inverse is clearly R 1( ) R( ) RT( ): Translations and rotations are examples of solid-body transforma-tions: transformations which do not alter the size or shape of an object. 9