Top Banner
2D Transformation
46

2D Transformation

Feb 22, 2016

Download

Documents

aldona

2D Transformation. 2D Transformation. Transformation changes an object’s: Position(Translation) Size(Scaling) Orientation(Rotation) Shape(Deformation) Transformation is done by a sequence of matrix operations applied on vertices. Vector Representation. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2D Transformation

2D Transformation

Page 2: 2D Transformation

• Transformation changes an object’s:– Position (Translation)– Size (Scaling)– Orientation (Rotation)– Shape (Deformation)

• Transformation is done by a sequence of matrix operations applied on vertices.

2D Transformation

Page 3: 2D Transformation

Vector Representation

• If we define a 2D vector as:

• A transformation in general can be defined as:

xy

⎛⎝⎜

⎞⎠⎟

x ' =ax+by+ cy'=dx + ey+ f x '

y '1

⎝⎜⎜

⎠⎟⎟ =

a b cd e f0 0 1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

x 'y '

⎛⎝⎜

⎞⎠⎟ = a b

d e⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟ +

cf

⎛⎝⎜

⎞⎠⎟

Page 4: 2D Transformation

Transformation: Translation

• Given a position (x, y) and an offset vector (tx, ty):

x ' =x+ txy'=y+ ty

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

1 tx1 ty

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

x 'y '

⎛⎝⎜

⎞⎠⎟ = 1

1⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟ +

txty

⎛⎝⎜

⎞⎠⎟

Page 5: 2D Transformation

Transformation: Translation

• To translate the whole shape:

Page 6: 2D Transformation

Transformation: Rotation

• Default rotation center: origin

Page 7: 2D Transformation

Transformation: Rotation• If We rotate around the origin,• How to rotate a vector (x, y) by an angle of θ ?

• If we assume:x =rcosφy=rsinφ

x ' =rcos(θ +φ)y'=rsin(θ +φ)

Page 8: 2D Transformation

Transformation: Rotationx =rcosφy=rsinφ

x ' =rcos(θ +φ)y'=rsin(θ +φ)

Before Rotation After Rotation

x ' =rcosφcosθ −rsinφsinθ =xcosθ −ysinθy'=rcosφsinθ + rsinφcosθ =xsinθ + ycosθ

x 'y '

⎛⎝⎜

⎞⎠⎟ = cosθ −sinθ

sinθ cosθ⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

cosθ −sinθsinθ cosθ

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

Page 9: 2D Transformation

Transformation: Rotation

• To translate the whole shape:

θ

Page 10: 2D Transformation

Transformation: Scaling• Change the object size by a factor of s:

x ' =s⋅xy'=s⋅y

x 'y '

⎛⎝⎜

⎞⎠⎟ = s

s⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

ss

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

(2, 1)

(-2, -1)

s = 2

(4, 2)

(-4, -2)

Page 11: 2D Transformation

Transformation: Scaling• But when the object is not center at origin

(5, 3)

(1, 1)

s = 2

(10, 6)

(2, 2)(3, 2)

(6, 4)

Page 12: 2D Transformation

Transformation: Scaling

x ' =s⋅xy'=s⋅y

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

ss

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟or

x ' =sx⋅xy'=sy⋅y

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

sxsy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟or

• Isotropic (uniform) scaling

• Anistropic (non-uniform) scaling

Page 13: 2D Transformation

Summaryx 'y '

⎛⎝⎜

⎞⎠⎟ = 1

1⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟ +

txty

⎛⎝⎜

⎞⎠⎟

x 'y '

⎛⎝⎜

⎞⎠⎟ =

sxsy

⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

x 'y '

⎛⎝⎜

⎞⎠⎟ = cosθ −sinθ

sinθ cosθ⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

• Translation:

• Rotation

• Scaling

Page 14: 2D Transformation

Summary

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

ss

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

cosθ −sinθsinθ cosθ

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

1 ox1 oy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

• Translation:

• Rotation

• Scaling

We use the homogenous coordinate to make sure transformations have the same form.

Page 15: 2D Transformation

Why use 3-by-3 matrices?

• Transformations now become the consistent.• Represented by as matrix-vector

multiplication.

• We can stack transformations using matrix multiplications.

Page 16: 2D Transformation

Some math…

• Two vectors are orthogonal if:

u⋅v=u0v0 +u1v1 +u2v2 +L =0

u =

u0

u1u2

M

⎜⎜⎜⎜

⎟⎟⎟⎟, v=

v0v1v2M

⎜⎜⎜⎜

⎟⎟⎟⎟

uTv= u0 u1 u2 L( )

v0v1v2M

⎜⎜⎜⎜

⎟⎟⎟⎟=u⋅v

Page 17: 2D Transformation

A Quiz

u =123

⎝⎜⎜

⎠⎟⎟, v=

−101

⎝⎜⎜

⎠⎟⎟ u =

121

⎝⎜⎜

