2d and 3d coordinate systems and...

86
Graphics & Visualization Chapter 3 2D and 3D Coordinate Systems and Transformations Graphics & Visualization: Principles & Algorithms

Upload: others

Post on 31-May-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization

Chapter 3

2D and 3D Coordinate Systems

and Transformations

Graphics & Visualization: Principles & Algorithms

Page 2: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 2

• In computer graphics is often necessary to change:

the form of the objects

the coordinate system

• Examples:

In a model of a scene, the digitized form of a car may be used in several

instances, positioned at various points & directions and in different sizes

In animation, an object may be transformed from frame to frame

As objects traverse the graphics pipeline, they change their coordinate

system: object coordinates world coordinates

world coordinates eye coordinates

Coordinates transformations:

- tools of change

- the most important & classic topic in computer graphics

Introduction

Page 3: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 3

• Points in 3D Euclidean space E3: 3×1 column vectors (here)

• Linear transformations: - 3×3 matrices

- they are post-multiplied by a point to

produce another point

Introduction (2)

x

y

z

p

p

p

p

1 2 3

4 5 6

7 8 9

·

xx

yy

zz

p m m m p

p m m m p

m m m pp

Page 4: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 4

• If points were represented by 1×3 row vector the linear

transformations would be pre-multiplied by the point

• Right-handed three-dimensional coordinate systems are used:

Introduction (3)

[ ]x y zp p pp

1 4 7

2 5 8

3 6 9

·x y zx y z

m m m

p p p p p p m m m

m m m

Page 5: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 5

• Mathematics: Transformations:

mappings whose domain & range are the same set (e.g. E3 to E3)

• Computer graphics & visualization: Affine Transformations:

transformations which preserve important geometric properties

of the objects being transformed

• Affine transformations preserve affine combinations

• Examples of Affine combinations:

line segments, convex polygons, triangles, tetrahedra

the building blocks of our models

Affine Transformations - Combinations

Page 6: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 6

• An affine combination of points is a point :

: affine coordinates of p with respect to

where

• Convex affine combination:

if all ai ≥ 0 , i = 0,1,…,n

The affine combination p is within the convex hull of the original points

E.g.1: Line segment between points p1 and p2 is the set of points p:

p = a1 ∙ p1 + a2 ∙ p2 with 0 ≤ a1 ≤ 1 and a2 = 1 – a1

E.g. 2: Triangle with vertices p1,p2,p3 is the set of points p:

p = a1 ∙ p1 + a2 ∙ p2 + a3 ∙ p3 with 0 ≤ a1, a2, a3≤ 1 and a1+ a2 + a3 = 1

Affine Combinations 3E0 1 np ,p , ...,p

3Ep

0

n

i

i

a ip p

0 1, ,..., na a a 0 1 np ,p , ...,p

0

1n

i

i

a

0 np ,...,p

Page 7: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 7

• Affine transformation:

transformation which preserves affine combinations

it retains the inter-relationship of the points

• A transformation is affine if

where : an affine combination

• The result of the application of an affine transformation onto the result

p of an affine combination should equal the affine combination of the

result of performing the affine transformation on the defining points

with the same weights ai

• E.g.

Affine Transformations

3 3: E E0

( ) ( )n

i

i

a ip p

0

n

i

i

a ip p

Page 8: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 8

• Practical consequence:

Internal points need not be transformed

It suffices to transform the defining points

• E.g. to perform an affine transformation on a triangle:

Transform its three vertices only, not its (infinite) interior points

General affine transformation

Mappings of the form (1) where A is a 3×3 matrix

is a 3×1 matrix

are affine transformations in E3 .

Proof: we shall show that (1) preserves affine combinations

Affine Transformations (2)

( ) · p A p t

t

0 0 0 0

0 0

( ) ( )

( ) ( ).

n n n n

i i i i

i i i i

n n

i i

i i

a a a a

a a

i i i

i i

p A p t Ap t

A p t p

Page 9: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 9

• 2D results generalize to 3D

• Four basic affine transformations:

2D Affine Transformations

translation

shearing rotation

scaling

Page 10: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 10

• Defines a movement by a certain distance in a certain direction,

both specified by the translation vector

• The translation of a 2D point

by a vector gives another

point :

• Instantiation of where

(I is the 2×2 identity matrix)

2D Translation

T=[x,y]p

T'=[x',y']p

[ , ]T

x yd dd

p p d

( ) · p A p t and A I t d

Page 11: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 11

• Changes the size of objects

• Scaling factor: specifies the change in each direction

• 2D: sx & sy are the scaling factors which are multiplied by the

respective coordinates of p=[x, y]T

• Scaling of 2D point p=[x, y]T to give p’=[x’, y’]T :

• Instantiation of where

2D Scaling

0( , )· where ( , )

0

x

x y x y

y

ss s s s

sp S p S

( ) · p A p t ( , ) and x ys s A S t 0

Page 12: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 12

• Not possible to observe the effect on a single point

• If scaling factor < 1 reduce the object’s size

scaling factor > 1 increase the object’s size

• Translation side-effect (proportional to the scaling factor):

The object has moved toward the origin of the x-axis (sx < 1)

The object has moved away from the origin of the y-axis (sy > 1)

• Isotropic scaling:

If all the scaling factors are equal (in 2D sx = sy)

Preserves the similarity of objects (angles)

• Mirroring:

Special case, using a -1 scaling factor

About x-axis: S(1,-1) and y-axis: S(-1,1)

2D Scaling (2)

Page 13: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 13

• Turns the objects about the origin

• The distance from the origin does not change, only the orientation

changes

• Counterclockwise rotation is positive

• We can estimate from :

Thus:

2D Rotation

[ , ]Tx yp [ , ]Tx yp

cos( ) (cos cos sin sin ) cos sin

sin( ) (cos sin sin cos ) s

in cos

x l l x y

y l l x y

( )· p R p

Page 14: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 14

• where:

• Rotation is an instantiation of the general affine transformation

where and

2D Rotation (2)

( ) · p A p t

cos sin( )

sin cosR

( )A R t 0

Page 15: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 15

• Increases one of the object’s coordinates by an amount equal to the

other coordinate times a shearing factor

• Physical example: cards placed flat on a table and then tilted by a hard

book.

2D Shear

Page 16: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 16

