Matrices and Transforms Matrix and Vector Properties Vectors are just 1xn or nx1 matrices Column or row major – depends on which kind of vector you choose Homogeneous coordinates – invented to allow translations (a non-linear operation) to be represented using matrix multiplication (instead of addition) Representation in memory (C/C++) Matrix Operations Addition and Subtraction Matrix multiplication Transpose Determinant (if non-zero, inverse exists; if 1, pure rotation [special orthogonal]) Matrix inverse (same as transpose for an orthogonal matrix) Atomic Transforms Translation o As a vector operation o Using homogenous coordinates Rotation o Around one axis o Around an arbitrary axis Scale o Uniform; Non-uniform (this will hose normals) Shearing o [ 1 0 shx 0 ][ 0 1 shy 0 ][ 0 0 1 0 ][ 0 0 0 1 ], gives [ (x + shx z) (y + shy z) z 1] Concatenation: Multiply atomic matrices to generate composite matrix – order matters! Types of Matrices o Special Orthogonal (aka Isotropic): each row or col is a unit vector; the vectors are mutually orthogonal [ = pure rotation matrices] o Affine: Preserves parallelism of lines and relations of distances, but not lengths and angles [ = special orthogonal + translation + (non-uniform) scale + shear] (product of rotation, translation, scale and/or shear) Transforming Vectors vs. Points Vectors ignore the translation In homogeneous coordinates, vectors have w=0, not w=1 Describing Position, Orientation and Scale of a Rigid Body Think of the rigid body as having a local coordinate system (Model Space) o Front, Left, Up o Roll (rotation about Front), Pitch (rot about Left), Yaw (rot about Up) The world has a global coordinate system (World Space) Pos, Orient and Scale of a rigid body is really just a matrix transforming the Model Space coord. system into World Space (Model-to-World) Transforming Coordinate Systems Matrix as (i, j, k, t) vectors Mc-p = [ ic jc kc tc ] scaling applies to upper 3x3 REVISIT: Representation in memory (C/C++) Alternatives for Representing Rotations Euler Angles o Pros: simplest/smallest representation, intuitive (pitch, yaw, roll), can interpolate for simple rotations about a single axis o Cons: gimbal lock, difficult to interpolate arbitrary rotations Matrices o Pros: no gimbal lock, easy to apply to rigid body representation, corresponds directly to coordinate axes o Cons: large (9 elements), unintuitive, difficult to interpolate Axis + Angle o Pros: no gimbal lock, intuitive o Cons: difficult to interpolate Quaternions o What are they? q qxi + qyj + qzk + qw = [ qv, qw ] o Unit quaternions are a representation for rotations: where qw = cos(½) and qx = qy = qz = sin(½), n = unit rotation axis; i.e. [sin(½)n, cos(½) ] o Pros: no gimbal lock, relatively intuitive (much like axis + angle), easy to interpolate (LERP, SLERP) o Cons: more expensive to apply to rigid body orientations (either need mat-to-quat and quat-to-mat, or do quat-vector and quat-quat multiplies) o Conjugate: q = –qxi – qyj – qzk + qw o Norm/Length: |q| = qq o Inverse: q–1 = q / |q| = q / (qq) = q (conjugate) for unit quaternions o Sum: (q + r) = (qx + rx)i + (qy + ry)j + (qz + rz)k + (qw + rw) = [(qv + rv), (qw + rw)] o Product: q r = (qw rx + qx rw + qy rz – qz ry)i + (qw ry + qy rw – qx rz + qz rx)j + (qw rz + qz rw + qx ry – qy rx)k + (–qx rx – qy ry – qz rz + qw rw) = [ qv, qw ] [ rv, rw ] = [ (qwrv + rwqv + qv rv), (qwrw – qv rv) ] o Rotation of a point p by quaternion q: First, convert p into a “quaternion form” p = [ p, 0 ] Next, find p' = qpq–1 = qpq = (qp)q Finally, convert p' back to p' by simply extracting it from [ p', 0 ] o Interpolation: s1 = LERP(q, r, ) = (1 – )q + ()r (e.g. = 0.2: 20% of the way from q to r) s2 = SLERP(q, r, ) = ()q + ()r where we define cos–1(qv rv) and then find = ((1 – )) / sin() and = () / sin() A Note on Degrees of Freedom o DOF = dimension – constraint count o Euler Angles: dimension=3, constraints=0, DOF=3 o Quaternions: dimension=4, constraints=1 (unit quaternion), DOF=3 o 3x3 Matrices: dimension=9, constraints=6 (all 6 unit vectors), DOF=3 Resources o http://skal.planet-d.net/demo/matrixfaq.htm#Q1 o http://mathworld.wolfram.com/Quaternion.htm o http://ggt.sourceforge.net/html/group__Interp.html#a1