⎠⎟⎟, v=

−101

⎝⎜⎜

⎠⎟⎟

Page 18: 2D Transformation

Some math…

• A matrix is orthogonal if

and only if:

mi ⋅m φ=0, ∀i, φ(i ≠φ)

M =

| | |m 0 m 1 m 2 L

| | |

⎝⎜⎜⎜

⎠⎟⎟⎟

Page 19: 2D Transformation

Some math…

• A matrix M is orthogonal if and only if:

m i ⋅m φ=0, ∀i, φ

MTM=

− m 0T −

− m 1T −

− m 2T −

M

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

| | |m 0 m 1 m 2 L

| | |

⎝⎜⎜⎜

⎠⎟⎟⎟

Diagonal Matrix

=

? 0 00 ? 00 0 ?

O

⎜⎜⎜⎜

⎟⎟⎟⎟

=

m0Tm0 m0

Tm1 m0Tm2

m1Tm0 m1

Tm1 m1Tm2 L

m2Tm0 m1

Tm2 m2Tm2

M O

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

Page 20: 2D Transformation

Some math…

• A vector is normalized if:

v0v0 +v1v1 +v2v2 +L =1

v =

v0v1v2M

⎜⎜⎜⎜

⎟⎟⎟⎟

vTv= v0 v1 v2 L( )

v0v1v2M

⎜⎜⎜⎜

⎟⎟⎟⎟=v⋅v=1

Page 21: 2D Transformation

Some math…

• A matrix is orthonormal

if and only if:

mi ⋅m φ=0, ∀i, φ(i ≠φ)

M =

| | |m 0 m 1 m 2 L

| | |

⎝⎜⎜⎜

⎠⎟⎟⎟

mi ⋅m i =1,

Ortho:

Normalized:

Page 22: 2D Transformation

Some math…

• A matrix M is orthonormal if and only if:

MTM=

− m 0T −

− m 1T −

− m 2T −

M

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

| | |m 0 m 1 m 2 L

| | |

⎝⎜⎜⎜

⎠⎟⎟⎟

Identity Matrix

=

1 0 00 1 00 0 1

O

⎜⎜⎜⎜

⎟⎟⎟⎟

=

m0Tm0 m0

Tm1 m0Tm2

m1Tm0 m1

Tm1 m1Tm2 L

m2Tm0 m2

Tm2 m1Tm2

M O

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

Page 23: 2D Transformation

Examples

• A 2D rotational matrix is orthonormal:

x 'y '

⎛⎝⎜

⎞⎠⎟ = m 0 m 1( )

xy

⎛⎝⎜

⎞⎠⎟ = cosθ −sinθ

sinθ cosθ⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

m0 ⋅m 1 =−cosθsinθ +sinθ cosθ =0Ortho:

m0 ⋅m 0 =cosθ cosθ +sinθ sinθ =1Normalized:m1 ⋅m 1 =cosθ cosθ +sinθ sinθ =1

Page 24: 2D Transformation

Examples

• Is a 2D scaling matrix orthonormal? (if sx, sy is not 1)

x 'y '

⎛⎝⎜

⎞⎠⎟ = m 0 m 1( )

xy

⎛⎝⎜

⎞⎠⎟ =

sx 00 sy

⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

m0 ⋅m 1 =sx⋅0 + 0 ⋅sy=0Ortho:

m0 ⋅m 0 =sx⋅sxNormalized:m1 ⋅m 1 =sy⋅sy

Page 25: 2D Transformation

SummaryTransformation Exists? Ortho? Normalized?

Translation No NA NA

Rotation Yes Yes Yes

Scaling Yes Yes No

2D Transformation (2-by-2 matrix)

Transformation Exists? Ortho? Normalized?

Translation Yes No No

Rotation Yes Yes Yes

Scaling Yes Yes No

2D Transformation (3-by-3 matrix)

Page 26: 2D Transformation

Transformation: Shearing

• y is unchanged.• x is shifted according to y.

x ' =x+ h⋅yy'=y

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

1 h1

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟or

Page 27: 2D Transformation

Transformation: Shearing

x ' =xy'=g⋅x + y

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

1g 1

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟or

Page 28: 2D Transformation

Facts about shearing

• Any 2D rotation matrix can be defined as the multiplication of three shearing matrices.

• Shearing doesn’t change the object area.

• Shearing can be defined as:rotation->scaling->rotation

Page 29: 2D Transformation

In fact,• Without translation, any 2D transformation

matrix can be defined as:Rotation->Scaling->rotation

x 'y '

⎛⎝⎜

⎞⎠⎟ = a b

c d⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

x 'y '

⎛⎝⎜

⎞⎠⎟ =

cosφ −sinφsinφ cosφ

⎛⎝⎜

⎞⎠⎟ sx

sy⎛⎝⎜

⎞⎠⎟

cosθ −sinθsinθ cosθ

⎛⎝⎜

⎞⎠⎟