• We can estimate from :

shear along x axis

shear along y axis

where α and b are the respective shear factors

• In matrix form:

shear on x axis , where

shear on y axis , where

• Shear is an instantiation of the general affine transformation

where or and

2D Shear (2) [ , ]Tx yp [ , ]Tx yp

,

,

x x ay y y

x x y bx y

( ) · p A p t t 0( )b yA SH( )ax

A SH

( )·ax

p SH p1

( )0 1

aaxSH

( )·byp SH p1 0

( )1

bb

ySH

Page 17: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 17

• Useful transformations in computer graphics and visualization rarely

consist of a single basic affine transformation

• All transformations must be applied to all objects of a scene

• Objects are defined by thousands or even millions of vertices

• EXAMPLE: Rotate a 2D object by 45o and then isotropically scale it

by a factor of 2

Apply the rotation matrix:

Then apply the scaling matrix:

Composite Transformations

2 2

2 2(45 )

2 2

2 2

R

2 0(2,2)

0 2S

Page 18: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 18

• It is possible to apply the matrices sequentially to every vertex p:

S(2, 2) (R(45o) p) NOT EFFICIENT

• Another way is to exploit the associative property of matrix

multiplication and apply the pre-computed composite to the vertices:

(S(2, 2) R(45o) )p MORE EFFICIENT

• Thus the composite transformation is only computed once and the

composite matrix is applied to the vertices

• Matrix multiplication is not commutative the order of multiplying

the transformation matrices is important

• Having chosen the column representation of points transformation

matrices are right-multiplied by the points write the matrix

composition in the reverse of the order of application

Composite Transformations (2)

Page 19: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 19

• To apply the sequence of transformations T1,T2, ...,Tm, we compute

the composite matrix Tm ·... ·T2 ·T1

• Problem with the translation transformation: Translation cannot be

described by a linear transformation matrix such as:

• Thus translation cannot be included in a composite transformation

• Solution to the problem homogeneous coordinates

Composite Transformations (3)

x ax by

y cx dy

Page 20: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 20

• Homogeneous Coordinates use one additional dimension than the

space we want to represent

• 2D space: , where w is the new coordinate that corresponds to

the extra dimension; w ≠ 0

• Fixing w=1 maintains our original dimensionality by taking slice w=1

• In 2D we use the plane w=1 instead of the xy-plane

Homogeneous Coordinates

x

y

w

Page 21: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 21

• Points whose homogeneous coordinates are multiples of each other

are equivalent:

e.g., [1,2,3]T and [2,4,6]T represent the same point

• The actual point that they represent is given by their unique basic

representation, which has w = 1 and is obtained by dividing all

coordinates by w:

[x/w, y/w, w/w]T = [x/w, y/w, 1]T

• In general, we use the basic representation for points, and we ensure

that our transformations preserve this property

Homogeneous Coordinates (2)

Page 22: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 22

• How do homogeneous coordinates treat the translation problem?

• Exploit the fact that points have w = 1, in order to represent the

translation of a point p = [x, y, w]T by a vector , as a

linear transformation:

• Transformation on the w-coordinate ensures that the resulting point

has

Homogeneous Coordinates (3)

[ , ]x yd dd

1 0

0 1

0 0 1 1

x x

y y

x x y d w x d

y x y d w y d

w x y w

[ , , ]Tx y wp 1w

Page 23: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 23

• The above linear expressions can be encapsulated in matrix form, thus

treating translation in the same way as the other basic affine

transformations

• In non-homogeneous transformations, the origin [0, 0]T is a fixed

point:

• A positive effect of homogeneous coordinates is that there is no fixed

point under homogeneous affine transformations

Homogeneous Coordinates (4)

0 0·0 0

a b

c d

Page 24: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 24

• The 2D origin is now [0, 0, 1]T which is not a fixed point

• The point [0, 0, 0]T is outside w=1 plane disallowed since it has

w=0

• From here on points will be represented by their homogeneous

coordinates:

2D [x, y, 1]T

3D [x, y, z, 1]T

Homogeneous Coordinates (4)

Page 25: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 25

• 2D homogeneous translation matrix:

• Translation is treated like the other basic affine transformations:

• The last row of a homogeneous transformation matrix is always

[0, 0, 1] in order to preserve the unit value of the w-coordinate

• 2D homogeneous scaling matrix:

2D Homogeneous Affine Transformations

1 0

( ) 0 1

0 0 1

x

y

d

dT d

0 0

( , ) 0 0

0 0 1

x

x y y

s

s s sS

( )· p T d p

Page 26: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 26

• 2D homogeneous rotation matrix:

• 2D homogeneous shear matrices:

shear on x axis

shear on y axis

2D Homogeneous Affine Transformations (2)

cos sin 0

( ) sin cos 0

0 0 1

R

1 0

( ) 0 1 0

0 0 1

a

ax

SH

1 0 0

( ) 1 0

0 0 1

b bySH

Page 27: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 27

• Often necessary to reverse a transformation

• 2D inverse homogeneous translation matrix:

• 2D inverse homogeneous scaling matrix:

2D Homogeneous Inverse Transformations

1 0

( ) ( ) 0 1

0 0 1

x

y

d

d-1T d T -d

10 0

1 1 1( , ) ( , ) 0 0

0 0 1

x

x y

x y y

s

s ss s s

-1S S

Page 28: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 28

• 2D inverse homogeneous rotation matrix:

• 2D inverse homogeneous shear matrix:

shear on x axis

shear on y axis

2D Homogeneous Inverse Transformations (2)

cos sin 0

( ) ( ) sin cos 0

0 0 1

-1R R

1 0

( ) ( ) 0 1 0

0 0 1

a

a a-1

x xSH SH

1 0 0

( ) ( ) 1 0

0 0 1

b b b-1

y ySH SH

Page 29: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 29

• Applying an object transformation on an object is equivalent to the

application of the inverse transformation on the coordinate system

(axis transformation)

EXAMPLE:

• Isotropically scaling an object by a factor of 2 is equivalent to

isotropically scaling the coordinate system axis by a factor of 1/2

(shrinking)

2D Homogeneous Inverse Transformations

Page 30: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 30

Some properties of homogeneous affine transformation matrices:

• for isotropic scaling only (sx=sy)

Properties of Homogeneous Matrices

