180 likes | 391 Views
Matrices. " The interpretation of a matrix as a geometrical operator is the foundation of mathematical transformations useful in computer graphics ." – Larry Roberts
E N D
Matrices "The interpretation of a matrix as a geometrical operator is the foundation of mathematical transformations useful in computer graphics." – Larry Roberts “This 4D coordinate system is one of the most subtle and beautiful constructs used in computer science, and it is certainly the biggest intellectual hurdle to jump when learning computer graphics.” – Peter Shirley An m by n matrix has m rows and n columns. The identity matrix is a square matrix with 1 along the diagonal, 0 elsewhere
Matrices Addition is defined for two matrices of the same dimension. Multiplication: an m by n matrix may be multiplied by an n by p matrix, yielding an m by p matrix. In general, matrix multiplication is not commutative, AB ≠ BA. But it is distributive: A(B+C) = AB+AC and (A+B)C = AC+BC. And associative: A(BC) = (AB)C.
Post-multiplication of Vector by a Matrix the vector is written as a row followed by the matrix; the vector dimension must equal the number of matrix rows. Graphics yesteryear. Pre-multiplication of Vector by a Matrix the vector is written as a column after the matrix; the vector dimension must equal the number of matrix columns. Graphics today. Transpose of a Matrix An m by n matrix A is transposed to AT, an n by m matrix, such that AT(i)j) = A(j)(i). The following identity holds: AB = (BTAT)T If M is a square matrix, and v a row vector (x, y, z), then
Types of Transformations Euclidean: rigid body operations (rotation and translation): angles remain congruent, distances unchanged, parallelism preserved Affine: rotation, translation, uniform or differential scale; parallelism and ratio of parallel distances preserved Projective: rotation, translation, scale, perspective; straight lines preserved
Rotation Two-Dimensional Transformations From the Angle Sum Identities, we know a point (x, y) may be rotated counter-clockwise about the origin by amount θ radians: x’ = xcosθ−ysinθ y’ = xsinθ+ycosθ This is an example of a linear system, which is easily represented by a matrix: (x’, y’) =
Scale Change size x’ = sxx; y’ = syy (x’, y’) =
Translation Straight line motion x’ = x+tx; y’ = y+ty (x’, y’) = Here, an additional column in the matrix and an additional ‘homogeneous’ coordinate of 1 in the vector allow matrix representation of translation (in effect, addition is being disguised as multiplication).
Use of a Homogeneous Coordinate A 2 by 3 matrix allows translation, but a square matrix is desired because: • unlike non-square matrices, it is invertable • a vector times a square matrix yields a vector of the same dimension Thus, in addition to an extra row, an extra column is added The multiplication yields the same x' and y', as well as an additional homogeneous coordinate. “Homogeneous" denotes the fact that all terms in an equation are of the same degree; this results in certain uniformities and facilitates certain operations. It is often convenient to solve n-dimensional problems in an n+1 hyperspace. "The use of homogeneous coordinates throughout is extremely important in order to maintain the simplicity of the results although its original purpose was to allow perspective transformations." -- Lawrence Roberts, 1966
Consistent use of Homogeneous Coordinates Scale Rotation Translation
Staying Centered To scale a point p with respect to some fixed center (cx, cy) Move p, scale, then move back: x’ = cx+sx(px-cx); y’ = cy+sy(py-cy) In matrix form mat4 xform = Translate(-c)*Scale(s)*Translate(c); To rotate a point p about some fixed center (cx, cy) Move p, rotate, then move back: x’ = cx+(x−cx)cosθ−(y−cy)sinθ y’ = cy+(x−cx)sinθ+(y−cy)cosθ In matrix form mat4 xform = Translate(-c)*RotateZ(θ)*Translate(c);
OpenGL uses a right-handed coordinate system, right-handed rotations, and column-major matrices That is, OpenGL expects an argument GLdouble *m to point to a series of doubles, the first four representing the first column of the matrix, the second four the second column, etc. C++, however, represents matrices in row-major. Thus, double m[4][4] = { {cos, -sin, 0, 0}, {sin, cos, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} } means that m, when passed to a GL routine, is regarded as having {cos, -sin, 0, 0} (the first row!) as the first column. To convert from row-order to column-order (or vice versa) simply transpose the matrix. For pure rotations, the transpose means exchanging −sin(a) and sin(a), which yields the same rotation but in the opposite direction.
Degeneracies Quaternions
Mat.h: 9 elements of a 4x4 matrix are affected by rotation Combination of rotations about X, Y, and Z axes can produce a rotation about any arbitrary axis And yet, that arbitrary axis can be defined by 3 elements, along with an amount of the rotation Thus, quaternions are more efficient, and they …