Download Euler Transform

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Matrix calculus wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Covariance and contravariance of vectors wikipedia , lookup

Four-vector wikipedia , lookup

Matrix multiplication wikipedia , lookup

Orthogonal matrix wikipedia , lookup

Rotation matrix wikipedia , lookup

Transcript
CS 450: COMPUTER GRAPHICS
3D TRANSFORMATIONS
SPRING 2015
DR. MICHAEL J. REALE
INTRODUCTION
• So far, we’ve covered 2D transformations
• Now we will cover 3D transformations
•
Since we’re using homogeneous coordinates, we will use 4x4 matrices
• Overview:
•
Translation, Scaling, and Shear
•
Transforming to/from Another Basis
•
Rotation
•
•
Around the Standard Axes
•
Around an Arbitrary Axis
•
Using Euler Transform
Normal transform
AFFINE TRANSFORMS
• Affine transform = linear transformation followed by a translation
•
Parallel lines stay parallel after an affine transform
•
•
Does NOT necessarily preserve lengths and angles
Has form:
 a xx
a
M   yx
 a zx

0
a xy
a xz
a yy
a yz
a zy
a zz
0
0
bx 
by 
bz 

1
 a xx
a
B  M * A   yx
 a zx

0
a xy
a xz
a yy
a yz
a zy
a zz
0
0
bx   x   xaxx  ya xy  za xz  bx 
by   y   xayx  ya yy  za yz  by 

bz   z   xazx  ya zy  za zz  bz 
  

1  1  
1

• All translation, rotation, scaling, reflection, and shearing matrices are affine
TRANSLATION, SCALING, AND SHEAR
3D TRANSLATION
• The 3D translation matrix isn’t that far removed from the 2D variation:
• Example: move point 5 units in x, -7 units in y, and 3 units in z:
1
0
T (5,7,3)  
0

0
5
1 0  7 
0 1 3

0 0 1
0 0
1
0
B  T (5,7,3) * A  
0

0
1
0
T (t x , t y , t z )  
0

0
0 0 tx 
1 0 t y 
0 1 tz 

0 0 1
5   x   x  5
1 0  7  y   y  7

0 1 3   z   z  3
  

0 0 1  1   1 
0 0
3D SCALING
• The 3D scaling matrix too is very similar (it also only scales along the
standard axes):
sx
0
S (sx , s y , sz )  
0

0
0
0
sy
0
0
sz
0
0
• Example: make something twice as large in the z direction:
1
0
S (1,1,2)  
0

0
0 0 0
1 0 0
0 2 0

0 0 1
1
0
B  S (1,1,2) * A  
0

0
0 0 0  x   x 
1 0 0  y   y 

0 2 0  z   2 z 
   
0 0 1  1   1 
0
0
0

1
3D SHEAR
• The 3D shear matrices are a little more complicated
•
Six basic shear matrices  all shear in planes orthogonal to the standard axes
•
Example: shear in x direction based on value of z:
1
0
H xz ( s )  
0

0
0 s 0
1 0 0
0 1 0

0 0 1
• Shear matrices preserve volume (determinant equals 1)
1
0
B  H xz ( s ) * A  
0

0
0 s 0  x   x  sz 
1 0 0  y   y 

0 1 0  z   z 
  

0 0 1  1   1 
TRANSFORMING TO/FROM ANOTHER BASIS
CHANGING TO A NEW ORTHONORMAL BASIS IN 3D
• Assuming our basis (r, s, t) is orthonormal, we can change from the standard basis to the new basis
with:
r T   rx
 T 
M  s   sx
tT  tx
  
ry
sy
ty
rz 

sz 
t z 
• The 4x4 homogeneous matrix would be:
 rx
s
M  x
tx

0
ry
rz
sy
sz
ty
tz
0
0
0
0
0

1
CHANGING BACK TO THE STANDARD BASIS 3D
• To change BACK to the standard basis, remember that M is orthogonal (because ultimately it is a
rotation matrix):
M 1  M T  r
 rx
s t   ry
 rz
sx
sy
sz
• The 4x4 homogeneous matrix would be:
 rx
r
M 1  M T   y
 rz

0
sx
tx
sy
ty
sz
tz
0
0
0
0
0