1 1 2 2 2 2 1 1 1 2 1 2

( )· ( ) ( )· ( ) ( )

( , )· ( , ) ( , )· ( , ) ( · , · )

( 1)· ( 2) ( 2)· ( 1) ( 1 2)

( , )· ( ) ( )· ( , )

x y x y x y x y x x y y

x y x y

s s s s s s s s s s s s

s s s s

T d1 T d2 T d2 T d1 T d1 d2

S S S S S

R R R R R

S R R S

Page 31: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 31

EXAMPLE 1: Rotation about an arbitrary point

Determine the transformation matrix R(θ, p)

required to perform rotation about an arbitrary

point p by an angle θ

SOLUTION

• Step 1: Translate by

• Step 2: Rotate by θ, R(θ)

• Translate by

2D Transformation Example 1

, ( ) p T p

, ( )p T p

1 0 cos sin 0 1 0 cos sin cos sin

( , ) 0 1 · sin cos 0 · 0 1 sin cos sin cos

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

x x x x y

y y y x y

p p p p p

p p p p p pR

Page 32: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 32

EXAMPLE 2: Rotation of a triangle about a point

Rotate the triangle by 45o about the point p=[-1,-1]T, where

a=[0,0]T, b=[1,1]T and c=[5,2]T

SOLUTION

• The triangle can by represented by the matrix

• We shall apply R(θ, p) [Ex. 1] to the triangle:

• The rotated triangle is where a’=[-1, ]T, b’=[-1, ]T

and c’=[ ]T

2D Transformation Example 2

abc

0 1 5

0 1 2

1 1 1

T

2 2 31 1 1 2 12 2 20 1 52 2 9

(45 ,[ 1, 1] )· 2 1· 0 1 2 2 1 2 2 1 2 12 2 2

1 1 10 0 1 1 1 1

TR T

a b c 2 1 2 2 13 9

2 1, 2 12 2

Page 33: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 33

EXAMPLE 3: Scaling about an arbitrary point

Determine the transformation matrix S(sx,sy,p) required to perform

scaling by sx and sy about an arbitrary point p

SOLUTION

• Step 1: Translate by

• Step 2: Scale by sx and sy , S(sx,sy)

• Step 3: Translate by to undo the initial translation

2D Transformation Example 3

, ( ) p T p

, ( )p T p

1 0 0 0 1 0 0

( , , ) 0 1 · 0 0 · 0 1 0

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

x x x x x x x

x y y y y y y y y

p s p s p p s

s s p p s p s p p sS

Page 34: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 34

EXAMPLE 4: Scaling of a triangle about a point

Double the lengths of the sides of triangle keeping its vertex c

fixed . The coordinates of its vertices are a=[0,0]T, b=[1,1]T , c=[5,2]T

SOLUTION

• The triangle can by represented by the matrix T [Ex. 2]

• We shall apply the matrix S(sx,sy,p) [Ex. 3] to the triangle, setting the

scaling factor equal to 2 and p=c

• The scaled triangle is where a’=[-5,-2]T, b’=[-3,0]T and

c’=[5,2]T

2D Transformation Example 4

abc

a b c

2 0 5 0 1 5 5 3 5

(2,2,[5,2,1] )· 0 2 2 · 0 1 2 2 0 2

0 0 1 1 1 1 1 1 1

TS T

Page 35: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 35

EXAMPLE 5: Axis transformation

Suppose that the coordinate system is translated by the vector .

Determine the matrix that describes this effect

SOLUTION

• The required transformation matrix must produce the coordinates of

the objects with respect to the new coordinate system. This is

achieved by applying the inverse translation to the objects:

• Similar argument holds for any other axis transformation. Its effect is

encapsulated by applying the inverse transformation to the objects

2D Transformation Example 5

[ , ]T

x yv vv

1 0

( ) 0 1

0 0 1

x

y

v

vT v

Page 36: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 36

EXAMPLE 6: Mirroring about an arbitrary axis

Determine the transformation matrix required to perform mirroring about

an axis specified by a point p=[px,py]T and a direction vector

SOLUTION

• Step 1: Translate by

• Step 2: Rotate by –θ (negative as it is clockwise), R(-θ)

θ forms between x-axis and vector and:

The two previous steps make the general axis coincide with the x-axis

• Step 3: Perform mirroring about the x-axis, S(1, -1)

• Step 4: Rotate by θ, R(θ)

2D Transformation Example 6

[ , ]T

x yv vv

, ( ) p T p

v

2 2sin

y

x y

v

v v 2 2cos x

x y

v

v v

Page 37: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 37

• Step 5: Translate by

Summarizing the previous steps we have:

2D Transformation Example 6 (2) , ( )p T p

1 0 cos sin 0 1 0 0 cos sin 0 1 0

0 1 · sin cos 0 · 0 1 0 · sin cos 0 · 0 1

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

x x

y y

p p

p pSYMM

2 2 2 2

2 2 2 2

cos sin 2sin cos (cos sin ) 2 sin cos

2sin cos sin cos (sin cos ) 2 sin cos

0 0 1

x x y

y y x

p p p

p p p

Page 38: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 38

EXAMPLE 7: Mirror polygon

Given a polygon, determine its mirror polygon with respect to (a) the line

y=2 and (b) the axis specified by the point p=[0,2]T and the vector

The polygon is given by its vertices a=[-1,0]T, b=[0,-2]T, c=[1,0]T and

d=[0,2]T

SOLUTION

• The polygon can be represented by matrix

• In case (a) p=[0,2]T and thus θ=0ο, sinθ=0, cosθ=1

and we have:

2D Transformation Example 7

[1,1]Tv

1 0 1 0

0 2 0 2

1 1 1 1

Π

[1,0]Tv

1 0 0 1 0 1 0 1 0 1 0

· 0 1 4 · 0 2 0 2 4 6 4 2

0 0 1 1 1 1 1 1 1 1 1

SYMM Π

Page 39: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 39

• In case (b) p=[0,2]T and thus sinθ = cosθ =

and we have:

where MSYM is the matrix of [Ex. 6]

2D Transformation Example 7 (2)

[1,1]Tv1

2

0 1 2 1 0 1 0 2 4 2 0

· 1 0 2 · 0 2 0 2 1 2 3 2

0 0 1 1 1 1 1 1 1 1 1

