Spatial Algebra
Rotation Matrix §
A rotation matrix is a matrix that is used to rotate a vector by applying Matrix Transformation.
#todo: think about Notation, whether to use $R(θ)$ or $[R_{θ}]$
Rotation in 2D §
In 2D, this is given by the matrix
$R(θ)=[cosθsinθ −sinθcosθ ]$
A great and simple explanation of the derivation can be found here.
 #todo Be able to derive this, I think you start from the idea that the radius from the origin is going to be the same.
Some Properties §

The inverse of a rotation matrix is its transpose (SkewSymmetry)
$R(θ)_{−1}=R(θ)_{T}$

Determinant of Rotation Matrix is 1
$det(R(θ))=1$

Composition of Rotation
$R(α)R(θ)=R(α+θ)$
Properties 1 and 2 of the Rotation Matrix makes it part of the Special Orthogonal Group (SO(n)).
Rotation in 3D §
In practice, for RigidBody Transformations, we actually use a $4×4$ matrix that combines rotation and translation. See Spatial Algebra for details.
Derivation §
A general rotation matrix derivation, taken from the Visual SLAM book, page 34.
For rotation. We start with a unitlength orthogonal base $e=(e_{1},e_{2},e_{3})$. After a rotation it becomes $e_{′}=(e_{1},e_{2},e_{3})$. Then, for the same vector $a$ (the vector does not move with the rotation of the coordinate system), its coordinates in these two coordinate systems are $[a_{1},a_{2},a_{3}]_{T}$ and $[a_{1},a_{2},a_{3}]_{T}$. Because the vector itself has not changed, according to the definition of coordinates, there are:
[ \mathbf{e}_1,\mathbf{e}_2,\mathbf{e}_3 ]\left[ \begin{array}{l} {a_1}\\{a_2}\\ {a_3} \end{array} \right] = \left[ \mathbf{e}_1', \mathbf{e}_2', \mathbf{e}_3' \right]\left[ \begin{array}{l} a'_1\\ a'_2\\ a'_3 \end{array} \right] \end{equation}$$
To describe the relationship between the two coordinates, we multiply the left and right sides of the above equation by $\left [ \begin {array}{l} \mathbf{e}_1^T\\ \mathbf{e}_2^T\\ \mathbf{e}_3^T \end {array} \right ]$, then the matrix on the left becomes an identity matrix, so:
$$\begin{equation} \left[ \begin{array}{l} {a_1}\\ {a_2}\\ {a_3} \end{array}\right]=\underbrace{\left[{\begin{array}{*{20}{c}} {\mathbf{e}_1^T\mathbf{e}_1'} & {\mathbf{e}_1^T\mathbf{e}_2'} & {\mathbf{e}_1^T\mathbf{e}_3'}\\ {\mathbf{e}_2^T\mathbf{e}_1'} & {\mathbf{e}_2^T\mathbf{e}_2'} & {\mathbf{e}_2^T\mathbf{e}_3'}\\ {\mathbf{e}_3^T\mathbf{e}_1'} & {\mathbf{e}_3^T\mathbf{e}_2'} & {\mathbf{e}_3^T\mathbf{e}_3'} \end{array}} \right]}_{\text{rotation matrix}}\left[ \begin{array}{l} a_1'\\ a_2'\\ a_3' \end{array} \right] \buildrel \Delta \over = \mathbf{R} \mathbf{a}'. \end{equation}$$
You can see that the [[notes/Rotation MatrixRotation Matrix]] $\mathbf{R}$ consists of the [[notes/Dot ProductInner Product]] between the two sets of bases, describing the same vector's coordinate transformation relationship before and after the rotation.
 Takes a bit of convincing for how it is an [[notes/Dot ProductInner Product]]: Do the actual matrix multiplication, see which values are summed
 $a_1 = e_1^T * (e' \cdot a')$
 $a_2 = e_2^T * (e' \cdot a')$
 $a_3 = e_3^T * (e' \cdot a')$
 Since the base vector's length is 1, it is actually the cosine of the angle between the base vectors. So the rotation matrix is also called **direction cosine matrix**
#### Rotation Matrices from [[notes/Euler AngleEuler Angle]]s
see https://articulatedrobotics.xyz/6rotations_3d/.
There are 3 possible axes of rotation in 3D, therefore we have the three following matrices:
$$R_x(\theta) = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\theta) & \sin(\theta) \\ 0 & \sin(\theta) & \cos(\theta)\end{bmatrix}$$
$$R_{y}(\theta) = \begin{bmatrix} \cos(\theta) & 0 & \sin(\theta) \\ 0 & 1 & 0 \\ \sin(\theta) & 0 & \cos(\theta)\end{bmatrix}$$
$$R_{z}(\theta) = \begin{bmatrix}\cos(\theta) & \sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}$$
You can combine all of these three together (from ChatGPT), but be careful because order MATTERS. See [[notes/Euler AngleEuler Angle]].
> [!question] How did you make that jump?
>
>
> How are these matrices with the $\cos$ and $\sin$ actually derived? The 2D derivation is shown at the top of the page. For 3D, it's a little more complicated. See visual slam book p.60. They mention https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
#### Problems with Rotations in 3D
[[notes/Steven M. LavalleSteven M. Lavalle]] talks about these: https://www.youtube.com/watch?v=Hwow9hcPi28&list=PLbMVogVj5nJSyt80VRXYCYrAvQuUb6dh&index=13&ab_channel=nptelhrd
1. Order matters (i.e. rotation is not commutative), just think about pitching then rolling, or rolling then pitching
2. Kinematic singularities
So we can look into [[notes/QuaternionQuaternion]]s.
Quaternions are also much nicer for interpolation of paths, once you look into motion planning.
You can't just interpolate rotation matrices to determine the orientation of the robot over time. But you can do it with quaternions.
### Composition of Rotations
You can compose rotations by doing something like this:
$$R_{1,3} = R_{1,2}R_{2,3}$$
where
 $R_{1,3}$ is the orientation measured from frame 1 to frame 3
> [!question] Why does this work?
>
>
> We know that $R_1 + R_2 \notin SO(3)$, but why $R_1 R_2 \in SO(3)$? How do we know this is always true?
>  $R_1 + R_2$ can easily be disproved with a counterexample.
>  But what about $R_1 R_2$?
>
> Do we need lie algebra to prove this? Should reason from [[notes/First PrinciplesFirst Principles]]. Notice that $R_1$ and $R_2$ are both in $SO(3)$ by definition. What makes it $SO(3)$? It holds the property of being both orthogonal and determinant of 1. The product will always be orthogonal with determinant 1, so $R_1 R_2 \in SO(3)$.
> Very easy proof:
>  Orthogonality: $(R_1R_2)(R_1R_2)^T = R_1R_2 R_2^T R_1^T = R_1 I R_1 ^T = I I = I$
>  Determinant: $det(R_1R_2) = det (R_1) det(R_2) = 1 \cdot 1 = 1$
You CAN compose rotations directly using rotation matrices. You don't need to convert to quaternions combine rotations.
You can also do this using [[notes/QuaternionQuaternion]]s.
> [!danger] 3D Rotation is NOT commutative
>
>
> In 2D, you are only rotation around the zaxis, so commutativity is guaranteed.
>
> However, in 3D, rotations can be done around different axes. Thus, commutativity is NOT guaranteed. See [[notes/Euler AngleEuler Angle]] where I demonstrate this.
>
> Neither are [[notes/QuaternionQuaternion]]s.
### Usage in [[notes/EigenEigen]]
Initialization
```cpp
Matrix3d rotation_matrix = Matrid3d::Identity();
rotation_m << r00, r01, r02, r10, r11, r12, r20, r21, r22;
```
### Related
 [[notes/Euler AngleEuler Angle]]
 [[notes/AxisAngle RepresentationAxisAngle Representation]]
 [[notes/QuaternionQuaternion]]