46
Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Embed Size (px)

Citation preview

Page 1: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Geometric Transformations

CENG 477 Introduction to Computer GraphicsFall 2015-2016

Computer EngineeringMETU

Page 2: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

2D Geometric Transformations

Page 3: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Basic Geometric Transformations

● Geometric transformations are used to transform the objects and the camera in a scene (for animation or modelling) and are also used to transform World Coordinates to View Coordinates

● Given the shape, transform all the points of the shape? Transform the points and/or vectors describing it.

● For example:Polygon: corner pointsCircle, Ellipse: center point(s), point at angle 0

● Some transformations preserves some of the attributes like sizes, angles, ratios of the shape.

Page 4: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Translation

● Simply move the object to a relative position.

TPP

PTP

+=

y'

x'=

t

t=

y

x=

t+y=y't+x=x'

y

x

yx

'

'

T

P

P'

Page 5: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation● A rotation is defined by a rotation axis and a

rotation angle. ● For 2D rotation, the parameters are rotation

angle (θ) and the rotation point (xr,yr).

● We reposition the object in a circular path arround the rotation point (pivot point)

θ

xr

yr

Page 6: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation

● When (xr,yr)=(0,0) we have

r

P

P'

cossinsincos)sin(

sinsincoscos)cos(

rrry

rrrx

sin

cos

ry

rx

The original coordinates are:

cossin

sincos

yxy

yxx

Substituting them in the

first equation we get:

In the matrix form we have:

where

PRP

cossin

sincosR

Page 7: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation

● Rotation around an arbitrary point (xr,yr)

● This equations can be written as matrix operations (we will see when we discuss homogeneous coordinates).

r

P

P'

cos)(sin)(

sin)(cos)(

rrr

rrr

yyxxyy

yyxxxx

(xr,yr)

Page 8: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Scaling

● Change the size of an object. Input: scaling factors (sx,sy)

PSP

S

'

=

s

s=

ys=y'xs=x'

y

x

yx

0

0

PP'

non-uniform vs.uniform scaling

Page 9: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Homogenous Coordinates● All transformations can be represented by matrix

operations.

● Translation is additive, rotation and scaling is multiplicative (+ additive if you rotate around an arbitrary point or scale around a fixed point); making the operations complicated.

● Adding another dimension to transformations make translation also representable by multiplication. Cartesian coordinates vs homogenous coordinates.

h

yh

xh

=

h

y

x

=Ph

y=y

h

x=x h

h

hh

Page 10: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

● Many points in homogenous coordinates can represent the same point in Cartesian coordinates.

● In homogenous coordinates, all transformations can be written as matrix multiplications.

Page 11: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Transformations in Homogenous C.

● Translation

● Rotation

● Scaling

Pt,tT=P

t

t

=t,tT

yx'

y

x

yx

100

10

01

PθR=P

θθ

θθ

=θR

'

100

0cossin

0sincos

Ps,sS=P

s

s

=s,sS

yx

y

x

yx

100

00

00

Page 12: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Composite Transformations

● Application of a sequence of transformations to a point:

PM

PMMP 12

Page 13: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Composite Transformations

● First: composition of similar type transformations

● If we apply to successive translations to a point:

yyxxyy

xx

y

x

y

x

yxyx t+t,t+tT=tt

tt

=t

t

t

t

=t,tTt,tT 212121

21

1

1

2

2

1122

100

10

01

100

10

01

100

10

01

PTT

PTTP

)},(),({

}),({),(

1122

1122

yxyx

yxyx

tttt

tttt

Page 14: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

yyxxyy

xx

y

x

y

x

yxyx ss,ss=ss

ss

=s

s

s

s

=s,ss,s 212121

21

1

1

2

2

1122

100

00

00

100

00

00

100

00

00

SSS

Composite Transformations

φ+θ=φ+θφ+θ

φ+θφ+θ

=φθ+φθφθ+φθ

φθφθφθφθ

=θθ

θθ

=φθ

R

RR

100

0)cos()sin(

0)sin()cos(

100

0coscossinsinsincoscossin

0cossinsincossinsincoscos

100

0cossin

0sincos

100

0cossin

0sincos

Page 15: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation around a pivot point– Translate the object so that the

pivot point moves to the origin

– Rotate around origin

– Translate the object so that the pivot point is back to its original position

100

sincos1cossin

sincos1sincos

100

10

01

100

0cossin

0sincos

100

10

01

θxθyθθ

θy+θxθθ

=y

x

θθ

θθ

y

x

=y,xθy,x

rr

rr

r

r

r

r

rrrr TRT

rr y,x T

R

rr y,xT

Page 16: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Scaling with respect to a fixed point

– Translate to origin

– Scale

– Translate back

100

10

10

100

10

01

100

00

00

100

10

01

yfy

xfx

f

f

y

x

f

f

ffyxff

sys

sxs

=y

x

s

s

y

x

=y,xs,sy,x TST

ff y,x T

yx s,sS

ff y,xT

Page 17: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Order of matrix compositions

● Matrix composition is not commutative. So, be careful when applying a sequence of transformations.

pivot same pivot

Page 18: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Other Transformations

● Reflection

100

010

001

100

010

001

100

010

001

Page 19: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

● Shear: Deform the shape like shifted slices.

(1,1)

(3,1)

(2,1)

x ' x shx y y ' y

1 shx 0

0 1 00 0 1

(0,1)

Page 20: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Transformations Between the Coordinate Systems

● Between different systems: Polar coordinates to cartesian coordinates

● Between two cartesian coordinate systems. For example, relative coordinates or window to viewport transformation.

Page 21: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

● How to transform from x,yto x',y' ?

● Superimpose x',y' to x,y

● Transformation:

– Translate so that (x0,y0)

moves to (0,0) of x,y

– Rotate x' axis onto x axis

xx

y

y'

x'

x0

y0

00, yxTθR

xx

y

y'x'

Page 22: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

● Alternate method for rotation:Specify a vector V for positivey' axis:

xx

y

y'

x'

V

yx v,v==

y'

V

Vv

:direction in ther unit vecto

yxxy u,u=v,v=

x'

u

v 90 clockwise rotate direction, in ther unit vecto

Page 23: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

● Elements of any rotation matrix can be expressed as elements of a set of orthogonal unit vectors:

100

0

0

100

0

0

yx

xy

yx

yx

vv

vv

=vv

uu

=R

xx

yy'

x'

P

0P

0

0

PP

PPv

=

Page 24: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

● Example:

xx'

yy'

P

0P

111

1.220.6

1.610.8

111

121

443

100

20.80.6

10.60.8

100

110

201

100

00.80.6

00.60.8

12,

0.60.8,

0.6,0.82.5

2

2.5

1.5

21.5

1.5,2

3.5,32,1

220

0

=T

=

=,=

=,=+

==

==

M

TvuRM

=u

PP

PPv

PP

0

TM T

Let triangle T be defined asthree column vectors:

Page 25: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Affine Transformations

● Coordinate transformations of the form:

● Translation, rotation, scaling, reflection, shear. Any affine transformation can be expressed as the combination of these.

● Rotation, translation, reflection: preserve angles, lengths, parallel lines

yyyyx

xxyxx

b+ya+xa=y'

b+ya+xa=x'

Page 26: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

3 DIMENSIONALTRANSFORMATIONS

Page 27: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

3D Transformations

● x,y,z coordinates. Usual notation:Right handed coordinate system

● Analogous to 2D we have 4 dimensions in homogenous coordinates.

● Basic transformations:

– Translation

– Rotation

– Scaling

x

y

z

z

x

y

y

z

x

Page 28: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Translation

● move the object to a relative position.

z

y

x

z

y

x

11000

100

010

001

1

z

y

x

t

t

t

z

y

x

z

y

x

PTP

P

P

Page 29: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation

● Rotation arround the coordinate axes

x axis y axis z axis

z

x

y

z

x

y

z

x

y

z

x

y

z

x

y

z

x

y

Counterclockwise when looking along the positive half towards origin

Page 30: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation around coordinate axes

● Arround x

● Arround y

● Arround z

1000

0cossin0

0sincos0

0001

)(

xR PRP )(x

1000

0100

00cossin

00sincos

)(

zR PRP )(z

1000

0cos0sin

0010

0sin0cos

)(

yR PRP )(y

Page 31: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Arround a Parallel Axis

● Rotating the object around a line parallel to one of the axes: Translate to axis, rotate, translate back.

z

y

x

z

y

x

z

y

x

z

y

x

Translate Rotate Translate back

PTRTP ),,0()(),,0( ppxpp zyzy

Page 32: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Figure from the textbook

Page 33: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Around an Arbitrary Axis

● Translate the object so that the rotation axis passes though the origin

● Rotate the object so that the rotation axis is aligned with one of the coordinate axes

● Make the specified rotation

● Reverse the axis rotation

● Translate back

z

y

x

Page 34: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Around an Arbitrary Axis

Page 35: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Around an Arbitrary Axis

),,( 12121212 zzyyxx PPV

),,( cbaV

Vuu is the unit vector along V:

First step: Translate P1 to origin:

1000

100

010

001

1

1

1

z

y

x

T

Next step: Align u with the z axiswe need two rotations: rotate around x axis to get u onto the xz plane, rotate around y axis to get u aligned with z axis.

Page 36: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Around an Arbitrary Axis

z

x

u

Align u with the z axis 1) rotate around x axis to get u into the xz plane, 2) rotate around y axis to get u aligned with the z axis

