Vector and Affine Math

Don Fussell Computer Science Department The University of Texas at Austin

University of Texas at Austin CS354 - Computer Graphics Don Fussell Vectors

A vector is a direction and a magnitude Does NOT includeVectors a point of reference Usually thought of as an arrow in space Vectors can be added together and multiplied by scalars Zero vector has no length or direction

University of Texas at Austin CS354 - Computer Graphics Don Fussell Vector Spaces

Set of vectors Closed under the following operations

Vector addition: v1 + v2 = v3

Scalar multiplication: s v1 = v2 n a v v Linear combinations: ∑ i i = i=1 Scalars come from some field F e.g. real or complex numbers Basis Dimension

University of Texas at Austin CS354 - Computer Graphics Don Fussell Axioms

Vector addition is associative and commutative Vector addition has a (unique) identity element (the 0 vector) Each vector has an additive inverse So we can define vector subtraction as adding an inverse Scalar multiplication has an identity element (1) Scalar multiplication distributes over vector addition and field addition Multiplications are compatible (a(bv)=(ab)v)

University of Texas at Austin CS354 - Computer Graphics Don Fussell Coordinate Representation

Pick a basis, order the vectors in it, then all vectors in the space can be represented as sequences of coordinates, i.e. coefficients of the basis vectors, in order. Example: Cartesian 3-space Basis: [i j k] : xi + yj + zk Coordinate representation: [x y z] a[x1 y1 z1]+ b[x2 y2 z2 ] = [ax1 + bx2 ay1 + by2 az1 + bz2 ] University of Texas at Austin CS354 - Computer Graphics Don Fussell Row and Column Vectors

We can represent a vector, v = (x,y), in the plane

as a column vector " x% $ ' # y&

as a row vector x y € [ ]

University€ of Texas at Austin CS354 - Computer Graphics Don Fussell Linear Transformations

Given vector spaces V and W A function f : V → W is a linear map or linear transformation if f (a1v1 +... + amvm ) = a1 f (v1)+... + am f (vm )

University of Texas at Austin CS354 - Computer Graphics Don Fussell Transformation Representation

We can represent a 2-D transformation M by a matrix "a b% M = $ ' #c d& If v is a column vector, M goes on the left: v' = Mv " x! % " a b %" x % € $ ' = $ '$ ' #$ y! &' # c d &#$ y &' If v is a row vector, MT goes on the right: v' = vMT

" a c $ " x! y! $ = " x y $& ' # % # % b d We will use column vectors. # %

University of Texas at Austin CS354 - Computer Graphics Don Fussell Two-dimensional transformations

Here's all you get with a 2 x 2 transformation matrix M: # x "& #a b& # x& % ( = % ( % ( $ y "' $c d' $ y'

So: x " = ax + by € y " = cx + dy

We will develop some intimacy with the elements a, b, c, d… €

University of Texas at Austin CS354 - Computer Graphics Don Fussell Identity

Suppose we choose a=d=1, b=c=0: Gives the identity matrix: "1 0% $ ' #0 1&

Doesn't€ change anything

University of Texas at Austin CS354 - Computer Graphics Don Fussell Scaling

Suppose b=c=0, but let a and d take on any positive value: Gives a scaling matrix: "a 0% $ ' #0 d&

Provides differential (non-uniform) scaling in x and y: y y

€ "2 0% x " = ax 2 2 $ ' #0 2& 1 1 y " = dy x x 12 12 y € "1 2 0% $ ' 2 # 0 2& 1 € x 12

University of Texas €at Austin CS354 - Computer Graphics Don Fussell Reflection

Suppose b=c=0, but let either a or d go negative. Examples: y y

x x

y y

#− 1 0& #1 0 & % ( % ( $ 0 1' x x $0 −1'

€ € University of Texas at Austin CS354 - Computer Graphics Don Fussell Shear

Now leave a=d=1 and experiment with b The matrix "1 b% $ ' #0 1& gives: x " = x + by

€ y " = y y y

"1 1% $ ' € 1 1 #0 1&

x x 1 1

University of Texas at Austin CS354 - Computer€ Graphics Don Fussell Effect on unit square

