1 Modeling Transformations Md. Tanvir Al Amin* Lecturer, Dept. of CSE, BUET tanviralamin@gmail.com...

Post on 16-Jan-2016

213 views 0 download

Tags:

Transcript of 1 Modeling Transformations Md. Tanvir Al Amin* Lecturer, Dept. of CSE, BUET tanviralamin@gmail.com...

1

Modeling Transformations

Md. Tanvir Al Amin*Lecturer, Dept. of CSE, BUET

tanviralamin@gmail.com

CSE 409

*Special Thanks to Tanvir Parvez, Fredo Durand, James O Brien, Tomas Lozano-Perez, Jovan Popovic

Modeling Transformation - Class 1

2

Transformation Background

What is Transformation ?

3

What is a Transformation

Transformation:– An operation that changes one configuration

into another

For images, shapes, etc.– A geometric transformation maps positions that

define the object to other positions– Linear transformation means the transformation

is defined by a linear function... which is what matrices are good for.

4

5

What is a Transformation?

A function that maps points x to points x':Applications: animation, deformation, viewing,

projection, real-time shadows, …

6

Simple Transformations

Can be combinedAre these operations invertible?Yes, except scale = 0

7

Rigid-Body / Euclidean Transforms

Preserves distancesPreserves angles

TranslationRotation

Rigid / Euclidean

Identity

8

Similitudes / Similarity Transforms

Preserves angles

TranslationRotation

Rigid / Euclidean

Similitudes

Isotropic ScalingIdentity

9

Linear Transformations

TranslationRotation

Rigid / EuclideanLinear

Similitudes

Isotropic ScalingIdentity

Scaling

Shear

Reflection

10

Linear Transformations

L(p + q) = L(p) + L(q)L(ap) = a L(p)

TranslationRotation

Rigid / EuclideanLinear

Similitudes

Isotropic Scaling

Scaling

Shear

ReflectionIdentity

Translation is not linear because ap+t ≠ a(p+t)

11

Affine Transformations

preserves parallel lines

TranslationRotation

Rigid / EuclideanLinear

Similitudes

Isotropic Scaling

Scaling

Shear

ReflectionIdentity

Affine

6.837 Fall 06 – Durand 12

Projective Transformations

preserves lines

TranslationRotation

Rigid / EuclideanLinear

Affine

Projective

Similitudes

Isotropic Scaling

Scaling

Shear

Reflection

Perspective

Identity

13

Groups

Properties of a group (element, operation):– There exists a neutral element– There exists an inverse for each member– The elements are "closed under composition"– The composition operation is associative

Example: integers under addition0 is the neutral element-x is the inverse of xx+y is an integer(x+y)+z=x+(y+z)

14

Groups and Transformations

Properties of a group:– There exists an identity mapping– There exists an inverse mapping for each function– The functions are "closed under composition"– The composition operation is associative

These properties might seem trivial at first glance, but they are actually very important, because when these conditions are shown for any class of functions and their two-argument composition operation, then they form an algebraic group. – One of the consequences is that any series of translations can

be composed to a single translation. – Another consequence is that the inverse is unique.

15

How are Transforms Represented?

x' = ax + by + c

y' = dx + ey + f

x'

y'

a b

d e

c

f=

x

y+

p' = M p + t

16

Translation in homogenous coordinates

x' = ax + by + c

y' = dx + ey + f

x'

y‘

1

a b

d e

0 0

c

f

1

=x

y

1

p' = M p

x'

y'

a b

d e

c

f=

x

y+

p' = M p + t

Affine formulation Homogeneous formulation

17

Homogeneous Co-ordinates

Translation, scaling and rotation are expressed (non-homogeneously) as:– translation: P = P + T– Scale: P = S · P– Rotate: P = R · P

Composition is difficult to express, since translation not expressed as a matrix multiplication

Homogeneous coordinates allow all three to be expressed homogeneously, using multiplication by 3 matrices

W is 1 for affine transformations in graphics

18

Homogeneous Coordinates

Add an extra dimension• in 2D, we use 3 x 3 matrices

• In 3D, we use 4 x 4 matrices

Each point has an extra value, wx'

y'

z'

w'

=

x

y

z

w

a

e

i

m

b

f

j

n

c

g

k

o

d

h

l

p

p' = M p

19

Homogeneous Coordinates

Most of the time w = 1, and we can ignore it

If we multiply a homogeneous coordinate by an affine matrix, w is unchanged

x'

y'

z'

1

=

x

y

z

1

a

e

i

0

b

f

j

0

c

g

k

0

d

h

l

1

20

Homogeneous Co-ordinates

P2d is a projection of Ph onto the w = 1 plane

So an infinite number of points correspond to : they constitute the whole line (tx, ty, tw)

x

y

w Ph(x,y,w)

P2d(x,y,1)

w=1

Homogeneous Visualization

Mechanics of Rigid Transformations

Translate

Rotate

Scale

22

23

Translation – 2D

(4,5) (7,5)

Y

XBefore Translation

1

*

100

10

01

1

y

x

d

d

y

x

TPPd

dT

y

xP

y

xP

y

x

y

x

Form sHomogeniou

x’ = x + dx y’ = y + dy

(7,1) (10,1)

X

Y

Translation by (3,-4)

24

Translation – 3D

PPdddT

dz

dy

dx

z

y

x

d

d

d

zyx

z

y

x

z

y

x

