48
CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

Embed Size (px)

Citation preview

Page 1: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CS 450: COMPUTER GRAPHICS

2D TRANSFORMATIONSSPRING 2015

DR. MICHAEL J. REALE

Page 2: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

INTRODUCTION

• Now that we have some linear algebra under our respective belts, we can start using it in graphics!

• So far, for each primitive, we have a description that includes the positions of the vertices model coordinates

• Examples:

• A square with vertices (-1,-1), (1,-1), (1,1), (-1,1)

• A teapot with its vertices relative to its bottom

• We would like to be able to place this primitive in the world wherever we want (as well as rotate it and/or scale it) use a model transform

• Recall: Model coordinates (relative to model) MODEL TRANSFORM World coordinates

• We would also like to be able to get the world coordinates relative to the camera use view transform

• Recall: World coordinates VIEW TRANSFORM Camera (Eye) Coordinates

Page 3: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

TRANSFORMATIONS

• Transformations or transforms = operations that change position, orientation, and/or size of geometric object

• Use matrices to perform transformations on vectors/points

• Examples: model transforms, the view transform, projection transform, etc.

• Geometric vs. Modeling transformation

• Sometimes considered the same

• Sometimes:

• Geometric = transforming the whole “object”

• Modeling = gives hierarchy for pieces of the object (e.g., arms relative to torso in human body model)

Page 4: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

OVERVIEW

• Three most basic transformations:

• Rotation

• Scaling (i.e., resizing the object)

• Translation (i.e., moving/placing the object)

• Other transformations: shear, reflection, etc.

• We’re going to start with 2D transformations first

• Then, later, we’ll move on to 3D transformations

• Keep in mind, we will be building simple transformations (with certain implicit assumptions)

• However, we will combine these to make more powerful transformations

Page 5: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ROTATION

Page 6: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ROTATION: INTRODUCTION

• To rotate an object, we need:

• Rotation angle how much to rotate

• Counterclockwise in plane we’re rotating

• Rotation axis what we’re rotating around

• In 2D, just use z axis

• WARNING: Rotation performed around ORIGIN

• Origin (0,0) = rotation point (or pivot point)

• (We’ll talk later about how to rotate around an arbitrary rotation point)

Page 7: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ROTATION: DERIVING

• P = (x,y) original point

• P’ = (x’,y’) transformed point

• ϕ = original angle of point (x,y) from x axis

• θ = difference in angle between old and new point

• So, our original point (x,y) and transformed point (x’,y’) in polar coordinates are as follows

• After substitution, we can express the transformed point in terms of θ only:

BABABA

BABABA