Let's see how a general 2 x 2 transformation M affects the unit square: "a b% $ ' [p q r s] = [p( q( r ( s( ] #c d& "a b% "0 1 1 0% "0 a a + b b% $ ' $ ' = $ ' #c d& #0 0 1 1& #0 c c + d d& y y

€ s r 1

p q x x 1 University of Texas at Austin CS354 - Computer Graphics Don Fussell Effect on unit square, cont.

Observe: Origin invariant under M M can be determined just by knowing how the corners (1,0) and (0,1) are mapped a and d give x- and y-scaling b and c give x- and y-shearing

University of Texas at Austin CS354 - Computer Graphics Don Fussell Rotation

From our observations of the effect on the unit square, it should be easy to write down a matrix for “rotation about the origin”:

y y "1 % "cos( θ)% $ ' → $ ' #0 & # sin(θ)& 1 "0 % "− sin(θ)% $ ' → $ ' x x #1 & # cos(θ) & 1

Thus $cos( € θ) −sin(θ)' MR = R(θ) = & ) % sin(θ) cos(θ) (

University of Texas at Austin CS354 - Computer Graphics Don Fussell

€ Linear transformations

The unit square observations also tell us the 2x2 matrix transformation implies that we are representing a vector in a new coordinate system: v' = Mv ! a b $! x $ = # &# & " c d %"# y %&

! x $ = ! u w $# & " % "# y %&

= x ⋅ u + y⋅ w where u=[a c]T and w=[b d]T are vectors that define a new basis for a linear space. The transformation to this new basis (a.k.a., change of basis) is a linear transformation.

University of Texas at Austin CS354 - Computer Graphics Don Fussell Limitations of the 2 x 2 matrix

A 2 x 2 linear transformation matrix allows Scaling Rotation Reflection Shearing

Q: What important operation does that leave out?

University of Texas at Austin CS354 - Computer Graphics Don Fussell Points

A point is a location in space Cannot be added or multiplied together Subtract two points to get Pointsthe vector between them Points are not vectors

•UniversityA point of Texas atis Austin a location CS354 - Computer in Graphics space Don Fussell • Cannot be added or multiplied together • Subtract two points to get the vector between them Affine transformations

In order to incorporate the idea that both the basis and the origin can change, we augment the linear space u, w with an origin t. Note that while u and w are basis vectors, the origin t is a point. We call u, w, and t (basis and origin) a frame for an . Then, we can represent a change of frame as:

p! = x ⋅ u + y⋅ w + t

This change of frame is also known as an . How do we write an affine transformation with matrices?

University of Texas at Austin CS354 - Computer Graphics Don Fussell Basic Vector Arithmetic

! r $ ! x $ ! r + x $ ! ax $ # & # & # & # & u = # s & v = # y & u + v = # s + y & av = # ay & # & # & # & # & " t % " z % " t + z % " az %

v v = x2 + y2 + z2 norm(v) = v

University of Texas at Austin CS354 - Computer Graphics Don Fussell Parametric line segment

Or line, or ray, or just linear interpolation

p = p0 + t(p1 − p0 ) = (1− t)p0 + tp1 ! $ ! $ ! $ ! x $ x0 x1 − x0 (1− t)x0 + tx1 # & # & # & # & # y & = # y0 &+ t# y1 − y0 & = # (1− t)y0 + ty1 & # & # & # & # z & z z z (1 t)z tz " % "# 0 %& "# 1 − 0 %& "# − 0 + 1 %&

Line segment 0 ≤ t ≤1 Ray 0 ≤ t ≤ ∞ Line −∞ ≤ t ≤ ∞

University of Texas at Austin CS354 - Computer Graphics Don Fussell VectorDot dot product product

u⋅ v = rx + sy + tz = u v cos(φ) • Formula: • Alternately: University of Texas at Austin CS354 - Computer Graphics Don Fussell • Where φ is the angle between the vectors ProjectionProjection / rejection

u⋅ v Projection (u component parallel to v) w = v v ⋅ v • Projection: Rejection (u component orthogonal to v) u − w • W isParticularly the part useful of U when that vectors lies areon normalized V

University of Texas at Austin CS354 - Computer Graphics Don Fussell • Rejection: Just U - W Cross product

• Formula: Cross product • Creates a vector that is: i j k # sz − ty & • Perpendicular to the inputs% ( w = u × v = r s t = % tx − rz ( x y z % ry − sx ( • Length Cross$ product' intuition w is orthogonal to u and v • Right-hand w oriented= u v sin(φ) w area of parallelogram use right-hand rule u × v = −(v × u) (u × v)× w ≠ u ×(v × w) University of Texas at Austin CS354 - Computer Graphics Don Fussell • If U & V point along the same line, W = 0 • Useful for constructing local coordinate frames • Length of cross product is area of parallelogram spanned by U and V (divide by 2 for area of the triangle) Determinants

a b = ad − bc c d a b c e f d f d e d e f = a − b + c = aei − afh + bfg − bdi + cdh − ceg h i g i g h g h i

det(MT) = det(M) det(AB) = det(A)det(B) if det(M) = 0, M is singular, has no inverse

University of Texas at Austin CS354 - Computer Graphics Don Fussell Plane equation

Given normal vector N orthogonal to the plane and any point p in the plane N⋅ p + d = 0 ! x # ! #% & " a b c $% y Triangle&+ d = ax + by + cz + d =normals0 % & " z $ For a triangle

N = norm((v1 − v0 )×(v2 − v0 )) Order matters, usually CCW

University of Texas• atEvery Austin CS354 triangle - Computer lies Graphics in a Don plane Fussell • 2 choices of normal, pick one by convention • CCW winding is usually used • Formula: Homogeneous Coordinates

To represent transformations among affine frames, we can loft the problem up into 3-space, adding a third component to every point: p! = Mp " % a b tx " x % $ '$ '

= $ c d ty '$ y ' $ '$ 1 ' #$ 0 0 1 &'# & " % x " %$ ' = # u w t &$ y ' $ 1 ' # & = x ⋅ u + y⋅ w +1⋅ t Note that [a c 0]T and [b d 0]T represent vectors and T T T [tx ty 1] , [x y 1] and [x' y' 1] represent points.

University of Texas at Austin CS354 - Computer Graphics Don Fussell Homogeneous coordinates

This allows us to perform translation as well as the linear transformations as a matrix operation:

p" = MTp

# x "& #1 0 tx& # x& % ( % ( % ( y " 0 1 t y % ( = % y( % ( $% 1'( $%0 0 1'( $%1 '(

x " = x + tx

y " = y + ty y y

"1 0 1 % $ ' 0 1 1 2 11€ $ ' #$0 0 1 &'

x x 1 1 University of Texas at Austin CS354 - Computer Graphics Don Fussell € Rotation about arbitrary points

Until now, we have only considered rotation about the origin.

With homogeneous coordinates, you can specify a rotation, Rq, T about any point q = [qx qy 1] with a matrix:

y y y y

θ q x x x x

1. Translate q to origin 2. Rotate 3. Translate back Line up the matrices for these step in right to left order and multiply.

Note: Transformation order is important!!

University of Texas at Austin CS354 - Computer Graphics Don Fussell Points and vectors

From now on, we can represent points as have an additional coordinate of w=1.

Vectors have an additional coordinate of w=0. Thus, a change of origin has no effect on vectors.

Q: What happens if we multiply a matrix by a vector?

These representations reflect some of the rules of affine operations on points and vectors: vector + vector → vector ⋅ vector → point −point →

point + vector → point + point →

One useful combination of affine operations is: p(t) = p0 + tv

Q: What €does this describe?

University of Texas at Austin €CS354 - Computer Graphics Don Fussell Barycentric coordinates

A set of points can be used to create an affine frame. Consider a triangle ABC and a point p: A

• p

C B € We can form a frame with an origin C and the vectors from C to the other vertices:

u = A − C v = B− C t = C

We can then write P in this coordinate frame p = αu + βv + t

€ € The coordinates (α, β, γ) are called the barycentric coordinates of p relative to A, B, and C.

University of Texas at Austin CS354 - Computer Graphics Don Fussell Computing barycentric coordinates

For the triangle example we can compute the barycentric coordinates of P: % Ax Bx Cx( %α ( %p x ( ' * ' * ' * A B C A B C p α + β + γ = ' y y y* 'β * = ' y * &' 1 1 1 )* &' γ )* &' 1 )* Cramer’s rule gives the solution:

px Bx Cx Ax px Cx Ax Bx px

€p y By Cy Ay py Cy Ay By py 1 1 1 1 1 1 1 1 1 α = β = γ = Ax Bx Cx Ax Bx Cx Ax Bx Cx

Ay By Cy Ay By Cy Ay By Cy 1 1 1 1 1 1 1 1 1 Computing the determinant of the denominator gives:

BxCy − ByCx + AyCx − AxCy + AxBy − AyBx € University of Texas at Austin CS354 - Computer Graphics Don Fussell

€ Barycentric coords from area ratios

Now, let’s rearrange the equation from two slides ago:

BxCy − ByCx + AyCx − AxCy + AxBy − AyBx

= (Bx − Ax )(Cy − Ay ) − (By − Ay )(Cx − Ax )

The determinant is then just the z-component of (B-A) × (C-A), which is two times the area of triangle ABC! € Thus, we find: SArea(pBC) SArea(ApC) SArea(ABp) α = β = γ = SArea(ABC) SArea(ABC) SArea(ABC)

Where SArea(RST) is the signed area of a triangle, which can be computed with cross-products. € University of Texas at Austin CS354 - Computer Graphics Don Fussell Affine and convex combinations

Note that we seem to have added points together, which we said was illegal, but as long as they have coefficients that sum to one, it’s ok.

We call this an affine combination. More generally p = α1p1 +…+ α npn n is a proper affine combination if: ∑αi =1 i=1 €

Note that if the αi ‘s are all positive, the result is more specifically called a .

Q: Why is it called a convex combination?

University of Texas at Austin CS354 - Computer Graphics Don Fussell Basic 3-D transformations: scaling

Some of the 3-D transformations are just like the 2-D ones. For example, scaling:

y y # x "& #s x 0 0 0& # x& % ( % ( % ( y " 0 s 0 0 y % ( = % y ( % ( % z "( % 0 0 sz 0( % z( x x % ( % ( % ( $ 1' $ 0 0 0 1' $1 '

z z €

University of Texas at Austin CS354 - Computer Graphics Don Fussell Translation in 3D

# x "& #1 0 0 tx& # x& % ( % ( % ( y " 0 1 0 t y % ( = % y( % ( % z "( %0 0 1 tz( % z( % ( % ( % ( $ 1' $0 0 0 1' $1 '

y y €

x x

z z

University of Texas at Austin CS354 - Computer Graphics Don Fussell Rotation in 3D

Rotation now has more possibilities in 3D: $1 0 0 0' & ) &0 cos(θ) −sin(θ) 0) y Rx (θ) = &0 sin(θ) cos(θ) 0) R & ) y %0 0 0 1( $ cos(θ) 0 sin(θ) 0' x & ) 0 1 0 0 Rz Rx R (θ) = & ) y &− sin(θ) 0 cos(θ) 0) z & ) % 0 0 0 1( Use right hand rule $cos( θ) −sin(θ) 0 0' & ) sin(θ) cos(θ) 0 0 R (θ) = & ) z & 0 0 1 0) & ) % 0 0 0 1(

University of Texas at Austin CS354 - Computer Graphics Don Fussell € Shearing in 3D

Shearing is also more complicated. Here is one example: # x "& #1 b 0 0& # x& % ( % ( % ( y " 0 1 0 0 y % ( = % ( % ( % z "( %0 0 1 0( % z( % ( % ( % ( $ 1' $0 0 0 1' $1 '

y y

€ x x

z z We call this a shear with respect to the x-z plane.

University of Texas at Austin CS354 - Computer Graphics Don Fussell Preservation of affine combinations

A transformation F is an affine transformation if it preserves affine combinations: F (α1p1 +…+ α npn ) = α1F(p1 ) +…+ α n F(pn ) n where the p are points, and: i ∑αi =1 i=1 Clearly,€ the matrix form of F has this property. One special example is a matrix that drops a dimension. For example: # x& # x "& #1 0 0 0& % ( % ( % ( y y " = 0 1 0 0 % ( % ( % ( % z( $% 1'( $%0 0 0 1'( % ( $1 ' This transformation, known as an orthographic projection, is an affine transformation. We’ll use this fact later… € University of Texas at Austin CS354 - Computer Graphics Don Fussell Properties of affine transformations

Here are some useful properties of affine transformations: Lines map to lines Parallel lines remain parallel Midpoints map to midpoints (in fact, ratios are always preserved)

r t p' pq s p "q " : q' s s ! ratio = = = q : r' qr t q" r " t p

University of Texas at Austin € CS354 - Computer Graphics Don Fussell Next Lecture

More about ray tracing, math, and transforms

University of Texas at Austin CS354 - Computer Graphics Don Fussell Thanks

Some material for these slides provided by Christian Miller

University of Texas at Austin CS354 - Computer Graphics Don Fussell