Top Banner
2D Transformations 2D Transformations
46

2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Dec 30, 2015

Download

Documents

Cory Greene
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 Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

2D Transformations2D Transformations

Page 2: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Translation x’ = x + tx

y’ = y + ty

translation vector: T =

P = P’ =

P’ = P + T

x

y

(x,y)

(x’,y’)txty

xy

x’y’

Page 3: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Rotation

Rotation about the originx’ = r.cos(+)

y’ = r.sin(+)

Rotation matrix:

R =

P’ = R . P

x

y

(x,y)

(x’,y’)

cos -sin sin cos

r

Page 4: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Rotation

Rotation about an arbitrary point (xr, yr)

x’ = xr + (x-xr).cos - (y-yr).sin y’ = yr + (x-xr).sin - (y-yr).cos

x

y

(x,y)

(x’,y’)

(xr,yr)

Page 5: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Scaling

x’ = x . sx

y’ = y . sy

Scaling matrix : S =

P’ = S . P

x

y

sx 0 0 sy

Page 6: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Scaling

Fixed point scaling

x’ = x . sx + xf (1-sx)

y’ = y . sy + yf (1-sy)

x

y (xf,yf)

Page 7: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Homogeneous Coordinates

(x, y) (x, y, h)

h = 1

= 1/h

(x, y, h) = (x/h, y/h, 1)

(x, y, h)(0,0,1)

(x, y, h)

x

h

y

Page 8: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Homogeneous Coordinates

(x,y) (xh, yh, h)

xh= h.x

yh= h.y

(xh, yh, h) = (h.x, h.y, h)

Page 9: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Translation

P = P’ =

translation matrix: T(tx, ty) =

P’ = T(tx, ty) . P

1 0 tx0 1 ty0 0 1

xy1

x’y’1

Page 10: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Rotation

Rotation matrix: R =

P’ = R() . P

cos -sin 0sin cos 00 0 1

Page 11: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Scaling

Scaling matrix : S =

P’ = S(sx, sy) . P

sx 0 0 0 sy 00 0 1

Page 12: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Inverse Transformations

T-1 =

R-1 =

S-1 =

0 0 -tx 0 0 -ty0 0 1

1/sx 0 0 0 1/sy 00 0 1

cos sin 0-sin cos 00 0 1

Page 13: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Composite Transformations

P’ = M3 . M2 . M1 . P= M . P

M: composite matrix

T(t2x, t2y) . T(t1x, t1y) = T( t1x+t2x, t1y+t2y )

R(2) . R(1) = R( 1+2 )

S(s2x, s2y) . S(s1x, s1y) = S( s1x.s2x, s1y.s2y )

Page 14: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Pivot-point Rotation

x

y(xr, yr)

Page 15: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Pivot-point Rotation

x

y

1. Translate the object so that pivot-point is moved to the originT(-xr, -yr) . P

Page 16: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Pivot-point Rotation

x

y

1. Translate the object so that pivot-point is moved to the originT(-xr, -yr) . P

2. Rotate object about the originR() . T(-xr, -yr) . P

Page 17: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Pivot-point Rotation

1. Translate the object so that pivot-point is moved to the originT(-xr, -yr) . P

2. Rotate object about the originR() . T(-xr, -yr) . P

3. Translate the object so that pivot-point is returned to its original positionT(xr, yr) . R() . T(-xr, -yr) . P

= R(xr, yr, )

x

y

Page 18: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Fixed-point Scaling

x

y(xf, yf)

Page 19: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Fixed-point Scaling

x

y

1. Translate the object so that fixed-point is moved to the originT(-xf, -yf) . P

Page 20: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Fixed-point Scaling

x

y

1. Translate the object so that fixed-point is moved to the originT(-xf, -yf) . P

2. Scale the object with respect to the originS(sx, sy) . T(-xf, -yf) . P

Page 21: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Fixed-point Scaling

x

y

1. Translate the object so that fixed-point is moved to the originT(-xf, -yf) . P

2. Scale the object with respect to the originS(sx, sy) . T(-xf, -yf) . P

3. Use inverse translation in step (1)

T(xf, yf) . S(sx, sy) . T(-xf, -yf) . P = S(xf, yf, sx, sy)

Page 22: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Scaling Directions

x

y

1. Rotate2. Scale 3. Rotate back

R-1() . S(s1, s2) . R()

s2

