transformation wen-chieh (steve) lin department of computer science & institute of multimedia...

60
Transformation Wen-Chieh (Steve) Lin Department of Computer Science & Institute of Multimedia Engineering Rich Riesenfeld’s CG Slides, Shirley, Fundamentals of Computer Graphics, Chap 6.2 —6.5, 5.2

Post on 20-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

TransformationTransformation

Wen-Chieh (Steve) Lin

Department of Computer Science &

Institute of Multimedia Engineering

Rich Riesenfeld’s CG Slides,

Shirley, Fundamentals of Computer Graphics, Chap 6.2—6.5, 5.2

ILE5014 Computer Graphics 10F 2

Uses of TransformationsUses of Transformations

• Modeling transformations– build complex models by positioning

simple components

– transform from object coordinates to world coordinates

• Viewing transformations– placing the virtual camera in the

world

– i.e. specifying transformation from world coordinates to camera coordinates

• Animation– vary transformations over time to

create motion

WORLD

OBJECT

CAMERA

ILE5014 Computer Graphics 10F 3

Brief Review on BasesBrief Review on Bases

• A basis is a linearly independent set of vectors whose combinations will get you anywhere within a space, i.e. span the space

• n vectors are required to span an n-dimensional space

• If the basis vectors are normalized and mutually orthogonal, the basis is orthonormal

ILE5014 Computer Graphics 10F 4

Brief Review on Bases (cont.)Brief Review on Bases (cont.)

• There are lots of possible bases for a given vector space; there’s nothing special about a particular basis—but our favorite is probably one of these.

y

x

z

zx

y

ILE5014 Computer Graphics 10F 5

Vectors Represented in a BasisVectors Represented in a Basis

Every vector has a unique representation in a given basis

• V = v1E1 + v2E2 + … vnEn

• The vectors {E1, E2, …, En} are the basis

• The scalars (v1, v2, …, vn) are the components or coordinates of V with respect to that basis

• Changing the basis changes the components, but not the vector

ILE5014 Computer Graphics 10F 6

Linear TransformationLinear Transformation

• A function (or map, or transformation) F is linear if F(A+B) = F(A)+F(B) and F(kA) = kF(A)

for all vectors A and B, and all scalars k.

• Any linear map is completely specified by its effect on a set of basis vectors:

V = v1E1 + v2E2 + v3E3

F(V) = F(v1E1 + v2E2 + v3E3) = F(v1E1 ) + F(v2E2 ) + F(v3E3) = v1F(E1 ) + v2F(E2 ) + v3F(E3)

ILE5014 Computer Graphics 10F 7

Example: Rotation of a BasisExample: Rotation of a Basis

,

,

,

E1

E2

E3

E’1

E’2

E’3

E’i=F(Ei)

V = v1E1 + v2E2 + v3E3

F(V) = F(v1E1 + v2E2 + v3E3) = F(v1E1 ) + F(v2E2 ) + F(v3E3) = v1F(E1 ) + v2F(E2 ) + v3F(E3) = v1E’1 + v2E’2 + v3E’3

ILE5014 Computer Graphics 10F 8

Transforming a Vector

• The coordinates of the transformed basis vector (in terms of the original basis vectors):

The transformed general vector V becomes:

and its coordinates (still w.r.t. the original basis) are

The matrix multiplication formula!v1 = (f11v1 + f12v2 + f13v3)v2 = (f21v1 + f22v2 + f23v3)v3 = (f31v1 + f32v2 + f33v3)