*),,(

11

*

1000

100

010

001

z

y

x

dzz

dyy

dxx

25

Scaling – 2D

(4,5) (7,5)

Y

X(2,5/4) (7/2,5/4)

X

Y

Before Scaling Scaling by (1/2, 1/4)

y

x

y

x

y

x

sy

sx

y

x

s

s

PPS

ysy

xsx

*

**

0

0

*

*

*

Types of Scaling:

Differential ( sx != sy )Uniform ( sx = sy )

1

*

100

00

00

1

Form sHomogeniou

y

x

s

s

y

x

y

x

26

Scaling – 3D

1

*

*

*

1

*

1000

000

000

000

*),,(

z

y

x

z

y

x

zyx

sz

sy

sx

z

y

x

s

s

s

PPsssS

Original

scale all axes

scale Y axiszsz

ysy

xsx

z

y

x

*

*

*

Rotation – 2D

sin

cos

r

rv

rota

ted

cossinsincos

sinsincoscos expand

rry

rrx

cossin

sincos

sin

cosbut

yxy

yxx

ry

rx

original

sin

cos

r

rv

28

Rotation – 2D

(5,2) (9,2)

Y

X

(2.1,4.9)

(4.9,7.8)

X

YBefore Rotation Rotation of 45 deg. w.r.t. origin

1

*

100

0cossin

0sincos

1

Form sHomogeniou

y

x

y

x

cos*sin*

sin*cos**

cossin

sincos

*

yx

yx

y

x

PPR

yyx

xyx

cos*sin*

sin*cos*

Modeling Transformation - Class 2

29

30

Rotation – 3D

1

cos*sin*

sin*cos*

1

*

1000

0100

00cossin

00sincos

*,

z

yx

yx

z

y

x

PPR k

For 3D-Rotation 2 parameters are needed

Angle of rotation

Axis of rotation

Rotation about z-axis:

31

Rotation about Y-axis & X-axis

1

cos*sin*

sin*cos*

1

*

1000

0cos0sin

0010

0sin0cos

*,

zx

y

zx

z

y

x

PPR j

1

cos*sin*

sin*cos*

1

*

1000

0cossin0

0sincos0

0001

*,

zy

zy

x

z

y

x

PPR iAbout x-axis

About y-axis

32

Rotation about Z axis

About z axis

x'

y'

z'

1

=

x

y

z

1

cos θ

sin θ

0

0

-sin θ

cos θ

0

0

0

0

1

0

0

0

0

1

ZRotate(θ)

x

y

z

p

p'

θ

Rotation

How to rotate around (kx, ky, kz), a unit vector on an arbitrary axis …Example : Rotate 30 degree around vector

2i+1j+3k For now, solution is Rodrigues Formula– Can it be found from some rotation around x

axis, then some rotation around y axis, then z axis ?

– We will munch it later ….

33

34

Rotation

About (kx, ky, kz), a unit vector on an arbitrary axis(Rodrigues Formula)

x'

y'

z'

1

=

x

y

z

1

kxkx(1-c)+c

kykx(1-c)+kzs

kzkx(1-c)-kys

0

0

0

0

1

kykx(1-c)-kzs

kyky(1-c)+c

kzky(1-c)+kxs

0

kxkz(1-c)+kys

kykz(1-c)-kxs

kzkz(1-c)+c

0

Rotate(k, θ)

x

y

z

θ

k

We will return to this case of arbitrary rotation later …

where c = cos θ & s = sin θ

Non Rigid Transforms

35

Non Rigid Transforms

We just studied Translation, Scale, Rotate All these are Rigid Transformations Is Shear rigid ?

– No– Shear is Linear Transform

However, Translate, Scale, Rotate, Shear, Reflect all are member of general class “Affine Transformation”

36

37

Mirror Reflection

100

010

001

axis-Xabout Reflection

xM

yyxx

100

010

001

axis-Yabout Reflection

yM

yyxx

(1,1)

(1,-1)

Y

X

(-1,1) (1,1)

X

Y

38

Shearing Transformation

100

01

01

100

01

001

100

010

01

b

a

SHbSH

a

SH xyyx

unit cubeSheared in X

directionSheared in Y

directionSheared in both X

and Y direction

39

Shear along Z-axis

1

*

*

1

*

1000

0100

010

001

*),(

z

shzy

shzx

z

y

x

sh

sh

PPshshSH

y

x

y

x

yxxy

y

x

z

Inverse Transforms

40

41

Inverse Transformations

y-y

x-x

),(-(sx,sy)

T(-θ

-(θ

(-dx,-dy)-

(dx,dy)

MM

MM

SS

RRR

TT

sysx

1

1

1

)()1)

1

:RefMirror

: Sclaing

: Rotation

:nTranslaito

11

Composing Transformations

42

43

How are transforms combined?

(0,0)

(1,1)(2,2)

(0,0)

(5,3)

(3,1)Scale(2,2) Translate(3,1)

TS =2

0

0

2

0

0

1

0

0

1

3

1

2

0

0

2

3

1=

Scale then Translate

Use matrix multiplication: p' = T ( S p ) = TS p

Caution: matrix multiplication is NOT commutative!

0 0 1 0 0 1 0 0 1

44

Non-commutative Composition

Scale then Translate: p' = T ( S p ) = TS p

Translate then Scale: p' = S ( T p ) = ST p

(0,0)

(1,1)(4,2)

(3,1)

(8,4)

(6,2)

(0,0)

(1,1)(2,2)

(0,0)

(5,3)

(3,1)Scale(2,2) Translate(3,1)

Translate(3,1) Scale(2,2)

45

TS =

2

0

0

0

2

0

0

0

1

1

0

0

0

1

0

3

1

1

ST =

2

0

0

2

0

0

1

0

0

1

3

1

Non-commutative Composition

Scale then Translate: p' = T ( S p ) = TS p2

0

0

0

2

0

3

1

1

2

0

0

2

6

2

=

=

Translate then Scale: p' = S ( T p ) = ST p

0 0 1 0 0 1 0 0 1

46

Combining Translations, Rotations

Order matters!! TR is not the same as RT (demo)General form for rigid body transformsWe show rotation first, then translation

(commonly used to position objects) on next slide. Slide after that works it out the other way

47

Rotate then Translate

' ( )P TR P MP RP T

11 12 13 11 12 13

21 22 23 21 22 23

31 32 33 31 32 33

1 0 0 0

0 1 0 0

0 0 1 0 0 1

0 0 0 1 0 0 0 1 0 0 0 1

x x

y y

z z

T R R R R R R T

T R R R R R R T R TM

T R R R R R R T

48

Translate then Rotate

' ( ) ( )P RT P MP R P T RP RT

11 12 13

3 3 3 3 3 121 22 23

1 331 32 33

0 1 0 0

0 0 1 0

0 10 0 0 1

0 0 0 1 0 0 0 1

x

y

z

R R R T

R R TR R R TM

R R R T

49

Classification of Transformations Again

1. Rigid-body Transformation Preserves parallelism of lines Preserves angle and length e.g. any sequence of R() and T(dx,dy)

2. Affine Transformation Preserves parallelism of lines Doesn’t preserve angle and length e.g. any sequence of R(), S(sx,sy) and T(dx,dy)

unit cube 45 deg rotaton Scale in X not in Y

Example Affine Transforms

50

51

Properties of rigid-body transformation

1002221

1211

y

x

trr

trr

The following Matrix is Orthogonal if the upper left 2X2 matrix has the following properties

1.A) Each row are unit vector. sqrt(r11* r11 + r12* r12) = 1

