28
computer graphics • transforms © 2008 fabio pellacini • 1 geometric transformations computer graphics • transforms © 2008 fabio pellacini • 2 linear algebra review • matrices – notation basic operations matrix-vector multiplication

geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 1

geometric transformations

computer graphics • transforms © 2008 fabio pellacini • 2

linear algebra review

• matrices– notation– basic operations– matrix-vector multiplication

Page 2: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 3

matrices

• notation for matrices and vectors– use column form for vectors

!

M =m11

m12

m21

m22

"

# $

%

& ' = mij[ ] [ ]Tvv

v

v

21

2

1=!

"

#$%

&=v

computer graphics • transforms © 2008 fabio pellacini • 4

matrix operations

• addition

• scalar multiply

][][ ijijij nmtNMT +=!+=

][][ ijij mtMT !! ="=

Page 3: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 5

matrix operations

• matrix-matrix multiply– row-column multiplication– not commutative– associative

!"

#$%

&='= (

k

kjikij nmtMNT ][

!"

#$%

&!"

#$%

&=!

"

#$%

&

2221

1211

2221

1211

2221

1211

nn

nn

mm

mm

tt

tt

computer graphics • transforms © 2008 fabio pellacini • 6

matrix operations

• matrix-vector multiply– row-column multiplication

!"

#$%

&='= (

k

kikivmuM ][vu

!"

#$%

&!"

#$%

&=!

"

#$%

&

2

1

2221

1211

2

1

v

v

mm

mm

u

u

Page 4: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 7

special matrices

• identity

• zero

[ ]!"#

$

===

j ifor 0

jifor1

ijiI !"

#$%

&=

10

01I

MIMMI ==

[ ] 0== ijoO !"

#$%

&=

00

00O

MOM =+

computer graphics • transforms © 2008 fabio pellacini • 8

matrix operations

• transpose– flip along the diagonal

• inverse– will not compute explicitly in this course

][][ jiij

TmtMT =!=

IMMMMTMMT ===!=""" 111

Page 5: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 9

matrix operations oroperties

• linearity of multiplication

• associativity of multiplication

• transpose and inverse of matrix multiply

MBMABAM

BABA

!"!"

"""

+=+

+=+

)(

)(

111)(

)(

!!!=

=

ABAB

ABABTTT

CABBCA )()( =

computer graphics • transforms © 2008 fabio pellacini • 10

geometric transformation

• function that maps points to points

• different transformations have restriction on theform of X– we will look at linear, affine and projections

)(' ppp X=!

Page 6: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 11

2D transformations

computer graphics • transforms © 2008 fabio pellacini • 12

translation

• simplest form• inverse

tppt

+=)(T

tppptt

!== !

!)()(

1TT

Page 7: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 13

linear transformation

• fundamental property

• can be represented in matrix form

• other properties– maps origin to origin– maps lines to lines– parallel lines remain parallel– length ratios are preserved– closed under composition

)()()( qpqp XXX !"!" +=+

pp MX =)(

computer graphics • transforms © 2008 fabio pellacini • 14

uniform scale

!"

#$%

&=!

"

#$%

&!"

#$%

&=

y

x

y

x

s sp

sp

p

p

s

sS

0

0p

ssSS/1

1

=!

Page 8: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 15

non-uniform scale

!"

#$%

&=!

"

#$%

&!"

#$%

&=

yy

xx

y

x

y

x

ps

ps

p

p

s

sS

0

0ps

)/1,/1(

1

yx ssSS =!

s

computer graphics • transforms © 2008 fabio pellacini • 16

rotation

!"

#$%

&

+

'=!

"

#$%

&!"

#$%

& '=

((

((

((

(((

cossin

sincos

cossin

sincos

yx

yx

y

x

pp

pp

p

pR p

!! "

"= RR

1

Page 9: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 17

shear

!"

#$%

&

+

+=!

"

#$%

&!"

#$%

&=

yxy

yxx

y

x

y

x

pps

psp

p

p

s

sSh

1

1ps

computer graphics • transforms © 2008 fabio pellacini • 18

reflection

!"

#$%

&'=!

"

#$%

&!"

#$%

&'=

y

x

y

x

x p

p

p

pRl

10

01p

!"

#$%

&

'

'=!

"

#$%

&!"

#$%

&

'

'=

y

x

y

x

o p

p

p

pRl

10

01p

...=pyRl

Page 10: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 19

combining translation and lineartransforms• represent linear together with translation

– rigid body transformation are a subset of this

• goal: unified format for all transformations

tppt

+=MXM

)(,

computer graphics • transforms © 2008 fabio pellacini • 20

homogeneous coordinates

• represent points with 1 additional coordinate w– set it to 1 for points

!!!

"

#

$$$

%

&

=

!!!

"

#

$$$

%

&

=

1

y

x

w

y

x

p

p

p

p

p

p

Page 11: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 21

homogeneous coordinates

• represent translation with a 3x3 matrix

• add one row and column to linear transforms

!!!

"

#

$$$

%

&

+

+

=

!!!

"

#

$$$

%

&

!!!

"

#

$$$

%

&

=

11100

10

01

yy

xx

y

x

y

x

tp

tp

p

p

t

t

T pt

!!!

"

#

$$$

%

&

+

+

=

!!!

"

#

$$$

%

&

!!!

"

#

$$$

%

&

=

11100

0

0

2221

1211

2221

1211

yx

yx

y

x

pmpm

pmpm

p

p

mm

mm

Mp

computer graphics • transforms © 2008 fabio pellacini • 22

affine transformations

• combining linear and translation in one matrix

• properties– does not map origin to origin– maps lines to lines– parallel lines remain parallel– length ratios are preserved– closed under composition

!"

#$%

&!"

#$%

&=+=

110

pttpp

MMX

Page 12: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 23

affine transformations

!!!

"

#

$$$

%

&

=

100

10

01

y

x

t

t

Tt

!!!

"

#

$$$

%

&

=

100

00

00

y

x

s

s

Ss

!!!

"

#

$$$

%

& '

=

100

0cossin

0sincos

((

((

(R

translation

scale

rotation

computer graphics • transforms © 2008 fabio pellacini • 24

compositing transformations

• applying one transformation after another– expressed by function composition

• for the transforms presented before,computed by matrix multiplication

))(())((' 1212 ppp XXXX o==

))(()())(())(( 12121212 pppp MMMMXXXX ===o

Page 13: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 25

compositing transformations

• translation

• linear transformations

• affine transformations

!"

#$%

&!"

#$%

& +=!

"

#$%

&!"

#$%

&!"

#$%

&

11011010

2112pttptt III

!"

#$%

&!"

#$%

&=!

"

#$%

&!"

#$%

&!"

#$%

&

110

0

110

0

10

01212

pp MMMM

!"

#$%

&!"

#$%

& +=!

"

#$%

&!"

#$%

&!"

#$%

&

11011010

212121122pttptt MMMMM

computer graphics • transforms © 2008 fabio pellacini • 26

compositing transformations

• composition is not commutative

rotate, then translate

translate, then rotate

Page 14: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 27

compositing transformations

• composition is not commutative

rotate, then translate

translate, then rotate

computer graphics • transforms © 2008 fabio pellacini • 28

compositing transformations

• composition is not commutative

rotate, then translate

translate, then rotate

Page 15: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 29

complex transformation

• often represented as combination of simpler ones– intuitive geometric interpretation

• rotation around arbitrary axis– translate to axis center– rotate– translate back

aaa != TRTR "",

computer graphics • transforms © 2008 fabio pellacini • 30

rotation around arbitrary axis

Page 16: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 31

rotation around arbitrary axis

computer graphics • transforms © 2008 fabio pellacini • 32

rotation around arbitrary axis

Page 17: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 33

rotation around arbitrary axis

computer graphics • transforms © 2008 fabio pellacini • 34

transforming points and vectors

• points and vectors are different entities– vectors: encode direction (difference of points)– points: encode position (origin plus a vector)

• points and vector transform differently– we have shown how points transforms previously– vectors simply ignore the translation

)()()()()()(

)(

qptqtpqpv

tpp

qpv

!=+!+=!=

+=

!=

MMMXXX

MX

Page 18: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 35

transforming points and vectors

• use homogeneous coordinates with w=0

• everything is consistent!

• but what is that “w” anyway?

!"

#$%

&=!

"

#$%

&!"

#$%

&

0010

vvt MM

computer graphics • transforms © 2008 fabio pellacini • 36

homogeneous coordinates

• points– will become useful later on

• vectors

!!!

"

#

$$$

%

&

'

!!!

"

#

$$$

%

&

'

w

wp

wp

p

p

ppy

x

y

x

yx

1

),(

!!!

"

#

$$$

%

&

'

0

),(y

x

yxv

v

vv

Page 19: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 37

coordinate systems review

• points are represented wrt a coordinate system– cartesian coordinates in the canonical coord. system

• canonical coordinate system

ypyxpxoyxopp )()(),( !+!+=++="=yxyxpppp

)1,0(

)0,1(

)0,0(

=

=

=

y

x

o

computer graphics • transforms © 2008 fabio pellacini • 38

coordinate system review

• write a point in a new coordinate system

• can be represented as an affine matrix multiply

'''''')'(')'(')','(' yxoypyxpxoppyxyx

pppp ++=!+!+="=

!"

#$%

&!"

#$%

&=!

"

#$%

&=!

"

#$%

&

!!!

"

#

$$$

%

&

!!!

"

#

$$$

%

&

=

!!!

"

#

$$$

%

&

=

!!!

"

#

$$$

%

&

1

'

100

'''

1

'

1

1

'

'

100

'''

'''

1

'

'

1

poyxppM

p

p

oyx

oyx

p

p

Mp

p

y

x

yyy

xxx

y

x

y

x

Page 20: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 39

coordinate system review

• an affine transform is a change of coord. system– another interpretation for combination of transforms

• what is the matrix I should use to change coord?– just invert previous definition

• i.e. invert combination of translation and orthonormal

!!!

"

#

$$$

%

&

'

'

=

!"

#$%

&=!

"

#$%

&(!

"

#$%

&=!

"

#$%

&

'

'

100

'''

'''

11

'

1

'

1

1

1

yyx

xyx

oyy

oxx

M

MMpppp

computer graphics • transforms © 2008 fabio pellacini • 40

3D transformations

Page 21: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 41

2D to 3D transformations

• adopt homogeneous formulation in 3d– points have 4 coordinates– use 4x4 matrices for transformations

• most concept generalize very easily– rotation much more complex

computer graphics • transforms © 2008 fabio pellacini • 42

affine transformations

!!!!

"

#

$$$$

%

&

=

1000

100

010

001

z

y

x

t

t

t

Tttranslation

scale

!!!!

"

#

$$$$

%

&

=

1000

000

000

000

z

y

x

s

s

s

Ss

Page 22: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 43

affine transformations

!!!!

"

#

$$$$

%

& '

=

1000

0100

00cossin

00sincos

((

((

(zR

rotation around z

computer graphics • transforms © 2008 fabio pellacini • 44

affine transformations

!!!!

"

#

$$$$

%

&

'=

1000

0cos0sin

0010

0sin0cos

((

((

(yR

rotation around x

!!!!

"

#

$$$$

%

&

'=

1000

0cossin0

0sincos0

0001

((

(((xR

rotation around y

Page 23: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 45

rotation around arbitrary axis

• in 2d, rotation are around a point– change coordinate frame (translation)– rotate around the origin– change coordinate frame back– simple geometric construction

• in 3d, they are around an axis– change coordinate frame (align z with axis)– rotate around z axis– change coordinate frame back– complex geometric construction

aaa != TRTR "",

aaaFRFR !!

1

,

"=

computer graphics • transforms © 2008 fabio pellacini • 46

rotation around arbitrary axis

• use a change of coordinate system– define new coordinate system with z’ parallel to axis and

origin on the axis

• build transform matrix as seen previously

!"

#$%

&=

'

1000

''''1

ozyxF

Page 24: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 47

construct 3d frame from vectors

• given two non-parallel vectors a and b– i.e. a plane– set x, y parallel to a, b– x = a / |a|

z = x × b; z = z / |z|y = z × x

• given the vector a– set z parallel to a, choose arbitrary x, y– continue as above

computer graphics • transforms © 2008 fabio pellacini • 48

representing rotations

• 3 rotations around major axis– remember to choose order– simple but has quirks when combining rotations

• will use this for simplicity

• axis and angle– combinations of rotations can be represented this way– with more formalism become elegant and consistent

• quaternions

Page 25: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 49

transforming normals

• points and vectors works• tangents, i.e. differences of points, works too• normals works differently

– defined as orthogonal to the transformed surface– i.e. orthogonal to all tangents

computer graphics • transforms © 2008 fabio pellacini • 50

transforming normals

• by definition• after transform• for all t, we have• which gives

• normals are transformed by the inverse transpose

0==! ntntT

0)()( =nt XMT

0=nt XMTT

0)( 1=

!nt

TTTMM

Page 26: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 51

transformation hierarchies

• often need to transform an object wrt another– e.g. the computer on the table– when the table moves, the computer moves

• naturally build a hierarchy of transformation– to transform the table, apply its transform– to transform the computer, apply the table and the

computer transform

computer graphics • transforms © 2008 fabio pellacini • 52

transformation hierarchies

• represented as a tree data structure– transformation nodes– object nodes - leaves

• walk the tree when drawing• very convenient representation for objects

– all objects can be defined in their simplest form– e.g. every sphere can be represented by a

transformation applied to the unit sphere

Page 27: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 53

transformation hierarchies

root

transform transform

sphere triangle transform sphere

triangle sphere

computer graphics • transforms © 2008 fabio pellacini • 54

implementing transformation hierarchies

• transformation function for each node– get the parent matrix– multiply the parent and current matrices– pass the combined matrix when calling children

• stack of transforms– push/pop when walking down/up– used by graphics libraries (OpenGL)– more flexible– generalized mechanism for all attributes

Page 28: geometric transformations - Fabio Pellacinipellacini.di.uniroma1.it/.../lectures/04_Transforms_Web.pdf · 2017-09-12 · computer graphics • transforms © 2008 fabio pellacini •

computer graphics • transforms © 2008 fabio pellacini • 55

raytracing and transformations

• transform the object– simple for triangles

• since they transforms to triangles– but most objects require complex intersection tests

• spheres do not transforms to spheres, but ellipsoids

• transform the ray– much more elegant– works on any surface– allow for much simpler intersection tests

• only worry about unit sphere, all others are transformed

computer graphics • transforms © 2008 fabio pellacini • 56

raytracing and transformations

• transforming rays– transform origin/direction as point/vector– note that direction is not normalized now

• i.e. ray parameter is not the distance

• intersect a transformed object– transform the ray by matrix inverse– intersect surface– transform hit point and normal by matrix