33122111)( EEEE 11 fffF

332222122 )( EEEE 1 fffF 333223133)( EEEE 1 fffF

F(V) = v1F(E1 ) + v2F(E2 ) + v3F(E3) = (f11E1+f21E2+f31E3)v1 + (f12E1+f22E2+f32E3)v2 + (f13E1+f23E2+f33E3)v3

=(f11v1+ f12v2 + f13v3)E1 + (f21v1+ f22v2+ f23v3) E2 + (f31v1+ f32v2 + f33v3)E3

ILE5014 Computer Graphics 10F 9

Brief Review on MatricesBrief Review on Matrices

• An nxn matrix F represents a linear function in n dimensions

– i-th column shows what the function does to the corresponding basis vector

3

2

1

333231

232221

131211

3

2

1

ˆ

ˆ

ˆ

v

v

v

fff

fff

fff

v

v

v

ILE5014 Computer Graphics 10F 10

Brief Review on MatricesBrief Review on Matrices

• Transformation = linear combination of columns of F

– first component of the input vector scales first column of the matrix

– accumulate into output vector

– repeat for each column and component

3

2

1

333231

232221

131211

3

2

1

ˆ

ˆ

ˆ

v

v

v

fff

fff

fff

v

v

v

ILE5014 Computer Graphics 10F 11

Brief Review on MatricesBrief Review on Matrices

• Usually compute it a different way:

– dot row i with input vector to get component i of output vector

3

2

1

333231

232221

131211

3

2

1

ˆ

ˆ

ˆ

v

v

v

fff

fff

fff

v

v

v

ILE5014 Computer Graphics 10F 12

Aside: Matrix DecompositionAside: Matrix Decomposition

• Any matrix M can be decomposed via singular value decomposition (SVD) into a rotation times a scale times another rotation

• If M is symmetric,

• See Chap. 5.2.7 for SVD

M = R1SR2

M = RSR-1

ILE5014 Computer Graphics 10F 13

Basic 2D TransformationsBasic 2D Transformations

• Translation

• Scaling

• Rotation about origin by Ө

ILE5014 Computer Graphics 10F 14

Basic 2D Transformations (cont.)Basic 2D Transformations (cont.)

• Shear

y

ayx

y

xaShx 10

1

ybx

x

y

x

bShy 1

01

y

yx

y

xShx

tan

10

tan1

ILE5014 Computer Graphics 10F 15 15x x

y

y

yx

y

xShx

tan

10

tan1

y tanΦ

Φ

Geometric View of Shear in xGeometric View of Shear in x

ILE5014 Computer Graphics 10F 16

Affine TransformationAffine Transformation

• A function F is affine if it is linear plus a translation

– Thus the 1-D transformation y = mx + b is not linear, but affine

– Similarly for a translation and rotation of a coordinate system

– Affine transformations preserve lines

ILE5014 Computer Graphics 10F 17

Homogeneous CoordinatesHomogeneous Coordinates

• Translation is not linear--how to represent it as a matrix?

• Trick: add extra coordinate to each vector

• This extra coordinate is the homogeneous coordinate, or w

1100

10

01

1

y

x

t

t

y

x

y

x

ILE5014 Computer Graphics 10F 18

Homogeneous CoordinatesHomogeneous Coordinates

• When extra coordinate is used, vector is said to be represented in homogeneous coordinates

• The associated transformation matrices Homogeneous Transformations

• Drop extra coordinate after transformation (project to w=1)

11001

2221

1211

y

x

tff

tff

y

x

y

x

ILE5014 Computer Graphics 10F 19

w!? Where did that come from?w!? Where did that come from?

• Practical answer:– w is a clever algebraic trick.

– Don’t worry about it too much. The w value will be 1.0 for the time being.

– If w is not 1.0, divide all coordinates by w to make it so.

ILE5014 Computer Graphics 10F 20

w!? Where did that come from?w!? Where did that come from?

• Clever Academic Answer:

– (x, y, w) coordinates form a 3D projective space.

– All nonzero scalar multiples of (x, y, 1) form an equivalence class of points that project to the same 2D Cartesian point (x, y).

– For 3-D graphics, the 4D projective space point (x, y, z, w) maps to the 3D point (x, y, z) in the same way.

ILE5014 Computer Graphics 10F 21

Homogeneous 2D TransformationsHomogeneous 2D Transformations

• The basic 2D transformations becomeTranslate: Scale: Rotate:

• Any affine transformation can be expressed as a combination of these.

• We can combine homogeneous transforms by multiplication.

• Now any sequence of translate/scale/rotate operations can be collapsed into a single homogeneous matrix

100

10

01

y

x

t

t

100

00

00

y

x

s

s

100

0cossin

0sincos

ILE5014 Computer Graphics 10F 22

Location and Direction in 2D Homogeneous CoordinatesLocation and Direction in 2D Homogeneous Coordinates

• Location

• Direction

• Homogeneous coordinates can elegantly represent the translational invariance of a direction vector!

1

y

x

p

0

y

x

v

11100

10

01

' x

x

y

x

ty

tx

y

x

t

t

p

vv

00100

10

01

' y

x

y

x

t

t

y

x

ILE5014 Computer Graphics 10F 23

Composition of TransformationComposition of Transformation

• Build compound transformations by stringing basic ones together, e.g.

– “first apply a scale S, and then a rotation R

V3 = R(SV1)

V2 = SV1, then V3 = RV2

ILE5014 Computer Graphics 10F 24

Composition of TransformationComposition of Transformation

• Any sequence of linear transformations can be collapsed into a single matrix formed by multiplying the individual matrices together

• This is good: can apply a whole sequence of transformation at once

V3 = R(SV1)

V2 = SV1, then V3 = RV2 V3 = MSV1

M = RS

M = M1M2 ...Mn

ILE5014 Computer Graphics 10F 25

Sequences of TransformationsSequences of Transformations

• Often the same transformations are applied to many points

• Calculation time for the matrices and combination is negligible compared to that of transforming the points

• Reduce the sequence to a single matrix, then transform

M

M

M

x x x x x x x x x x

x' x' x' x' x' x' x' x' x'

PA

RA

ME

TE

RS

MA

TR

ICE

S

UNTRANSFORMED POINTS

TRANSFORMED POINTS

ILE5014 Computer Graphics 10F 26

Order of Transformations Does Matter!Order of Transformations Does Matter!

• Matrix multiplication isn’t commutative!

ILE5014 Computer Graphics 10F 27

Post Multiply vs. Pre MultiplyPost Multiply vs. Pre Multiply

• Consider the composite function ABCD, i.e. p’ = ABCDp

• Matrix multiplication is associative, so we can calculate from right to left or left to right: ABCD = (((AB) C) D) = (A (B (CD))).

• Iteratively replace either the leading or the trailing pair by its product

• Postmultiply: left-to-right (reverse of function application.)

• Premultiply: right-to-left (same as function application.)

• Postmultiply: left-to-right (reverse of function application.)

• Premultiply: right-to-left (same as function application.)

M D

M CM

M BM

M AM

M A

M MB

M MC

M MD

or both give the same result.

Premultiply Postmultiply

ILE5014 Computer Graphics 10F 28

Column Vector ConventionColumn Vector Convention

• The convention in the previous slides

– transformation is by matrix times vector, Mv

– textbook uses this convention, 90% of the world too

• The composite function A(B(C(D(x)))) is the matrix-vector product ABCDx

11

333231

232221

131211

y

x

mmm

mmm

mmm

y

x

ILE5014 Computer Graphics 10F 29

Beware: Row Vector Convention

• The transpose is also possible

• How does this change things?– all transformation matrices must be transposed

– ABCDx transposed is xTDTCTBTAT

– pre- and post-multiply are reversed

• OpenGL uses transposed matrices!– You only notice this if you pass matrices as arguments to

OpenGL subroutines, e.g. glLoadMatrix.

– Most routines take only scalars or vectors as arguments.

332313

322212

312111

11

mmm

mmm

mmm

yxyx

ILE5014 Computer Graphics 10F 30

Windowing TransformationWindowing Transformation

• [a, A] x [b, B] [c, C] x [d, D]

• Translate(-a, -b)

• Scale

• Translate(c, d)

100

10

01

100

00

00

100

10

01

b

a

bB

dDaA

cC

d

c

M

ILE5014 Computer Graphics 10F 31

Basic 3D TransformationsBasic 3D Transformations

• 3-D transformations are very similar to the 2-D case

• Homogeneous coordinate transforms require 4x4 matrices

• Scaling and translation matrices are simply:

1000

100

010

001

z

y

x

t

t

t

1000

000

000

000

z

y

x

S

S

S

ILE5014 Computer Graphics 10F 32

3D Rotation Matrices3D Rotation Matrices

• Rotation is a bit more complicated in 3-D

• More possible axes of rotation

• e.g., rotate about z-axis: only x and y coordinate change

1000

0100

00cossin

00sincos

Rotate-z(Φ) =

ILE5014 Computer Graphics 10F 33

3D Rotation Matrices about x- and y- axes3D Rotation Matrices about x- and y- axes

1000

0cossin0

0sincos0

0001

Rotate-x(Φ) =

1000

0cos0sin

0010

0sin0cos

Rotate-y(Φ) =

ILE5014 Computer Graphics 10F 34

Properties of Rotation MatricesProperties of Rotation Matrices

• Rotation matrices are orthonormal

– Rows/Columns are mutually orthonormal

– Rows/Columns form a Cartesian coordinate

• Geometrically, R-1(θ) = R(-θ)

• Algebraically, R-1(θ) = RT(θ)

– Prove by yourself that RT(θ)R(θ) = R(θ)RT(θ) = I using orthonormality of R

• R-1(θ) = R(-θ) = RT(θ)

ILE5014 Computer Graphics 10F 35

Arbitrary 3D Rotations Arbitrary 3D Rotations

• Can we construct an arbitrary 3D rotation using basic rotation matrices Rx() Ry() Rz()?

y

x

a+θ

α

z

ILE5014 Computer Graphics 10F 36

Rotate + about axis a: Ra(+ ) Rotate + about axis a: Ra(+ )

y

z

x

a+θ

α

ILE5014 Computer Graphics 10F 37

First, Rotate about z by : Rz( ) First, Rotate about z by : Rz( )

Now in the

(y-z)-plane

y

z

x

a

α

ILE5014 Computer Graphics 10F 38

Then Rotate about x by + : Rx( ) Then Rotate about x by + : Rx( )

Rotate in the

(y-z)-plane

y

z

x

a

ILE5014 Computer Graphics 10F 39

Now, + Rotation about z-axis: Rz(+ )Now, + Rotation about z-axis: Rz(+ )

Now aligned with z-axis

39

y

z

x

a

ILE5014 Computer Graphics 10F 40

Then rotate about x by - : Rx(- ) Then rotate about x by - : Rx(- )

Rotate again in

the (y-z)-plane

y

z

x

a

ILE5014 Computer Graphics 10F 41

Now, + Rotation about z by : Rz( ) Now, + Rotation about z by : Rz( )

Now to original position of a

y

z

x

a-α

ILE5014 Computer Graphics 10F 42

We Effected + rotation about Arbitrary axis a: Ra(+ ) We Effected + rotation about Arbitrary axis a: Ra(+ )

y

z

x

a

y

z

x

α

)()()( RRR z xa )( Rz)()( RRx z

ILE5014 Computer Graphics 10F 43

Rotation about Arbitrary AxisRotation about Arbitrary Axis

• Rotation about a-axis effected by (nonunique) composition of 5 elementary rotations

• We show arbitrary rotation as succession of 5 rotations about coordinate axes

ILE5014 Computer Graphics 10F 44

1000

0)cos()sin(0

0)sin()cos(00001

10000100

00)cos()sin(

00)sin()cos(

)(

Ra

1000010000cossin00sincos

1000

0cossin0

0sincos00001

1000010000cossin00sincos

)( Rz

In matrix terms, Ra(+ ) = )( Rz

)( Rx

)( Rx

)( Rz

ILE5014 Computer Graphics 10F 45

Euler Angles for 3-D RotationsEuler Angles for 3-D Rotations

• Euler angles - 3 rotations about each coordinate axis, however

– angle interpolation for animation generates bizarre motions

– rotations are order-dependent, and there are no conventions about the order to use

• Widely used anyway, because they're “simple”

• Coordinate axis rotationsRotate-x(Φ)Rotate-y(θ)Rotate-z(φ)

ILE5014 Computer Graphics 10F 46

Euler Angles for 3-D RotationsEuler Angles for 3-D Rotations

• Roll, pitch, yaw angle

ILE5014 Computer Graphics 10F 47

QuaternionsQuaternions

• Complex numbers can represent 2-D rotations

• Quaternions, a generalization of complex numbers, can represent 3-D rotations

• Quaternions represent 3-D rotations with 4 numbers:

– 3 give the rotation axis - magnitude is sin(α/2)

– 1 gives cos(α/2)

– unit magnitude - points on a 4-D unit sphere

ILE5014 Computer Graphics 10F 48

What is a Normal?What is a Normal?

• Indication of outward facing direction for lighting and shading

Order of definition ofvertices in OpenGL

Right hand rule

Note: GL conventions… glFrontFace(GL_CCW) glFrontFace(GL_CW)

ILE5014 Computer Graphics 10F 49

Transforming Normal VectorsTransforming Normal Vectors

• If we transform normals like points

• We need a different rule to transform normals.

ILE5014 Computer Graphics 10F 50

Normals Transform Like PlanesNormals Transform Like Planes

plane transform to

point transform to

spacedtransforme in plane on point for equation

spaceoriginal in plane on point for equation

TTT

T

T

T

T

TTT

da,b,c

zyxdcba

dczbyax

nMMnn

Mpp

pn

MpMn

pMMn

Ipn

np

pnpnpn

11

1

1

)(

))((

)(

0

:magic some do answer, thefind To

? transform should how ed, transformis If

offset. theis normal, plane theis )(

1 , where,0

writtenbecan 0 planeA

ILE5014 Computer Graphics 10F 51

Inverses of Transformation MatricesInverses of Transformation Matrices

• Inverse geometrically

– T-1(tx, ty, tz) = T(-tx, -ty, -tz)

– S-1(sx, sy, sz) = S(1/sx, 1/sy, 1/sz)

– R-1 = R

• M = M1M2...Mn

2zyx1 )Rs,s,S(sRM

11

11n

1n MMMM

1

TT12

1 /1/1/1 )Rs,s,sS(RM zyx

SVD

ILE5014 Computer Graphics 10F 52

Coordinate TransformationsCoordinate Transformations

• So far, we think of transformation as moving points around.

• Transformation can be viewed as changing the coordinate system

Moving points

Moving coordinate

=

ILE5014 Computer Graphics 10F 53

Global vs. Local Coordinate SystemGlobal vs. Local Coordinate System

• Need to handle multiple coordinate systems

• A “canonical” coordinate system is usually designated as the frame-of-reference for all other coordinates

– Global coordinate

– World coordinate

– Frame of reference

• The others are called local coordinates

ILE5014 Computer Graphics 10F 54

Global vs. Local Coordinate SystemGlobal vs. Local Coordinate System

• x, y, u, v, o, e are all in global system

• P in global: (x(0), y(0)), P in local: (u(1), v(1))

• Convert local to global: vuep )1()1()0()0( ),( uuyx

11

0

0

100

10

01

1

)1(

)1(

)0(

)0(

v

u

e

e

y

x

y

x

vu

1100

10

01

1001

)0(

)0(

)1(

)1(

y

x

e

e

v

u

y

xT

T

v

u

ILE5014 Computer Graphics 10F 55

Transformation as Change in Coordinate SystemTransformation as Change in Coordinate System

• Useful in many situations

• Use most natural coordination system locally

• Tie things together in a global system

ILE5014 Computer Graphics 10F 56

ExampleExample

1

2

34

y

x

ILE5014 Computer Graphics 10F 57

is the transformation that takes a

point in coordinate system j and

converts it to a point in coordinate

system i

M ji

ExampleExample

p j)(

p i)(

pMpji

ji

)()(

pMpkj

kj

)()(

MMM kjki ji

ILE5014 Computer Graphics 10F 58

Example: Rotating CoordinateExample: Rotating Coordinate

X0

Y0

X1

Y1

Local coordinateGlobal coordinate

X1 Y1

Basis or Local Coordinate Frame

pMpji

ji

)()(

ILE5014 Computer Graphics 10F 59

Change of Coordinate SystemChange of Coordinate System

• Describe the old coordinate system in terms of the new one.

x’y’y

x

ILE5014 Computer Graphics 10F 60

Change of Coordinate SystemChange of Coordinate System

Move to the new coordinate system and describe the one old.

x

y

x

y Old is a negative rotation of the new.x’

y’