3D Transformations
3D Transformations
x' 1 0 0 t x x P
x
y' 0 1 0 t y y
z' = 0 0 1 t z z
1 0 0 1 1
z
0
H&B 9-1:304-305
3D Rotation 1
Rotate over angle a around z - as :
x' = x cosa − y sin a
y ' = x sin a + y cosa z
y
z' = z P’
Or a
P' = R z (a )P, with P
cosa − sin a 0 0 x
sin a cosa 0 0
R z (a ) =
0 0 1 0
0 1
0 0 H&B 9-2:305-313
3D Rotation 2
z y z y z y
x x x
H&B 9-2:305-313
3D Rotation 3
z→x z x→ y
z y y z y
x→ y y→z
y→z z→x
x x x
H&B 9-2:305-313
3D Rotation around arbitrary axis 1
Rotation around axis, parallel to coordinate axis, through point Q.
For example. the z - as. Similar as 2D rotation :
1. Translate over -Q;
2. Rotate around z - axis;
2
3 z
3. Translate back over Q.
Or : P’ y
P' = T(Q)R z (a )T( −Q)P a
1 Q P
x
H&B 9-2:305-313
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.
y P2
P1
z x
H&B 9-2:305-313
3D Rotation around arbitrary axis 3
y P2 y y
P2'
P1 P1' P1'
x x z x
z z ''
P
2
P1' P1' P1
z x x
''
P
2 z z x
3. rotate around 4. rotate back 5. translate back
z-axis
3D Rotation around arbitrary axis 3
y P2 y y
P2'
P1 T(−P1) '
R
P' P
1
1
x x z x
z z ''
P
2
P1 T(−P1) '
R
P' P
1
1
x x z x
z z ''
P
2
y y
P2'
2. rotate axis
'
P P1'
1
x z x
z P''
2
u is mapped to z-axis; y
u
v is mapped to y-axis; v
w is mapped to x-axis. w
x
z
H&B 9-2:305-313
3D Rotation around arbitrary axis 6
Construct orthonormal axis frame u, v, w:
u = (P2−P1) / |P2−P1|
v = u (1,0,0) / | u (1,0,0) | y
w=vu u
v
(If u = (a, 0, 0), then use (0, 1, 0)) w
x
This frame is orthonormal: z
Unit length axes: u.u = v.v = w.w = 1
All axes perpendicular: u.v = v.w = w.u = 0
H&B 9-2:305-313
3D Rotation around arbitrary axis 7
Generic rotation matrix :
y
r11 r12 r13 0 u
v
r21 r22 r23 0
R = w
r r r33 0 x
31 32
0 1
0 0 z
Fill in R such that
ux 0 vx 0 wx 1
u y 0 vy 1 wy 0
R = , R = , and R =
uz 1 vz 0 wz 0
1 1 1 1 1 1
H&B 9-2:305-313
3D Rotation around arbitrary axis 8
Fill R, such that
ux 0 vx 0 wx 1
uy 0 vy 1 wy 0
R = , R = , and R =
u 1 v 0 w 0
z z z
1 1 1 1 1 1
y
u
Solution :
v
wx wy wz 0 w
x
vx vy vz 0
R =
0
z
ux uy uz
0 1
0 0
Done! But how to find R−1 ? H&B 9-2:305-313
Inverse of rotation matrix 1
Each rotation matrix is an orthonormal matrix M:
ux vx wx
M = (u v w ) = u y vy wy y
u u
z vz wz v
w
The frame u, v, w is orthonormal: x
Unit length axes: u.u = v.v = w.w = 1 z
All axes perpendicular: u.v = v.w = w.u = 0.
uT u u u v u w 1 0 0
M M = v (u v w ) = v u v v v w = 0 1 0 = I
T T
T w u w v w w 0 0 1
w
H&B 9-2:305-313
3D Rotation with quaternions
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)
H&B 9-2:313-317
3D scaling
Scale with factors sx, sy,sz :
x’= sx x, y’= sy y, z’= sz z
or
P' = SP , or
x' s x 0 0 0 x
y' 0 sy 0 0 y
z' = 0 0 sz 0 z
1 0 1 1
0 0
H&B 9-3:317-319
More 3D transformations
+/- same as in 2D:
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, reflection
4. Parallel lines remain parallel
5. 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
puppet
puppet
H&B 11:383-391
Hierarchical modeling 5
Local coordinates: coordinates of node
wheel
H&B 11:383-391
Hierarchical modeling 6
Implementation: Many variations possible.
DrawWorld
DrawTrain(P1, S1);
wereld DrawTrain(P2, S2);
DrawTrain(P, S);
trein Translate(P); Scale(S);
DrawChimney(); …
DrawWheel(W1); DrawWheel(W2); DrawWheel(W3);
Scale(1/S); Translate(-P);
DrawWheel(W);
wiel Translate(W); DrawCircle(radius); Translate(-W);
H&B 11:383-391
Hierarchical modeling 7
Use 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 7
DrawTwoGrids
DrawGrid;
Translate(7,0);
DrawGrid;
DrawGrid
for i := 1 to 5 do
for j := 1 to 5 do
How many lines of code you DrawCell(i, j, (i+j) mod 2 = 0);
need for this picture?
DrawCell(x, y, use_red)
SetColor(dark_grey);
Mwah, 10-20 should do DrawRect(x+0.2,y, 0.7, 0.7);
if use_red then SetColor(red)
else SetColor(light_grey);
Aim for as clean as possible, DrawRect(x, y+0.2, 0.7, 0.7);
not as compact as possible
H&B 11:383-391
Next…
• We know how to transform objects
• Next step: Viewing objects