15.01.2015 Views

2D Matrix Transformations Lecture Slides

2D Matrix Transformations Lecture Slides

2D Matrix Transformations Lecture Slides

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>2D</strong> <strong>Matrix</strong> <strong>Transformations</strong><br />

Consider a drawing made of lines and<br />

polygons:<br />

CS-184: Computer Graphics<br />

Prof. James O’Brien<br />

Consider another one:<br />

Reuse of Shapes<br />

Some butterflies same as others but moved<br />

Others distorted, but mostly the same<br />

Painful to do each one from scratch<br />

Wastes memory<br />

Representing Lines and Polygons<br />

Representing Points<br />

Just a list of points<br />

May use indexed vertex lists<br />

Points are vectors in<br />

2<br />

R<br />

or<br />

3<br />

R<br />

Relative to some origin and some coordinate axes<br />

2<br />

<br />

p = [2,4]<br />

4<br />

Origin, 0<br />

1


Basic <strong>Transformations</strong><br />

Uniform/isotropic<br />

Non-uniform/anisotropic<br />

Rotations<br />

Cos(<br />

θ ) − Sin(<br />

θ ) <br />

p'<br />

= <br />

p<br />

Sin(<br />

θ ) Cos( θ ) <br />

Rotate<br />

Scale<br />

Rotate<br />

Translate<br />

Shear -- not really “basic”<br />

Rotations<br />

Rotations<br />

Rotations are counter-clockwise<br />

Right hand rule<br />

Could do the other way, but we don’t<br />

Preserve lengths/distance to origin<br />

Orthonormal matrices<br />

Det == 1, not –1<br />

Note: rotate by zero degrees gives identity matrix<br />

Note: rotate +/- 360 degrees stays the same<br />

Scales<br />

Scale<br />

Uniform/isotropic<br />

Non-uniform/anisotropic<br />

sx<br />

0 <br />

p'<br />

= p<br />

0 s<br />

y <br />

Scales<br />

If Sx and Sy are the same scale is uniform<br />

Scaling by Sx or Sy < 0 causes problems<br />

If both are –1, it looks like 180 degree rotation<br />

2


Shears<br />

Translation ( method 1)<br />

Shear<br />

1 H<br />

yx <br />

p'<br />

= p<br />

H<br />

xy<br />

1 <br />

Translate<br />

t<br />

p' = p + <br />

t<br />

x<br />

y<br />

<br />

<br />

<br />

True, but not the right way to think about shears….<br />

Does not look like the others…..<br />

Arbitrary matrix transformation<br />

For everything but translation we have:<br />

p ' = Ap<br />

In a short while, we will make translation fit in too.<br />

What does an arbitrary A matrix mean<br />

Singular Value Decomposition<br />

(or polar decomposition)<br />

For any matrix A, we can write SVD:<br />

<br />

A = QSR<br />

where Q and R are orthonormal and S is diagonal<br />

Also write Polar Decomposition:<br />

<br />

A = QRSR<br />

(Different Q! Still orthonormal.)<br />

Decomposing matrices<br />

We can force Q and R to have Det=1 so they are<br />

rotations.<br />

Any matrix is now:<br />

– Rotation:Rotation:Scale:Rotation<br />

– That means shear is not primitive (in some sense)<br />

Multiple transformations<br />

What does something like the following mean<br />

p ' = BAp<br />

“Apply A to p and then apply B to the result.”<br />

p ' = B(<br />

Ap)<br />

= ( BA)<br />

p = Cp<br />

Lots of transformations all get composted into one.<br />

But translations are being left out…<br />

3


Translations again<br />

We could do this:<br />

p ' = B(<br />

Ap + t)<br />

= BAp + Bt = Cp + u<br />

Ugly and awkward<br />

Some systems do it anyhow.<br />

Every transformation would be a matrix and a<br />

vector…<br />

Homogeneous coordinates<br />

Take a vector that represents a point and<br />

append a 1 at the end<br />

p<br />

p = <br />

p<br />

x<br />

y<br />

<br />

<br />

<br />

p<br />

p~<br />

=<br />

<br />

<br />

p<br />

<br />

1<br />

x<br />

y<br />

<br />

<br />

<br />

<br />

Homogeneous translation<br />

1<br />

p<br />

~ ' =<br />

<br />

<br />

0<br />

<br />

0<br />

0<br />

1<br />

0<br />

p<br />

~ ~<br />

' = Ap<br />

~<br />

tx<br />

<br />

p<br />

t<br />

<br />

y <br />

p<br />

1 <br />

<br />

1<br />

x<br />

y<br />

<br />

<br />

<br />

<br />

The tildes are for clarity. If only using homogeneous coordinates,<br />

we would omit them.<br />

Other transformations<br />

<br />

~ A<br />

A =<br />

<br />

0<br />

0<br />

Now everything looks the same…<br />

0<br />

0<br />

<br />

<br />

1<br />

Composing matrices<br />

Example<br />

When we rotate, we rotate about the origin<br />

Same for scale<br />

How could we do it about another point<br />

versus<br />

4


Example<br />

Example<br />

Step 1: Translate point you want to<br />

rotate about to the origin<br />

Step 2: Rotate as desired<br />

Translate (-C)<br />

Rotate (θ)<br />

Example<br />

Step 3: Put back where it was<br />

Scaling about an arbitrary axis<br />

•Diagonal matrices are only axis-aligned scales<br />

Translate (C)<br />

p<br />

~<br />

' = ( −T)<br />

RTp<br />

~<br />

= Ap<br />

~<br />

Scaling about an arbitrary axis<br />

1. Translate so that axis passes through origin<br />

Scaling about an arbitrary axis<br />

2. Rotate axis to align with X or Y axis<br />

5


Scaling about an arbitrary axis<br />

3. Scale<br />

Scaling about an arbitrary axis<br />

4. Un-do rotation<br />

5. Un-do translation<br />

Trivia: scale matrices (w/o translation) are symmetric<br />

R SR<br />

Scene Hierarchy<br />

Point Vectors and Direction Vectors<br />

Linearity<br />

Ray equations<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!