CSE 167: Introduction to Computer Graphics Lecture #2...

38
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

Transcript of CSE 167: Introduction to Computer Graphics Lecture #2...

Page 1: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

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

Page 2: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Announcements

� Monday October 3: Discussion Assignment 1

� Friday October 7: Assignment 1 due

2

Page 3: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Today’s Topics

� Vectors and matrices

� Affine transformations

� Homogeneous coordinates

3

Page 4: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

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)

Surface normals Surface

Normal vector

4

Page 5: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

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

sa=

sax

say

saz

−a=

−ax

−ay

−az

where s is a scalar

5

Page 6: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Vector Magnitude

� The magnitude (length) of a vector is:

� 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

v2

= vx

2+ vy

2+ vz

2

v = vx

2+ vy

2+ vz

2

v

v

6

Page 7: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Dot Product

a ⋅ b = aibi∑a ⋅ b = axbx + ayby + azbz

a ⋅ b = a b cosθ

7

Page 8: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

a

b

a ⋅ b = a b cosθ

cosθ =a ⋅ b

a b

θ = cos−1 a ⋅ b

a b

Angle Between Two Vectors

8

Page 9: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

area of parallelogram ab

if a and b are parallel

(or one or both degenerate)

a × b

a × b = a b sinθ

a × b =

a × b = 0

Cross Product

is a vector perpendicular to both a

and b, in the direction defined by

the right hand rule

9

Page 10: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Cross Product

10

� × � =

������

×

����

��

=

���� − ����

���� − ����

���� − ����

Page 11: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

������

×

����

��

=

���� − ����

���� − �������� − ����

������

×

����

��

=

���� − ����

���� − �������� − ����

Cross Product Calculation

11

������

×

����

��

=

���� − ����

���� − �������� − ����

Page 12: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Matrices

� Rectangular array of numbers

� Square matrix if m = n

� In graphics almost always: m = n = 3; m = n = 4

12

Page 13: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Matrix Addition

13

Page 14: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Multiplication With Scalar

14

Page 15: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Matrix Multiplication

15

Page 16: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Matrix-Vector Multiplication

16

Page 17: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Identity Matrix

17

Page 18: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Matrix Inverse

If a square matrix M is non-singular, there exists a unique inverse M-1 such that

18

Page 19: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Today’s Topics

� Vectors and matrices

� Affine transformations

� Homogeneous coordinates

19

Page 20: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

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

Page 21: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Uniform Scale

� Uniform scale matrix in 2D

� Analogous in 3D:

21

0 0

0 0

0 0

Page 22: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Non-Uniform Scale

� Nonuniform scaling matrix in 2D

� Analogous in 3D: 22

Page 23: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Rotation in 2D

� Convention: positive angle rotates counterclockwise

� Rotation matrix

23

Page 24: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Rotation in 3D

Rotation around coordinate axes

24

Page 25: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Rotation in 3D

� Concatenation of rotations around x, y, z axes

� are called Euler angles

� Result depends on matrix order!

25

Page 26: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Rotation about an Arbitrary Axis

� Complicated!

� Rotate point [x,y,z] about axis [u,v,w] by angle θ:

26

Page 27: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

How to rotate around a Pivot Point?

Rotation around

origin:

p’ = R p

Rotation around

pivot point:

p’ = ?

27

Page 28: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Rotating point p around a pivot point

1. Translation T 2. Rotation R 3. Translation T-1

28

p’ = T-1 R T p

Page 29: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Concatenating transformations

� Given a sequence of transformations M3M2M1

� Note: associativity applies

29

Page 30: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Today’s Topics

� Vectors and matrices

� Affine transformations

� Homogeneous coordinates

30

Page 31: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Translation

� Translation in 2D

� Translation matrix T=?

31

tx

ty

�� =����

����

= �� = �����

=? ?

? ?

����

Page 32: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

Translation

� Translation in 2D: 3x3 matrix

� Analogous in 3D: 4x4 matrix

32

Page 33: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

33

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

Page 34: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

34

Homogeneous Coordinates

� Add an extra component. 1 for a point, 0 for a vector:

� Combine M and d into single 4x4 matrix:

� And see what happens when we multiply…

p =

px

py

pz

1

rv =

vx

vy

vz

0

mxx mxy mxz dx

myx myy myz dy

mzx mzy mzz dz

0 0 0 1

Page 35: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

35

Homogeneous Point Transform

� Transform a point:

� Top three rows are the affine transform!

� Bottom row stays 1

′px

′py

′pz

1

=

mxx mxy mxz dx

myx myy myz dy

mzx mzy mzz dz

0 0 0 1

px

py

pz

1

=

mxx px + mxy py + mxz pz + dx

myx px + myy py + myz pz + dy

mzx px + mzy py + mzz pz + dz

0 + 0 + 0 + 1

M

px

py

pz

+ rd

Page 36: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

36

Homogeneous Vector Transform

� Transform a vector:

� Top three rows are the linear transform

� Displacement d is properly ignored

� Bottom row stays 0

′vx

′vy

′vz

0

=

mxx mxy mxz dx

myx myy myz dy

mzx mzy mzz dz

0 0 0 1

vx

vy

vz

0

=

mxxvx + mxyvy + mxzvz + 0

myxvx + myyvy + myzvz + 0

mzxvx + mzyvy + mzzvz + 0

0 + 0 + 0 + 0

M

vx

vy

vz

Page 37: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

37

Homogeneous Arithmetic

� Legal operations always end in 0 or 1!

vector+vector: M

0

+

M

0

M

0

vector-vector: M

0

M

0

M

0

scalar*vector: sM

0

M

0

point+vector: M

1

+

M

0

M

1

point-point: M

1

M

1

M

0

point+point: M

1

+

M

1

M

2

scalar*point: sM

1

M

s

weighted average

affine combination

of points: 1

3

M

1

+

2

3

M

1

M

1

Page 38: CSE 167: Introduction to Computer Graphics Lecture #2 ...ivl.calit2.net/wiki/images/0/0e/02_LinearAlgebraF16.pdf · CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra

38

Homogeneous Transforms

� Rotation, Scale, and Translation of points and vectors unified in a single matrix transformation:

� Matrix has the form:� Last row always 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

mxx mxy mxz dx

myx myy myz dy

mzx mzy mzz dz

0 0 0 1

′p = M p