1
tx 
t y 
t z 
ROTATION IN 3D: INTRODUCTION
3D ROTATION
• Rotation in 3D can be specified a number of ways:
•
Around the standard axes  about the x, y, and z axes
•
Around an arbitrary axes  will involve a composite transform with a change of base
•
Using Euler angles  using pitch, yaw, and roll angles
•
Using Quaternions
• Remember: all rotation matrices are orthogonal  R-1(θ) = RT(θ) = R(-θ)
ROTATION AROUND THE STANDARD AXES
3D ROTATION AROUND STANDARD AXES
• Rx(θ) rotation about x axis:
• Ry(θ)  rotation about y axis:
• Rz(θ)  rotation about z axis:
0
1
0 cos 
Rx ( )  
0 sin 

0
0
0
 sin 
cos 
0
 cos 
 0
R y ( )  
 sin 

 0
0
0
0

1
0 sin 
1
0
0 cos 
0
0
Extended version
of 2D rotation,
since in 2D we are
effectively
ALWAYS rotating
around the z axis
0
0
0

1
cos 
 sin 

z ( ) 
 0

 0
 sin 
cos 
0
0
0 0
0 0
1 0

0 1
ROTATION AROUND AN ARBITRARY AXIS
3D ROTATION AROUND ARBITRARY AXIS
• Given an axis r we want to rotate around:
•
Find two other vectors s and t to form an orthonormal basis:
•
Transform into that basis
•
Rotation about x axis (equivalent to r)
•
Transform back to standard basis
ALL YOUR BASIS ARE BELONG TO US
• Normalize r (if it isn’t already)
•
Find temporary s  find smallest component of r and set it to zero, swap two remaining components and negate one
of them (numerically stable):
(0,rz , ry ), if rx  ry and rx  rz

s  (rz ,0, rx ), if ry  rx and ry  rz
(r , r ,0), if r  r and r  r
z
x
z
y
 y x
s  (0,rz , ry )
s  s/ s
r  s  rx * 0  ry (rz )  rz (ry )  0
•
Normalize s:
•
Compute t using cross product:
•
r = “x axis”
s = “y axis”
t = “z axis”
t  rs
Note: s will be orthogonal to r.
For example, if rx was the smallest element
of r:
Note: t will be orthogonal to r and s
BASIS CHANGE, ROTATE, AND THEN BACK AGAIN
• Recall the matrix for doing a basis change (and undoing it):
 rx
s
M  x
tx

0
ry
rz
sy
sz
ty
tz
0
0
0
0
0

1
 rx
r
M 1  M T   y
 rz

0
• So, our complete rotation matrix X will be:
 rx
r
X  M T Rx ( ) M   y
 rz

0
sx
tx
sy
ty
sz
tz
0
0
0 1
0
0 0 cos 
0 0 sin 

1  0
0
0
 sin 
cos 
0
0  rx
0  s x
0  t x

1  0
ry
rz
sy
sz
ty
tz
0
0
0
0
0

1
sx
tx
sy
ty
sz
tz
0
0
0
0
0

1
ROTATION ABOUT AN ARBITRARY AXIS
 rx
r
X  M T Rx ( ) M   y
 rz

0
sx
tx
sy
ty
sz
tz
0
0
0 1
0
0 0 cos 
0 0 sin 

1  0
0
0
 sin 
cos 
0
0  rx
0  s x
0  t x

1  0
ry
rz
sy
sz
ty
tz
0
0
0
0
0

1
ROTATION USING EULER TRANSFORM
EULER TRANSFORM
• Euler Transform
•
Named after the great Swiss mathematician Leonhard Euler
(1707-1783)
•
Intuitively  camera like your head (or jet plane)
•
LOTS of different ways to formulate this, but all concatenation
of rotations
•
One common approach: y axis  then x axis  then z axis:
E (h, p, r )  Rz (r ) Rx ( p) R y (h)
•
Angle around y axis  h  “yaw” (or “head”)
•
Angle around x axis  p  “pitch”
•
Angle around z axis  r  “roll”
Note: roll going in wrong direction
in figure
Recall:
INVERSE OF EULER TRANSFORM
( AB)T  BT AT
• Euler transform  all rotations  orthogonal matrix!
E  E  Rz Rx Ry   R R R
1
T
• …or you can just get ET directly
T
T
y
T
x
T
z
PROBLEM: GIMBAL LOCK
•
•
Degrees of freedom (DOF) = how many variables you
need to describe movement
•
Train on track  one degree of freedom =
how far along track it is
•
Object in space 
3 translation + 3 rotation = 6 degrees of freedom!
Gimbal lock = when rotations are made such that we lose a degree of freedom
•
Example: using y / x / z ordering  set pitch (x axis rotation) to 90° (or π/2 radians)  y axis in line with z axis!
cos r cos h  sin r sin h 0 cos r sin h  sin r cos h 
E (h,  / 2, r )  sin r cos h  cos r sin h 0 sin r sin h  cos r cos h 