B) Each column are unit vector. sqrt(c11* c11 + c12* c12) = 1

2.A) Rows will be perpendicular to each other (r11 , r12 ) . ( r21 , r22) = 0

B) Columns will be perpendicular to each other (c11 , c12 ) . (c21 ,c22) = 0

e.g. Rotation matrix is orthogonal

100

0cossin

0sincos

• Orthogonal Transformation Rigid-Body Transformation

• For any orthogonal matrix B B-1 = BT

52

Associativity of Matrix Multiplication

Create new affine transformations by multiplying sequences of the above basic transformations.

q = CBAp

q = ( (CB) A) p = (C (B A))p = C (B (Ap) ) etc.

matrix multiplication is associative.

But to transform many points, best to do

M = CBA

then do q = Mp for any point p to be rendered.

To transform just a point, better to do q = C(B(Ap))

For geometric pipeline transformation, define M and set it up with the model-view matrix and apply it to any vertex subsequently defined to its setting.

Example Composite Transforms

53

2D Case

54

Rotation of about P(h,k): R,P

R,P=

Q(x,y)

P(h,k)

Step 1: Translate P(h,k) to origin

T(-h ,-k)

Q1(x’,y’

)

P1 (0,0)

Step 2: Rotate w.r.t to origin

R*

Q2(x’,y’

)

P2 (0,0)

Step 3: Translate (0,0) to P(h,k0)

T(h ,k) *P3(h,k)

Q3(x’+h, y’

+k)

55

Reflection about line L, ML

Step 1: Translate (0,b) to origin

T(0 ,-b)ML =

Step 2: Rotate - degrees

Step 3: Mirror reflect about X-axis

R(-) *T(0 ,b) *

Step 4: Rotate degrees

Step 5: Translate origin to (0,b)

M x*R() *

(0,b)

Y

X

t

O

Y

XO

Y

XO

Y

XO

Y

XO

(0,b)

Y

X

t

O

Scale about Arbitrary Axis

56

Scale about arbitrary axis

57

Scale about arbitrary axis

58

Scale about arbitrary axis

59

Scale about arbitrary axis

60

Shear

Shears are not really primitive transformsRelated to non-axis-aligned scales Express shear in terms Euclidean

transformation and non uniform scale– Its your homework – Hints on next slide– ??

61

Singular Value Decomposition

62

Decomposing Matrices

63

64

Problems to be solved:

Schaum’s outline series:

Problems: 4.1 4.2

4.3, 4.4, 4.5 => R,P 4.6, 4.7, 4.8 => Ssx,sy,,P

4.9, 4.10, 4.11, 4.21 => ML

4.12 => Shearing Pg-281(1.24), Pg-320(5.19)

=> Circular view-port

Modeling Transformation - Class 3, 4

65

Exploring rotations in 3D

66

Recap : Rotation

67

Recap : Rotations

68

Recap : Axis Aligned Rotations

69

Recap : Rotation around Arbitrary Axisa=(ax,ay,az) is a normalized vector

A point x is to be rotated by angle theta around this vectorFind the Rotation Matrix

70

Arsenal

That old rotation problem, for what we will check at least three solutions– Euler Theorem (Old school thing)

