Last active
March 28, 2019 11:20
-
-
Save fospathi/1bfbc5d5a1f1df588aa31732a8cd451e to your computer and use it in GitHub Desktop.
3D rotation matrices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Affine transformation matrices to rotate around (1) the X-axis; (2) the Y-axis; (3) the Z-axis; (4) an arbitrary 3D line. A | |
right-handed coordinate system is assumed. | |
1. X-axis | |
Z-axis | 'B A and B are two points in a YZ-plane | |
| ' ∠ AOB = β | |
| ' `A ∠ YOA = α | |
| ' ` |OA| = |OB| = r | |
| ' ` | |
_|_'_`_ _ _ _ _ _ _ | |
O/ Y-axis | |
/ | |
/ | |
/ X-axis | |
A = (y, z) | |
= (rcos{α}, rsin{α}) | |
Let R be a rotation from A to B through an angle β | |
R: (y, z) → (rcos{α+β}, rsin{α+β}) | |
→ (rcos{α}cos{β}-rsin{α}sin{β}, rsin{α}cos{β}+rcos{α}sin{β}) | |
→ (ycos{β}-zsin{β}, zcos{β}+ysin{β}) | |
Thus, we require a matrix M such that | |
|x| | x | | |
M |y| = | ycos{β}-zsin{β} | | |
|z| | zcos{β}+ysin{β} | | |
|1| | 1 | | |
Such an affine matrix is | |
| 1 0 0 0 | | |
M = | 0 cos{β} -sin{β} 0 | | |
| 0 sin{β} cos{β} 0 | | |
| 0 0 0 1 | | |
2. Y-axis | |
X-axis | 'B A and B are two points in a ZX-plane | |
| ' ∠ AOB = β | |
| ' `A ∠ ZOA = α | |
| ' ` |OA| = |OB| = r | |
| ' ` | |
_|_'_`_ _ _ _ _ _ _ | |
O/ Z-axis | |
/ | |
/ | |
/ Y-axis | |
A = (z, x) | |
= (rcos{α}, rsin{α}) | |
Let R be a rotation from A to B through an angle β | |
R: (z, x) → (rcos{α+β}, rsin{α+β}) | |
→ (rcos{α}cos{β}-rsin{α}sin{β}, rsin{α}cos{β}+rcos{α}sin{β}) | |
→ (zcos{β}-xsin{β}, xcos{β}+zsin{β}) | |
Thus, we require a matrix M such that | |
|x| | xcos{β}+zsin{β} | | |
M |y| = | y | | |
|z| | zcos{β}-xsin{β} | | |
|1| | 1 | | |
Such an affine matrix is | |
| cos{β} 0 sin{β} 0 | | |
M = | 0 1 0 0 | | |
| -sin{β} 0 cos{β} 0 | | |
| 0 0 0 1 | | |
3. Z-axis | |
Y-axis | 'B A and B are two points in an XY-plane | |
| ' ∠ AOB = β | |
| ' `A ∠ XOA = α | |
| ' ` |OA| = |OB| = r | |
| ' ` | |
_|_'_`_ _ _ _ _ _ _ | |
O/ X-axis | |
/ | |
/ | |
/ Z-axis | |
A = (x, y) | |
= (rcos{α}, rsin{α}) | |
Let R be a rotation from A to B through an angle β | |
R: (x, y) → (rcos{α+β}, rsin{α+β}) | |
→ (rcos{α}cos{β}-rsin{α}sin{β}, rsin{α}cos{β}+rcos{α}sin{β}) | |
→ (xcos{β}-ysin{β}, ycos{β}+xsin{β}) | |
Thus, we require a matrix M such that | |
|x| | xcos{β}-ysin{β} | | |
M |y| = | ycos{β}+xsin{β} | | |
|z| | z | | |
|1| | 1 | | |
Such an affine matrix is | |
| cos{β} -sin{β} 0 0 | | |
M = | sin{β} cos{β} 0 0 | | |
| 0 0 1 0 | | |
| 0 0 0 1 | | |
4. Arbitrary 3D line | |
Suppose the line's direction vector was pointing out of the screen towards you, then let a positive rotation angle | |
indicate an anticlockwise rotation. | |
The line will first be subject to a sequence of reversible transformations such that after they are applied the line | |
coincides with the origin and is orientated so that its direction vector points in the positive Z-axis direction. | |
Since the line now coincides with the Z-axis the actual rotation transformation which rotates the point around the line | |
is applied using the Z-axis rotation technique. Finally, the transformations which were applied to change the line's | |
position and orientation are reversed. | |
Let the line be defined by a point on the line Q=(q_x, q_y, q_z) and a direction D=(d_x, d_y, d_z). | |
a. Translate the line to the origin | |
| 1 0 0 -q_x | | |
A = | 0 1 0 -q_y | | |
| 0 0 1 -q_z | | |
| 0 0 0 1 | | |
b. Rotate the translated line's direction vector around the Z-axis onto the ZX-plane, specifically the half plane | |
where x >= 0 | |
Y-axis | D(d_x, d_y) ∠ DOX = θ_z = -atan2{d_y, d_x} | |
| ' | |
| ' | |
| ' | |
| ' | |
_|_'_ _ _ _ _ _ _ D'(√[(d_x)²+(d_y)²], 0) | |
O| X-axis | |
Let B: D → D', thus | |
| cos{θ_z} -sin{θ_z} 0 0 | | |
B = | sin{θ_z} cos{θ_z} 0 0 | | |
| 0 0 1 0 | | |
| 0 0 0 1 | | |
c. Rotate the line's newly oriented direction vector around the Y-axis so it coincides with the positive Z-axis | |
direction | |
X-axis | D'(d_z, √[(d_x)²+(d_y)²]) ∠ D'OZ = θ_y = -atan2{√[(d_x)²+(d_y)²], d_z} | |
| ' | |
| ' | |
| ' | |
| ' | |
_|_'_ _ _ _ _ _ _D'' | |
O| Z-axis | |
Let C: D' → D'', thus | |
| cos{θ_y} 0 sin{θ_y} 0 | | |
C = | 0 1 0 0 | | |
| -sin{θ_y} 0 cos{θ_y} 0 | | |
| 0 0 0 1 | | |
d. Perform the actual rotation around the line now that it coincides with the positive Z-axis | |
| cos{θ} -sin{θ} 0 0 | | |
D = | sin{θ} cos{θ} 0 0 | | |
| 0 0 1 0 | | |
| 0 0 0 1 | | |
e. Reverse step (c.) | |
| cos{-θ_y} 0 sin{-θ_y} 0 | | cos{θ_y} 0 -sin{θ_y} 0 | | |
C⁻¹ = | 0 1 0 0 | = | 0 1 0 0 | | |
| -sin{-θ_y} 0 cos{-θ_y} 0 | | sin{θ_y} 0 cos{θ_y} 0 | | |
| 0 0 0 1 | | 0 0 0 1 | | |
f. Reverse step (b.) | |
| cos{-θ_z} -sin{-θ_z} 0 0 | | cos{θ_z} sin{θ_z} 0 0 | | |
B⁻¹ = | sin{-θ_z} cos{-θ_z} 0 0 | = | -sin{θ_z} cos{θ_z} 0 0 | | |
| 0 0 1 0 | | 0 0 1 0 | | |
| 0 0 0 1 | | 0 0 0 1 | | |
g. Reverse step (a.) | |
| 1 0 0 q_x | | |
A⁻¹ = | 0 1 0 q_y | | |
| 0 0 1 q_z | | |
| 0 0 0 1 | | |
Let R be a transformation matrix which rotates around the line by θ radians, then we have | |
R = A⁻¹ * B⁻¹ * C⁻¹ * D * C * B * A | |
Let cy = cos{θ_y} | |
cz = cos{θ_z} | |
sy = sin{θ_y} | |
sz = sin{θ_z} | |
Cθ = cos{θ} | |
Sθ = sin{θ} | |
where θ_y = -atan2{√[(d_x)²+(d_y)²], d_z} and θ_z = -atan2{d_y, d_x}, then | |
|1 0 0 q_x| | cz sz 0 0| |cy 0 -sy 0| |Cθ -Sθ 0 0| | cy 0 sy 0| |cz -sz 0 0| |1 0 0 -q_x| | |
R = |0 1 0 q_y|*|-sz cz 0 0|*|0 1 0 0|*|Sθ Cθ 0 0|*| 0 1 0 0|*|sz cz 0 0|*|0 1 0 -q_y| | |
|0 0 1 q_z| | 0 0 1 0| |sy 0 cy 0| |0 0 1 0| |-sy 0 cy 0| |0 0 1 0| |0 0 1 -q_z| | |
|0 0 0 1 | | 0 0 0 1| |0 0 0 1| |0 0 0 1| | 0 0 0 1| |0 0 0 1| |0 0 0 1 | | |
|1 0 0 -q_x| | |
= M * |0 1 0 -q_y| | |
|0 0 1 -q_z| | |
|0 0 0 1 | | |
where | |
M₀₀ = Cθ*(cy*cy*cz*cz+sz*sz) + cz*cz*sy*sy | |
M₀₁ = Cθ*cz*sz*(1-cy*cy) - Sθ*cy*(cz*cz+sz*sz) - cz*sy*sy*sz | |
M₀₂ = Cθ*cy*cz*sy + Sθ*sy*sz - cy*cz*sy | |
M₀₃ = q_x | |
M₁₀ = Cθ*cz*sz*(1-cy*cy) + Sθ*cy*(cz*cz+sz*sz) - cz*sy*sy*sz | |
M₁₁ = Cθ*(cy*cy*sz*sz+cz*cz) + sy*sy*sz*sz | |
M₁₂ = -Cθ*cy*sy*sz + Sθ*cz*sy + cy*sy*sz | |
M₁₃ = q_y | |
M₂₀ = Cθ*cy*cz*sy - Sθ*sy*sz - cy*cz*sy | |
M₂₁ = -Cθ*cy*sy*sz - Sθ*cz*sy + cy*sy*sz | |
M₂₂ = Cθ*sy*sy + cy*cy | |
M₂₃ = q_z | |
Thus, | |
| M₀₀ M₀₁ M₀₂ (-q_x*M₀₀ - q_y*M₀₁ - q_z*M₀₂ + q_x) | | |
R = | M₁₀ M₁₁ M₁₂ (-q_x*M₁₀ - q_y*M₁₁ - q_z*M₁₂ + q_y) | | |
| M₂₀ M₂₁ M₂₂ (-q_x*M₂₀ - q_y*M₂₁ - q_z*M₂₂ + q_z) | | |
| 0 0 0 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment