CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra Primer Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2016
Announcements Monday October 3: Discussion Assignment 1 Friday October 7: Assignment 1 due 2
Today s Topics Vectors and matrices Affine transformations Homogeneous coordinates 3
Vectors Give direction and length in 3D Vectors can describe Difference between two 3D points Speed of an object Surface normals (directions perpendicular to surfaces) Normal vector Surface normals Surface 4
Vector arithmetic using coordinates a = ax ay az b = bx by bz a + b = ax + bx ay + by az + bz a b = ax bx ay by az bz ax a= ay az sax sa= say saz where s is a scalar 5
Vector Magnitude The magnitude (length) of a vector is: v 2 = v x 2 + v y 2 + v z 2 v = v x 2 + v y 2 + v z 2 A vector with length of 1.0 is called unit vector We can also normalize a vector to make it a unit vector Unit vectors are often used as surface normals v v 6
Dot Product a b = a i b i a b = a x b x + a y b y + a z b z a b = a b cosθ 7
Angle Between Two Vectors a b = a b cosθ cosθ = a b a b b θ = cos 1 a b a b a 8
Cross Product a b is a vector perpendicular to both a and b, in the direction defined by the right hand rule a b = a b sinθ a b = area of parallelogram ab a b = 0 if aand b are parallel (or one or both degenerate) 9
Cross Product = = 10
Cross Product Calculation = = = 11
Matrices Rectangular array of numbers Square matrix if m = n In graphics almost always: m = n = 3; m = n = 4 12
Matrix Addition 13
Multiplication With Scalar 14
Matrix Multiplication 15
Matrix-Vector Multiplication 16
Identity Matrix 17
Matrix Inverse If a square matrix M is non-singular, there exists a unique inverse M -1 such that 18
Today s Topics Vectors and matrices Affine transformations Homogeneous coordinates 19
Affine Transformations Most important for graphics: rotation, translation, scaling Wolfram MathWorld: An affine transformation is any transformation that preserves collinearity (i.e., all points lying on a line initially still lie on a line after transformation) and ratios of distances (e.g., the midpoint of a line segment remains the midpoint after transformation). Implemented using matrix multiplications 20
Uniform Scale Uniform scale matrix in 2D Analogous in 3D: 0 0 0 0 0 0 21
Non-Uniform Scale Nonuniform scaling matrix in 2D Analogous in 3D: 22
Rotation in 2D Convention: positive angle rotates counterclockwise Rotation matrix 23
Rotation in 3D Rotation around coordinate axes 24
Rotation in 3D Concatenation of rotations around x, y, z axes are called Euler angles Result depends on matrix order! 25
Rotation about an Arbitrary Axis Complicated! Rotate point [x,y,z] about axis [u,v,w] by angle θ: 26
How to rotate around a Pivot Point? Rotation around origin: p = R p Rotation around pivot point: p =? 27
Rotating point p around a pivot point 1. Translation T 2. Rotation R 3. Translation T -1 p = T -1 R T p 28
Concatenating transformations Given a sequence of transformations M 3 M 2 M 1 Note: associativity applies 29
Today s Topics Vectors and matrices Affine transformations Homogeneous coordinates 30
Translation Translation in 2D t x t y Translation matrix T=? = = = =???? 31
Translation Translation in 2D: 3x3 matrix Analogous in 3D: 4x4 matrix 32
Homogeneous Coordinates Basic: a trick to unify/simplify computations. Deeper: projective geometry Interesting mathematical properties Good to know, but less immediately practical We will use some aspect of this when we do perspective projection 33
Homogeneous Coordinates Add an extra component. 1 for a point, 0 for a vector: p = Combine M and d into single 4x4 matrix: p x p y p z 1 And see what happens when we multiply r v = v x v y v z 0 m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 34
Homogeneous Point Transform Transform a point: p x p y p z 1 = m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 p x p y p z 1 = m xx p x + m xy p y + m xz p z + d x m yx p x + m yy p y + m yz p z + d y m zx p x + m zy p y + m zz p z + d z 0 + 0 + 0 + 1 Top three rows are the affine transform! Bottom row stays 1 M p x p y p z + r d 35
Homogeneous Vector Transform Transform a vector: v x v y v z 0 = m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 v x v y v z 0 = m xx v x + m xy v y + m xz v z + 0 m yx v x + m yy v y + m yz v z + 0 m zx v x + m zy v y + m zz v z + 0 0 + 0 + 0 + 0 M v x v y v z Top three rows are the linear transform Displacement d is properly ignored Bottom row stays 0 36
Homogeneous Arithmetic Legal operations always end in 0 or 1! M vector+vector: 0 + M M 0 0 M vector-vector: 0 M M 0 0 scalar*vector: s M M 0 0 M point+vector: 1 + M M 0 1 M point-point: 1 M M 1 0 M point+point: 1 + M M 1 2 scalar*point: s M M 1 s weighted average affine combination of points: 1 M 3 1 + 2 M 3 M 1 1 37
Homogeneous Transforms Rotation, Scale, and Translation of points and vectors unified in a single matrix transformation: p = M p Matrix has the form: Last row always 0,0,0,1 m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 Transforms can be composed by matrix multiplication Same caveat: order of operations is important Same note: transforms operate right-to-left 38