– Rodrigues Formula (The beautiful Construction)

– Quaternion (Latest hero in the town)

71

Euler Theorem

That old school thing ….

72

Euler Angles

Before considering our composite rotation around a rotation axis which ultimately will be called Euler Axis, we examine another way of rotating a body from one orientation to another orientation ….

These are the Euler Angles

73

Special note : Don’t confuse Euler theorem and Euler Angles. Euler angles are not concerned with our axis of composite rotation. Rather they specify a way to orient a co-ordinate system with another by means of defined rotationsEuler theorem is concerned with our rotation axis, which we are going to study once we are done with Euler angles.

Euler Angles

Euler angles a means of representing the

spatial orientation of any frame of the space as a

composition of rotations from a reference frame.

The fixed system is denoted in lower case (x,y,z)

and the rotated system in upper case letters

(X,Y,Z).

Line of nodes (N) The intersection of the xy

and the XY coordinate planes.

• α is the angle between the x-axis and the line of nodes.

• β is the angle between the z-axis and the Z-axis. • γ is the angle between the line of nodes and the X-axis.

Angle signs and ranges

α and γ range are defined modulo 2π radians. A valid range could be A valid range could be (-π, π]

β range covers π radians (but can't be said to be modulo π). For example could be [0, π] or [-π/2, π/2]

The angles α, β and γ are uniquely determined except for the singular case that – xy and the XY planes are identical,

– the z axis and the Z axis having the same or opposite directions. • if the z-axis and the Z-axis are the same, β = 0 and only (α+γ) is uniquely defined (not

the individual values),

• if the z-axis and the Z-axis are opposite, β = π and only (α-γ) is uniquely defined (not the individual values). These ambiguities are known as gimbal lock in applications.

75

Euler Rotations

Change one of the Euler angles while leaving the other two constant.

Neither expressed in terms of external or internal frame, but in mixture.

• first angle moves the line of nodes around the external axis z • second rotates around the line of nodes • third one is an intrinsic rotation around an axis fixed in the body

The three rotations are called Precession, Nutation, and intRinsic rotation

Euler rotations are commutative.Used in aerodynamics. We are not going to use them

76

Intrinsic or Extrinsic Rotations

Intrinsic rotation (in terms of moving body or local coordinate) or Extrinsic rotation (global coordinate) is

possible also possible with Euler angles.

77

78

Intrinsic and Extrinsic Rotations

Intrinsic Rotation

• Rotate the XYZ system about the z-axis by α.

The x-axis now lies on the line of nodes.• Rotate the XYZ system again about the new

rotated x-axis by β. • Rotate the XYZ system a third time about the

new z-axis by γ.

Extrinsic Rotation

• Rotate the XYZ-system about the z-axis by γ.

• Rotate the XYZ-system again about the x-axis

by β.• Rotate the XYZ-system a third time about the

z-axis by α.

Be careful, in this notation, rotations are around z-x-z. Here y is not involved. But there are other conventions also.

Order of rotations

3D rotations do not commuteEuler angles are not unique !!For a specific composite rotation, there are 12

possible orderings of three individual axis-aligned rotations, and each produce different Euler angles β1, β2, β3

One of them are called "proper" Euler angles and the other Tait–Bryan angles

79

Matrix of Euler Angles

– In z-x-z convention :

– Writing the matrices

– Denoting cos by c and sin by s

- Homework : Get Euler angles from the rotation matrix

80

)().().( zxz RRRR

Why Euler angles ?

When we want to go between one orientation to another, we may use the Euler anglesBut use of euler angles impose problems as

well like :

More on this later…

81

Gimbal Lock

Consider a case of a level sensing platform on an aircraft flying due North with its three gimbal axes mutually perpendicular (i.e., roll, pitch and yaw angles each zero). If the aircraft pitches up 90 degrees, the aircraft and platform's Yaw axis gimbal becomes parallel to the Roll axis gimbal, and changes about yaw can no longer be compensated for (see illustration, red ring is pivoted to green ring and green ring is pivoted to blue ring).

The word lock is misleading: no gimbal is restrained, all three gimbals can still rotate freely about their respective axis of suspension. Nevertheless, because of the parallel orientation of both the yaw and roll gimbal axes, there is no axis available to accommodate yaw rotation.

More later

82

Euler Theorem

We are now back to our previous problem, finding the rotation matrix of an arbitrary rotation around an axis.

The result is due to Euler

83

Special note : Don’t confuse Euler theorem and Euler Angles. Euler theorem is concerned with our rotation axis, we are going to study it now.Euler angles are not concerned with our axis of composite rotation. Rather they specify a way to orient a co-ordinate system with another by means of defined rotations.

Euler’s Theorem

Euler’s Theorem states that– Any rotation (or sequence of rotations) about a

point is equivalent to a single rotation about some axis through that

84

Ref : Hill’s Book, page 239

(Original Statement) Quomodocunque sphaera circa centrum suum conuertatur, semper assignari potest diameter, cuius directio in situ translato conueniat cum situ initiali.

Translation: When a sphere is moved around its centre it is always possible to find a diameter whose direction in the displaced position is the same as in the initial position.

Ref : Wikipedia

This axis is called Euler

Axis

Euler’s Theorem

Implication of Euler’s Theorem – The θ angle rotation around vector a can be built

from composing axis-aligned rotations– We are now going to find that …

85

Finding the Rotation Matrix

Our previous method Step 1,2 Perform two rotations so that a becomes aligned

with the z-axis (two rotations are necessary) Step 3 Do the required θ rotation around z-axis Step 4,5 Undo the alignment rotations to restore a to its

original direction

86

Alignment

Rotation

Trace Back

Trivial but you should be careful when doing in hands

Step 1,2

We now study a composite transformationAV,N = aligning a vector V with a vector N

To find the rotation matrix, we need to find Av,k

87

88

AV : aligning vector V with k

Av = R,i

V = aI + bJ + cK

x

y

z

b

a

c

k

22λ

λcos

λsin

by axis-about x Rotate :1 Step

cbc

b

b

|V|

x

y

z

b

a

k

( 0, b,c )b

|V|

x

y

z

a

k

|V|

( a, 0, )

( 0, 0, )

( 0, b,c )

89

AV : aligning vector V with k

Av = R,i R-,j *

22λ

λcos

λsin

by axis-about x Rotate :1 Step

cbc

b

222

|V|

|V|

λ)cos(

|V|)sin(

-by axis-yabout V Rotate :2 Step

cba

a

P( a, b, c)

b

x

y

z

b

a

c

k

|V|

( a, 0, )( 0, b,c )b

x

y

z

b

a

c

|V|

( 0, 0, |V|)

( 0, b,c )

a

90

AV : aligning vector V with k

AV-1 = AV

T

AV,N = AN-1 * AV

1000

0

00

0

λλ

λ-

λ-λ

Vc

Vb

Va

bc

Vac

Vab

V

VA

Finding the rotation matrix

Now we have done step 1,2 : AV

For step 3, we have to rotate theta angle around z axisThen take the inverse of step 1,2,

AV-1 = AV

T

There are actually 5 rotations here. None of which are Euler angles α or β or γ

91

vzTv ARAR )(

Axis Angle Notation

92

sin)cos1(

100

010

001

cos),(2

2

2

zzyzx

zyyyx

zxyxx

aaaaa

aaaaa

aaaaa

aR

0

0

0

xy

xz

yz

aa

aa

aa

Rod

rigu

es F

orm

ula

The Geometry of a Rotation

93

The Geometry of a Rotation

94

The Geometry of Rotation

95

The Geometry of Rotation

So we find, the rotation vector is :

This is a vector equation, which is goodBut, we need a matrix form also, to work

easily.

96

Rodrigues Formula

Vecto

r For

m

Matrix Form

sin)cos1(

100

010

001

cos),(2

2

2

zzyzx

zyyyx

zxyxx

aaaaa

aaaaa

aaaaa

kR

0

0

0

xy

xz

yz

aa

aa

aa

Rodrigues Formula

Vector and Matrix forms

How we find these equivalent matrix and vector forms ?Check next slide

97

sin)cos1(

100

010

001

cos),(

),()(

2

2

2

zzyzx

zyyyx

zxyxx

T

aaaaa

aaaaa

aaaaa

kR

XkRxR

zyxX

0

0

0

xy

xz

yz

aa

aa

aa

Vector and Matrix algebra

98

Cross product in Matrix Form

99

Finding rotations from a rotation matrixGiven, R is a pure rotation matrixFind axis of rotation K(kx,ky,kz)

and angle theta from RRecall rodrigues formula, it gives R Check that :Trace(R ) = Sum of diagonal elements of the 3x3

rotation matrix

100

2

1)(cos

RTrace

R

Finding rotations from a rotation matrixCheck that :

101

T

xy

xz

yz

RR

kk

kk

kk

)sin(2

1

0

0

0

)()sin(2

1

)()sin(2

1

)sin(2

1

2,11,2

1,33,1

3,22,3

RRk

RRk

RRk

z

y

x

R

Modeling Transformation - Class 5

102

Quaternion

Have you ever thought about the operations of vector algebra ? We can add vectors, subtract vectors … But multiplication ?

– We can multiply vectors, But, two ways !!– Dot product outputs scalar– Cross product outputs vector

Hey, what about division – Man….r u insane ? vector division !!

103

Quaternion

Vector algebra contains subtraction as the inverse operation for addition, Null vector being the identity element. But there is no inverse vector for

multiplication operation !! That’s why we can’t divide vectors.Why no inverse ?

– A . B is scalar, inverse is out of question– A × B is vector, can we have an inverse ?

104

In search of an vector inverse…

So, we want an inverse A-1 for a vector A, so that A × A-1 = Identity Or may be, we want that

– If B × A = C, then• Given C and B, find A.

• A= B-1 × C

– Is it actually possible ??

105

NONO

Why ?

If A × B = C then– Given C and A, find B– If A, B, C are scalars, 5 × 3 = 15. Hence

15 / 5 = 3, uniquely defined.– But if A, B, C are vectors. B is not uniquely

defined now !!• We just know, B lies on the plane normal to C.

• B can be any vector lying on the plane normal to C, as long as

106

||

||sin||

A

CB

ϕ

Why we are craving for inverse

So, what is the reason behind our wild goose search for an inverse of cross ?Because, inverse of cross product could

capture rotation around an axisThink other wayIf C and B is given,

– A is ϕ rotation away– On the plane

normal to C107

Axis of rotation

Unique

How to uniquely determine B ??– The problem is not complete actually.– Only, C and A alone are not sufficient for

rotation– We need either |B| or ϕ (or sinϕ or cosϕ) to get

A from B and C– Which means, Vector B and C alone aren’t

sufficient, a scalar is needed also.– Here comes quaternion : scalar + 3D vector

108

ϕ

Is it really new ?

No, actuallyWe already know something … Think about complex numbers A point (x,y) on a euclidean space can be

thought as a vector, where inverse is not possible.But if we think it as a complex number,

then inverse is possible !!!

109

Complex Numbers

So, a 2D vector, ai+bj is not invertibleBut a complex number a+ib has inverse…

– Why ???

– Because there is a defined Identity element

– More importantly, there is a necessary relationship for a multiplicative space

i2 = -1And this lets us to have the idea of

conjugation110

Complex Number

Actually, a complex number is not really a 2D vector.Complex Number = Scalar + 1D VectorI said Quaternion = Scalar + 3D VectorSo, what necessary properties do we need

now ?– We need something to operate with

multiplication, something like i2 = -1

111

Great minds in History

Hamilton knew that the complex numbers could be viewed as points in a plane, and he was looking for a way to do the same for points in space. Points in space can be represented by their coordinates, which are triples of numbers, and for many years Hamilton had known how to add and multiply triples of numbers. But he had been stuck on the problem of division: He did not know how to take the quotient of two points in space

The breakthrough finally came on Monday 16 October 1843 in Dublin, when Hamilton was on his way to the Royal Irish Academy where he was going to preside at a council meeting. While walking along the towpath of the Royal Canal with his wife, the concept behind quaternions was taking shape in his mind. Hamilton could not resist the impulse to carve the formulae for the quaternions into the stone of Brougham Bridge as he passed by it.

i2 = j2 = k2 = ijk = − 1

112

Quaternion Arsenal

– A quaternion H = [v,w]• v is vector, w is scalar

• v is 3D vector, hence

• H = [(x,y,z),w]

• Real numbers [0,s]

• Pure Vectors [v,0]

113

Quaternion Arsenal

114

Quaternion Arsenal

115

Quaternion Arsenal

116

Quaternions as Rotation

A unit quaternion aids for vector rotationIf q is a unit quaternion, p is a point stored in homogenous

coordinate [x,y,z,w] as a quaternionqpq-1 rotates p around

117

Rotating a vector

118

Representing objects in 3D

119

120

Orientation

Thumb points to +ve Z-axisFingers show +ve rotation from X to Y

axis

Y

X

Z (out of page)

Y

X

Z (larger z areaway from viewer)

Right-handed orentation Left-handed orentation

121

Vectors in 3D

Have length and direction

V = [xv , yv , zv]Length is given by the Euclidean Norm

||V|| = ( xv2 + yv

2 + zv2 )

Dot Product

V • U = [xv, yv, zv]•[xu, yu, zu]

= xv*xu + yv*yu + zv*zu = ||V|| || U|| cos ß

Cross Product V U

= [yv*zu - zv yu , -xv*zu + zv*xu , xv*yu – yv*xu ]= ||V|| || U|| sin ßV U = - ( U x V)

(xv,yv,zv)V=aI+bJ+cK

x

y

z

122

3D Equation of Curve & Line

Parametric equations of Curve & LineCurve

Line

bta

thz

tgy

tfx

C

:

VtPPPtPL

t

tzzzz

tyyyy

txxxx

L

PPPPV

0010

010

010

010

0110

)(

10:

P0(x0,y0,z0)

P1(x1,y1,z1)

t > 1

Vt < 0

t =1

t = 00 < t < 1

x

y

z

C

123

3D Equation of Surface & Plane

Parametric equations of Surface & PlaneSurface

Plane : with Normal, N

dtc

bsa

tshz

tsgy

tsfx

S

,

,

,

:

P0

N

kCjBiAN

DCzByAx

ˆˆˆ

0

124

3D Plane

Ways of defining a plane

1. 3 points P0, P1, P2 on the plane

2. Plane Normal N & P0 on plane

3. Plane Normal N & a vector V on the plane

Plane Passing through P0, P1, P2

P0

P1

P2

N

V

)(

0

0ˆ)(ˆ)(ˆ)()ˆˆˆ(

0

ˆˆˆ

000

000

0

2010

CzByAxD

DCzByAx

kzzjyyixxkCjBiA

PPN

kCjBiAPPPPN

where

plane the on is z) y,P(x, if

125

Revisit : Affine Transformation

Transformation – is a function that takes a point (or vector) and maps that point (or vector) into another point (or vector).

A coordinate transformation of the form:

x’ = axx x + axy y + axz z + bx ,

y’ = ayx x + ayy y + ayz z + by ,

z’ = azx x + azy y + azz z + bz ,

is called a 3D affine transformation.

11000

'

'

'

z

y

x

baaa

baaa

baaa

w

z

y

x

zzzzyzx

yyzyyyx

xxzxyxx

The 4th row for affine transformation is always [0 0 0 1]. Properties of affine transformation:

– translation, scaling, shearing, rotation (or any combination of them) are examples affine transformations.

– Lines and planes are preserved.– parallelism of lines and planes are also preserved, but not angles and

length.

Composite 3D Transformations

126

127

Object Transformation

Line: Can be transformed by transforming the end pointsPlane:(described by 3-points) Can be

transformed by transforming the 3-pointsPlane:(described by a point and Normal)

Point is transformed as usual. Special treatment is needed for transforming Normal

128

Composite Transformations – 3D

Some of the composite transformations to be studied are:AV,N = aligning a vector V with a vector N

– We already studied it

R,L = rotation about an axis L( V, P )

MN,P = Mirror Reflection w.r.t a plane

Aligning a plane P with a plane Q

129

R,L : rotation about an axis L

Let the axis L be represented by vector V and passing through point P

1.Translate P to the origin

2. Align V with vector k

3. Rotate about k

4. Reverse step 2

5. Reverse step 1

R,L = T-PAV *R,k *AV-1 *T-P

-1 *

V

P

Q

Q'

L

z

x

y

k

130

MN,P : Mirror reflection

Let the plane be represented by plane normal N and a point P in that plane

x

y

z N

P

131

MN,P : Mirror reflection

Let the plane be represented by plane normal N and a point P in that plane

1.Translate P to the origin

MN,P = T-P

x

y

z

N

P

132

MN,P : Mirror reflection

Let the plane be represented by plane normal N and a point P in that plane

1.Translate P to the origin

2. Align N with vector k

MN,P = T-PAN *

N

P

x

y

z

133

MN,P : Mirror reflection

Let the plane be represented by plane normal N and a point P in that plane

1.Translate P to the origin

2. Align N with vector k

3. Reflect w.r.t xy-plane

MN,P = T-PAN *S1,1,-1 *

N

P

x

y

z

134

x

y

z

MN,P : Mirror reflection

Let the plane be represented by plane normal N and a point P in that plane

1.Translate P to the origin

2. Align N with vector k

3. Reflect w.r.t xy-plane

4. Reverse step 2

MN,P = T-PAN *S1,1,-1 *AN-1 *

135

MN,P : Mirror reflection

Let the plane be represented by plane normal N and a point P in that plane

1.Translate P to the origin

2. Align N with vector k

3. Reflect w.r.t xy-plane

4. Reverse step 2

5. Reverse step 1

MN,P = T-PAN *S1,1,-1 *AN-1 *T-P

-1 *

x

y

z N

P

136

Further Composition

The Composite Transform must have

– Translation of P1 to Origin T

zx

y3P

1P2PT

– Some Combination of Rotations R

Rx

y

z 2P

3P 1P

z

x

y3P

1P2P

Fig. 1 Fig. 2

Translate points in fig. 1 into points in fig 2 such that:– P3 is moved to yz plane

– P2 is on z-axis

– P1 is at Origin

137

Finding R

xx

zyx

zyx

zzz

yyy

xxx

Rx.x R

RRR

RRR

zRyRxR

zRyRxR

zRyRxR

rrr

rrr

rrr

R

R

vextor ofcomponent :Note

other each to

lar perpendicu are ii)

