|
This class contains the 4 components of a quaternion and the standard operators and functions needed for quaternion arithmetic. More...
#include <Quaternion.h>
Public Member Functions | |
Quaternion () | |
Constructor; sets quaternion value to (1,0,0,0) More... | |
Quaternion (Mdouble q0, Mdouble q1, Mdouble q2, Mdouble q3) | |
Alternative constructor. Sets quaternion value to (q0,q1,q2,q3) More... | |
Quaternion (Vec3D normal) | |
void | setUnity () |
Sets quaternion value to (1,0,0,0) More... | |
bool | isUnity () const |
Checks if the quaternion value is (1,0,0,0) More... | |
Quaternion | operator+ (const Quaternion &a) const |
Adds another quaternion and returns the result. More... | |
Quaternion | operator- (const Quaternion &a) const |
Subtracts another quaternion and returns the result. More... | |
Quaternion | operator* (Mdouble a) const |
Multiplies by a scalar. More... | |
Quaternion | operator/ (Mdouble a) const |
Divides by a scalar. More... | |
Quaternion & | operator+= (const Quaternion &a) |
Adds another quaternion. More... | |
Quaternion & | operator-= (const Quaternion &a) |
Subtracts another quaternion. More... | |
Quaternion & | operator*= (Mdouble a) |
Multiplies *this by a scalar. More... | |
Quaternion & | operator/= (Mdouble a) |
Divides by a scalar. More... | |
void | normalise () |
Makes this Quaternion unit length |q|=1. More... | |
void | setLength (Mdouble length) |
Make this Quaternion a certain length |q|=length. More... | |
Mdouble | getLength () const |
Calculates the length of this Quaternion: \( \sqrt{a\cdot a} \). More... | |
Mdouble | getLengthSquared () const |
Calculates the squared length of this Quaternion: \( a\cdot a \). More... | |
Mdouble | getComponent (int index) const |
Returns the requested component of this Quaternion. More... | |
Quaternion | angularVelocityBodyFixedFrameToAngularDisplacement (Vec3D v) const |
Quaternion | angularDisplacementTimeDerivative (Vec3D v) const |
Converts an angular momentum v=omega into a quaternion rate of change, q(t+dt)-q(t)/dt. More... | |
void | updateAngularDisplacement (Vec3D angularVelocityDt) |
Vec3D | applyCInverse (Quaternion q) const |
Converts quaternion rate of change into an angular momentum omega. More... | |
void | setComponent (int index, double val) |
Sets the requested component of this Quaternion to the requested value. More... | |
bool | isEqualTo (const Quaternion &other, double tol) const |
Checks if the length this Quaternion is equal the length of other with a certain tolerance. More... | |
Vec3D | getEuler () const |
Convert a quaternion to Euler angles. See Wikipedia for details. More... | |
void | setEuler (const Vec3D &e) |
Convert Euler angles to a quaternion. See Wikipedia for details. More... | |
Mdouble | getAngleZ () const |
Converts a quaternion to the rotation angle in the XY plane (for Mercury2D). See Wikipedia for details. More... | |
void | setAngleZ (Mdouble psi) |
Converts the rotation angle in the XY plane into a quaternion (for Mercury2D). See Wikipedia for details. More... | |
MatrixSymmetric3D | rotateInverseInertiaTensor (const MatrixSymmetric3D &invI) const |
Converts the inverse inertia tensor from the reference frame to the lab frame; see See QuaternionsWouter.pdf for details, where this operation is denoted by \(A*I^{-1}*A^T\). More... | |
void | rotateTensor (SmallMatrix< 3, 3 > I) const |
Vec3D | getAxis () const |
Converts the quaternions into a normal vector by rotating the vector x=(1,0,0); see See Wiki for details. More... | |
void | setOrientationViaNormal (Vec3D normal) |
void | getRotationMatrix (SmallMatrix< 3, 3 > &A) const |
void | rotate (Vec3D &position) const |
Vec3D | rotate (const Vec3D &position) const |
void | rotate (SmallVector< 3 > &position) const |
void | rotateBack (Vec3D &position) const |
Vec3D | rotateBack (const Vec3D &position) const |
Mdouble | getDistance (Vec3D p, Vec3D p0) const |
Static Public Member Functions | |
static Mdouble | getDistance (const Quaternion &a, const Quaternion &b) |
Calculates the distance between two Quaternion: \( \sqrt{\left(a-b\right) \cdot \left(a-b\right)} \). More... | |
static Mdouble | getDistanceSquared (const Quaternion &a, const Quaternion &b) |
Calculates the squared distance between two Quaternion: \( \left(a-b\right) \cdot \left(a-b\right) \). More... | |
static Mdouble | getLength (const Quaternion &a) |
Calculates the length of a Quaternion: \( \sqrt{a\cdot a} \). More... | |
static Mdouble | getLengthSquared (const Quaternion &a) |
Calculates the squared length of a Quaternion: \( a\cdot a \). More... | |
static Quaternion | getUnitQuaternion (const Quaternion &a) |
Returns a unit Quaternion based on a. More... | |
Public Attributes | |
Mdouble | q0 |
the zeroth component of the quaternion q = (q0,q1,q2,q3) More... | |
Mdouble | q1 |
the first component of the quaternion q = (q0,q1,q2,q3) More... | |
Mdouble | q2 |
the second component of the quaternion q = (q0,q1,q2,q3) More... | |
Mdouble | q3 |
the third component of the quaternion q = (q0,q1,q2,q3) More... | |
Friends | |
std::ostream & | operator<< (std::ostream &os, const Quaternion &a) |
Adds elements to an output stream. More... | |
std::istream & | operator>> (std::istream &is, Quaternion &a) |
Adds elements to an input stream. More... | |
Quaternion | operator+ (Mdouble a, const Quaternion &b) |
Adds a scalar to a quaternion. More... | |
Quaternion | operator- (Mdouble a, const Quaternion &b) |
Subtracts the elements of a quaternion from a scalar. More... | |
Quaternion | operator- (const Quaternion &a) |
Subtracts a quaternion. More... | |
Quaternion | operator* (Mdouble a, const Quaternion &b) |
Multiplies all elements by a scalar. More... | |
This class contains the 4 components of a quaternion and the standard operators and functions needed for quaternion arithmetic.
Implementation of a 3D quaternion (by Vitaliy). Modifications 21:9:2009 - Added the inclusion guards and some include objects
A quaternion is a four-dimensional vector q = (q0,q1,q2,q3) that satisfies |q|^2=q0^2+q1^2+q2^2+q3^2=1. It can be used to describe the orientation of an object in space.
The properties (e.g. inertia tensor) of any interactable (particle or wall) in MercuryDPM are described in its reference frame. The quaternion is used to rotate the object into the lab frame (the system geometry). A few examples to help visualising quaternions can be found here .
The unit quaternion, q=(1,0,0,0) denotes the state where the lab frame and the reference frame is identical.
To see how to convert a quaternion to Euler angles or to compare it to a rotation of an object around a axis, see Wikipedia for details.
Quaternion::Quaternion | ( | ) |
Constructor; sets quaternion value to (1,0,0,0)
Default constructor
References setUnity().
Referenced by angularDisplacementTimeDerivative(), angularVelocityBodyFixedFrameToAngularDisplacement(), getUnitQuaternion(), operator*(), operator+(), operator-(), operator/(), and setOrientationViaNormal().
Alternative constructor. Sets quaternion value to (q0,q1,q2,q3)
Alternative constructor, lets you define all four elements.
[in] | q0 | the q0-component |
[in] | q1 | the q1-component |
[in] | q2 | the q2-component |
[in] | q3 | the q3-component |
|
inline |
normal |
References setOrientationViaNormal().
Quaternion Quaternion::angularDisplacementTimeDerivative | ( | Vec3D | v | ) | const |
Converts an angular momentum v=omega into a quaternion rate of change, q(t+dt)-q(t)/dt.
Given v = \omega * dt, with omega the angular velocity, this computes the change in angular displacement to be added in the time integration. This is equivalent to applying the matrix \tilde{C}
References q0, q1, q2, q3, Quaternion(), Vec3D::X, Vec3D::Y, and Vec3D::Z.
Referenced by updateAngularDisplacement().
Quaternion Quaternion::angularVelocityBodyFixedFrameToAngularDisplacement | ( | Vec3D | v | ) | const |
Vec3D Quaternion::applyCInverse | ( | Quaternion | q | ) | const |
Converts quaternion rate of change into an angular momentum omega.
Mdouble Quaternion::getAngleZ | ( | ) | const |
Vec3D Quaternion::getAxis | ( | ) | const |
Converts the quaternions into a normal vector by rotating the vector x=(1,0,0); see See Wiki for details.
References q0, q1, q2, and q3.
Referenced by HorizontalBaseScrew::convertLimits(), InfiniteWall::createVTK(), AxisymmetricIntersectionOfWalls::getDistanceAndNormal(), HorizontalBaseScrew::getDistanceAndNormal(), InfiniteWall::getDistanceAndNormal(), InfiniteWall::getDistanceNormalOverlapSuperquadric(), InfiniteWall::getNormal(), and CGFields::OrientationField::setFields().
Mdouble Quaternion::getComponent | ( | int | index | ) | const |
Returns the requested component of this Quaternion.
returns the quaternion element belonging to the given index.
[in] | index | the index of interest (should be 0, 1 or 2) |
References ERROR, logger, q0, q1, q2, and q3.
Referenced by mathsFunc::isEqual().
|
static |
Calculates the distance between two Quaternion: \( \sqrt{\left(a-b\right) \cdot \left(a-b\right)} \).
Calculates the distance (i.e. the length of the difference) between two quaternions NB: this is a STATIC function!
[in] | a | the first quaternion |
[in] | b | the second quaternion |
References getDistanceSquared().
Referenced by InfiniteWall::getDistance(), and isEqualTo().
|
static |
Calculates the squared distance between two Quaternion: \( \left(a-b\right) \cdot \left(a-b\right) \).
Calculates the square of the distance (i.e. the length of the difference) between two quaternions. NB: this is a STATIC function!
[in] | a | the first quaternion |
[in] | b | the second quaternion |
References q0, q1, q2, and q3.
Referenced by getDistance().
Vec3D Quaternion::getEuler | ( | ) | const |
Convert a quaternion to Euler angles. See Wikipedia for details.
Get the Euler angles of this quaternion. Example of visualising Euler angles can be found here
References constants::pi, q0, q1, q2, and q3.
Referenced by EllipticalSuperQuadricCollision::setupInitialConditions().
Mdouble Quaternion::getLength | ( | ) | const |
Calculates the length of this Quaternion: \( \sqrt{a\cdot a} \).
Calculates the length of this quaternion
References getLengthSquared().
Referenced by setLength().
|
static |
Calculates the length of a Quaternion: \( \sqrt{a\cdot a} \).
Calculates the length of a given quaternion NB: this is a STATIC function!
[in] | a | quaternion to be measured. |
References getLength().
Referenced by getLength().
Mdouble Quaternion::getLengthSquared | ( | ) | const |
Calculates the squared length of this Quaternion: \( a\cdot a \).
Calculates the square of the length of itself
References q0, q1, q2, and q3.
Referenced by getLength(), and normalise().
|
static |
Calculates the squared length of a Quaternion: \( a\cdot a \).
Calculates the square of the length of a given quaternion. NB: this is a STATIC function!
[in] | a | the quaternion. |
References q0, q1, q2, and q3.
Referenced by getUnitQuaternion().
void Quaternion::getRotationMatrix | ( | SmallMatrix< 3, 3 > & | A | ) | const |
Retrieves the rotation matrix
[out] | A | The rotation matrix |
References A, q0, q1, q2, and q3.
Referenced by SuperQuadricParticle::computeHessianLabFixed(), rotate(), rotateTensor(), ShapeGradientHessianTester::testCushion(), ShapeGradientHessianTester::testEllipsoid(), and ShapeGradientHessianTester::testRoundedBeam().
|
static |
Returns a unit Quaternion based on a.
Calculates the unit quaternion of a given quaternion (unless it is a quaternion with zero length; in that case it returns a 3D quaternion with each element equal to zero). NB: this is a STATIC function!
[in] | a | the quaternion of interest |
References getLengthSquared(), and Quaternion().
bool Quaternion::isEqualTo | ( | const Quaternion & | other, |
double | tol | ||
) | const |
Checks if the length this Quaternion is equal the length of other with a certain tolerance.
Checks if the length of the quaternion is equal to the one given in the first argument (other), with a tolerance given in the second argument (tol).
[in] | other | the 3D quaternion to check against |
[in] | tol | the tolerance |
References getDistance().
|
inline |
void Quaternion::normalise | ( | ) |
Makes this Quaternion unit length |q|=1.
Normalises the quaternion, i.e. divides all elements by the quaternions length (resulting in a quaternion in the same direction, but with unit length).
References ERROR, getLengthSquared(), and logger.
Referenced by setOrientationViaNormal(), and updateAngularDisplacement().
Quaternion Quaternion::operator* | ( | Mdouble | a | ) | const |
Multiplies by a scalar.
Multiplies each element with a scalar
[in] | a | the scalar to be multiplied with |
References q0, q1, q2, q3, and Quaternion().
Quaternion & Quaternion::operator*= | ( | Mdouble | a | ) |
Quaternion Quaternion::operator+ | ( | const Quaternion & | a | ) | const |
Quaternion & Quaternion::operator+= | ( | const Quaternion & | a | ) |
Quaternion Quaternion::operator- | ( | const Quaternion & | a | ) | const |
Quaternion & Quaternion::operator-= | ( | const Quaternion & | a | ) |
Quaternion Quaternion::operator/ | ( | Mdouble | a | ) | const |
Divides by a scalar.
Divides each element by a scalar
[in] | a | the scalar to be divided by |
References q0, q1, q2, q3, and Quaternion().
Quaternion & Quaternion::operator/= | ( | Mdouble | a | ) |
Returns the rotated position
void Quaternion::rotate | ( | SmallVector< 3 > & | position | ) | const |
Applies the rotation to a position
Rotate the given vector from the body-fixed angles to the lab-fixed angles. This is the same as multiplying with the rotation matrix, A.
References A, and getRotationMatrix().
void Quaternion::rotate | ( | Vec3D & | position | ) | const |
Applies the rotation to a position
References q0, q1, q2, and q3.
Referenced by SuperQuadricParticle::computeShapeGradientLabFixed(), HorizontalBaseScrew::convertLimits(), NurbsWall::getDistanceAndNormal(), Screw::getDistanceAndNormal(), BasicIntersectionOfWalls::getDistanceAndNormal(), BasicUnionOfWalls::getDistanceAndNormal(), IntersectionOfWalls::getDistanceAndNormal(), LevelSetWall::getDistanceAndNormal(), InfiniteWall::getDistanceNormalOverlapSuperquadric(), BaseWall::getInteractionWith(), BasicIntersectionOfWalls::getVTK(), BasicUnionOfWalls::getVTK(), TriangleMeshWall::updateBoundingBoxGlobal(), TriangleWall::updateVertexAndNormal(), AxisymmetricIntersectionOfWalls::writeVTK(), BasicUnionOfWalls::writeVTK(), IntersectionOfWalls::writeVTK(), LevelSetWall::writeVTK(), NurbsWall::writeVTK(), Screw::writeVTK(), ScrewsymmetricIntersectionOfWalls::writeVTK(), NurbsWall::writeWallDetailsVTK(), and WearableNurbsWall::writeWallDetailsVTK().
Returns the inverse rotated position
void Quaternion::rotateBack | ( | Vec3D & | position | ) | const |
Applies the inverse rotation to a position
Rotate the given vector from the lab-fixed angles to the body-fixed angles. This is the same as multiplying with the inverse/transpose of the rotation matrix, A^T = A^{-1}.
References q0, q1, q2, and q3.
Referenced by SuperQuadricParticle::computeHessianLabFixed(), SuperQuadricParticle::computeShape(), SuperQuadricParticle::computeShapeGradientLabFixed(), AxisymmetricIntersectionOfWalls::convertLimits(), ScrewsymmetricIntersectionOfWalls::convertLimits(), NurbsWall::getDistanceAndNormal(), Screw::getDistanceAndNormal(), BasicIntersectionOfWalls::getDistanceAndNormal(), BasicUnionOfWalls::getDistanceAndNormal(), IntersectionOfWalls::getDistanceAndNormal(), LevelSetWall::getDistanceAndNormal(), InfiniteWall::getDistanceNormalOverlapSuperquadric(), BaseWall::getInteractionWith(), WearableNurbsWall::storeDebris(), ShapeGradientHessianTester::testCushion(), ShapeGradientHessianTester::testEllipsoid(), and ShapeGradientHessianTester::testRoundedBeam().
MatrixSymmetric3D Quaternion::rotateInverseInertiaTensor | ( | const MatrixSymmetric3D & | invI | ) | const |
Converts the inverse inertia tensor from the reference frame to the lab frame; see See QuaternionsWouter.pdf for details, where this operation is denoted by \(A*I^{-1}*A^T\).
References constants::i, q0, q1, q2, q3, MatrixSymmetric3D::XX, MatrixSymmetric3D::XY, MatrixSymmetric3D::XZ, MatrixSymmetric3D::YY, MatrixSymmetric3D::YZ, and MatrixSymmetric3D::ZZ.
void Quaternion::rotateTensor | ( | SmallMatrix< 3, 3 > | I | ) | const |
References A, and getRotationMatrix().
void Quaternion::setAngleZ | ( | Mdouble | psi | ) |
Converts the rotation angle in the XY plane into a quaternion (for Mercury2D). See Wikipedia for details.
References mathsFunc::cos(), q0, q1, q2, q3, and mathsFunc::sin().
Referenced by DPMBase::readNextDataFile().
void Quaternion::setComponent | ( | int | index, |
double | val | ||
) |
Sets the requested component of this Quaternion to the requested value.
Sets the element of the quaternion belonging to the first argument (index) to the value given in the second argument (val).
[in] | index | index of element of interest, |
[in] | val | value to be set |
void Quaternion::setEuler | ( | const Vec3D & | e | ) |
Convert Euler angles to a quaternion. See Wikipedia for details.
References mathsFunc::cos(), q0, q1, q2, q3, mathsFunc::sin(), Vec3D::X, Vec3D::Y, and Vec3D::Z.
Referenced by BaseParticle::oldRead(), DPMBase::readNextDataFile(), and BaseInteractable::setOrientationViaEuler().
void Quaternion::setLength | ( | Mdouble | length | ) |
Make this Quaternion a certain length |q|=length.
Sets the length of the quaternion to a given scalar (while maintaining the direction).
[in] | length | the length to be set |
References getLength().
void Quaternion::setOrientationViaNormal | ( | Vec3D | normal | ) |
Used to the the normal of an InfiniteWall that has a normal into the x-direction by default. This can be changed by resetting the wall orientation; thus the normal is the vector (1,0,0) rotated by this quaternion.
Defines one possible orientation that rotates the x-axis into the direction given by normal
[in] | normal | the vector that the x-axis is rotated into. |
References normalise(), Vec3D::normalise(), q0, q1, q2, q3, Quaternion(), Vec3D::X, Vec3D::Y, and Vec3D::Z.
Referenced by Quaternion(), and BaseInteractable::setOrientationViaNormal().
void Quaternion::setUnity | ( | ) |
Sets quaternion value to (1,0,0,0)
Sets q0 to 1, and all other elements to zero.
References q0, q1, q2, and q3.
Referenced by BaseInteractable::BaseInteractable(), and Quaternion().
void Quaternion::updateAngularDisplacement | ( | Vec3D | angularVelocityDt | ) |
References angularDisplacementTimeDerivative(), and normalise().
Referenced by BaseInteractable::rotate().
|
friend |
Multiplies all elements by a scalar.
Multiplies each element of a given quaternion (b) by a given scalar (a). NB: this is a global function and a friend of the Quaternion class. Gets called when a scalar multiplication of the form (Mdouble) * (Quaternion) is performed.
[in] | a | the scalar |
[in] | b | the quaternion |
|
friend |
Adds a scalar to a quaternion.
Adds a scalar to the elements of given quaternion NB this is a global function and a friend of the Quaternion class. Gets called when addition operation of the form (Mdouble) + (Quaternion) is performed.
[in] | a | the scalar to be added |
[in] | b | the quaternion the scalar gets added to. |
|
friend |
Subtracts a quaternion.
Returns the negative of a given quaternion. NB: this is a global function and a friend of the Quaternion class. Gets called when a negation operation of the form - (Quaternion) is performed.
[in] | a | the quaternion to be negated |
|
friend |
Subtracts the elements of a quaternion from a scalar.
Subtracts each element of a given quaternion from a scalar NB this is a global function and a friend of the Quaternion class. Gets called when subtraction operation of the form (Mdouble) - (Quaternion) is performed.
[in] | a | the scalar |
[in] | b | the quaternion to be subtracted the scalar gets subtracted from. |
|
friend |
Adds elements to an output stream.
Adds all elements of the quaternion to an output stream. NB: this is a global function and a friend of the Quaternion class!
[in] | os | the output stream, |
[in] | a | The quaternion of interest |
|
friend |
Adds elements to an input stream.
Reads all elements of a given quaternion from an input stream. NB: this is a global function and a friend of the Quaternion class!
[in,out] | is | the input stream |
[in,out] | a | the quaternion to be read in |
Mdouble Quaternion::q0 |
the zeroth component of the quaternion q = (q0,q1,q2,q3)
Referenced by angularDisplacementTimeDerivative(), angularVelocityBodyFixedFrameToAngularDisplacement(), applyCInverse(), getAngleZ(), getAxis(), getComponent(), getDistance(), getDistanceSquared(), getEuler(), getLengthSquared(), getRotationMatrix(), isUnity(), operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), Quaternion(), rotate(), rotateBack(), rotateInverseInertiaTensor(), setAngleZ(), setComponent(), setEuler(), setOrientationViaNormal(), and setUnity().
Mdouble Quaternion::q1 |
the first component of the quaternion q = (q0,q1,q2,q3)
Referenced by angularDisplacementTimeDerivative(), angularVelocityBodyFixedFrameToAngularDisplacement(), applyCInverse(), getAngleZ(), getAxis(), BaseWall::getAxis(), getComponent(), getDistance(), getDistanceSquared(), getEuler(), getLengthSquared(), getRotationMatrix(), isUnity(), operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), Quaternion(), rotate(), rotateBack(), rotateInverseInertiaTensor(), setAngleZ(), setComponent(), setEuler(), setOrientationViaNormal(), and setUnity().
Mdouble Quaternion::q2 |
the second component of the quaternion q = (q0,q1,q2,q3)
Referenced by angularDisplacementTimeDerivative(), angularVelocityBodyFixedFrameToAngularDisplacement(), applyCInverse(), getAngleZ(), getAxis(), BaseWall::getAxis(), getComponent(), getDistance(), getDistanceSquared(), getEuler(), getLengthSquared(), getRotationMatrix(), isUnity(), operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), Quaternion(), rotate(), rotateBack(), rotateInverseInertiaTensor(), setAngleZ(), setComponent(), setEuler(), setOrientationViaNormal(), and setUnity().
Mdouble Quaternion::q3 |
the third component of the quaternion q = (q0,q1,q2,q3)
Referenced by angularDisplacementTimeDerivative(), angularVelocityBodyFixedFrameToAngularDisplacement(), applyCInverse(), getAngleZ(), getAxis(), BaseWall::getAxis(), getComponent(), getDistance(), getDistanceSquared(), getEuler(), getLengthSquared(), getRotationMatrix(), isUnity(), operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), Quaternion(), rotate(), rotateBack(), rotateInverseInertiaTensor(), setAngleZ(), setComponent(), setEuler(), setOrientationViaNormal(), and setUnity().