24
March 30, 2022 March 30, 2022 Week 5-2D Transformations Week 5-2D Transformations 1 2D Transformations 2D Transformations By: KanwarjeetSingh By: KanwarjeetSingh

25 May 201525 May 201525 May 2015Week 5-2D Transformations1 2D Transformations By: KanwarjeetSingh

Embed Size (px)

Citation preview

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 11

2D Transformations2D Transformations

By: KanwarjeetSinghBy: KanwarjeetSingh

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 22

Matrix mathMatrix math

Is there a difference between possible representations?Is there a difference between possible representations?

dfce

bfae

f

e

dc

ba

dfbecfaedc

bafe

dfcebfaedb

cafe

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 33

Pick a conventionPick a convention

We’ll use the column-vector representation for a point.We’ll use the column-vector representation for a point.

Which implies that we use Which implies that we use pre-multiplicationpre-multiplication of the of the transformation – it appears before the point to be transformation – it appears before the point to be transformed in the equation.transformed in the equation.

What if we needed to switch to the other convention (to use What if we needed to switch to the other convention (to use some library, for instance)? How could we do that?some library, for instance)? How could we do that?

DyCx

ByAx

y

x

DC

BA

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 44

TranslationTranslation

A translation moves all A translation moves all pointspoints in in an object along the same an object along the same straight-line path to new straight-line path to new positionspositions..

The path is represented by a The path is represented by a vector, called the vector, called the translationtranslation or or shift vectorshift vector..

We can write the components:We can write the components:

pp''xx = = ppx x + + ttxx

pp''yy = = ppy y + + ttyy

or in matrix form:or in matrix form:

PP' ' = = PP ++ TT

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 55

RotationRotation

A rotation repositions all A rotation repositions all points in an object along a points in an object along a circular path in the plane circular path in the plane centered at the pivot point.centered at the pivot point.

First, we’ll assume the pivot is First, we’ll assume the pivot is at the origin.at the origin.

We can write the components:We can write the components:

pp''x x = = ppx x cos cos – – ppy y sin sin

pp''y y = = ppx x sin sin + + ppy y cos cos

or in matrix form:or in matrix form:

PP' ' = = R R •• PP

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 66

More rotationMore rotation

Another convention, we’ll take Another convention, we’ll take to be to be counterclockwisecounterclockwise, as in , as in our example.our example.

R, the rotation matrix, looks like:R, the rotation matrix, looks like:

cossin

sincosR

cossin

sincos

cossin

sincos

yx

yx

y

x