vectorsunit are i)

Transform body-Rigid isR

be Let

,,

,,

...

...

...

333231

232221

131211

138

Finding Rz

z

z

z

z

zyx

zyx

zyx

TT

R

zR

yR

xR

zRzRzR

yRyRyR

xRxRxR

RRkR

21

21

21

21

21

21

PP

PP

PP

PP

PP

PP

.

.

.

1

0

0

...

...

...

ˆ 1

R

z

x

y

3P

1P2P

x

y

z 2P

3P 1P

Rz

k

kR

R

ˆ

21

21

21

PP

PP

axis-z along PP aligns

139

R

x

y

z 2P

3P 1P

z

x

y

3P

1P 2P

Finding Rx

x

x

x

x

zyx

zyx

zyx

R

zR

yR

xR

zRzRzR

yRyRyR

xRxRxR

iR

2131

2131

2131

2131

2131

2131

PPPP

PPPP

PPPP

PPPP

PPPP

PPPP

.

.

.

0

0

1

...

...

...

ˆ1 Rx

i

iR

R

ˆ

2131

2131

2131

PPPP

PPPP

axis-x along PPPP aligns

Rz

k

140

Finding Ry

y

y

y

y

zyx

zyx

zyx

R

zR

yR

xR

zRzRzR

yRyRyR

