M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Trasf. di rotazione (in 2D)
(di un angolo β )
+
= −
β β
β β
cos sin
sin cos
y x
y x
y f x
x
(x’,y’) (x,y)
x y
ρ α β
x
y y
Trasf. di rotazione (in due D)
(di un angolo β )
(x’,y’) (x,y)
x y
z
ρ α β
x y
x y
+
= −
β β
β β
cos sin
sin cos
y x
y x
y
f x
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Rotazione attorno all'asse z
(x’,y’)
(x,y)
x y
z
α ρ
α ρ
sin cos
=
= y x
partenza:
) sin(
) cos(
β α ρ
β α ρ
+
′ =
+
′ = y
arrivo: x
ρ α
β
β β
β β
cos sin
sin cos
y x
y x
+
=
−
= β α ρ β α ρ
β α ρ β α ρ
cos sin sin
cos
sin sin cos
cos
+
=
−
=
rimane invariata
z
• Cioè quelle lineari
• Cioè
– coi vettori:
– coi punti:
• Tutte quelle cha abbiamo visto appartengono a questa categoria
– Quali altre?
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
q = (p) v = (u) Trasformazioni AFFINI
p
f f f
f
q
f
) ( )
( )
( v 0 v 1 f v 0 f v 1
f α + β = α + β
) ( )
( )
( p v f p f v
f + α = + α
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Trasformazioni Affini (o lineari)
• Preservano:
– la colinearità
• tutti i punti
inizialmente su una linea giacciono ancora
su di una linea
dopo la trasformazione
– i rapporti tra le distanze
• il punto di mezzo di un segmento rimane il punto di mezzo di un segmento anche dopo la trasformazione.
– il parallelismo fra rette
in generale non preservano:
distanze, angoli, aree
se lo fanno, le chiamiamo
“trasf. Rigide”
“isometrie” o
Rappresentazione di punti e vettori in coordinate omogenee
= 1 z y x p
= 0 z y x v r
Punti: Vettori:
1 0
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Trasformazioni Affini
• Si possono tutte esprimere come moltiplicazione con matrice
sempre
=
1 1 0 0 0 1
3 2 1
34 33 32 31
24 23 22 21
14 13 12 11
3 2 1
γ γ γ
α α α α
α α α α
α α α α
γ γ γ f
coordinate omogenee punto di partenza
= 1
3 2 1
δ δ δ
coordinate omogenee punto di arrivo
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
conta solo questo
Trasformazioni Affini
• Si possono esprimere come moltiplicazione con matrice
sempre
=
1 1 0 0 0 1
3 2 1
34 33 32 31
24 23 22 21
14 13 12 11
3 2 1
γ γ γ
α α α α
α α α α
α α α α
γ γ γ f
coordinate omogenee vettore di partenza
= 1
3 2 1
δ δ δ
coordinate omogenee vettore di arrivo
0 0 ... 0
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Esempio: trasformazione di traslazione rigida
+
=
′
′
′
0 1 1
z y x
z y x
z y x
α α α
=
1 1 0 0 0
1 0 0
0 1 0
0 0 1
1
z y x
z y x f
z y x
α α α posso riscriverla come:
e cioè:
+ + +
=
1 1
z y x
z y x
z y x
f α
α α
vettore di traslazione
Trasformazione di Traslazione rigida
l'inversa è ovviamente:
=
1 0 0 0
1 0 0
0 1 0
0 0 1 ) , , (
z y x
z y
x
α
α α α
α α T
−
−
−
=
−
−
−
− =
1 0 0 0
1 0 0
0 1 0
0 0 1 ) , , ( ) , ,
1 (
z y x
z y x z
y
x
α
α α α
α α α
α
α T
T
matrice di
traslazione:
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Trasformazione di Traslazione rigida
cosa succede se la applico ad un vettore ?
=
1 1 0 0 0
1 0 0
0 1 0
0 0 1
0
z y x
z y x f
z y x
α α α
0 0
= 0 z y x
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
) ( γ S
matrice di scaling
Trasformazione di Scalatura uniforme
x y
x y
=
1 1
z y x
z y x
f γ
γ γ
=
1 1 0 0 0
0 0
0
0 0 0
0 0 0
1
z y x
z y x
f γ
γ
γ
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Trasformazione di Scalatura generica
x y
x y
=
1 1
z y x
z y x f
z y x
γ γ γ
) , ,
( γ
xγ
yγ
zS
matrice di scaling
=
1 1 0 0 0
0 0
0
0 0 0
0 0 0
1
z y x
z y x f
z y x
γ γ γ
inversa?
Trasf. di rotazione attorno all'asse z
(di un angolo β )
+
−
=
z y x
y x
z y x
f β β
β β
cos sin
sin cos
x
(x’,y’) (x,y)
x y
z
ρ α β
x
y y
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Rotazione attorno all'asse z
(x’,y’)
(x,y)
x y
z
ρ
z z
y x
y
y x
x
=
+
=
−
′ =
'
cos sin
'
sin cos
β β
β β
=
1 0 0 0
0 1 0 0
0 0 cos sin
0 0 sin - cos )
( β β
β β
Z
θ R
= +
=
′
′
′
1 cos sin
sin - cos
1 ) ( 1
z y x
y x
z y x z R
y x
Z
β β
β β
β
α β
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Rotazione attorno all'asse x , y , o z
=
1 0 0 0
0 1 0 0
0 0 cos sin
0 0 sin - cos )
( θ θ
θ θ
Z
θ R
=
1 0 0
0
0 cos sin
0
0 sin - cos 0
0 0 0
1 )
( θ θ
θ θ θ
R
X
=
1 0 0 0
0 cos 0 sin -
0 0 1 0
0 sin 0 cos )
( θ θ
θ θ
Y
θ R
e le inverse?
T
1 ( ) ( )
)
( θ
Xθ
Xθ
X
R R
R − = − =
da XKCD
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
http://xkcd.com/184/
Shearing
• Es: spostamento in x proporzionale alla pos y
=
′ =
′ = +
′ =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 cot 1 ) (
cot
θ θ
θ
H
xyz z
y y
y x x
matrice di shearing
Comporre trasformazioni
• Le trasf affini sono chiuse per composizione.
…infatti…
• La moltiplicazione e’ associativa!
B·(A·x) = (A·B)·x
– La matrice A «fa» una trasf a – La matrice B «fa» una trasf b
– La matrice (A*B) «fa» la trasf b , seguita dalla a
• Due (o N ) trasformazioni al prezzo di una!
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Ripassino: moltiplicazione matrice matrice
• Attenzione all'inversione: (AB) -1 = B -1 A -1
• Associativa si, ma commutativa no!
AB ≠ BA
• previsione:
determinare il corretto ordine delle trasformazioni non sarà intuitivo
x y
x y
RT TR
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Rotazioni generiche
• Una rotazione generica è definita da:
– angolo u , – asse v
– punto di applicazione p f
• come si fa?
vediamo prima:
rotaz attorno ad assi paralleli all’asse delle X
Rotazione intorno ad un asse parallelo all'asse z
x y
z x
y z
y
traslazione T -1
y
traslazione T
1
2
3
1. Porto il centro di rot nell'origine 2. Ruoto
3. Rimetto a posto
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Rotazione intorno ad un asse parallelo all'asse z
x y
z x
y
z
f( p ) T ( R ( T = -1 p ) )
x y
traslazione T -1
rotazione R x
y
traslazione T
1
2
3
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Composizione di trasformazioni
• Moltiplicazione matrici (vettori) ha la propretà associativa
f( p ) = T ( R ( T -1 p ) )
= (T R T -1 ) p
una matrice M 4x4 che fa tutto.
• considerazioni sull'efficienza
• cosa possiamo dire sulla forma di M ?
• cosa succede se moltiplichiamo un vettore per M ?
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Punti VS vettori
x y
z x
y
z
M
p M( p )
v M( v )
p = ( * , * , * , 1 ) punto all'angolo della casa (punto) v = ( * , * , * , 0 ) velocità vettoriale del fumo (vettore)
Rotazioni generiche
• Una rotazione generica è definita da:
– angolo u , – asse v
– punto di applicazione p f
• come si fa?
Ripasso: prodotto Matrice – Vettore
• “riga x colonna”
• cioè prodotti dot con vettori righe
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
· =
A
D C B
·
·
·
·
A
B
C
D
Ripasso: prodotto Matrice – Vettore
• “riga x colonna”
• cioè combinazione lineare di vettori colonna
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
· =
x z y w
x · + y · + z · + w ·
A B C D A B C D
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Ripasso: Sistema di riferimento (o frame )
• Dato da:
– Assi x, y, z (vettori) – Origine (un punto)
• Un sist di rif. può essere:
– ortogonale
(se i vettori lo sono fra loro) (altrimenti: “obliquo”)
– ortonormale
(se i vettori della base sono ortogonali e normali)
• Frame “canonico”:
– origine (0,0,0)
– assi: (1,0,0), (0,1,0), (0,0,1)
Sistema di riferimento ( frame )
• Definito da
– un punto base (origine) p 0
– e una base vettoriale { v 0 , v 1 , v 2 }
• Posso esprimere (univocamente) ogni punto p come:
p = v 0 η 0 + v 1 η 1 + v 2 η 2 + p 0
coordinate omogenee di p lin indip
• cioè: p = v 0 v 1 v 2 p 0
η 0
η 1
η 2
1
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Sistema di riferimento ( frame )
• Definito da
– un punto base (origine) p 0
– e una base vettoriale { v 0 , v 1 , v 2 }
• Posso esprimere (univocamente) ogni vettore v come:
v = v 0 η 0 + v 1 η 1 + v 2 η 2 + p 0
coordinate omogenee di p
• cioè: v = v 0 v 1 v 2 p 0
η 0 η 1 η 2 0
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Chiralità di un frame («handedness»)
• Un sistema di riferimento ortogonale può essere immaginato sinistrorso o destrorso
sinistrorso
«left-handed»
destrorso
«right-handed»
z y x × =
• Ricordiamoci di usare la STESSA MANO per immaginare il prodotto cross
z y x × =
comunque!
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Cambio di frame
• tutte le transf. affini lineari si possono vedere come un cambio di frame
– comprese quelle viste:
• traslazione
• scaling (uniforme o no)
• shearing
• rotazioni
Cambio di frame
• ogni transf. affini si può vedere come
un cambio di sistema di riferimento
• le colonne della matrice descrivono – 3 assi
– origine
del sistema di partenza
descritte nelle coord del sist. di arrivo
Es: traslazione
1.5 2.3
p = (1.5, 2.3)
frame di Partenza
coordinate di p nel frame di Partenza x
y
x y
frame di arrivo
= (12.5, 8.1) 8.1
12.5
coordinate di p in frame di Partenza
1 1
11 5.8
=
1 0 . 0
1 . 8
5 . 12
1 0 . 0
3 . 2
5 . 1
1 0 0 0
0 . 0 1 0 0
8 . 5 0 1 0
11 0 0 1
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
matrice per passare dal frame di partenza al frame di arrivo
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
matrice di cambio di frame
Cambio di frame (cambio di sistema di riferimento)
• Dati due sistemi di riferimento: { v 1 , v 2 , v 3 , p 0 } { u 1 , u 2 , u 3 , q 0 }
0 3 43 2 42 1 41 0
3 33 2 32 1 31 3
3 23 2 22 1 21 2
3 13 2 12 1 11 1
p v v
v q
v v
v u
v v
v u
v v
v u
+ +
+
=
+ +
=
+ +
=
+ +
=
γ γ
γ
γ γ
γ
γ γ
γ
γ γ
γ
• Esprimo uno in termini dell’altro:
p = =
coordinate di p nel primo sist. di rif.:
coordinate di p nel sec. sist. di rif.:
=
1 1 0 0 0 1
3 2 1
43 33 23 13
42 32 22 12
41 31 21 11
3 2 1
b b b
a a a
γ γ γ γ
γ γ γ γ
γ γ γ γ
v 0 v 1 v 2 p 0
a 0 a 1 a 2 1
u 0 u 1 u 2 q 0
b 0
b 1
b 2
1
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 4 / 1 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
matrice di cambio di frame
Cambio di frame (cambio di sistema di riferimento) caso particlare: arrivo a sistema di riferimento canonico
• Dati due sistemi di riferimento: { v 1 , v 2 , v 3 , p 0 } { u 1 , u 2 , u 3 , q 0 }
) , , (
) , , (
) , , (
) , , (
23 22 21 0
23 22 21 3
23 22 21 2
13 12 11 1
γ γ γ
γ γ γ
γ γ γ
γ γ γ
=
=
=
=
q u u u
• Esprimo uno in termini dell’altro:
=
1 1 0 0 0 1
3 2 1
43 33 23 13
42 32 22 12
41 31 21 11
3 2 1
b b b
a a a
γ γ γ γ
γ γ γ γ
γ γ γ γ
u 1 u 2 u 3 q
p = =
coordinate di p nel primo sist. di rif.:
coordinate di p nel sec. sist. di rif.:
1 0 0
0 1 0
0 0 1
0 0 0
a 0 a 1 a 2 1
u 0 u 1 u 2 q 0
b 0 b 1 b 2 1
Transform
z
y
x v
0v
1v
2v
0v
1v
2 coordinate in cuisono definiti i vertici dell’oggetto (“object coords”)
screen Coordinates