SYMM Π

Page 40: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 40

EXAMPLE 8: Window-to-Viewport transformation

The contents of a 2D window must be transferred to a 2D “viewport”.

Both the window and “viewport” are rectangular parallelograms with

sides parallel to the x- and y-axis. Determine the window to viewport

transformation matrix. Also determine how objects are deformed by

this transformation.

SOLUTION

Suppose that the window and the viewport are defined by two opposite

vertices and

• Step 1: Translate to the origin, using where

2D Transformation Example 8

[ , ] ,[ , ]T T

xmin ymin xmax ymaxw w w w [ , ] ,[ , ]T T

xmin ymin xmax ymaxv v v v

[ , ]T

xmin yminw w ( )min

T w

[ , ]T

xmin yminw wmin

w

Page 41: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 41

• Step 2: Scale the window to the size of the viewport, using S(sx, sy)

where

• Step 3: Translate to the minimum viewport vertex ,

using where

Summarizing the previous steps we have:

2D Transformation Example 8 (2)

xmax xminx

xmax xmin

v vs

w w

ymax ymin

y

ymax ymin

v vs

w w

[ , ]T

xmin yminv v

( )min

T v [ , ]T

xmin yminv vmin

v

( )· ( , )· ( )x ys s WV min minM T v S T w

0 0

1 0 1 0

0 1 · 0 0 ·0 1

0 0 1 0 0 10 0 1

xmax xmin

xmax xmin

xmin xmin

ymax ymin

ymin ymin

ymax ymin

v v

w wv w

v vv w

w w

Page 42: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 42

Finally :

2D Transformation Example 8 (3)

0

0

0 0 1

xmax xmin xmax xminxmin xmin

xmax xmin xmax xmin

ymax ymin ymax ymin

ymin ymin

ymax ymin ymax ymin

v v v vv w

w w w w

v v v vv w

w w w wWVM

Page 43: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 43

• MWV contains non-isotropic scaling (sx≠sy) objects will be

deformed. A circle will become an ellipse and a square will become a

rectangular parallelogram.

• The aspect ratios of the window and the viewport are defined as the

ratios of their x- to their y-sizes:

• If aw≠av then objects are deformed. To avoid this, is to use the largest

part of the viewport with the same aspect ratio as the window. For

example we can change the vxmax or the vymax boundary of the

viewport in the following manner:

if (av>aw) then vxmax = vxmin+ aw*(vymax – vymin)

else if (av<aw) then

2D Transformation Example 8 (4)

( )xmax xminymax ymin

w

v vv v

a

,xmax xmin xmax xminw v

ymax ymin ymax ymin

w w v va a

w w v v

Page 44: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 44

EXAMPLE 9: Window-to-Viewport transformation instances

Determine the window-to-viewport transformation from the window:

to the viewport :

If there is deformation, how can it be corrected?

SOLUTION

• Direct application of the MWV [Ex. 8]

For the window and viewport pair gives

• Now and , so there is distortion since . It can

be corrected by reducing the size of the viewport by setting:

2D Transformation Example 9

[ , ] [1,1] ,[ , ] [3,5]T T T T

xmin ymin xmax ymaxw w w w

[ , ] [0,0] ,[ , ] [1,1]T T T T

xmin ymin xmax ymaxv v v v

1 10

2 2

1 10

4 4

0 0 1

WVM

1

2wa

1

1va ( )v wa a

1*( )

2xmax xmin w ymax yminv v a v v

Page 45: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 45

EXAMPLE 10: Tilted window-to-viewport transformation

Suppose that the window is tilted and given by

its four vertices a=[1,1]T, b=[5,3]T, c=[4,5]T

and d=[0,3]T. Determine the transformation

that maps it to the viewport

SOLUTION

• Step 1: Rotate the window by angle –θ about a point a. For this we

shall use matrix R(θ, p) [Ex. 1], instantiating it as R(-θ, a) where

• Step 2: Apply the window to viewport transformation MWV [Ex. 8]

2D Transformation Example 10

[ , ] [0,0] ,[ , ] [1,1]T T T T

xmin ymin xmax ymaxv v v v

TILT

WVM

1sin

5

2cos

5

Page 46: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 46

Before Step 2 we must determine the maximum x- and y- coordinates of

the rotated window by computing:

Thus , and we have:

2D Transformation Example 10 (2)

1 2 5

( , )· 1 5

1

ac R c

[ , ] ,[ , ]T T

xmin ymin xmax ymaxw w w wa c

1 1 2 1 3 1 1 30 1

2 5 2 5 5 5 5 5 10 10

1 1 1 2 1 1 2 1· ( , ) 0 · 1

5 5 55 5 5 5 5

0 0 10 0 1 0 0 1

aTILT

WV WVM M R

Page 47: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 47

• 3D homogeneous coordinates are similar to 2D

• Add an extra coordinate to create [x, y, z, w]T

where w corresponds to the extra dimension

• Points whose homogeneous coordinates are multiples are equivalent

e.g. [1, 2, 3, 2]T and [2, 4, 6, 4]T

• Basic representation of a point

is unique

has w=1

is obtained by dividing by w : [x/w, y/w, z/w, w/w]T = [x/w, y/w, z/w, 1]T , w≠0

Example:

• Obtain a 3D projection of 4D space by setting w=1

• Points: 4×1 vectors. Transformations: 4×4 matrices

3D Homogeneous Affine Transformations

T T1 2 3 2 2 4 6 4 1 3[ , , , ] [ , , , ] [ ,1, ,1]2 2 2 2 4 4 4 4 2 2

T

Page 48: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 48

• Specified by a 3-dimensional vector

• Matrix form:

• can be combined with other affine transformation matrices by

matrix multiplication

• Inverse translation:

3D Homogeneous Translation T[ , , ]x y zd d dd

1 0 0

0 1 0( )

0 0 1

0 0 0 1

x

y

z

d

d

dT d

( )T d

1( ) ( ) T d T d

Page 49: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 49

• Three scaling factors: sx, sy, sz

• If scaling factor < 1 the object’s size is reduced

in the respective dimension

scaling factor > 1 the object’s size is increased

• Matrix form:

• Scaling has a translation side-effect, proportional to the scaling factor

3D Homogeneous Scaling

0 0 0

0 0 0( , , )

0 0 0