xRxRxR

jR

xz

xz

xz

RR

RR

RR

.

.

.

0

1

0

...

...

...

ˆ1

jR

R

ˆ

xz

xz

RR

axis- yalong RR aligns

R

x

y

z 2P

3P 1P

z

x

y

3P

1P 2P

Rx

i

Rz

k

Ry

j

141

Problems to be solved:

Schaum’s outline series:

Problems: 6.1

6.2, 6.5, 6.9, 6.10, 6.11, 6.12 Av

6.3, 6.4 R,L 6.6, 6.7, 6.8 MN,P

Transforming Normal Correctly

142

What is the problem

You have known of Normal vectorsNormal vector to a plane is used for

lighting and shading purposeWhen a Plane is transformed, the normal

vector may not be transformed correctly.The programmer needs to specify correct

normal after transformation

143

144

Normal

Surface Normal: unit vector that is locally perpendicular to the surface

145

Why is the Normal important?

It's used for shading — makes things look 3D!

object color only Diffuse Shading

146

Visualization of Surface Normal

± x = Red± y = Green± z = Blue

147

How do we transform normals?

Object SpaceWorld Space

nOS

nWS

6.837 Fall 06 – Durand 148

Transform Normal like Object?

translation? rotation? isotropic scale? scale? reflection? shear?perspective?