cossinsincos)sin(

sinsincoscos)cos(

cossin'

sincos'

yxy

yxx

cossinsincos)sin('

sinsincoscos)cos('

sin

cos

rrry

rrrx

ry

rx

Recall:

Page 8: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ROTATION MATRIX• We have:

• If our rotation matrix transform is R, then:

• Therefore, our 2D rotation matrix is:

cossin'

sincos'

yxy

yxx

PRP '

cossin

sincosR

'

''

y

xP

y

xP

'

'

cossin

sincos

cossin

sincos'

y

x

yx

yx

y

xPRP

Page 9: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING

Page 10: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING: INTRODUCTION

• Scaling an object = altering the size of an object

• The scaling we will be doing here simply multiplying each coordinate by a scaling factor:

• The corresponding scaling matrix transformation

y

x

syy

sxx

'

'

y

x

s

s

y

x

y

x

0

0

'

'

Page 11: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING: FACTORS

• Scaling factor > 1.0 enlarge

• Scaling factor < 1.0 shrink

• Scaling factor < 0 negative scaling resizes AND reflects object

• Uniform scaling = scaling factors are all the same (e.g., sx = sy)

• Otherwise, called differential scaling

Page 12: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING: ASSUMPTIONS

• WARNING: Because of the way we are doing scaling:• Only scaling in X or Y direction (or both), but NOT in arbitrary direction!

• Scaling relative to ORIGIN!

• ORIGIN = fixed point (point unaffected by scaling)

• (We’ll talk later about how to use a different fixed point)

Page 13: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

TRANSLATION

Page 14: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

TRANSLATION: INTRODUCTION

• Translation = moving a point by a certain distance (tx , ty)

• (tx , ty) = translation distances = translation vector = shift vector

• If we’re stuck with 2x2 matrices and 2x1 vectors, we have to add vectors to perform a translation:

y

x

tyy

txx

'

'

'

''

y

x

ty

tx

t

t

y

xTPP

y

x

y

x

Page 15: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

TRANSLATION: PROBLEM

• At some point, we would like to be able to combine multiple transformations into a single matrix:

• This means we can multiply all our transformations together first (M), and then apply it to each point we want

• HOWEVER, because translation is handled as addition, we need to compute intermediate steps:

PM

PABCDEF

PABCDEFP

)(

'

TPRP )('

Page 16: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

HOMOGENEOUS COORDINATES

Page 17: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

HOMOGENEOUS COORDINATES

• To fix this, we will extend our 2x2 matrices (and our 2x1 vectors) to 3x3 matrices (and to 3x1 vectors)

• Homogeneous coordinates = for 2D coordinates, extension to (xh , yh , h)

• h = homogeneous parameter nonzero value such that:

• Often just set h = 1 (x, y) becomes (x, y, 1)

• Often use “w” instead of “h” (especially for 3D vectors (x,y,z,w) )

• As we’ll see, this allows us to represent translation as a matrix multiplication!

h

yy

h

xx hh

Page 18: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

TRANSLATION MATRIX WITH HOMOGENEOUS COORDINATES

• The 2D translation matrix is sometimes represented as T(tx , ty )

11100

10

01

11

'

'

y

x

y

x

ty

tx

y

x

t

t

y

x

Ty

x

Page 19: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ROTATION MATRIX WITH HOMOGENEOUS COORDINATES

• The 2D rotation matrix is sometimes represented as R(θ)

1100

0cossin

0sincos

1

'

'

y

x

y

x

Page 20: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING MATRIX WITH HOMOGENEOUS COORDINATES

• The 2D scaling matrix is sometimes represented as S(sx , sy)

1100

00

00

1

'

'

y

x

s

s

y

x

y

x

Page 21: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

PATTERN WITH HOMOGENEOUS COORDINATE MATRICES• With the translation matrix, we purposely use the additional elements of the matrix (in this case, the extra

column):

• For rotation, scaling, and shear matrices (discussed later), the original matrix is augmented with an extra row and column of zeros (except for the last (row,column) position, which is set to 1):

100

10

01

y

x

t

t

T

1000

0

0

0

100

0

0

222120

121110

020100

1110

0100

mmm

mmm

mmm

Mmm

mm

M

Page 22: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

HOMOGENEOUS COORDINATES: POINTS VS. VECTORS

• Recall: a vector can also be interpreted as:

• Location (w = 1)

• Direction (w = 0)

• …in space

• Note: sometimes, location called “point” and direction called “vector”

• Depending on how we want to interpret the vector, we will set a different value for w

Page 23: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

HOMOGENEOUS COORDINATES: POINTS VS. VECTORS

• Points all transformations should have an effect (translation, rotation, scaling, etc.)

• w set to 1

• Direction translation has no meaning (other transformation should work though)

• w set to 0

11100

10

01

11

'

'

y

x

y

x

ty

tx

y

x

t

t

y

x

Ty

x

00100

10

01

00

'

'

y

x

y

x

t

t

y

x

Ty

x

y

x

Page 24: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

INVERSE TRANSFORMATIONS

Page 25: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

INVERSE TRANSFORMATIONS

• Fortunately, the inverses of the translation, rotation, and scaling matrices can be computed directly:

• Applying an inverse transformation does the opposite transformation

• T-1 translate object (-tx , -ty )

100

01

0

001

100

0cossin

0sincos

100

10

01111

y

x

y

x

s

s

SRt

t

T

Page 26: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

QUICK ASIDE: INVERSE OF ROTATION MATRIX

• We computed the inverse directly by using the negative angle (-θ) only sine was affected by this

• It turns out, any rotation matrix is ORTHOGONAL inverse = transpose = swapping rows and columns

• Also note: |R| = cos2 θ + sin2 θ = 1

100

0cossin

0sincos

100

0cossin

0sincos1

RRR T

Page 27: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CHANGING COORDINATE SYSTEMS

Page 28: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CHANGING 2D COORDINATE SYSTEMS

• Given a point P, many times we want to get the coordinates of this point relative to some OTHER coordinate basis vectors and origin point

• Example: viewing transform getting coordinates with respect to camera’s view (camera’s basis vectors) and camera’s starting point (camera’s origin)

• As with all our slides thus far, we’ll stick to 2D for now.

• For a 2D coordinate system, we need:

• An origin point (x0, y0)

• Two axes x’ and y’

Page 29: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CHANGING 2D COORDINATE SYSTEMS

• To change 2D coordinate systems:

• Translate so that the new origin is at (0,0) T(-x0, -y0)

• Rotate the x’ axis onto the x axis need to get angle θ between x’ and x R(-θ)

• Final transformation: R(-θ) T(-x0, -y0)

Page 30: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

COMPUTING NEW COORDINATE AXES

• If we only know one vector V (and we want y’ to point in the same direction):

• Alternatively, we might be given P0 (origin) and P1 (endpoint of y’ axis):

),('

'

xy

y

x

vvx

v

v

V

Vvy

01

01'PP

PPvy

Page 31: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CHANGING 2D COORDINATE AXES ONLY

• If our new coordinate axes are u = (ux, uy) and v = (vx, vy), then we can construct the ROTATION matrix directly:

• When multiplying by a point P, P will be projected on each axis (P dotted with each axis):

100

0

0

yx

yx

vv

uu

R

111100

0

0

' vP

uP

vpvp

upup

p

p

vv

uu

PRP yyxx

yyxx

y

x

yx

yx

Page 32: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

CHANGING 2D COORDINATE AXES ONLY

• To drive the point home:

100

0

0

100

0cossin

0sincos

)(

cos90cossin90sincos)90sin(

sin90sinsin90coscos)90cos(

sin

cos

yx

yx

y

x

y

x

vv

uu

R

v

v

u

u

BABABA

BABABA

cossinsincos)sin(

sinsincoscos)cos(

Recall:

Page 33: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

“UNDOING” A 2D COORDINATE AXIS CHANGE• To go back to the standard basis, we need to get the original coordinates back

• To get the x coordinate P’x * ux + P’y * vx P’x * x’

• To get the y coordinate P’x * uy + P’y * vy P’y * y’

• The matrix form becomes:

1

''

''

1

'

'

100

0

0

'1yyyy

xyxx

y

x

yy

xx

vpup

vpup

p

p

vu

vu

PRP

Page 34: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

“UNDOING” A 2D COORDINATE AXIS CHANGE

• Note: If coordinate axes are orthonormal matrix R is orthogonal R-1 = RT

100

0

0

100

0

01

yy

xxT

yx

yx

vu

vu

RRvv

uu

R

Page 35: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

COMPOSITE TRANSFORMATIONS

Page 36: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

COMPOSITE TRANSFORMATIONS

• Composite transformation matrix = product of individual transformations

• Process of forming one is called concatenation or composition of matrices

• Example: apply M1 then M2 M = M2M1

• NOTE THE ORDER! Must multiply from RIGHT to LEFT!!!

PM

PMMP

12'

Page 37: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

WHY USE COMPOSITE TRANSFORMATIONS?

• There are a LOT of different things you can do with these; we’ll talk about a few very common examples:

• Rotation around an arbitrary pivot point

• Scaling around an arbitrary fixed point

• Scaling in an arbitrary direction

Page 38: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ROTATION AROUND AN ARBITRARY PIVOT POINT• Let’s say we want to rotate around an

pivot point (px , py)

• Basic idea:

• Translate (-px , -py) (px , py) is now at origin

• Rotate points

• Translate back to (px , py)

100

10

01

100

0cossin

0sincos

100

10

01

),()(),( y

x

y

x

yxyx p

p

p

p

ppTRppT

Page 39: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING AROUND AN ARBITRARY FIXED POINT

• Say we want to scale an object relative a fixed point (px , py)

• Basic idea:

• Translate (-px , -py) (px , py) is now at origin

• Scale points

• Translate back to (px , py)

100

10

01

100

00

00

100

10

01

),(),(),(

y

x

y

x

y

x

yxyxyx

p

p

s

s

p

p

ppTssSppT

Page 40: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SCALING IN AN ARBITRARY DIRECTION

• Let’s assume our fixed point is already at the origin

• Basic idea:

• Rotate our arbitrary axis u into the x axis

• Scale along x

• Rotate back

• If u is our x’ axis, then our y’ axis = v = (-uy , ux):

100

0

0

100

00

00

100

0

0

),(1yx

yx

y

x

yy

xx

yx vv

uu

s

s

vu

vu

RssSR

Page 41: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

ORDER MATTERS!

• What order you apply your matrices will affect what transformations you perform!!!

• Example: rotation then scale vs. scale then rotation

• First transformation RIGHT-most matrix when multiplying!

Page 42: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

RIGID-BODY TRANSFORMATIONS

Page 43: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

RIGID-BODY TRANSFORMATIONS

• Rigid-body transformation = only includes translation and/or rotation

• All angles and distances between coordinate positions preserved

• Intuitively, object doesn’t change shape only changes position and/or orientation

• Also called a rigid-motion transformation

• In 2D, has the general form:

• WARNING: (trx , try) != (tx , ty ) from earlier! (Depends on order of transformations)

• Upper-left 2x2 matrix orthonormal vector set

100yyyyx

xxyxx

trrr

trrr

Page 44: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

OTHER TRANSFORMATIONS (REFLECTION AND SHEAR)

Page 45: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

REFLECTION TRANSFORMATION

• Reflection transformation = transformation that produces a mirror image of object

• In 2D 180° rotation about axis of reflection axis

• Reflection about line y = 0 (x axis):

• Reflection about line x = 0 (y axis):

• Reflection about origin:

100

010

001

100

010

001

100

010

001

Page 46: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

REFLECTION ACROSS DIAGONALS

• Reflection across y = x:

• Reflection across y = -x:

100

001

010

100

001

010

Page 47: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SHEAR TRANSFORMATION

• Shear transformation = distorts shape of object like as if the object is composed of layers sliding over each other

• Shear in x-direction:

• Shear in y-direction:

11100

010

01

' y

yxx

y

xx

x p

php

p

ph

PHP

11100

01

001

' xyy

x

y

x

yy php

p

p

p

hPHP

Page 48: CS 450: COMPUTER GRAPHICS 2D TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE

SHEAR TRANSFORMATION

• WARNING: By default, assumes reference line is either the x axis or the y axis (for the x-shear and y-shear, respectively)