0 0 0 1

x

y

x y z

z

s

ss s s

sS

Page 50: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 50

• Isotropic scaling:

if sx = sy = sz

preserves the similarity of objects (angles)

• Mirroring:

about one of the major planes (xy, xz, yz)

using a -1 scaling factor

e.g. mirroring about the xy-plane is S(1, 1, -1)

• Inverse scaling:

3D Homogeneous Scaling (2)

1 1 1 1( , , ) ( , , )x y z

x y z

s s ss s s

S S

Page 51: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 51

• Different from 2D rotation: rotate about an axis, not a point

• Basic rotation transformations: rotate about the 3 main axes x, y, z

• Rotation about an arbitrary axis: by combining basic rotations

• Positive rotation about an axis α:

in right-handed coordinate system is the one in

the counterclockwise direction when looking from

the positive part of an axis toward the origin

3D Homogeneous Rotation

Positive rotation about y-axis

Page 52: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 52

• The distance from the axis of rotation does not change

• Rotation does not affect the coordinate that corresponds to the axis of

rotation

• Rotation matrices about the main axes:

• Inverse rotation:

• Rotations can also be expressed using quaternions

3D Homogeneous Rotation(2)

1 0 0 0

0 cos sin 0( )

0 sin cos 0

0 0 0 1

xR

cos 0 sin 0

0 1 0 0( )

sin 0 cos 0

0 0 0 1

yR

cos sin 0 0

sin cos 0 0( )

0 0 1 0

0 0 0 1

zR

( ) ( ), ( ) ( ) ( ) ( ), and -1 -1 -1

x x y y z zR R R R R R

Page 53: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 53

• “Shears” object along one of the major planes

• Increases 2 coordinates by an amount equal to the 3rd coordinate times

the respective shearing factors

• 3 cases in 3D shear: xy, xz, yz

• xy shear: increases x by

increases y by

• Inverse shear:

3D Homogeneous Shear

coordinatea z

coordinateb z

1 0 0

0 1 0( , )

0 0 1 0

0 0 0 1

a

ba b

xySH

1 0 0

0 1 0 0( , )

0 1 0

0 0 0 1

a

a bb

xzSH

1 0 0 0

1 0 0( , )

0 1 0

0 0 0 1

aa b

byz

SH

( , ) ( , ),

( , ) ( , ),

( , ) ( , )

a b a b

a b a b

a b a b

-1

xy xy

-1

xz xz

-1

yz yz

SH SH

SH SH

SH SH

Similar for xz & yz :

Page 54: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 54

EXAMPLE 11: Composite Rotation - Bending

Compute the bending matrix:

rotation about the x-axis by θx rotation about the y-axis by θy

Does the order of the rotations matter?

SOLUTION

1.

2. Compute the reverse order

so the order of the rotations matters

3D Transformation Example 11

cos 0 sin 0 1 0 0 0 cos sin sin cos sin 0

0 1 0 0 0 cos sin 0 0 cos sin 0( )· ( ) ·

sin 0 cos 0 0 sin cos 0 sin sin cos cos cos 0

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

y y y x y x y

x x x x

y x

y y x x y x y x y

BEND y xM R R

1 0 0 0 cos 0 sin 0 cos 0 sin 0

0 cos sin 0 0 1 0 0 sin sin cos sin cos 0( )· ( ) ·

0 sin cos 0 sin 0 cos 0 cos sin sin cos cos 0

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

y y y y

x x x y x x y

x y

x x y y x y x x y

BEND x yM R R

'BEND BENDM M

Page 55: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 55

EXAMPLE 12: Alignment of Vector with Axis

Determine the transformation that aligns a given vector

with the unit vector along the positive z-axis.

SOLUTION

Using two rotations:

• Step 1: Rotate about x by θ1 so that is mapped onto which lies on

the xz-plane, Rx(θ1)

• Step 2: Rotate about y by θ2 so that it coincides with , Ry(θ2)

3D Transformation Example 12

( )A v

[ , , ]Ta b cv k̂

Initial Step1 Step 2

v

v

1v

1v

Page 56: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 56

• Alignment matrix :

• Compute angle θ1:

θ1 is equal to the angle formed between the projection of onto the yz-plane

and the z-axis

For the tip p of we have p=[a, b, c]T

The tip of its projection on yz is p΄=[0, b, c]T

Assuming that b, c are not both 0 we get:

Thus,

3D Transformation Example 12 (2) ( )A v 2 1( ) ( )· ( ) y xA v R R

v

1 12 2 2 2

,sin cos b c

b c b c

2 2 2 21 1

1

1 1

2 2 2 2

1 0 0 0

1 0 0 00 0

0 cos sin 0( )

0 sin cos 00 0

0 0 0 1

0 0 0 1

c b

b c b c

b c

b c b c

xR

v

Page 57: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 57

• Apply Rx(θ1) to , to get its xz projection :

• Note that:

• Compute θ2:

• Thus

3D Transformation Example 12 (3) 1vv

1 1 2 2

0( )· ( )·

1 1

aa

b

c b c1 x x

v R v R

2 2 2| | | | a b c 1v v

2 2

2 22 2 2 2 2 2

, sin os ca b c

a b c a b c

2 2

2 2 2 2 2 22 2

22 2

2 2

2 2 2 2 2 2

0 0cos 0 sin 0

0 1 0 0 0 1 0 0( )

sin 0 cos 00 0

0 0 0 1

0 0 0 1

b c a

a b c a b c

a b c

a b c a b c

yR

Page 58: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 58

• Compute matrix :

• Compute the inverse matrix (useful in next example):

• If b=c=0 then coincides with the x-axis

rotate about y by 90 or -90

depending on the sign of α

3D Transformation Example 12 (4) ( )A v

2 1

2 2 2 2 2

0| | | | | |

0 0( ) ( )· ( )

0| | | | | |

0 0 0 1

| | and

ab ac

c b

a b c

where a b c b c

y x

v v v

A v R R

v v v

v

1( )A v

1 1 1

2 1 1 2 1 2

0 0| | | |

0| | | |( ) ( ( )· ( )) ( ) · ( ) ( )· ( )

0| | | |

0 0 0 1

a

ab c b

ac b c

1

y x x y x y

v v

v vA v R R R R R R

v v

2

0 0 0| |

0 1 0 0( ) ( )