0
1
0
cos( r  h) 0 sin( r  h) 
  sin( r  h) 0  cos( r  h)


0
1
0
Only dependent on one angle (r+h)
ORDER OF ROTATIONS
• There are 24 different kinds of rotation orders one can use for the Euler transform:
•
z / x / y  animation
•
z / x / z  animation and physics
•
Only get gimbal lock with 180° rotation around x
• Unfortunately, NONE of them eliminate the possibility of gimbal lock
PROS AND CONS OF EULER TRANSFORM
• Pros:
•
Intuitive
•
•
E.g., nod head “yes”  change pitch up and down
Useful for small angle changes or view orientation
• Cons:
•
Gimbal lock
•
Interpolation between two different sets of Euler angles  nontrivial
•
Cannot just interpolate each angle
•
Two different sets of Euler angles can give SAME final rotation!
• For these reasons, we will explore quaternions instead…
GETTING EULER ANGLES FROM AN ORTHOGONAL
MATRIX
• If you have an orthogonal matrix F, you can get the Euler angles like this:
 f 00
F   f10
 f 20
f 01
f11
f 21
f 02 
cos r cos h  sin r sin p sin h  sin r cos p cos r sin h  sin r sin p cos h 
f12   Rz (r ) Rx ( p ) R y (h)  E (h, p, r )  sin r cos h  cos r sin p sin h cos r cos p sin r sin h  cos r sin p cos h 


f 22 
 cos p sin h
sin p
cos p cos h
f 21  sin p
f 01  sin r

  tan r
f11
cos r
f 20  sin h

  tan h
f 22
cos h
h  arctan(  f 20 , f 22 )
p  arcsin( f 21 )
•
Problems:
•
If f01 = f11 = 0  can’t use arctan 
however, can use simplified version of
F
•
If p outside internal of arcsin (+-90°),
cannot get p
•
In general, (h,p,r) are not unique!
•
Above approach can be numerically
unstable
r  arctan(  f 01, f11 )
NORMAL TRANSFORM
TRANSFORMING THE NORMAL
• There are cases wherein, if we blindly apply a transformation to the surface normal, it will not give us
the right answer
GENERAL CASE
• Given a transform M, the normal should be multiplied by the transpose of the matrix’s adjoint:
adj (M )
T
• Normal may have to be renormalized afterwards
• Can also use transpose of the inverse; however:
•
Can be overkill
•
Inverse may not exist; adjoint ALWAYS exists
Recall:
M
1
1

adj ( M )
M
SIMPLIFYING CASES
Just need this part if transform is affine:
• Expensive to compute 4x4 adjoint
• Translation will not affect normal (just a direction, not a point)
• If transformation is affine  just get adjoint of upper-left 3x3 submatrix
 a xx
a
M   yx
 a zx

0
a xy
a xz
a yy
a yz
a zy
a zz
0
0
bx 
by 
bz 

1
MORE SIMPLIFYING CASES
• If transform  only translations, rotations, UNIFORM scaling
•
Translation  no effect
•
UNIFORM scaling  only affects length of normal (we will renormalize anyway)
•
Rotations  transpose = inverse  so transpose of inverse  transpose of transpose  original matrix!
• So, can use original transformation!
• If transform  only translations and rotations
•
Don’t need to renormalize!
WHEN YOU DON’T NEED TO WORRY ABOUT THIS
• Note: If you’re just recomputing the normal for each triangle after transformation, then obviously this
isn’t an issue
• Also note: tangent vectors can just be transformed with everything else (they don’t need special
treatment)