C++ library to represent 3d rotations
Example at: http://nbviewer.jupyter.org/github/balintkaszas/rotations/blob/master/doc.ipynb
Implementation of 3 representation of spatial rotations:
- Axis-angle
- Quaternion
- Matrix
Assume
In this case, the components describe a rotation of angle
Rotating the vector
where
A composite rotation is simply a rotation by the quaternion-product.
If det
- Axis - angle <-> Matrix : https://en.wikipedia.org/wiki/Rotation_matrix#Axis_and_angle
- Quaternion -> Axis-angle:
$[\cos \frac{\alpha}{2} ; \sin \frac{\alpha}{2} \frac{\mathbf{n}}{||\mathbf{n}||}]$ - Quaternion -> Axis-angle:
$\alpha = 2 \text{atan2}(|v|, w)$ ,$\mathbf{n} = \frac{\mathbf{v}}{\sin (\alpha/2)}$ - Matrix <-> Quaternion: https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion
- Matrix -> Axis-angle: https://en.wikipedia.org/wiki/Rotation_matrix#Conversion_from_and_to_axis–angle
3 basic, templated classes:
axisAngle
quaternion
Matrix3
If the conversion is reasonable (the structures represent rotation), conversion between them is:
convertToAxisAngle()
convertToQuaternion()
convertToMatrix()
Rotation of a std::vector
is contained in an std::optional
std::vector<double> r {x, y, z};
Matrix3<double> m {a11, a12, a13, a21, a22, a23, a31, a32, a33};
std::optional<std::vector<double>> rPrime = m*r;
if(rPrime){
rPrime.value();
}
std::vector<double> r {x, y, z};
Matrix3<double> quaternion {wq, xq, yq, zq};
std::optional<std::vector<double>> rPrime = rotateByQuaternion(q, r);
if(rPrime){
rPrime.value();
}
quaternions and matrices can be multiplied to give composite rotations
Rotation of
Same with a quaternion: $$ \mathbf{q} = [\cos 15^0, \sin 15^0, 0, 0] = [-0.7596879, 0.6502878, 0., 0.] \ [0,\mathbf{r}'] = \mathbf{q}[0, \mathbf{r}] \mathbf{q}^{-1} = [0, 0, 0.1542515, 0.9880316] $$
Rotation of an ellipse, in main.cpp
. Rotation around the axis