1. Translation Transformations: 2D Transforms Relocation of point WRT frame Given P = (x, y), translation T (dx, dy) Then P (x, y ) = T (dx, dy) P, where x = x + dx, y = y + dy Using matrix representation T (dx, dy) = dx dy 2. Rotation P = x y P = P (x, y) + T (dx, dy) = x y + dx dy = x + dx y + dy Rotation about origin Positive rotation counterclockwise (righthand rule) Given P = (x, y), rotation R(θ) Then P (x, y ) = R(θ) P, where x = x cosθ y sinθ, y = x sinθ + y cosθ Derivation: (a) x given by r cosφ, y by r sinφ (b) Using cos(θ + φ) = cosθ cosφ sinφ sinθ, sin(θ + φ) = cosθ sinφ + sinφ cosθ (c) x = r cosθ cosφ r sinφ sinθ (d) Substituting x for r cosφ and y for r sinφ gives x = x cosθ y sinθ (e) Similarly, y = r cosθ sinφ + r sinφ cosθ = x sinθ + y cosθ 1
Transformations: 2D Transforms (2) 3. Scale Using matrix representation cosθ sinθ R(θ) = sinθ cosθ P = R(θ) P (x, y) = cosθ sinθ sinθ cosθ x y = x cosθ y sinθ x sinθ + y cosθ Change of proportion Affects location Given P = (x, y), scale S(sx, sy) Then P (x, y ) = S(sx, sy) P, where x = sx x, y = sy y Using matrix representation S(sx, sy) = sx 0 0 sy 4. Shear P = S(sx, sy) P (x, y) = sx 0 0 sy x y = x sx y sy Change of one coordinate in proportion to the other Given P = (x, y), shear H x (h x ) Then P (x, y ) = H x (h x ) P, where x = x + h x y, y = y Using matrix representation H x (h x ) = 1 h x 0 1 P = H x (h x ) P (x, y) = 1 h x 0 1 x y = x + h x y y 2
Similarly for shear H y (h y ) x = x, y = y + h y x Transformations: 2D Transforms (3) H y (h y ) = 1 0 h y 1 If desire shear to result in a specific angle, use cotθ for h x, where θ is angle of slant wrt x axis 5. These are affine transformations Parallelism is maintained, but angles and lengths are not preserved 6. Potential problem with these formulations: Rotation, scale, and shear are multiplicative, while translation is additive 3
Transformations: Homogeneous Coordinates Earlier, noted that, given frame v 1, v 2, v 3, P 0, Can represent vector v as α 1 v 1 + α 2 v 2 + α 3 v 3 Can represent point P as β 1 v 1 + β 2 v 2 + β 3 v 3 + P 0 Note that representation of both v and P have same form, which is problemmatic An alternative is the following: P = [ β 1 β 2 β 3 1 ] v = [ α 1 α 2 α 3 0 ] v 1 v 2 v 3 P 0 v 1 v 2 v 3 P 0 = β 1 v 1 + β 2 v 2 + β 3 v 3 + P 0 = α 1 v 1 + α 2 v 2 + α 3 v 3 Representations using this formulation are v = α 1 α 2 α 3 0 and P = This approach uses 4 values to represent points and vectors in 3-space The 4th coordinate is called a homogeneous coordinate, denoted w Refer to earlier discussion relating affine spaces to vector spaces All points of the form P = w β 1 w β 2 w β 3 w A homogenized point is one in which w = 1 β 1 β 2 β 3 1 represent the same point in 3-space 4
Transformations: Homogeneous Coordinates (2) Vectors (w = 0) are referred to as points at infinity Consider w x y x y 1 2 4 2 4 1/2 2 4 4 8 1/4 2 4 8 16... 0 2 4 5
Transformations: 2D Transforms in Homogeneous Representation 1. Translation: T (dx, dy) = 2. Rotation: R(θ) = 1 0 dx 0 1 dy 0 0 1 cosθ sinθ 0 sinθ cosθ 0 0 0 1 3. Scale: S(sx, sy) = sx 0 0 0 sy 0 0 0 1 4. Shear: H x (h x ) = 1 h x 0 0 1 0 0 0 1 6
Transformations: Import of Linear Functions Using homogeneous representations, q = t(p), u = t(v), where t represents a transform CG concerned with transforms that are linear functions t is a linear function iff for any scalars α, β and points p, q t(αp + βq) = αt(p) + βt(q) I.e., can calculate the transform of linear combinations of points by linearly combining the transformed points Note that lines, whose parametric equations are of form αp + βq, are linear combinations of points Ramifications for CG: To transform a line, we do not have to do so on a point-by-point basis We can simply transform the endpoints, and then redraw the line between them 7
Transformations: Window to Viewport Mapping Points in world system are projected onto projection plane during projection The projection plane is then rasterized, which includes converting projected points in world coordinates to corresponding points in the viewport (window coordinates) This represents a transformation in 2-space To accomplish this: 1. Translate projection plane to origin 2. Scale to size of viewport 3. Translate to origin of viewport Details: Given: 1. Projection plane defined by (x min, y min ) and (x max, y max ) 2. Viewport defined by (u min, v min ) and (u max, v max ) 1. Translate projection plane to origin: T ( x min, y min ) = 1 0 x min 0 1 y min 0 0 1 2. Scale to size of viewport: Projection plane width = x max x min ; height = y max y min Viewport width = u max u min ; height = v max v min S ( umax u min, v ) max v min = x max x min y max y min u max u min x max x min 0 0 v 0 max v min y max y min 0 0 0 1 3. Translate to origin of viewport: T (u min, v min ) = 1 0 u min 0 1 v min 0 0 1 8
Transformations: Window to Viewport Mapping (2) Combining: M w v = T (u min, v min ) S ( umax u min, v ) max v min T ( x min, y min ) x max x min y max y min If P = = x y 1 u max u min x max x min 0 x min then 0 v max v min y max y min y min ( umax u min x max x min ( vmax v min y max y min 0 0 1 M P = ( umax u min (x x min ) x max x min ( vmax v min (y y min ) y max y min 1 ) ) + u min + v min ) ) + u min + v min 9
Transformations: 3D Basically the same as for 2D, but extended by a row and column 1. Translation: 2. Scale: 1 0 0 dx 0 1 0 dy 0 0 1 dz 0 0 0 1 sx 0 0 0 0 sy 0 0 0 0 sz 0 0 0 0 1 3. Rotation: Requires 3 separate matrices, 1 about each axis (a) About the x-axis R x (θ) = (b) About the y-axis R y (θ) = (c) About the z-axis R z (θ) = 1 0 0 0 0 cosθ sinθ 0 0 sinθ cosθ 0 0 0 0 1 cosθ 0 sinθ 0 0 1 0 0 sinθ 0 cosθ 0 0 0 0 1 cosθ sinθ 0 0 sinθ cosθ 0 0 0 0 1 0 0 0 0 1 10
4. Shear Transformations: 3D (2) Requires 3 separate matrices, 1 based on each coordinate (a) Based on z (in x, y): H xy (h x, h y ) = (b) Based on y (in x, z): H xz (h x, h z ) = (c) Based on x (in y, z): H yz (h y, h z ) = 1 0 h x 0 0 1 h y 0 0 0 1 0 0 0 0 1 1 h x 0 0 0 1 0 0 0 h z 1 0 0 0 0 1 1 0 0 0 h y 1 0 0 h z 0 1 0 0 0 0 1 11
Transformations: Compound Transformations (Catenation) To perform general transform of object 1. Translate to origin 2. Scale, rotate, shear 3. Translate to final position Resulting matrix has form r 11 r 12 r 13 t x r 21 r 22 r 23 t y r 31 r 32 r 33 t z 0 0 0 1 where r ij represent rotation, scale, and shear factors t k represent translation factors 2 approaches to deriving general rotation matrix 1. Brute force 2. Orthogonal matrices 12
Transformations: Orthogonal Matrices An orthogonal matrix O is a square matrix with the following special properties. 1. Each row is a unit vector. 2. Each of the row vectors is perpendicular to the other row vectors. 3. Each column is a unit vector. 4. Each of the column vectors is perpendicular to the other column vectors. 5. O 1 = O T 6. If one of the row vectors is multiplied by O, the result is equivalent to rotating that row vector about the origin so that it becomes coincident with an axis: The first row vector rotates into the X axis, The second row into the Y axis, The third into the Z axis. 7. Multiplying one of the principal axes by O has the following results. Multiplying the X axis will transform it into the first column vector of O. Multiplying the Y axis will transform it into the second column vector of O. Multiplying the Z axis will transform it into the third column vector of O. The above properties allow generation of a rotation matrix by direct construction This is achieved by 1. Identifying 3 orthonormal axes associated with the object to be rotated 2. These axes should be such that after the rotation, they are lined up with the world axes 3. Placing unit vectors representing these axes into an orthogonal matrix, so that they rotate into the appropriate world axis 13
Transformations: Tranformations and Normals The effect of transforms on normals is important Let n be the normal to a plane, and v be a vector in the plane Then, n v = 0 In matrix representation, n T v = 0 Let v = p p 0 Let M be a transform matrix that transforms v v To maintain orthogonality, we want n n such that n v = 0 Let N be the transform matrix that transforms n n If (n ) T v = 0, then 1. (Nn) T (Mv) = 0 2. n T N T Mv = 0 Since n T v = 0, 1. N T M = I, and 2. N T = M 1 N = (M 1 ) T Note that M 1 may not exist 14
Transformations: Tranformations and Normals (2) When will a transform maintain orthogonality? 1. Let n be the normal to a plane going through point p 0 2. The plane is defined by the points n (p p 0 ) = 0 3. Let M be a transform that transforms p 0 p 0 4. The transformed plane will be defined by w (p p 0) = 0 5. Using matrix representation, w T (p p 0) = 0 = w T (Mp Mp 0 ) = w T M(p p 0 ) 6. This can be rewritten as w T M(p p 0 ) = ((w T M) T ) T (p p 0 ) = (M T w) T (p p 0 ) 7. Now, (M T w) T (p p 0 ) = 0 only if (M T w) = n, which holds when w = (M T ) 1 n 8. Therefore, (M T ) 1 = M when M T = M 1 In other words, w n when M is an orthogonal matrix. As a result, translations and rotations do not affect the normals, but scales and shears do. 15
Transformations: Rotation Problems - Gimbal Lock Consider an object oriented wrt the X-axis Now consider this object oriented wrt the Z-axis via a rotation about Y Represent this orientation as (0, 90, 0) This equivalent to the transform R z (0)R y (90)R x (0) in OpenGL Note that this rotation lines up the X-axis of the object with the global Z-axis Now, consider you want one of the following 1. Slight shift about Z: (±ɛ, 90, 0) Does the job 2. Slight shift about Y: (0, 90 ± ɛ, 0) Does the job 16
Transformations: Rotation Problems - Gimbal Lock (2) 3. Slight shift about X: (0, 90, ±ɛ) Does NOT do the job Result is a change of ±ɛ about the Z-axis! Caused by X having been rotated into Z Problem called gimbal lock Change of orientation can NOT be effected by a simple change in orientation 17
Transformations: Rotation Problems - Keyframe Animation Consider the animation of a change in orientation E.g., K 1 = (0, 90, 0) K 2 = (90, 45, 90) Net result is rotation of object about X-axis by 45 0 in Y-Z plane Using parametric interpolation, the intermediate orientation will be (45, 62.5, 45) This will result in the object rotating OUT of the Y-Z plane as it moves between K 1 and K 2! This path is not what is intuitively desired Problem again due to orientation of X-axis with Z-axis 18
Transformations: Euler Angle Representation Rather than expressing rotation in terms of the fixed global axes, we can express them in terms of an object s local frame These angles are called Euler angles These frequently referred to as roll, pitch, and yaw Consider the following change in orientation: pitch = α, yaw = β, and roll = γ Let R i(θ) represent a rotation about a singly transformed axis Let R i (θ) represent a rotation about a doubly transformed axis 1. The first rotation is achieved by R x (α) This rotates the object and its local frame! 2. The 2nd rotation is achieved by R y(β)r x (α) This is achieved in terms of the global frame using R x (α)r y (β)r x ( α)r x (α) = R x (α)r y (β) 3. The 3rd rotation is achieved by R z(γ)r y(β)r x (α) This is achieved in terms of the global frame using R x (α)r y (β)r z γr y ( β)r x ( α)r x (α)r y (β) = R x (α)r y (β)r z (γ) The result is simply the same series of rotations, in reverse order of the global approach The moral: Euler angles inherit the same problems as using rotations about the world axes 19
Euler s theorem: Transformations: Axis-angle Representation Any orientation can be derived from another by means of a single rotation about some axis Can represent rotations as a 4-tuple: (v x, v y, v z, θ) Consider 1. An initial orientation (v 1, θ 1 ) and 2. A new orientation (v 2, θ 2 ) Can easily interpolate between the initial and goal state by interpolating from 1. v 1 to v 2 2. θ 1 to θ 2 The axis A about which v 1 rotates = v 1 v 2 The angle φ by which v 1 rotates = cos 1 v 1 v 2 v 1 v 2 20
Transformations: Axis-angle Representation (2) Then 1. v i = R A (kφ)v 1 2. φ i = (1 k)θ 1 + kθ 2 where R A is rotation transform 0 i n 0 k 1 k = i n This produces a smooth sequence of orientations without the undesirable effects seen with using rotations about world or local axes Implementation problematic 21
Transformations: Complex Numbers (Review) Complex number z is ordered pair (x, y) where z = x + yi x is real part, y is imaginary part i = 1 = (0, 1) Pure imaginary number has form z = (0, y) Real number has form z = (x, 0) Complex arithmetic If z 1 = (x 1, y 1 ), z 2 = (x 2, y 2 ) Conjugate z of z = x yi z 1 + z 2 = (x 1 + x 2, y 1 + y 2 ) z 1 z 2 = (x 1 x 2 y 1 y 2, x 1 y 2 + x 2 y 1 ) ( z 1 x1 x 2 + y 1 y 2 = z 2 x 2 2 + y2 2, x ) 2y 1 x 1 y 2 x 2 2 + y2 2 Modulus (absolute value) of z = z = zz = x 2 + y 2 22
Transformations: Complex Numbers - Graphical Representation 1. Complex plane Just like 2D points in XY plane Conjugate represents reflection about X-axis Modulus represents distance from origin (vector length of z) 2. Polar coordinates Let r = z and θ = angle from X-axis to vector from origin to z z = r(cosθ + isinθ) = re iθ Euler s formula: e iθ = cosθ + isinθ Complex arithmetic z 1 z 2 = (r 1 r 2 )e i(θ 1+θ 2 ) z 1 /z 2 = (r 1 /r 2 )e i(θ 1 θ 2 ) n [ ( ) ( z = n θ + 2πk θ + 2πk r cos + isin r r k = 0, 1,..., n 1 Roots lie on circle of radius n r about origin of complex plane Represent vertices of regular polygon with n sides )] 23
Transformations: Quaternions Quaternions can be thought of as complex numbers extended to higher dimensions Represented by a 4-tuple of 1 real and 3 imaginaries: q = s + ia + jb + kc = (s, v) i, j, k have the following properties: 1. i 2 = j 2 = k 2 = 1 2. ij = ji = k 3. jk = kj = i 4. ki = ik = j Quaternion arithmetic Let q 1 = s 1 + ia 1 + jb 1 + kc 1, q 2 = s 2 + ia 2 + jb 2 + kc 2 1. q 1 + q 2 = s 1 + s 2 + i(a 1 + a 2 ) + j(b 1 + b 2 ) + k(c 1 + c 2 ) = (s 1 + s 2, v 1 + v 2 ) 2. q 1 q 2 = (s 1 s 2 v 1 v 2, s 1 v 2 + s 2 v 1 + v 1 v 2 ) 3. q 1 + q 2 = q 2 + q 1 4. (q 1 + (q 2 + q 3 ) = (q 1 + q 2 ) + q 3 ) 5. q 1 q 2 q 2 q 1 6. (q 1 (q 2 q 3 ) = (q 1 q 2 ) q 3 ) Magnitude: Inverse: q 2 = s 2 + v 1 v 1 1. q 1 = (1/ q 2 )(s, v) 2. qq 1 = q 1 q = (1, 0) 3. (q 1 q 2 ) 1 = q2 1 q1 1 24
Transformations: Quaternion Rotation Consider rotation by of some point p by θ about vector û that extends from the origin Consider the unit quaternion q = (s, v), where 1. s = cos(θ/2) 2. v = ûsin(θ/2) 3. û = 1 Represent point p = (x, y, z) by quaternion P = (0, p), where p = (x, y, z) To rotate p around û, compute P = qp q 1, where P = (0, p ), and p = (x, y, z ) 1. p = s 2 p + (p v)v + 2s(v p) + v (v p) = M R(θ) p 2. Letting v = [ a b c ] T where a = û x sin(θ/2),... and substituting in above equation for p results in the quaternion rotation matrix: 25
3. M R (θ) = = Transformations: Quaternion Rotation (2) 1 2b 2 2c 2 2ab 2sc 2ac + 2sb 2ab + 2sc 1 2a 2 2c 2 2bc 2sa 2ac 2sb 2bc + 2sa 1 2a 2 2b 2 u 2 x(1 cosθ) + cosθ u x u y (1 cosθ) u z sinθ u x u z (1 cosθ) + u y sinθ u y u x (1 cosθ) + u z sinθ u 2 y(1 cosθ) + cosθ u y u z (1 cosθ) u x sinθ u z u x (1 cosθ) u y sinθ u z u y (1 cosθ) + u x sinθ u 2 z(1 cosθ) + cosθ using cos 2 (θ/2) sin 2 (θ/2) = 1 2sin 2 (θ/2) = cosθ 2cos(θ/2)sin(θ/2) = sinθ Rotations using the inverse of a quaternion are equivalent to rotation about the same axis in the opposite direction. Given quaternions p and q, R q (R p (v)) = q(pvp 1 )q 1 = (qp)v(qp) 1 = R qp (v) 26
Transformations: Quaternion Rotation - Basis Consider rotation of vector r about unit vector ˆn by θ Result is vector r r can be decomposed into 2 vectors: 1. r p parallel to ˆn 2. r n normal to ˆn, lying in the plane of the circle traced out by r Let v be normal to both r p and r n Then 1. r p = (ˆn r)ˆn 2. r n = r r p 3. v = ˆn r n = ˆn r 4. r n = (cosθ)r n + (sinθ)v 27
Transformations: Quaternion Rotation - Basis (2) So r = r p + r n = r p + (cosθ)r n + (sinθ)v = (ˆn r)ˆn + (cosθ)(r (ˆn r)ˆn) + (sinθ)ˆn r = (cosθ)r + (1 cosθ)(ˆn r)ˆn + (sinθ)ˆn r For any quaternion q = (s, v), There exists a v and θ [ π, π] such that 1. q = (cosθ, v sinθ) 2. If q is a unit quaternion, then q = (cosθ, ˆv sinθ) Consider quaternion p = (0, r) qpq 1 = (0, (s 2 v v)r + 2(v r)v + 2s(v r)) Letting q = (cosθ, (sinθ)ˆn), qpq 1 = (0, (cos 2 θ sin 2 θ)r + 2sin 2 θ(ˆn r)ˆˆn + ˆn r = (0, rcos2θ + (1 cos2θ)(ˆn r)ˆn + sin2θ(ˆn r) The 2 formuli for qpq 1 are the same except that one angle is twice the other Hence, to calculate a rotation of θ about an axis n, use qpq 1, where q = (cos(θ/2), sin(θ/2)n)) 28