2iv60 computer graphics set 4:3d transformations and hierarchical modeling jack van wijk tu/e
TRANSCRIPT
From 2D to 3D
• Much +/- the same:– Translation, scaling– Homogeneous vectors: one extra coordinate– Matrices: 4x4
• Rotation more complex
3D Translation 1
Translate over vector (tx, ty, tz ):
x’=x+ tx, y’=y+ ty , z’=z+ tz
or
x
y
P
P+T
H&B 9-1:304-305
T
z
z
y
x
t
t
t
z
y
x
z
y
x
TPP
TPP'
en ,
'
'
'
'
met ,
3D Translation 2
In 4D homogeneous coordinates:
x
y
P
P+T
H&B 9-1:304-305
T
z
11000
100
010
001
1
'
'
'
or ,
z
y
x
t
t
t
z
y
x
z
y
x
MPP'
3D Rotation 1
z
P
H&B 9-2:305-313
x
yP’
1000
0100
00cossin
00sincos
)(
with,)(
Or
'
cossin'
sincos'
:as- around angleover Rotate
z
z
zz
yxy
yxx
z
R
PRP'
3D Rotation 2
z
x
y
Rotation around axis:- Counterclockwise, viewed from rotation axis
z
x
y z
x
y
H&B 9-2:305-313
3D Rotation 3
z
x
yz
x
y z
x
y
zy
yx
xz
xz
zy
yx
Rotation around axes:Cyclic permutation coordinate axes
xzyx H&B 9-2:305-313
3D Rotatie 4
zy
yx
xz
H&B 9-2:305-313
1000
0100
00cossin
00sincos
)(
with,)(
Or
'
cossin'
sincos'
:as- around angleover Rotate
z
z
zz
yxy
yxx
z
R
PRP'
1000
0cossin0
0sincos0
0001
)(
with,)(
Or
'
cossin'
sincos'
:as- around angleover Rotate
x
x
xx
zyz
zyy
x
R
PRP'
1
3D Rotation around arbitrary axis 1
z
P
x
yP’
Q
23
H&B 9-2:305-313
Q)P)T((T(Q)RP'
Q
Q
Q
z :Or
.over back Translate 3.
axis;- around Rotate 2.
;over Translate 1.
:rotation 2D asSimilar as.- theexample.For
.point through axis, coordinate toparallel axis, aroundRotation
z
-
z
z x
y
1P
2P
3D Rotation around arbitrary axis 2 Rotation around axis through two points P1 and P1 .More complex:1.Translate such that axis goes through origin;2.Rotate…3.Translate back again.
H&B 9-2:305-313
3D Rotation around arbitrary axis 3
z x
y
1P
2P
Initialz x
y
'1P
1. translate axis
'P2
z x
y
2. rotate axis
'1P
''P2
z x
y
3. rotate aroundz-axis
'1P
''P2
4. rotate backz x
y
'1P
'P2
z x
y
1P
2P
5. translate back
3D Rotation around arbitrary axis 3
z x
y
1P
2P
Initialz x
y
'1P
1. translate axis
'P2
z x
y
2. rotate axis
'1P
''P2
z x
y
3. rotate aroundz-axis
'1P
''P2
4. rotate backz x
y
'1P
'P2
z x
y
1P
2P
5. translate back
T(P1) R
Rz()R1 T(P1)
3D Rotation around arbitrary axis 3
z x
y
1P
2P
Initialz x
y
'1P
1. translate axis
'P2
z x
y
2. rotate axis
'1P
''P2
z x
y
3. rotate aroundz-axis
'1P
''P2
4. rotate backz x
y
'1P
'P2
z x
y
1P
2P
5. translate back
T(P1) R
Rz()R1 T(P1)
M = T(P1) R1Rz() RT(P1)
3D Rotation around arbitrary axis 4
z x
y
'1P
'P2
z x
y2. rotate axis
'1P
''P2
Difficult step:
Find rotation such that rotation axis aligns with z-axis.Two options:1. Step by step by step (see H&B, 309-312)2. Direct derivation of matrix
R
H&B 9-2:305-313
3D Rotation around arbitrary axis 5
z
x
yu
v
w
1. Construct orthonormal axis frame u, v, w;
2. Invent rotation matrix R, such that:
u is mapped to z-axis; v is mapped to y-axis; w is mapped to x-axis.
H&B 9-2:305-313
3D Rotation around arbitrary axis 6
z
x
yu
v
w
Construct orthonormal axis frame u, v, w:
u = (P2P1) / |P2P1|v = u (1,0,0) / | u (1,0,0) |w = v u(If u = (a, 0, 0), then use (0, 1, 0))
This frame is orthonormal:Unit length axes: u.u = v.v = w.w = 1All axes perpendicular: u.v = v.w = w.u = 0
H&B 9-2:305-313
3D Rotation around arbitrary axis 7
z
x
yu
v
w
H&B 9-2:305-313
1
0
0
1
1
and ,
1
0
1
0
1
,
1
1
0
0
1
such that in Fill
1000
0
0
0
:matrixrotation Generic
333231
232221
131211
z
y
x
z
y
x
z
y
x
w
w
w
v
v
v
u
u
u
rrr
rrr
rrr
RRR
R
R
3D Rotation around arbitrary axis 8
z
x
yu
v
w
Done! But how to find R1 ?
ind
H&B 9-2:305-313
1000
0
0
0
:Solution
1
0
0
1
1
en ,
1
0
1
0
1
,
1
1
0
0
1
such that , Fill
zyx
zyx
zyx
z
y
x
z
y
x
z
y
x
uuu
vvv
www
w
w
w
v
v
v
u
u
u
R
RRR
R
zzz
yyy
xxx
wvu
wvu
wvu
wvuM
Each rotation matrix is an orthonormal matrix M:
The frame u, v, w is orthonormal:Unit length axes: u.u = v.v = w.w = 1All axes perpendicular: u.v = v.w = w.u = 0.
Requested: M-1 such that M-1M = I
Inverse of rotation matrix 1
zx
yu
v
w
H&B 9-2:305-313
Requested: M-1 such that M-1M = ISolution:
In words:The inverse of a rotation matrix is the transpose.(For a rotation around the origin).
zyx
zyx
zyx
T
T
T
T
www
vvv
uuu
1
w
v
u
MM
Inverse of rotation matrix 2
H&B 9-2:305-313
I
wwvwuw
wvvvuv
wuvuuu
wvu
w
v
u
MM
100
010
001
T
T
T
T
Requested: M-1 such that M-1M = ISolution: M-1 = M-T
Check:
Inverse of rotation matrix 3
H&B 9-2:305-313
3D Rotation with quaternions
H&B 9-2:313-317
Quaternions:• Extension of complex numbers• Four components• Scalar value + 3D vector: q=(s,v)• Special calculation rules• Compact description of rotations• To be used if many complex rotations have to be done (esp. animation)
11000
000
000
000
1
'
'
'
or ,
z
y
x
s
s
s
z
y
x
z
y
x
SPP'
3D scaling
Scale with factors sx, sy,sz :
x’= sx x, y’= sy y, z’= sz z
or
H&B 9-3:317-319
More 3D transformations+/- same as in 2D:
• Matrix concatenation by multiplication• Reflection• Shearing• Transformations between coordinate systems
H&B 9-4, 9-5, 9-6:319-324
zzzzyzx
yyzyyyx
xxzxyxx
bzayaxaz
bzayaxay
bzayaxax
'
'
'
Affine transformations 1
Generic name for these transformations:affine transformations
H&B 9-7:324
Affine transformations 2
Properties:1. Transformed coordinates x’,y’ and z’ are linearly
dependent on original coordinates x, y en z.
2. Parameters aij en bk are constant and determine type of transformation;
3. Examples: translation, rotation, scaling, reflection4. Parallel lines remain parallel5. Only translation, rotation reflection: angles and
lengths are maintained
H&B 9-7:324
Hierarchical modeling 1
ComplexObject::= Composition of Objects
Object::= SimpleObject or ComplexObject
H&B 11:383-391
puppet
head torso arms legs
left arm right arm
lower armupper arm hand
Hierarchical modeling 2
Hierarchical model: tree structure …
puppet
head torso arms legs
left arm right arm
lower armupper arm handH&B 11:383-391
Hierarchical modeling 3
Hierarchical model: tree structure or
directed, acyclic graph puppet
head torso arms legs
left arm right arm
lower armupper arm handH&B 11:383-391
Hierarchical modeling 4
Leaf: primitive object
geometric object, possibly parametrised
Composite node: instruction for composition (usually union)
Leafs, nodes and/or edges:
transformations + other data
H&B 11:383-391
Hierarchical modeling 5
Local coordinates: coordinates of node
train
world
wheel
world coordinatestrain coordinateswheel coordinates
H&B 11:383-391
Hierarchical modeling 6
Implementation: Many variations possible.
trein
wereld
wiel
DrawWorld DrawTrain(P1, S1); DrawTrain(P2, S2);
DrawTrain(P, S); Translate(P); Scale(S); DrawChimney(); … DrawWheel(W1); DrawWheel(W2); DrawWheel(W3); Scale(1/S); Translate(-P);
DrawWheel(W); Translate(W); DrawCircle(radius); Translate(-W); H&B 11:383-391
Hierarchical modeling 7Use structure of model to structure implementation:- Hierarchy (use classes, procedures and functions);- Regularity (use loops);- Variation (use conditions).
How many lines of code you need for this picture?
H&B 11:383-391
Hierarchical modeling 7DrawTwoGrids DrawGrid; Translate(7,0); DrawGrid; DrawGrid for i := 1 to 5 do for j := 1 to 5 do DrawCell(i, j, (i+j) mod 2 = 0);
DrawCell(x, y, use_red) SetColor(dark_grey); DrawRect(x+0.2,y, 0.7, 0.7); if use_red then SetColor(red) else SetColor(light_grey); DrawRect(x, y+0.2, 0.7, 0.7);
How many lines of code you need for this picture?
Mwah, 10-20 should do
H&B 11:383-391