0 0 0| |

0 0 0 1

a

a

a

a

yA v R

v

Page 59: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 59

EXAMPLE 13: Rotation about an Arbitrary Axis using 2 Translations & 5 Rotations

Find the transformation which performs a rotation by an angle θ about an

arbitrary axis specified by a vector and a point p.

SOLUTION

transformation [Ex. 12] :

aligns an arbitrary vector with z-axis

use it to reduce the problem to rotation around z

• Step 1: Translate p to the origin,

• Step 2: Align with the z-axis using matrix

• Step 3: Rotate about the z-axis by the desired angle θ,

• Step 4: Undo the alignment,

• Step 5: Undo the translation,

3D Transformation Example 13

( )A v

v

v

( )T p

( )A v

( )z

R

( )-1A v

( )T p

( )· ( )· ( )· ( ) · ) ( -1

ROT-AXIS zM T p A v R A v T p

Page 60: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 60

EXAMPLE 14: Coordinate System Transformation using 1 Translation&3 Rotations

Determine the transformation required to align a given 3D

coordinate system with basis vectors with the xyz coordinate

system with basis vectors .

The origin of the 1st coordinate system relative to xyz is Olmn.

SOLUTION

Axis transformation:

aligning the basis to the basis

changing an object’s system from to

The solution is an extension of [Ex. 12]

3D Transformation Example 14

( )A v

ALIGNM

ˆ ˆ ˆ( , , )l m n

ˆ ˆ ˆ( , , )i j k

ˆ ˆ ˆ( , , )l m n ˆ ˆ ˆ( , , )i j k

ˆ ˆ ˆ( , , )i j k ˆ ˆ ˆ( , , )l m n

Page 61: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 61

• Step 1: Translate by -Olmn to make the 2 origins coincide,

• Step 2: Align the basis vector with the basis vector, using

of [Ex. 12],

• Step 3: Rotate by φ around the z-axis to align the other 2 axes,

Necessary to transform the or the vector by to estimate φ.

e.g. : the sinφ and cosφ values required for the rotation are

then the x and y components of

3D Transformation Example 14 (2)

( )A vn̂ k̂

)(lmn

T O

ˆ( )A n

( )z

R

ˆ( )· ( )· ( )ALIGN z lmn

M R A n T O

m̂l̂

ˆ ˆ( )·m A n m

m

ˆ( )A n

Page 62: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 62

CONCRETE EXAMPLE :

The orthonormal basis vectors of the 2 coordinate systems are:

and the origins of the 2 coordinate systems coincide ( Olmn = [0.0.0]T).

Find the transformation .

• The basis vectors of the 2nd system are expressed in terms of the 1st

• From the coordinates of [Ex. 12]:

3D Transformation Example 14 (3)

ALIGNM

1 0 0

ˆ ˆ ˆ0 , 1 , 0

0 0 1

3 32 2

29 1653 57

4 25 2ˆ ˆ ˆ , , 29 1653 57

2 2 7

2

9 1653 57

;i j k

l m n

2 2 2 22 2 7 2 7, , and λ ( ) ( )

57 57 57 57 57a b c b c

Page 63: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 63

• Compute :

• Compute :

• So

3D Transformation Example 14 (4)

ˆ( )A n

m

53 4 140

57 3021 3021

7 20 0

ˆ( ) 53 53

2 2 70

57 57 57

0 0 0 1

A n

32

1653

25

ˆ ˆ ˆ ˆ( )· ( )· 1653

20

16531

1

573

15

32

1537

37m A n m A n

32 57 an cosd 3

153715si

37n

Page 64: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 64

• Compute :

• Compute : the origins of the 2 coordinate systems coincide

so

• So,

3D Transformation Example 14 (5)

)(lmn

T O

( )z

R

57 323 0 0

1537 1537

32 57( ) 3 0 0

15371537

0 0 1 0

0 0 0 1

zR

)( lmn

T O ID

3 4 253 4 14057 32 0

3 0 0 29 29 2957 3021 30211537 1537

32 25 27 200 032 57ˆ( )· ( )· · % 1653 1653 16533 0 0 53 53

153715372 2 72 2 7

00 0 1 057 57 5757 57 57

0 0 0 10 0 0 1

ALIGN zM R A n ID

0

0 0 0 1

ˆ( )· ( )· ( )ALIGN z lmn

M R A n T O

Page 65: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 65

EXAMPLE 15: Change of Basis

Determine the transformation required to change the orthonormal

basis of a coordinate system with from to and

vice versa.

SOLUTION

Let the coordinates of the same vector in the 2 bases be .

If the coordinates of the basis vectors in B1 are:

then (from linear algebra):

3D Transformation Example 15

BASISM

ˆ ˆ ˆ1 ( , , )B 1 1 1

i j k ˆ ˆ ˆ2 ( , , )B 2 2 2

i j k

1 2 and B Bv v

ˆ ˆ ˆ2 2 2

i , j ,k

2, 1 2, 1 2, 1ˆ ˆ ˆ B B B

a d p

b e q

c f r

i j k

1 2·B B

a d p

b e q

c f r

v v

Page 66: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 66

Thus ,

B2 is an orthonormal basis is an orthonormal matrix

Homogeneous form:

3D Transformation Example 15 (2) a d p

b e q

c f r

1

BASISM

1

BASISM

Τ( )

a b c

d e f

p q r

1

BASIS BASISM M

0

0

0

0 0 0 1

a b c

d e f

p q rBASIS

M

Page 67: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 67

EXAMPLE 16: Coordinate System Transformation using Change of Basis

Use the change-of-basis result of Ex. 15 to align a given 3D coordinate

system with basis vectors with the xyz-coordinate system with

basis vectors .

The origin of the 1st coordinate system relative to xyz is Olmn.

SOLUTION

This is an axis transformation:

changing an object’s coordinate system from to

Change-of-basis replaces the 3 rotational transformations in Ex. 14

• Step 1: Translate by –Olmn to make the 2 origins coincide,

• Step 2: Use to change the basis from to

3D Transformation Example 16

ˆ ˆ ˆ( , , )l m n

ˆ ˆ ˆ( , , )i j k

ˆ ˆ ˆ( , , )l m n

ˆ ˆ ˆ( , , )i j k

ˆ ˆ ˆ( , , )i j k

ˆ ˆ ˆ( , , )l m n

( )lmn

T O

BASISM

Page 68: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 68

Where the basis vectors expressed in the basis are:

CONCRETE EXAMPLE [of Ex 14]:

No translation because the 2 origins coincide .

with

homogeneous

form

3D Transformation Example 16 (2)

( )

( )· ( )

( )

0 0 0 1

x y z

x y z

x y z

a b c a o b o c o

d e f d o e o f o

p q r p o q o r oALIGN2 BASIS lmn

M M T O

ˆ ˆ ˆ(l,m,n) ˆ ˆ ˆ(i, j,k)

ˆ ˆ ˆ[ , , ] , [ , , ] , [ , , ] and [ , , ] T T T

x y za b c d e f p q r o o o lmn

l m n O

3 4 2

29 29 29

32 25 2

1653 1653 1653

2 2 7

57 57 57

BASISM

3 4 2

29 29 29

32 25 2

1653 1653 1653

2 2 7

5

0

0

0

0 0 0

7 57 7

1

5

BASISM

Page 69: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 69

EXAMPLE 17: Rotation about an Arbitrary Axis using Change of Basis

Use the change-of-basis result of Ex. 15 to find an alternative

transformation which performs a rotation by an angle θ about an

arbitrary axis specified by a vector and a point p.

SOLUTION

• Let

• Equation of plane perpendicular to through p: α(x-xp)+b(y-yp)+c(z-zp)=0

• Let: q a point on that plane, such that q p and

• Normalize the vectors to define a coordinate system basis

with one axis being and the other 2 axes on the given plane

• Use transformation to align it with the xyz-coordinate system

• Perform the desired rotation by θ around the z-axis

3D Transformation Example 17

v

and

p

p

p

a x

b y

c z

v p

v

and m q p l m v

l,m, v

v

BASISM

ˆ ˆ ˆ(l,m,n)

Page 70: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 70

• Step 1: Translate p to the origin,

• Step 2: Align the basis with the basis,

• Step 3: Rotate about the z-axis by desired angle θ,

• Step 4: Undo alignment,

• Step 5: Undo the translation,

The algebraic derivation of the matrix is simpler than

3D Transformation Example 17 (2)

BASISM

-1

BASISM

( )T p

ˆ ˆ ˆ(l,m, v) ˆ ˆ ˆ(i, j,k)

( )z

R

( )T p

( )· · ( )· · ( )1

ROT AXIS2 BASIS z BASISM T p M R M T p

ROT AXIS2M

ROT AXISM

Page 71: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 71

EXAMPLE 18: Rotation of a Pyramid

Rotate the pyramid defined by the vertices

and by about the axis defined by c and the vector .

SOLUTION

The pyramid is represented by a matrix P :

3D Transformation Example 18

[0,0,0] , [1,0,0] , [0,1, ] 0T T T a b c

[0,0,1]Td 45 T[0,1,1]v

0 1 0 0

0 0 1 0

0 0 0 1

1 1 1 1

P a b c d

Page 72: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 72

• Rotate the pyramid : use the matrix.

• The submatrices:

3D Transformation Example 18 (2)

( )· ( )· ( )· ( ) · ) ( -1

ROT-AXIS zM T p A v R A v T p

ROT-AXISM

1 0 0 0

1 0 0 0 1 10 0

0 1 0 1 2 2( ) ( )

0 0 1 0 1 10 0

2 20 0 0 1

0 0 0 1

1 1 1 0 0 00 0

2 2 1 10 0

1 1 2 20 0(45 ) ( )

2 2 1 10 0

0 0 1 0 2 2

0 0 0 1 0 0 0 1

1

z

T c A v

R A v

1 0 0 0

0 1 0 1( )

0 0 1 0

0 0 0 1

T c

Page 73: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 73

• Combine the submatrices to get:

• Compute the rotated pyramid:

• The vertices of the rotated pyramid are:

3D Transformation Example 18 (3)

2 1 1 1

2 2 2 2

1 2 2 2 2 2 2

2 4 4 4

1 2 2 2 2 2 2

2 4 4 4

0 0 0 1

ROT AXISM

1 1 20 1

2 2

2 2 4 2 2 21

· 4 4 2

2 2 2 4 20

4 4 2

1 1 1 1

ROT AXISP M P

1 2 2 2 2 1 2 4 2 2 4 2 2 2[ , , ] , [ , , ] , [0,1,0] and [1, , ]2 4 4 2 4 4

2 2

T T T Ta b c d

Page 74: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 74

• Used as an alternative way to express rotation

• Useful for animating rotations

• A quaternion consists of 4 real numbers: q = (s, x, y, z)

s scalar part of quaternion q

vector part of quaternion q

• Thus an alternative representation of quaternion q is:

• Can be viewed as an extension of complex numbers in 4D:

Using “imaginary units” i, j and k such that: i2=j2=k2=-1 & ij=k, ji=-k and so on

by cyclic permutation, quaternion q may be written as: q = s+ xi+ yj+ zk

• A real number u corresponds to the quaternion: q =

• An ordinary vector corresponds to the quaternion: q = (0, )

• A point p corresponds to the quaternion: q = (0, p)

Quaternions

( , , )x y zv

( , )q s v

( , )u 0

v v

Page 75: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 75

• Addition between quaternions:

• Multiplication between quaternions:

• Multiplication is associative

• Multiplication is not commutative

• The conjugate quaternion of q is defined as:

• It holds that:

• The norm of q is defined as:

Quaternions (2)

1 2 1 1 2 2 1 2 1 2( , ) ( , ) ( , )q q s v s v s s v v

1 2 1 2 1 2 1 2 2 1 1 2

1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2

1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2

· ( · , )

( , ,

, )

q q s s s s

s s x x y y z z s x x s y z z y

s y y s z x x z s z z s x y y x

v v v v v v

( , )q s v

1 2 2 1· ·q q q q

2 22 2 2 2 2 · ·q q q q q s s x y z v

Page 76: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 76

• It holds that:

• A unit quaternion is one whose norm:

• The inverse quaternion of q is defined as:

• It holds that:

• If then

Quaternions (3)

1 2 1 2·q q q q

1q

1

2

1q q

q

1 1· · 1q q q q

1q 1q q

Page 77: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 77

• Consider a rotation by an angle θ about an axis through the origin

whose direction is specified by a unit vector . The rotation can be

expressed by the unit quaternion:

• The above unit quaternion can be applied to a point p represented by

the quaternion p = (0, p) as:

• Thus:

where

• Notice that quaternion p’ represents a point p’ since its scalar part is 0

• Point p’ is exactly the image of the original point p after rotation by

angle θ about the given axis

Expressing rotation using quaternions

ˆ(cos , sin )2 2

q n

1· · · ·p q q q qp p20, ( ) 2 ( ) 2 ( ) .( )p s sv·v p v v·p v p

ˆcos and sin2 2

s v n

Page 78: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 78

• Expressing 2 consecutive rotations:

• The composite rotation is represented by the unit quaternion: q = q2q1

• Quaternion multiplication is simpler, requires fewer operations and is

numerically more stable than rotation matrix multiplication

• Proof of quaternion rotation:

Consider a unit vector , a rotation axis and the images , of after 2

consecutive rotations by θ/2 around

The respective quaternions are:

We observe :

Thus we write:

Similarly:

Expressing rotation using quaternions (2)

2 1 1 2 2 1 1 2 2 1 2 1·( · · )· ( · )· ·( · ) ( · )· ·( · )q q q q q q q q q q q q p p p

0v̂ n̂ 2v̂1v̂ 0v̂

0 0 1 1 2 2ˆ ˆ ˆ(0, ), (0, ), (0, )p p p v v v

0 1 0 1 and ˆˆ ˆ ˆ ˆcos · sin 2

2

v v n v v

0 1 0 1 1 0ˆ ˆ ˆ ˆ( · , ) · q p pv v v v

2 1·q p p

Page 79: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 79

Then:

since because and also

Thus results in the rotation of by angle θ about

• Using similar arguments, results in the same rotation for ,

whereas yields , which agrees with the fact that is the

axis of rotation

Expressing rotation using quaternions (3)

0 1 0 0 2 1 1 0 0 1 2 1 1 2 2· · ( · )· ·( · ) ( · )· · · · ·q p q p p p p p p p p p p p p p p

1· ·q p q 1v̂

ˆ·(0, )·q qn n̂ n̂

0· ·q p q 0v̂ n̂

1 1· ( 1,0) 1p p 1 1v 2 2 2 2ˆ ˆ( 1)· (0, ) (0, )p p v v

Page 80: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 80

• Generalizing the above for an arbitrary vector:

, , are linearly independent. Therefore a vector may be

written as a linear combination

• Then:

which is exactly a quaternion with 0 scalar part and a vector part made

up of the rotated components of

Expressing rotation using quaternions (4)

0v̂1v̂ n̂ p

0 0 1 1ˆˆ ˆp v v n

0 0 1 1

0 0 1 1

0 0 1 1

ˆˆ ˆ·(0, )· ·(0, )·

ˆˆ ˆ ·(0, )· ·(0, )· ·(0, )·

ˆˆ ˆ ( ·(0, )· ) ( ·(0, )· ) ( ·(0, )· )

q q q q

q q q q q q

q q q q q q

p v v n

v v n

v v n

p

Page 81: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 81

• The rotation matrix corresponding to a rotation represented by a unit

quaternion q = (s, x, y, z) is :

• If the following matrix

represents a rotation then the corresponding quaternion q = (s, x, y, z)

may be computed as follows

Conversion between Quaternions and Rotation Matrices

2 2

2 2

2 2

1 2 2 2 2 2 2 0

2 2 1 2 2 2 2 0

2 2 2 2 1 2 2 0

0 0 0 1

q

y z xy sz xz sy

xy sz x z yz sx

xz sy yz sx x yR

00 01 02

10 11 12

20 21 22

0

0

0

0 0 0 1

m m m

m m m

m m mR

Page 82: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 82

• Step 1:

• Step 2:

• If s = 0 (or is a number near 0), use a different set of relations:

then

Conversion between Quaternions and Rotation Matrices (2)

00 11 22

11

2s m m m

02 20 10 0121 12 , ,4 4 4

m m m mm mx y z

s s s

01 1000 11 22

02 20 21 12

11, ,

2 4

,4 4

m mx m m m y

x

m m m mz s

x x

Page 83: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 83

EXAMPLE 19: Rotation of a pyramid

Re- work example 18, using quaternions.

SOLUTION

• Step 1: Translation by so that the axis passes through the

origin

• Step 2: perform the rotation using Rq. The quaternion that expresses

the rotation by 45o about an axis with direction is:

where

An Example

, ( ) c T c

v̂45 45 sin 22.5 sin 22.5

ˆcos , sin (cos 22.5 , 0, , )2 2 2 2

( )q

v

2 21 cos 45 2 2 1 cos 45 2 2cos 22.5 ,sin 22.5

2 2 4

4

Page 84: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 84

Therefore:

• Step 3: Translate by

The final transformation is:

[Ex. 18]

An Example (2)

2 1 10

2 2 2

1 2 2 2 20

2 4 4

1 2 2 2 20

2 4 4

0 0 0 1

qR

, ( )c T c

3 ( ) · · ( )ROT AXIS q ROT AXIS M T c R T c M

Page 85: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 85

• Affine transformations preserve important geometric features of

objects. That’s why they are used in Computer Graphics and

Visualization

• For example let Φ be an affine transformation and p, q points, then:

,

states that the affine transformation of a line segment under Φ is

another line segment

• Ratios of distances on the line segment ( λ / (1-λ) ) are preserved

• Affine transformation subclasses: linear, similitudes, rigid

Geometric Properties

( (1 ) ) ( ) (1 ) ( )p q p q [0,1]

Page 86: 2D and 3D Coordinate Systems and Transformationsgraphics.cs.aueb.gr/cgvizbook/slides/CGVIZ_Chapter_3.pdf• 2D homogeneous translation matrix: 1 • Translation is treated like the

Graphics & Visualization: Principles & Algorithms Chapter 3 86

• Linear transformation can be presented by a matrix A

Linear: all homogeneous affine transformations

• Similitudes preserve the similarity of objects. The result is identical to

the initial object, except for its size

Similitudes: rotation, translation, isotropic scaling

• Rigid transformations preserve all the geometric features of objects

Rigid: rotations and translations

Geometric Properties (2)