s1

Page 23: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Rigid-Body Transformation

Page 24: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Reflection

x

y

1 0 0 0 -1 00 0 1

Page 25: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Reflection

x

y

-1 0 0 0 1 00 0 1

Page 26: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Reflection

x

y

-1 0 0 0 -1 00 0 1

Page 27: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Reflection

x

y

0 1 0 1 0 00 0 1

Page 28: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Shear

x

y

1 shx 0 0 1 00 0 1

x

y

Page 29: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Raster Methods

Translation

Block transfer of the refresh buffer area

Page 30: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Raster Methods

Rotation

900 counterclockwise: 1. reverse pixel values in each row2. interchange rows and columns

1800: 1. reverse the order of elements in each row2. reverse the order of rows

Page 31: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Raster Methods

Rotation

Other cases:

1. calculate the amount of overlap with the rotated pixel areas

2. compute the color by averaging the colors of the overlapped source pixels, weighted by their percentage of area overlap.

Page 32: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Raster Methods

Scaling

1. scale pixel areas in the original block using sx, sy

2. assign color of the destination pixel according to the overlap area.

Page 33: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Raster Methods

Reftection

1. reverse row and column values2. translate

Shear

Shift the position of array values along rows or columns

Page 34: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Transformations between 2D Coordinate Systems

x

y

y’

x’

y0

x0

x

y

y’

x’

M = R(-) . T(-x0, -y0)

Page 35: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Transformations between 2D Coordinate Systems

x

yy’

x’

v

V

v = (vx, vy)

u = (vy, -vx)

R = u ux uy 0

vx vy 00 0 1

Page 36: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL

glTranslate* (tx, ty, tz)• f (float)• d (double)

glRotate* (theta, vx, vy, vz)• theta: rotation angle in degrees• (vx, vy, vz) vector defines the orientation of the

rotation axis that passes through the coordinate origin

glScale* (sx, sy, sz)

Page 37: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL

glMatrixMode(GL_MODELVIEW);

glLoadIdentity ( );

glMultMatrixf(M2);

glMultMatrixf(M1);

/* M = M2 . M1 */

Page 38: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW)• sets up the matrix for transformations (4x4 modelview

matrix)

glLoadIdentity ( )• assigns identity matrix to the current matrix

glLoadMatrix*(16-element array)• assigns a 16-element array (in column major order) to the

current matrix

glMultMatrix*(16-element array)• postmultiplies a 16-element array (M’) with the current

matrix (M) : M ← M.M’

Page 39: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL

Matrix Stack

Top matrix on the stack is the “current matrix” Initially stack contains identity matrix Maximum stack depth is 32 glGetIntegerv (GL_MAX_MODELVIEW_STACK_DEPTH, stackSize)• returns the number of positions available in the modelview stack

glGetIntegerv (GL_MODELVIEW_STACK_DEPTH, numMats)• returns the number of matrices currently in the stack

glPushMatrix()• copies the current matrix at the top of the stack

glPopMatrix()• destroys the matrix at the top of the stack

Page 40: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(125.0, 125.0, 0.0);glRotatef(90.0, 0.0, 0.0, 1.0);glTranslatef(-125.0, -125.0, 0.0);Recti(50, 100, 200, 150);

/* T(125,125).R(90).T(-125,-125).P */

Page 41: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(125.0, 125.0, 0.0);glRotatef(90.0, 0.0, 0.0, 1.0);glTranslatef(-125.0, -125.0, 0.0);Recti(50, 100, 200, 150);

/* T(125,125).R(90).T(-125,-125).P */

Page 42: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(125.0, 125.0, 0.0);glRotatef(90.0, 0.0, 0.0, 1.0);glTranslatef(-125.0, -125.0, 0.0);Recti(50, 100, 200, 150);

/* T(125,125).R(90).T(-125,-125).P */

Page 43: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

Page 44: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

glLoadIdentity ( ); glRotatef(90.0, 0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

Page 45: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

glLoadIdentity ( ); glRotatef(90.0, 0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glLoadIdentity ( );glScalef(-0.5, 1.0, 1.0);Recti(50, 100, 200, 150);

Page 46: 2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glPushMatrix();glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

glPopMatrix();glPushMatrix();

glRotatef(90.0, 0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glPopMatrix();glScalef(-0.5, 1.0, 1.0);Recti(50, 100, 200, 150);