Saturday, September 4, 2010

Rotation Matrix

A rotation matrix is a matrix which, when multiplied by the vector representing a point, has the effect of rotating that point around the origin. A rotation around the origin is a linear geometrical transformation.
In general, any orthogonal matrix with a determinant of 1 is a rotation matrix.
In two dimensions, the 2×2 matrix that rotates a 2D vector around the origin by an angle of θ is:
R(θ)

    | cos(θ) -sin(θ) |
  = |                 |
    | sin(θ)  cos(θ) |
(Note: the matrix should be multiplied from the right by a column vector.)
As with any other linear geometrical transformation, rotations may be composed by multiplying the matricies (see matrix multiplication) that represent them.
Rotations in 2D commute. That is, the rotation of a column vector v by θ1 and followed by the rotation by θ2 has the same effect as a single rotation by θ12. Of course, this seems obvious in 2D. In matrix form, this means that:
R2)(R1)v) = R12)v
The vectors forming the columns of the matrix are what the unit vectors along the axes are transformed to. In three dimensions, rotation matricies are more complex. It's easiest to start with rotations around the three coordinate axes:
Rx(θ)


    | 1    0       0    |
  = | 0 cos(θ) -sin(θ) |
    | 0 sin(θ)  cos(θ) |
Ry(θ)

    |  cos(θ) 0 sin(θ) |
  = |     0    1    0   |
    | -sin(θ) 0 cos(θ) |
Rz(θ)

    | cos(θ) -sin(θ) 0 |
  = | sin(θ)  cos(θ) 0 |
    |    0       0    1 |
Here, Rx(θ) represents a rotation of θ around the x axis. Rotation about an arbitrary axis u is given by the following formula:
Ru(θ)


  = uuT + cos(θ) (I - uuT) + sin(θ) cross(u)
where u = [ ux uy uz ]T is a unit column vector and cross(u) is the cross product operator matrix. In addition, I is the 3×3 identity matrix, and the 3×3 matrix uuT is a projection matrix. Expanding the formula into a matrix, we get
|  uxuxvθ+cθ  uxuyvθ-uzsθ uxuzvθ+uysθ |
  = | uyuxvθ+uzsθ  uyuyvθ+cθ  uyuzvθ-uxsθ |
    | uzuxvθ-uysθ uzuyvθ+uxsθ  uzuzvθ+cθ  |
where vθ = 1-cos(θ), cθ = cos(θ) and sθ = sin(θ). Given an rotation matrix, you can extract the axis u and angle θ of the rotation that it represents. Suppose the rotation matrix R is given by
R
    | r11 r12 r13 |
  = | r21 r22 r23 |
    | r31 r32 r33 |
Then we can calculate
cosθ = ( r11 + r22 + r33 - 1) / 2
sinθ = ( (r21-r12)2 + (r13-r31)2 + (r32-r23)2 )1/2 / 2
ux = (r21-r12) / (2sinθ)
uy = (r13-r31) / (2sinθ)
uz = (r32-r23) / (2sinθ)
Note that u is undefined if θ is a multiple of 2π. That would mean that there actually is no rotation. Rotations in 3D do not commute. For example, a 90° rotation around the x axis followed by a 90° rotation around the z axis does not have the same effect as a 90° rotation around the z axis followed by a 90° rotation around the x axis.

No comments: