From 34e6e2a941ce1f8dad6492c8d672a7d5ac5233c3 Mon Sep 17 00:00:00 2001 From: Andreas Bircher Date: Tue, 3 May 2016 17:32:49 +0200 Subject: [PATCH] refactoring rotation classes and adding initial description --- matrix/Dcm.hpp | 63 ++++++++++++++- matrix/Euler.hpp | 91 +++++++++++++++++++++- matrix/Quaternion.hpp | 176 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 322 insertions(+), 8 deletions(-) diff --git a/matrix/Dcm.hpp b/matrix/Dcm.hpp index e7379d916b..bd622731c9 100644 --- a/matrix/Dcm.hpp +++ b/matrix/Dcm.hpp @@ -20,6 +20,11 @@ class Quaternion; template class Euler; +/** + * Direction cosine matrix class + * + * More elaborate class description + */ template class Dcm : public Matrix { @@ -28,20 +33,69 @@ public: typedef Matrix Vector3; + /** + * Standard constructor + * + * Initializes to identity + */ Dcm() : Matrix() { (*this) = eye(); } + /** + * Constructor from array + * + * Copies the given scalar value to all matrix fields. + * + * @param other array + */ Dcm(const Type *data_) : Matrix(data_) { } + /** + * Copy constructor + * + * More elaborate function description + * + * @param other Matrix33 to set dcm to + */ Dcm(const Matrix &other) : Matrix(other) { } + /** + * Constructor from quaternion + * + * More elaborate function description + * + * @param q quaternion to set dcm to + */ Dcm(const Quaternion & q) { + set_from_quaternion(q); + } + + /** + * Constructor from euler angles + * + * More elaborate function description + * + * @param euler euler angles to set dcm to + */ + Dcm(const Euler & euler) { + set_from_euler(euler); + } + + + /** + * Set from quaternion + * + * More elaborate function description + * + * @param q quaternion to set dcm to + */ + void set_from_quaternion(const Quaternion & q) { Dcm &dcm = *this; Type a = q(0); Type b = q(1); @@ -62,7 +116,14 @@ public: dcm(2, 2) = aSq - bSq - cSq + dSq; } - Dcm(const Euler & euler) { + /** + * Set from euler angles + * + * More elaborate function description + * + * @param euler euler angles to set dcm to + */ + void set_from_euler(const Euler & euler) { Dcm &dcm = *this; Type cosPhi = Type(cos(euler.phi())); Type sinPhi = Type(sin(euler.phi())); diff --git a/matrix/Euler.hpp b/matrix/Euler.hpp index 57dcbb48a2..24ffd326df 100644 --- a/matrix/Euler.hpp +++ b/matrix/Euler.hpp @@ -23,34 +23,113 @@ class Dcm; template class Quaternion; +/** + * Euler angles class + * + * More elaborate class description + */ template class Euler : public Vector { public: virtual ~Euler() {}; + /** + * Standard constructor + * + * More elaborate function description + */ Euler() : Vector() { } + /** + * Copy constructor + * + * More elaborate function description + * + * @param other vector to copy + */ Euler(const Vector & other) : Vector(other) { } + /** + * Constructor from Matrix31 + * + * More elaborate function description + * + * @param other Matrix31 to copy + */ Euler(const Matrix & other) : Vector(other) { } + /** + * Constructor from euler angles + * + * More elaborate function description + * + * @param phi_ roll + * @param theta_ pitch + * @param psi_ yaw + */ Euler(Type phi_, Type theta_, Type psi_) : Vector() + { + set_from_euler(phi_, theta_, psi_); + } + + /** + * Constructor from dcm + * + * More elaborate function description + * + * @param dcm_ dcm to set angles to + */ + Euler(const Dcm & dcm) : Vector() + { + set_from_dcm(dcm); + } + + /** + * Constructor from quaternion + * + * More elaborate function description + * + * @param q quaternion to set angles to + */ + Euler(const Quaternion & q) : + Vector() + { + set_from_quaternion(q); + } + + /** + * Set from euler angles + * + * More elaborate function description + * + * @param phi_ roll + * @param theta_ pitch + * @param psi_ yaw + */ + void set_from_euler(Type phi_, Type theta_, Type psi_) { phi() = phi_; theta() = theta_; psi() = psi_; } - Euler(const Dcm & dcm) : Vector() + /** + * Set from dcm + * + * More elaborate function description + * + * @param dcm_ dcm to set angles to + */ + void set_from_dcm(const Dcm & dcm) { Type phi_val = Type(atan2(dcm(2,1), dcm(2,2))); Type theta_val = Type(asin(-dcm(2,0))); @@ -70,8 +149,14 @@ public: psi() = psi_val; } - Euler(const Quaternion & q) : - Vector() + /** + * Set from dcm + * + * More elaborate function description + * + * @param q quaternion to set angles to + */ + void set_from_quaternion(const Quaternion & q) { *this = Euler(Dcm(q)); } diff --git a/matrix/Quaternion.hpp b/matrix/Quaternion.hpp index 7b845b1475..e135464537 100644 --- a/matrix/Quaternion.hpp +++ b/matrix/Quaternion.hpp @@ -20,6 +20,11 @@ class Dcm; template class Euler; +/** + * Quaternion class + * + * More elaborate class description + */ template class Quaternion : public Vector { @@ -29,11 +34,23 @@ public: typedef Matrix Matrix41; typedef Matrix Matrix31; + /** + * Constructor from array + * + * Copies the given scalar value to all quaternion fields. + * + * @param data_ array + */ Quaternion(const Type *data_) : Vector(data_) { } + /** + * Standard constructor + * + * More elaborate function description + */ Quaternion() : Vector() { @@ -44,13 +61,68 @@ public: q(3) = 0; } + /** + * Constructor from Matrix41 + * + * More elaborate function description + * + * @param other Matrix41 to copy + */ Quaternion(const Matrix41 & other) : Vector(other) { } + /** + * Constructor from dcm + * + * More elaborate function description + * + * @param dcm dcm to set quaternion to + */ Quaternion(const Dcm & dcm) : Vector() + { + set_from_dcm(dcm); + } + + /** + * Constructor from euler angles + * + * More elaborate function description + * + * @param euler euler angles to set quaternion to + */ + Quaternion(const Euler & euler) : + Vector() + { + set_from_euler(euler); + } + + /** + * Constructor from quaternion values + * + * More elaborate function description + * + * @param a set quaternion value 0 + * @param b set quaternion value 1 + * @param c set quaternion value 2 + * @param d set quaternion value 3 + */ + Quaternion(Type a, Type b, Type c, Type d) : + Vector() + { + set_from_quaternion(a, b, c, d); + } + + /** + * Set from dcm + * + * More elaborate function description + * + * @param dcm dcm to set quaternion to + */ + void set_from_dcm(const Dcm & dcm) { Quaternion &q = *this; q(0) = Type(0.5 * sqrt(1 + dcm(0, 0) + @@ -63,8 +135,14 @@ public: (4 * q(0))); } - Quaternion(const Euler & euler) : - Vector() + /** + * Set from euler angles + * + * More elaborate function description + * + * @param euler euler angles to set quaternion to + */ + void set_from_euler(const Euler & euler) { Quaternion &q = *this; Type cosPhi_2 = Type(cos(euler.phi() / (Type)2.0)); @@ -83,8 +161,17 @@ public: sinPhi_2 * sinTheta_2 * cosPsi_2; } - Quaternion(Type a, Type b, Type c, Type d) : - Vector() + /** + * Set from quaternion values + * + * More elaborate function description + * + * @param a set quaternion value 0 + * @param b set quaternion value 1 + * @param c set quaternion value 2 + * @param d set quaternion value 3 + */ + void set_from_quaternion(Type a, Type b, Type c, Type d) { Quaternion &q = *this; q(0) = a; @@ -93,6 +180,14 @@ public: q(3) = d; } + /** + * Quaternion multiplication operator + * + * More elaborate function description + * + * @param q quaternion to multiply with + * @return product + */ Quaternion operator*(const Quaternion &q) const { const Quaternion &p = *this; @@ -104,24 +199,53 @@ public: return r; } + /** + * Self-multiplication operator + * + * More elaborate function description + * + * @param other quaternion to multiply with + */ void operator*=(const Quaternion & other) { Quaternion &self = *this; self = self * other; } + /** + * Scalar multiplication operator + * + * More elaborate function description + * + * @param scalar scalar to multiply with + * @return product + */ Quaternion operator*(Type scalar) const { const Quaternion &q = *this; return scalar * q; } + /** + * Scalar self-multiplication operator + * + * More elaborate function description + * + * @param scalar scalar to multiply with + */ void operator*=(Type scalar) { Quaternion &q = *this; q = q * scalar; } + /** + * Computes the derivative + * + * More elaborate function description + * + * @param w direction + */ Matrix41 derivative(const Matrix31 & w) const { const Quaternion &q = *this; Type dataQ[] = { @@ -139,6 +263,11 @@ public: return Q * v * Type(0.5); } + /** + * Invert quaternion + * + * More elaborate function description + */ void invert() { Quaternion &q = *this; q(1) *= -1; @@ -146,6 +275,13 @@ public: q(3) *= -1; } + /** + * Invert quaternion + * + * More elaborate function description + * + * @return inverted quaternion + */ Quaternion inversed() { Quaternion &q = *this; Quaternion ret; @@ -156,12 +292,27 @@ public: return ret; } + /** + * Rotate quaternion from rotation vector + * + * More elaborate function description + * + * @param vec rotation vector + */ void rotate(const Vector &vec) { Quaternion res; res.from_axis_angle(vec); (*this) = (*this) * res; } + /** + * Rotation quaternion from vector + * + * More elaborate function description + * + * @param vec rotation vector + * @return quaternion representing the rotation + */ void from_axis_angle(Vector vec) { Quaternion &q = *this; Type theta = vec.norm(); @@ -174,6 +325,15 @@ public: from_axis_angle(vec,theta); } + /** + * Rotation quaternion from axis and scalar + * + * More elaborate function description + * + * @param axis axis of rotation + * @param theta scalar describing angle of rotation + * @return quaternion representing the rotation + */ void from_axis_angle(const Vector &axis, Type theta) { Quaternion &q = *this; if(theta < (Type)1e-10) { @@ -188,6 +348,14 @@ public: q(3) = axis(2) * magnitude; } + + /** + * Rotation vector from quaternion + * + * More elaborate function description + * + * @return vector, direction representing rotation axis and norm representing angle + */ Vector to_axis_angle() { Quaternion &q = *this; Type axis_magnitude = Type(sqrt(q(1) * q(1) + q(2) * q(2) + q(3) * q(3)));