xy

⎛⎝⎜

⎞⎠⎟

Orthonormal Diagonal Orthonormal

Singular Value Decomposition (SVD)

Page 30: 2D Transformation

Another fact

• Same thing when using homogenous coordinates: x '

y '1

⎝⎜⎜

⎠⎟⎟ =

a bc d

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

cosφ −sinφsinφ cosφ

1

⎜⎜⎜

⎟⎟⎟

sxsy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

cosθ −sinθsinθ cosθ

1

⎝⎜⎜

⎠⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

Orthonormal Diagonal Orthonormal

Page 31: 2D Transformation

Conclusion

• Translation, rotation, and scaling are three basic transformations.

• The combination of these can represent any transformation in 2D.

• That’s why OpenGL only defines these three.

Page 32: 2D Transformation

Rotation Revisit

Rotate about the origin Rotate about any center?

Page 33: 2D Transformation

Arbitrary Rotation

=

Step 1:Translate (-ox, -oy)

Step2:Rotate (θ)

Step 1:Translate (ox, oy)

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

1 ox1 oy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

cosθ −sinθsinθ cosθ

1

⎝⎜⎜

⎠⎟⎟

1 −ox1 −oy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

(ox, oy)

Page 34: 2D Transformation

Scaling Revisit

(5, 3)

(1, 1)

(10, 6)

(2, 2)

(3, 2)

(6, 4)

(10, 6)

(3, 2)

Page 35: 2D Transformation

Arbitrary Scaling

=

Step 1:Translate (-ox, -oy)

Step2:Scale(sx, sy)

Step 1:Translate (ox, oy)

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

1 ox1 oy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

sxsy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

1 −ox1 −oy

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

(ox, oy)

(ox, oy) can be arbitrary too!

Page 36: 2D Transformation

Rigid Transformation

• A rigid transformation is:

• Rotation and translation are rigid transformation.

• Any combination of them is also rigid.

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

a b cd e f

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

Orthonormal Matrix

Page 37: 2D Transformation

Affine Transformation• An affine transformation is:

• Rotation, scaling, translation are affine transformation.

• Any combination of them (e.g., shearing) is also affine.

• Any affine transformation can be decomposed into:Translation->Rotation->Scaling->Rotation

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

a b cd e f

1

⎝⎜⎜⎜

⎠⎟⎟⎟

xy1

⎝⎜⎜

⎠⎟⎟

(First)(Last)

=1 c

1 f1

⎜⎜⎜

⎟⎟⎟

a bd e

1

⎝⎜⎜

⎠⎟⎟

xy1

⎜⎜

⎟⎟

Page 38: 2D Transformation

We can also compose matrix

Translation Scaling Rotation Translation

xy1

⎝⎜⎜

⎠⎟⎟

M1 M2 M3 M4vv’

x 'y '1

⎝⎜⎜

⎠⎟⎟ =

v’ = (M1 (M2( M3 (M4 v))))

v’ = M v

Page 39: 2D Transformation

Some Math• Matrix multiplications are associative:

• Matrix multiplications are not commutative:

• Some exceptions are:• Translation X Translation• Scaling X Scaling• Rotation X Rotation• Uniform scaling X Rotation

A × B×C = A×B( )×C =A× B×C( )

A × B =B×A

Page 40: 2D Transformation

For example• Rotation and translation are not commutative:

1 11

1

⎝⎜⎜

⎠⎟⎟

0.6 −0.80.8 0.6

1

⎝⎜⎜

⎠⎟⎟ =

0.6 −0.8 10.8 0.6

1

⎝⎜⎜

⎠⎟⎟

0.6 −0.80.8 0.6

1

⎝⎜⎜

⎠⎟⎟

1 11

1

⎝⎜⎜

⎠⎟⎟ =

0.6 −0.8 0.60.8 0.6 0.8

1

⎝⎜⎜

⎠⎟⎟

Orders are important!

Page 41: 2D Transformation

How OpenGL handles transformation

• All OpenGL transformations are in 3D.

• We can ignore z-axis to do 2D transformation.

Page 42: 2D Transformation

For example

• 3D Translation

• 2D Translation

glTranslatef(tx, ty, tz)

glTranslatef(tx, ty, 0)

Page 43: 2D Transformation

For example• 3D Rotation

• 2D Rotation

glTranslatef(angle, axis_x, axis_y, axis_z)

Z-axis (0, 0, 1) to toward you

glTranslatef(angle, 0, 0, 1)

Page 44: 2D Transformation

OpenGL uses two matrices:

Projection Matrix

ModelView Matrix

Each object’s local coordinate

Each object’s World coordinate

Screen coordinate

For example: gluOrtho2D(…)

Page 45: 2D Transformation

How OpenGL handles transformation

• So you need to let OpenGL know which matrix to transform:

• You can reset the matrix as:

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

Page 46: 2D Transformation

For Example

Xcode time!