149

Transform Normal like Object? translation? rotation? isotropic scale?scale? reflection?shear?perspective?

150

Similitudes

What class of transforms?

TranslationRotation

Rigid / Euclidean

Linear

Affine

Projective

Similitudes

Isotropic Scaling

Scaling

Shear

Reflection

Perspective

IdentityTranslation

RotationIsotropic Scaling

IdentityReflection

a.k.a. Orthogonal Transforms

151

Transformation for shear and scale

IncorrectNormal

Transformation

CorrectNormal

Transformation

152

More Normal Visualizations

Incorrect Normal Transformation Correct Normal Transformation

153

Think about transforming the tangent plane to the normal, not the normal vector

So how do we do it right?

Original Incorrect Correct

nOS

Pick any vector vOS in the tangent plane,how is it transformed by matrix M?

vOSvWS

nWS

vWS = M vOS

154

Transform tangent vector vv is perpendicular to normal n:

nOST vOS = 0

nOST (M-1 M) vOS = 0

nWST = nOS

T (M-1)

(nOST M-1) (M vOS) = 0

(nOST M-1) vWS = 0

nWST vWS = 0

vWS is perpendicular to normal nWS:

nWS = (M-1)T nOS

nOS

vWS

nWS

vOS

Dot product

155

Comment