y

α

z

xu

y

βz

x

uu' αuz

y

Page 37: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Dot product and Cross Product

● v dot u = vx * ux + vy * uy + vz * uz. That equals also to |v|*|u|*cos(a) if a is the angle between v and u vectors. Dot product is zero if vectors are perpendicular.

v x u is a vector that is perpendicular to both vectors you multiply. Its length is |v|*|u|*sin(a), that is an area of parallelogram built on them. If v and u are parallel then the product is the null vector.

Page 38: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Around an Arbitrary Axis

z

x

uu' α

Align u with the z axis 1) rotate around x axis to get u into the xz plane, 2) rotate around y axis to get u aligned with the z axis

uz

We need cosine and sine of α for rotation

),,0( cbu

22 cos cbdd

c

z

z

uu

uu

bxzxz uuuuuu sin

sindb

d

b

d

c sin cos

1000

00

00

0001

)(

d

c

d

bd

b

d

c

x RProjection of u onyz plane

Page 39: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation Around an Arbitrary Axis

z

x

u

u''= (a,0,d)

Align u with the z axis 1) rotate around x axis to get u into the xz plane, 2) rotate around y axis to get u aligned with the z axis

duu

uu

z

z

cos

)(sin ayzyz uuuuuu

1000

00

0010

00

)(

sin cos

da

ad

ad

y

R

),,()()()()()(),,()( 111111 zyxzyx xyzyx TRRRRRTR

β

Page 40: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation, ... Alternative Method

Any rotation around origin can be represented by 3 orthogonal unit vectors:

1000

0

0

0

333231

232221

131211

rrr

rrr

rrr

R

Define a new coordinate system with the given rotation axis u using:

),,( zyx uuu

This matrix can be thought of asrotating the unit r1*, r2*, and r3* vectors

onto x, y, and z axes.

So, to align a given rotation axis u onto the z axis,we can define an (orthogonal) coordinate system and form this R matrix

zyx

x

xy

z

uuu

uu

uuu

uu

Page 41: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Rotation, ... Alternative Method

1000

0

00

0

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

)/,/,0(),,0()0,0,1(),,(

),,(

22

cbad

b

d

cd

ca

d

bad

dcadbadcbadbdc

dbdccb

bccba

cba

zyx

xx

xy

z

R

uuu

uuuu

uuu

uu

Check if this is equal to

)()( xy RR

Page 42: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Scaling

● Change the coordinates of the object by scaling factors.

x 'y 'z '1

sx 0 0 0

0 s y 0 0

0 0 sz 0

0 0 0 1

xyz1

P ' S P

z

y

x

z

y

x

PSP

P

P

Page 43: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Scaling with respect to a Fixed Point

● Translate to origin, scale, translate back

z

y

x

z

y x

z

y

x

z

y

x

Translate Scale Translate back

PTSTP ),,(),,( ffffff zyxzyx

Page 44: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Scaling with respect to a Fixed Point

1000

)1(00

)1(00

)1(00

),,(),,(

1000

00

00

00

1000

100

010

001

1000

100

010

001

1000

000

000

000

1000

100

010

001

),,(),,(

zfz

yfy

xfx

ffffff

fzz

fyy

fxx

f

f

f

f

f

f

z

y

x

f

f

f

ffffff

szs

sys

sxs

zyxzyx

zss

yss

xss

z

y

x

z

y

x

s

s

s

z

y

x

zyxzyx

TST

TST

Page 45: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Reflection

● Reflection over planes, lines or points

1 0 0 00 1 0 00 0 1 00 0 0 1

z

y

x

z

y

x

z

y

x

1 0 0 00 1 0 00 0 1 00 0 0 1

1 0 0 00 1 0 00 0 1 00 0 0 1

z

y

x

1 0 0 00 1 0 00 0 1 00 0 0 1

Page 46: Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU

Shear

● Deform the shape depending on another dimension

SH z

1 0 a 00 1 b 00 0 1 00 0 0 1

x and y value depends on z value of the shape

SH x

1 0 0 0a 1 0 0b 0 1 00 0 0 1

y and z value depends on x value of the shape