90

)1,5(),(

yx

0115

1105

5

1

90cos190sin5

90sin190cos5

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 77

ScalingScaling

Scaling alters the size of an Scaling alters the size of an object. Scales are about the object. Scales are about the the origin.the origin.

Scale factors between 0 and 1 Scale factors between 0 and 1 shrink objects. Scale factors shrink objects. Scale factors greater than 1 enlarge objects.greater than 1 enlarge objects.

We can write the components:We can write the components:

pp''x x = = ssxx •• ppx x

pp''y y = = ssyy •• ppyy

or in matrix form:or in matrix form:PP' ' = = S S •• PP

The scale factors need not be the The scale factors need not be the same in each direction.same in each direction.

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 88

More scalingMore scaling

We write a scale matrix as:We write a scale matrix as:

Scaling also translates objects; away from the origin if the Scaling also translates objects; away from the origin if the scale factor is greater than 1, or towards the origin if the scale factor is greater than 1, or towards the origin if the scale factor is less than 1.scale factor is less than 1.

What does scaling by 1 do?What does scaling by 1 do?

What is that matrix called?What is that matrix called?

What does scaling by a negative value do?What does scaling by a negative value do?

y

x

s

sS

0

0

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 99

Combining transformationsCombining transformations

We have a general transformation of a point:We have a general transformation of a point:PP'' = = MM •• PP + + AA

When we scale or rotate, we set When we scale or rotate, we set MM, and , and AA is the additive is the additive identity.identity.

When we translate, we set When we translate, we set AA, and , and MM is the multiplicative is the multiplicative identity.identity.

To combine multiple transformations, we must explicitly To combine multiple transformations, we must explicitly compute each transformed point.compute each transformed point.

It’d be nicer if we could use the same matrix operation all the It’d be nicer if we could use the same matrix operation all the time. But we’d have to combine multiplication and addition time. But we’d have to combine multiplication and addition into a single operation.into a single operation.

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1010

A less than obvious solutionA less than obvious solution

Let’s move our problem into 3D.Let’s move our problem into 3D.

Let point Let point ((xx, , yy) ) in 2D be represented by in 2D be represented by point point ((xx, , yy, 1), 1) in the new space. in the new space.

Scaling our new point by any value Scaling our new point by any value aa puts us somewhere along a puts us somewhere along a particular line: particular line: ((aaxx, , aayy, , aa).).

We can always map back to the We can always map back to the original 2D point by dividing by original 2D point by dividing by the last coordinate.the last coordinate.

The fact that all the points along each The fact that all the points along each line can be mapped back to the line can be mapped back to the same point in 2D gives this same point in 2D gives this coordinate system its name – coordinate system its name – homogeneous coordinateshomogeneous coordinates..

x,yx,y

ww

(0,0,0)(0,0,0)

w = 1w = 1(x,y,1)(x,y,1)

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1111

So what?So what?

Well, now we can wedge some addition into our multiplicative Well, now we can wedge some addition into our multiplicative matrix.matrix.

Our point now has three coordinates. So our matrix is needs Our point now has three coordinates. So our matrix is needs to be 3x3.to be 3x3.

We want a matrix which gives us:We want a matrix which gives us:

y

x

tyy

txx

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1212

Now what?Now what?

1100

10

01

1

y

x

t

t

y

x

y

x

11001

110

101

yx

tyxy

tyxx

y

x

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1313

And?And?

Rotations:Rotations:

Scales:Scales:

1100

0cossin

0sincos

1

y

x

y

x

1100

00

00

1

y

x

s

s

y

x

y

x

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1414

What of it?What of it?

We can represent any of our three transformations as a single We can represent any of our three transformations as a single matrix.matrix.No special cases when transforming a point – matrix No special cases when transforming a point – matrix •• vector. vector.

Composite transformations – matrix Composite transformations – matrix •• matrix. matrix.

Composite transformations:Composite transformations:Rotate about an arbitrary point – translate, rotate, translateRotate about an arbitrary point – translate, rotate, translate

Scale about an arbitrary point – translate, scale, translateScale about an arbitrary point – translate, scale, translate

Change coordinate systems – translate, rotate, scaleChange coordinate systems – translate, rotate, scale

Does the order of operations matter?Does the order of operations matter?

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1515

Is matrix multiplication associative?Is matrix multiplication associative?

dhlcfldgjcejdhkcfkdgicei

bhlaflbgjaejbhkafkbgiaei

lk

ji

dhcfdgce

bhafbgae

lk

ji

hg

fe

dc

ba

dhldgjcflcejdhkdgicfkcei

bhlbgjaflaejbhkbgiafkaei

hlgjhkgi

flejfkei

dc

ba

lk

ji

hg

fe

dc

ba

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1616

Is matrix multiplication commutative?Is matrix multiplication commutative?

dhcfdgce

bhafbgae

hg

fe

dc

ba

hdgbhcga

fdebfcea

dc

ba

hg

fe

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1717

Order of operationsOrder of operations

So, it does matter. Let’s look at an example:So, it does matter. Let’s look at an example:

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1818

Useful compositionsUseful compositions

Rotate about a pivot point:Rotate about a pivot point:

T(pivot) T(pivot) • • R(R() ) • • T(–pivot) T(–pivot) • P• P

Scale about a fixed point:Scale about a fixed point:

T(fixed) T(fixed) • • S(scale) S(scale) • • T(–fixed) T(–fixed) • P• P

General scaling directions:General scaling directions:

R(–R(–) ) • • S(scale) S(scale) • • R(R() ) • P• P

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 1919

Other transformationsOther transformations

Reflection:Reflection:

x-axisx-axis y-axisy-axis

100

010

001

100

010

001

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 2020

Other transformationsOther transformations

Reflection:Reflection:

originorigin line x=y line x=y

100

010

001

100

001

010

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 2121

Other transformationsOther transformations

Shear:Shear:

x-directionx-direction y-direction y-direction

100

010

01 xsh

100

01

001

ysh

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 2222

Coordinate system transformationsCoordinate system transformations

We often need to transform points from one coordinate We often need to transform points from one coordinate system to another:system to another:1.1. We might model an object in non-Cartesian space (polar)We might model an object in non-Cartesian space (polar)

2.2. Objects may be described in their own local systemObjects may be described in their own local system

3.3. Other reasons: textures, display, etcOther reasons: textures, display, etc

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 2323

Matrix multiplicationMatrix multiplication

So we can do a lot with one basic operation.So we can do a lot with one basic operation.

We’d better make this operation as fast as possible.We’d better make this operation as fast as possible.

Let’s start with the form of the matrix:Let’s start with the form of the matrix:

Why haven’t we used the bottom row of the matrix?Why haven’t we used the bottom row of the matrix?

1001 fed

cba

M

April 18, 2023April 18, 2023 Week 5-2D TransformationsWeek 5-2D Transformations 2424

Matrix multiplicationMatrix multiplication

Since we don’t use the bottom row of the 2D transformation Since we don’t use the bottom row of the 2D transformation matrix, we could have a special transform composition matrix, we could have a special transform composition operation:operation:

1001 fed

cba

M

1002 lkj

ihg

M

10010010021 feldiekdhejdg

cblaibkahbjag

lkj

ihg

fed

cba

MM