Math Review - Boise State Universitycs.boisestate.edu/~alark/cs464/lectures/Math_Review.pdf ·...
Transcript of Math Review - Boise State Universitycs.boisestate.edu/~alark/cs464/lectures/Math_Review.pdf ·...
Math Review
OpenGL Demos
double.c
alpha.c
unproject.c
Demo shapes example from Nate Robins’ tutorial
Math Review
A vector describes a length and a direction.
Length and direction gives offset
a
Vector Operations
A vector’s length is denoted |a| and is computed as
A unit vector is any vector whose length is one
The zero vector is the vector of zero length
222|| aaa zyxa
Vector Operations
Two vectors are added according to the parallelogram rule.
a
b
a+b
a
b a + b = b + a
Vector Addition is commutative
Vector Subtraction
A unary minus for a vector: -a is a vector with the same
length but opposite direction
This allows us to define subtraction
b – a = – a + b
a
b
b – a
Dot Product
Simplest way to multiply two vectors is the dot product
The dot product of two vectors a and b is denoted by a·b
and is also known as the scalar product because it returns
a scalar
a· b = |a| |b| cos φ
b
a
φ
Dot Product
The dot product is associative
a· b = b· a
The dot product is distributive
a· (b+c) = a· b + a· c
The dot product of two 3D vectors a and b
expressed in Cartesian coordinates is given by
a· b = xa xb + ya yb + za zb
Cross Product
The cross product of two vectors returns a 3D
vector perpendicular to both
Right-hand rule dictates direction of cross product
Cross Product
The cross product has a nice property that
a × ( b + c ) = a × b + a × c
However, as a consequence of the right-hand rule
a × b = – ( b × a )
The cross product of two 3D vectors a and b
expressed in Cartesian coordinates is given by
a × b = (ya zb - za yb, za xb - xa zb, xa yb - ya xb)
Orthogonal and Normal
Orthogonal = perpendicular: u· v = 0
Normal = unit-length : u· u = 1
Orthonormal: set of vectors both orthogonal and
normal
Orthogonal matrix: rows (& columns)
orthonormal For orthogonal matrices:
A-1 = AT
ATA = AAT = I
Examples?
Orthonormal Bases
Any set of three 3D vectors u, v and w form an
orthonormal basis provided they are
orthogonal and are each of unit length.
|u| = |v| = |w| = 1 and
u· v = v· w = w· u = 0
This orthonormal basis is right-handed if
w = u × v
otherwise it is left-handed
Coordinate systems
The Cartesian orthonormal basis has an implicit
origin
The vectors x, y and z are never explicitly stored and
neither is the canonical origin location O. It is also
called the global coordinate system.
To use another coordinate system with origin p and
orthonormal basis vectors u, v and w, we do store the
vectors explicitly.
Coordinate systems
The coordinate system associated with the object, such as the plane,
is called a local coordinate system
Local coordinate system
At a low level, the local frame is stored in canonical
coordinates, for eg
u = (xu , yu , zu) = xux + yuy + zuz
The origin is stored as an offset from the canonical origin
p = (xp , yp , zp) = o + xpx + ypy + zpz
Linear Interpolation
Perhaps the most common mathematical
operation in graphics is linear interpolation
To form a line segment between two points a and
b a parameter t is used for interpolation
p = ( 1 –t ) a + t b
This is interpolation because p goes through a and b
for t=0 and t=1 respectively
It is linear because the weighting terms t and 1-t
are linear polynomials of t.
Questions and Break!
Recommendation – Create a vector manipulation library
SIGGRAPH 2010 Papers video:
http://www.youtube.com/watch?v=-PMf3XrwPKo
Basis Vectors
A 2D vector can be written as a combination of any two
non-zero vectors which are not parallel
This property of the two vectors is called linear
independence
Two such vectors which are linearly independent form a
2D basis and the vectors are thus referred to as basis
vectors
Basis Vectors
For example, a vector c may be expressed as a
combination of two basis vectors a and b
c = aca + bcb
Vectors ac and bc are unique. More useful if they are
orthonormal.
Basis Vectors
In the Cartesian coordinate system, we have such ‘special’ vectors x, y and z (in case of 3D), which can be used to represent all other vectors.
Each vector is represented as three real numbers
a = xax + yay + zaz
where xa, ya and za are the real Cartesian coordinates of the 3D vector
Vector space
Given a basis for a vector space:
Each vector in the space is a unique linear combination of the
basis vectors
The coordinates of a vector are the scalars from this linear
combination
Note that a given vector will have different coordinates for
different bases
Example with Cartesian coordinate system
Slide courtesy: David Luebke
Linear Transformations
A linear transformation:
Maps one vector to another (useful when using local coordinate systems)
Preserves linear combinations
Thus behavior of linear transformation is completely determined by what it does to a basis
Turns out any linear transform can be represented by a matrix
Slide courtesy: David Luebke
Matrices
A matrix is an array of numeric elements that follow
certain arithmetic rules
Matrices are frequently used in computer graphics for a
variety of purposes
Demo from Nate Robins’ tutorial
http://www.youtube.com/watch?v=deSpf9EGcvw
Matrices
mnm2m1
2n2221
1n1211
MMM
MMM
MMM
M
3
2
v
v
v
v
1
• By convention, matrix element Mrc is located
at row r and column c:
• By (OpenGL) convention,
vectors are columns:
Slide courtesy: David Luebke
Matrix Arithmetic
Matrix times a constant
222120
121110
020100
aaa
aaa
aaa
A
then bA is
222120
121110
020100
bababa
bababa
bababa
babA ij
where b is a constant
Matrix Addition
222120
121110
020100
aaa
aaa
aaa
A
222120
121110
020100
bbb
bbb
bbb
B
• With matrices A, B
• To compute C = A + B
ijijij bac
Matrix Multiplication
• With matrices A, B
• To compute C = A B
222120
121110
020100
aaa
aaa
aaa
A
222120
121110
020100
bbb
bbb
bbb
B
jijijiij bababac 221100
Matrix Multiplication
• With matrices A, B
• To compute C = A B
• For example
222120
121110
020100
aaa
aaa
aaa
A
222120
121110
020100
bbb
bbb
bbb
B
jijijiij bababac 221100
20121011001010 bababac
Transpose of a Matrix
The transpose AT of a matrix A is one whose rows are
switched with its columns
2120
1110
0100
aa
aa
aa
A
121110
020100
aaa
aaaA
T
Determinants
If we have two 2D vectors r and s, we denote the
determinant as |rs|. This value is the signed area of the
parallelogram formed by the vectors
For two 2D vectors with cartesian coordinates (x1, y1)
and (x2, y2) the determinant can be written as
2121
21
21
2
2
1
1
xyyxyy
xx
y
x
y
x
Laplace’s expansion
The standard way of managing the algebra of
computing determinants
The key part is to find cofactors
Each element of a square matrix has a cofactor which
is the determinant of a matrix with one fewer row
and column
The smaller matrix is obtained by eliminating the row
and column that the element is in.
The sign of a cofactor is +ve is the sum of the row
and column indices is even and –ve otherwise
444342
343332
242322
11
aaaaaaaaa
a
444341
343331
242321
12
aaaaaaaaa
a
Cofactor
For example, for a 5 by 5 matrix, the cofactor of a42 is the
determinant of the 4 by 4 matrix with the 4th row and 2nd
column eliminated
For a 4 by 4 matrix, the cofactor for a11 is
44434241
34333231
24232221
14131211
aaaa
aaaa
aaaa
aaaa
A
c
c
The determinant of a matrix is found by taking the sum of
products of the elements of any row or column with
their cofactors
Matrix is singular if the determinant is 0
For example, for the 4 by 4 matrix in the previous slide
Determinant of a Matrix
4242323222221212 aaaaaaaaA c c c c
Lets solve an example of a 3x3 matrix on the whiteboard
44434241
34333231
24232221
14131211
aaaa
aaaa
aaaa
aaaa
A
Inverse of a Matrix
Determinants give us a great tool to compute the inverse
of a matrix
For a 4 by 4 matrix A, the inverse A-1 is given by
44342414
43332313
42322212
41312111
cccc
cccc
cccc
cccc
1
aaaa
aaaa
aaaa
aaaa
A
1A
Inverse of a Matrix
44342414
43332313
42322212
41312111
cccc
cccc
cccc
cccc
aaaa
aaaa
aaaa
aaaa
• This is the transpose of Matrix A where its
elements are replaced by their respective
cofactors
• This matrix is called the Adjoint of A
Inverse of a Matrix
If we multiple A with its adjoint,we get
....
....
....
...
...
...
...
...
....
....
....
A
a
a
a
aaaaa
14
13
12
1114131211
c
c
c
c
....
....
...
....
...
...
...
...
....
....
....
0
a
a
a
a
aaaa
14
13
12
11
24232221
c
c
c
c
On dividing the resultant matrix by |A|, we get the identity matrix.
Linear systems
We often encounter linear systems in computer graphics
with “n equations and n unknowns”.
We can write this in matrix form as
1zy2x5
1z3y4x2
4z2y7x3
1
1
4
z
y
x
125
342
273
Linear systems
A common shorthand for such systems is Ax = b
Many ways to solve such systems, but for small
systems, the Cramer’s rule is very useful.
125
342
273
121
341
274
x
125
342
273
125
142
473
z
125
342
273
115
312
243
y
Eigenvalues and Eigenvectors
Eigen-analysis is very important in science and
engineering
For example, eigenvalues are related to frequencies
The stability of a bridge can be predicted by eigen analysis of a
system modeling the bridge
Tacoma Narrows bridge collapse
http://www.youtube.com/watch?v=P0Fi1VcbpAI
Eigen in German means characteristic or special
Eigenvalues and Eigenvectors
Square matrices have eigenvalues and eigenvactors
associated with them
Eigenvectors are those non-zero vectors whose
directions do not change when multiplied by the matrix
Image credits: Wikipedia
Eigenvalues and Eigenvectors
For example, for a matrix A and a vector a,
λ is the eigenvalue associated with the eigenvector a
It means that vector a has been stretched/compressed,
but its direction is still the same
aAa
Eigenvalues and Eigenvectors
Extremely helpful for geometric transformations
Let us assume that a matrix has at least one eigenvector a
Let us find it using a standard manipulation,
0)(
0
aIA
IaAa
IaAa
I is the identity matrix
Matrix multiplication is distributive,
so we can group the matrices
Eigenvalues and Eigenvectors
can only be true if (A-λI) is singular
Singular = determinant is zero
For example, for a 2x2 matrix, the eigenvalues obey
Since this is a quadratic equation, there are exactly two
solutions to λ, which may or may not be unique or real
0)( aIA
0)()( 211222112211
2
2221
1211
aaaaaa
aa
aa
Recommended Exercises
Highly recommend writing a matrix operation library too
Read example on Page 105 (FCG 3rd edition)
Read about Singular Value Decomposition
Very useful technique in computer graphics, machine learning
and data analysis tasks
45
Trivia
When did Computer generated graphics make their
feature film debut and what was the name of the movie?
In the 1973 film West World (starring scary bald robot-
gone-mad Yul Brunner), when a pixilated image was used
to portray a shot from an android's point of view.