So the correct way to transform normals is:

But why did nWS = M nOS work for similitudes?Because for similitude / similarity

transforms,

(M-1)T = Me.g. for orthonormal basis:

nWS = (M-1)T nOS

xu

yu

zu

xv

yv

zv

xn

yn

zn

ux

vx

nx

uy

vy

ny

uz

vz

nz

M-1 =M =

Sometimes noted M-T

Rotational Interpolation

156

Camera Interpolation

How to smoothly interpolate a camera between two views ?

157

More about Euler Angles

Durand slideTait Bryan Angles

158

Flight Dynamics

Roll Pitch YawTrai

159

Roll, Pitch, Yaw

160

Interpolation by Euler AngleGimbal LockHow gimbal lock prevented (4th axis)Interpolation by Quaternion

161

162

Hierarchical Modeling

Many graphical objects are structured Exploit structure for

– Efficient rendering– Concise specification of model parameters– Physical realism

Often we need several instances of an object– Wheels of a car– Arms or legs of a figure– Chess pieces

Encapsulate basic object in a function Object instances are created in “standard” form Apply transformations to different instances Typical order: scaling, rotation, translation

163

OpenGL & Hierarchical Model

ABCC

glPushMatrix

– void glPushMatrix(void);

AB

glPushMatrix

– void glPoipMatrix(void);

ABC C

m

glGetFloatv

– void glGetFloatv(GL_MODELVIEW_MATRIX, *m);

ABC

Some of the OpenGL functions helpful for hierarchical modeling are:

164

Scene Graph

A scene graph is a hierarchical representation of a scene We will use trees for representing hierarchical objects

such that:– Nodes represent parts of an object

– Topology is maintained using parent-child relationship

– Edges represent transformations that applies to a part and all the subparts connected to that part

typedef struct treenode {GLfloat m[16]; // Transformationvoid (*f) ( ); // Draw functionstruct treenode *sibling;struct treenode *child;

} treenode;

Scene

Sun Star X

Earth Venus Saturn

Moon Ring

165

Example - Torso

Initializing transformation matrix for node

treenode torso, head, ...;

/* in init function */

glLoadIdentity();

glRotatef(...);

glGetFloatv(GL_MODELVIEW_MATRIX, torso.m);

Initializing pointers

torso.f = drawTorso;

torso.sibling = NULL;

torso.child = &head;

166

Generic Traversal

To render the hierarchy:– Traverse the scene graph depth-first– Going down an edge:

• push the top matrix onto the stack

• apply the edge's transformation(s)

– At each node, render with the top matrix– Going up an edge:

• pop the top matrix off the stack

167

Generic Traversal : Torso

Recursive definitionvoid traverse (treenode *root) {

if (root == NULL) return;

glPushMatrix();

glMultMatrixf(root->m);

root->f();

if (root->child != NULL) traverse(root->child);

glPopMatrix();

if (root->sibling != NULL) traverse(root->sibling);

}

C is really not the right language for this !!

168

Viewing Transformation

169

Viewing Pipeline Revisited

xw

yw

zw

pw

Modeling Transform

WorldCoordinates

Pw

yo

xo

zo

po

GraphicsPrimitives Po

ObjectCoordinates

ye

xe

-ze

pe

Viewing Transform Pe

EyeCoordinates

170

Viewing Transformation in OpenGL To setup the modelview matrix, OpenGL provides the

following function:

y

x

z

eye(eyex, eyey, eyez)

center(centerx, centery, centerz)

up(upx, upy, upz)

gluLookAtgluLookAt(( eyex, eyey, eyezeyex, eyey, eyez, , centerx, centery, centerzcenterx, centery, centerz, , upx, upy, upzupx, upy, upz ))

171

Implementation

We want to construct an Orthogonal FrameOrthogonal Frame such that,

xzy

x

z

eCeCeC

puvnormalizeeC

veC

eyecenternormalizev

eyeOC

...

.

.

.

C.e z

C.O(eye)

center

up(upx, upy, upz)

v

C.e x

C.e y

(1)(1) its origin is the point eyepoint eye

(2)(2) its -z basis vector points towards the point point centercenter(3)(3) the up vectorup vector projects to the up direction (+ve y-axis)

Let CC (for camera) denote this frame. Clearly,

172

Thank You