<commit_before>---
layout: post
title:  "Rigid body poses in 3D"
date:   2015-12-19
---
<p class="intro">

The primary difficulty when beginning to consider three-dimensional kinematics 
is due to the significant difference between two-dimensional and three-dimensional orientations. In two-dimensions, orientation can be represented using a single scalar value. In three-dimensions, a plethora of three-dimensional orientations are conceivable, and at least three values are necessary to represent all 3D rotations. We will not consider translation in three dimensions: it is treated exactly as in 2D (with an additional component to represent location along the \\(z\\) axis, of course).
 
We will again use the convention that frame \\(w\\) indicates a "world frame" and frame \\(s\\) indicates an alternative frame of reference.

### Representations of 3D orientation

#### Rotation matrices
We can describe the orientation of one reference frame in 3D with respect to 
another using \\(3\times 3\\) matrices analogously to how we define relative
orientations in 2D using \\(2\times 2\\) matrices. 

Again consider our alternative frame of reference \\(s\\), which is collocated with \\(w\\) but is oriented differently. Assume that the axes of \\(w\\) are named \\(\mathbf{a}\_x\\), \\(\mathbf{a}\_y\\), and \\(\mathbf{a}\_z\\) and that the respective axes of \\(s\\) are denoted \\(\mathbf{a}'\_x, \mathbf{a}'\_y\\), and \\(\mathbf{a}'\_z\\). All of these vectors are three-dimensional. One choice of the set of vectors \\(\mathbf{a}\_x, \mathbf{a}\_y\\), and \\(\mathbf{a}\_z\\) are the vectors \\(\begin{bmatrix}1 & 0 & 0\end{bmatrix}\^{\mathsf{T}}\\), \\(\begin{bmatrix}0 & 1 & 0\end{bmatrix}\^{\mathsf{T}}\\), and \\(\begin{bmatrix}0 & 0 & 1\end{bmatrix}\^{\mathsf{T}}\\), respectively. We can now transform vectors between the reference frames using this matrix:
$$
\ \_{w}\mathbf{R}\_{s} \triangleq \begin{bmatrix}\mathbf{a}'\_{x\_1} & \mathbf{a}'\_{y\_1} & \mathbf{a}'\_{z\_1} \end{bmatrix} 
$$
Like the \\(2 \times 2\\) rotation matrices, the matrix above is orthogonal. A nice computational implication of this property is that the inverse of a rotation matrix is equal to its inverse (which makes inversion both fast and numerically stable). If the coordinate frame is _right-handed_, then \\(\mathbf{a}\_x \times \mathbf{a}\_y = \mathbf{a}\_z\\) is a right-handed cross-product and the determinant of \\(\_w\mathbf{R}\_s = 1\\). If the coordinate frame is _left-handed_, then \\(\mathbf{a}\_x \times \mathbf{a}\_y = \mathbf{a}\_z\\) is a left-handed cross-product and \\(\textrm{det}(\ \_w\mathbf{R}\_s) = 1\\). 

I note that all vectors should be unit vectors and the set of vectors should be mutually orthogonal (hint: you should use this as a debugging check in your coursework). If one or both of these conditions is not satisfied, you will have significant problems!

##### Concatenating rotations
If we have two orientations, \\(\ \_w\mathbf{R}\_s\\) and \\(\ \_s\mathbf{R}\_r\\), we can concatenate them- effecting one rotation followed by another- using matrix multiplication: \\(\ \_w\mathbf{R}\_r =\_r\\). Of course, matrix multiplication is commutative, so the order of multiplication is important to get the proper rotation. This warning should sound familiar: use subscripts (or superscripts, or some other system that works for you), to make sure that your operations are correct. Just verifying that your calculations are right by testing one or two cases is dangerous!

#### Axis-angle
The axis-angle representation of orientation appears as the following:
$$
\begin{bmatrix}
u\_x & u\_y & u\_z
\end{bmatrix}\^{\mathsf{T}}
\theta
$$
This representation indicates that a rotation about the _unit vector_ \\(\begin{bmatrix} u\_x & u\_y & u\_z \end{bmatrix}\^{\mathsf{T}}\\) by an angle of \\(\theta\\).

The rotation matrix representation corresponds one-to-one to SO(3), the 
"special orthogonal group" of orientations in three-dimensions. However,
the axis-angle representation generally maps two-to-one to SO(3): the values \\((\begin{bmatrix} u\_x & u\_y & u\_z \end{bmatrix}\^{\mathsf{T}} \theta)\\) and \\((\begin{bmatrix} -u\_x & -u\_y & -u\_z \end{bmatrix}\^{\mathsf{T}} -\theta)\\) are identical. The exception is a singularity at \\((\begin{bmatrix} u\_x & u\_y & u\_z \end{bmatrix}\^{\mathsf{T}} 0)\\), where the values of \\(u\_x\\), \\(u\_y\\), and \\(u\_z\\) are arbitrary: all values map to an identity orientation. The singularity makes conversion from a rotation matrix to the axis-angle representation a little tricky, as will be seen below.

##### Conversion to rotation matrix<commit_msg>Fixed bug in poses3<commit_after>---
layout: post
title:  "Rigid body poses in 3D"
date:   2015-12-19
---
<p class="intro">

The primary difficulty when beginning to consider three-dimensional kinematics 
is due to the significant difference between two-dimensional and three-dimensional orientations. In two-dimensions, orientation can be represented using a single scalar value. In three-dimensions, a plethora of three-dimensional orientations are conceivable, and at least three values are necessary to represent all 3D rotations. We will not consider translation in three dimensions: it is treated exactly as in 2D (with an additional component to represent location along the \\(z\\) axis, of course).
 
We will again use the convention that frame \\(w\\) indicates a "world frame" and frame \\(s\\) indicates an alternative frame of reference.

### Representations of 3D orientation

#### Rotation matrices
We can describe the orientation of one reference frame in 3D with respect to 
another using \\(3\times 3\\) matrices analogously to how we define relative
orientations in 2D using \\(2\times 2\\) matrices. 

Again consider our alternative frame of reference \\(s\\), which is collocated with \\(w\\) but is oriented differently. Assume that the axes of \\(w\\) are named \\(\mathbf{a}\_x\\), \\(\mathbf{a}\_y\\), and \\(\mathbf{a}\_z\\) and that the respective axes of \\(s\\) are denoted \\(\mathbf{a}'\_x, \mathbf{a}'\_y\\), and \\(\mathbf{a}'\_z\\). All of these vectors are three-dimensional. One choice of the set of vectors \\(\mathbf{a}\_x, \mathbf{a}\_y\\), and \\(\mathbf{a}\_z\\) are the vectors \\(\begin{bmatrix}1 & 0 & 0\end{bmatrix}\^{\mathsf{T}}\\), \\(\begin{bmatrix}0 & 1 & 0\end{bmatrix}\^{\mathsf{T}}\\), and \\(\begin{bmatrix}0 & 0 & 1\end{bmatrix}\^{\mathsf{T}}\\), respectively. We can now transform vectors between the reference frames using this matrix:
$$
\ \_{w}\mathbf{R}\_{s} \triangleq \begin{bmatrix}\mathbf{a}'\_{x\_1} & \mathbf{a}'\_{y\_1} & \mathbf{a}'\_{z\_1} \end{bmatrix} 
$$
Like the \\(2 \times 2\\) rotation matrices, the matrix above is orthogonal. A nice computational implication of this property is that the inverse of a rotation matrix is equal to its inverse (which makes inversion both fast and numerically stable). If the coordinate frame is _right-handed_, then \\(\mathbf{a}\_x \times \mathbf{a}\_y = \mathbf{a}\_z\\) is a right-handed cross-product and the determinant of \\(\_w\mathbf{R}\_s = 1\\). If the coordinate frame is _left-handed_, then \\(\mathbf{a}\_x \times \mathbf{a}\_y = \mathbf{a}\_z\\) is a left-handed cross-product and \\(\textrm{det}(\ \_w\mathbf{R}\_s) = 1\\). 

I note that all vectors should be unit vectors and the set of vectors should be mutually orthogonal (hint: you should use this as a debugging check in your coursework). If one or both of these conditions is not satisfied, you will have significant problems!

##### Concatenating rotations
If we have two orientations, \\(\ \_w\mathbf{R}\_s\\) and \\(\ \_s\mathbf{R}\_r\\), we can concatenate them- effecting one rotation followed by another- using matrix multiplication: \\(\ \_w\mathbf{R}\_r =\ \_w\mathbf{R}\_s \cdot \ \_s\mathbf{R}\_r\\). Of course, matrix multiplication is commutative, so the order of multiplication is important to get the proper rotation. This warning should sound familiar: use subscripts (or superscripts, or some other system that works for you), to make sure that your operations are correct. Just verifying that your calculations are right by testing one or two cases is dangerous!

#### Axis-angle
The axis-angle representation of orientation appears as the following:
$$
\begin{bmatrix}
u\_x & u\_y & u\_z
\end{bmatrix}\^{\mathsf{T}}
\theta
$$
This representation indicates that a rotation about the _unit vector_ \\(\begin{bmatrix} u\_x & u\_y & u\_z \end{bmatrix}\^{\mathsf{T}}\\) by an angle of \\(\theta\\).

The rotation matrix representation corresponds one-to-one to SO(3), the 
"special orthogonal group" of orientations in three-dimensions. However,
the axis-angle representation generally maps two-to-one to SO(3): the values \\((\begin{bmatrix} u\_x & u\_y & u\_z \end{bmatrix}\^{\mathsf{T}} \theta)\\) and \\((\begin{bmatrix} -u\_x & -u\_y & -u\_z \end{bmatrix}\^{\mathsf{T}} -\theta)\\) are identical. The exception is a singularity at \\((\begin{bmatrix} u\_x & u\_y & u\_z \end{bmatrix}\^{\mathsf{T}} 0)\\), where the values of \\(u\_x\\), \\(u\_y\\), and \\(u\_z\\) are arbitrary: all values map to an identity orientation. The singularity makes conversion from a rotation matrix to the axis-angle representation a little tricky, as will be seen below.

##### Conversion to rotation matrix
{'fraction_non_alphanumeric': 0.14414414414414414, 'fraction_numerical': 0.008043758043758044, 'mean_word_length': 3.801750772399588, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6591843', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2603, 'n_words': 1170}