Quaternion Class Reference

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...
 
Quaternionoperator+= (const Quaternion &a)
 Adds another quaternion. More...
 
Quaternionoperator-= (const Quaternion &a)
 Subtracts another quaternion. More...
 
Quaternionoperator*= (Mdouble a)
 Multiplies *this by a scalar. More...
 
Quaternionoperator/= (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...
 
static Quaternion multiplyQuaternions (const Quaternion &q, const Quaternion &r)
 
static Quaternion fromAxisAndAngle (const Vec3D &axis, const Mdouble &angle)
 Creates a quaternion from a given rotation axis and an angle. 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...
 

Detailed Description

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

Todo:
Need to generalize this to n-dimensional quaternions of any type

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.

Constructor & Destructor Documentation

◆ Quaternion() [1/3]

Quaternion::Quaternion ( )

Constructor; sets quaternion value to (1,0,0,0)

Default constructor

12 {
13  setUnity();
14 }
void setUnity()
Sets quaternion value to (1,0,0,0)
Definition: Quaternion.cc:34

References setUnity().

Referenced by angularDisplacementTimeDerivative(), angularVelocityBodyFixedFrameToAngularDisplacement(), fromAxisAndAngle(), operator*(), operator+(), operator-(), operator/(), and setOrientationViaNormal().

◆ Quaternion() [2/3]

Quaternion::Quaternion ( Mdouble  q0,
Mdouble  q1,
Mdouble  q2,
Mdouble  q3 
)

Alternative constructor. Sets quaternion value to (q0,q1,q2,q3)

Alternative constructor, lets you define all four elements.

Parameters
[in]q0the q0-component
[in]q1the q1-component
[in]q2the q2-component
[in]q3the q3-component
24 {
25  this->q0 = q0;
26  this->q1 = q1;
27  this->q2 = q2;
28  this->q3 = q3;
29 }
Mdouble q1
the first component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:52
Mdouble q3
the third component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:60
Mdouble q2
the second component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:56
Mdouble q0
the zeroth component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:48

References q0, q1, q2, and q3.

◆ Quaternion() [3/3]

Quaternion::Quaternion ( Vec3D  normal)
inline
Todo:
should be explicit as teh conversion is not unique
Parameters
normal
77  {
79  }
void setOrientationViaNormal(Vec3D normal)
Definition: Quaternion.cc:523
void normal(const Vector< double > &x, Vector< double > &normal)
Definition: free_surface_rotation.cc:65

References WallFunction::normal(), and setOrientationViaNormal().

Member Function Documentation

◆ angularDisplacementTimeDerivative()

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}

404 {
405  return 0.5 * Quaternion(
406  -q1 * v.X - q2 * v.Y - q3 * v.Z,
407  q0 * v.X + q3 * v.Y - q2 * v.Z,
408  -q3 * v.X + q0 * v.Y + q1 * v.Z,
409  q2 * v.X - q1 * v.Y + q0 * v.Z);
410 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Quaternion()
Constructor; sets quaternion value to (1,0,0,0)
Definition: Quaternion.cc:11

References q0, q1, q2, q3, Quaternion(), and v.

Referenced by updateAngularDisplacement().

◆ angularVelocityBodyFixedFrameToAngularDisplacement()

Quaternion Quaternion::angularVelocityBodyFixedFrameToAngularDisplacement ( Vec3D  v) const
Todo:
should the arguments be passed by reference?
Todo:
rename to angularVelocityBodyFixedFrameToAngularDisplacement?
393 {
394  return Quaternion(
395  -q1 * v.X - q2 * v.Y - q3 * v.Z,
396  q0 * v.X - q3 * v.Y + q2 * v.Z,
397  q3 * v.X + q0 * v.Y - q1 * v.Z,
398  -q2 * v.X + q1 * v.Y + q0 * v.Z);
399 }

References q0, q1, q2, q3, Quaternion(), and v.

◆ applyCInverse()

Vec3D Quaternion::applyCInverse ( Quaternion  q) const

Converts quaternion rate of change into an angular momentum omega.

Todo:
rename to angularDisplacementToAngularVelocity?
423 {
424  return 2.0 * Vec3D(
425  -q1 * q.q0 + q0 * q.q1 - q3 * q.q2 + q2 * q.q3,
426  -q2 * q.q0 + q3 * q.q1 + q0 * q.q2 - q1 * q.q3,
427  -q3 * q.q0 - q2 * q.q1 + q1 * q.q2 + q0 * q.q3);
428 }
Definition: Kernel/Math/Vector.h:30
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:2019

References Eigen::numext::q, q0, q1, q2, and q3.

◆ fromAxisAndAngle()

static Quaternion Quaternion::fromAxisAndAngle ( const Vec3D axis,
const Mdouble angle 
)
inlinestatic

Creates a quaternion from a given rotation axis and an angle.

Parameters
axisThe rotation axis
angleThe angle to rotate around the axis
339  {
340  double axisMagnitude = axis.getLength();
341  logger.assert_debug(axisMagnitude > 0, "Quaternion::fromAxisAndangle: Axis Magnitude is %. Cannot compute a quaternion.", axisMagnitude);
342 
343  Mdouble halfAngle = angle / 2.0;
344  Mdouble sinHalfAngle = std::sin(halfAngle);
345  Mdouble q0 = std::cos(halfAngle);
346  Mdouble q1 = axis.X * sinHalfAngle / axisMagnitude;
347  Mdouble q2 = axis.Y * sinHalfAngle / axisMagnitude;
348  Mdouble q3 = axis.Z * sinHalfAngle / axisMagnitude;
349  return Quaternion(q0, q1, q2, q3);
350  };
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
double Mdouble
Definition: GeneralDefine.h:13
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Mdouble Y
Definition: Kernel/Math/Vector.h:45
Mdouble Z
Definition: Kernel/Math/Vector.h:45
Mdouble X
the vector components
Definition: Kernel/Math/Vector.h:45
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:350
double angle(const double &t)
Angular position as a function of time t.
Definition: jeffery_orbit.cc:98

References Jeffery_Solution::angle(), cos(), Vec3D::getLength(), logger, q0, q1, q2, q3, Quaternion(), sin(), Vec3D::X, Vec3D::Y, and Vec3D::Z.

◆ getAngleZ()

Mdouble Quaternion::getAngleZ ( ) const

Converts a quaternion to the rotation angle in the XY plane (for Mercury2D). See Wikipedia for details.

468 {
469  return -std::atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));
470 }
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:139

References atan2(), q0, q1, q2, and q3.

Referenced by MarbleRun::actionsAfterTimeStep().

◆ getAxis()

Vec3D Quaternion::getAxis ( ) const

Converts the quaternions into a normal vector by rotating the vector x=(1,0,0); see See Wiki for details.

482 {
483  const Mdouble q00 = q0*q0;
484  const Mdouble q11 = q1*q1;
485  const Mdouble q22 = q2*q2;
486  const Mdouble q33 = q3*q3;
487  const Mdouble q12 = q1*q2;
488  const Mdouble q30 = q3*q0;
489  const Mdouble q13 = q1*q3;
490  const Mdouble q20 = q2*q0;
491  return Vec3D(q00 - q33 + q11 - q22, 2.0 * (q12 + q30), 2.0 * (q13 - q20));
492 }

References q0, q1, q2, and q3.

Referenced by InfiniteWall::createVTK(), AxisymmetricIntersectionOfWalls::getDistanceAndNormal(), HorizontalBaseScrew::getDistanceAndNormal(), InfiniteWall::getDistanceAndNormal(), InfiniteWall::getDistanceNormalOverlapSuperquadric(), and InfiniteWall::getNormal().

◆ getComponent()

Mdouble Quaternion::getComponent ( int  index) const

Returns the requested component of this Quaternion.

returns the quaternion element belonging to the given index.

Parameters
[in]indexthe index of interest (should be 0, 1 or 2)
Returns
the value of the quaternion element belonging to the given index
214 {
215  switch (index)
216  {
217  case 0:
218  return q0;
219  case 1:
220  return q1;
221  case 2:
222  return q2;
223  case 3:
224  return q3;
225  default:
226  logger(ERROR,
227  "[Quaternion::getComponent] Index = %, which is too high for a 3D quaternion (should be 0-2).",
228  index);
229  return 0;
230  }
231 }
@ ERROR

◆ getDistance() [1/2]

Mdouble Quaternion::getDistance ( const Quaternion a,
const Quaternion b 
)
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!

Parameters
[in]athe first quaternion
[in]bthe second quaternion
Returns
the distance between the two arguments.
170 {
171  return std::sqrt(getDistanceSquared(a, b));
172 }
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
Scalar * b
Definition: benchVecAdd.cpp:17
static Mdouble getDistanceSquared(const Quaternion &a, const Quaternion &b)
Calculates the squared distance between two Quaternion: .
Definition: Quaternion.cc:182
const Scalar * a
Definition: level2_cplx_impl.h:32

Referenced by InfiniteWall::getDistance(), and Quaternion< float >::rotateInverseInertiaTensor().

◆ getDistance() [2/2]

Mdouble Quaternion::getDistance ( Vec3D  p,
Vec3D  p0 
) const

Calculates the distance from a wall through p0 whose normal is the vector (1,0,0). Used for the calculation of the distance in InfiniteWalls

639 {
640  const Mdouble q00 = q0 * q0;
641  const Mdouble q02 = 2 * q0 * q2;
642  const Mdouble q03 = 2 * q0 * q3;
643  const Mdouble q11 = q1 * q1;
644  const Mdouble q12 = 2 * q1 * q2;
645  const Mdouble q13 = 2 * q1 * q3;
646  const Mdouble q22 = q2 * q2;
647  const Mdouble q33 = q3 * q3;
648  return (q00 + q11 - q22 - q33) * (p0.X - p.X) +
649  ((q12 + q03) * (p0.Y - p.Y) + (q13 - q02) * (p0.Z - p.Z));
650 }
Vector3f p0
Definition: MatrixBase_all.cpp:2
float * p
Definition: Tutorial_Map_using.cpp:9

References p, p0, q0, q1, q2, and q3.

◆ getDistanceSquared()

Mdouble Quaternion::getDistanceSquared ( const Quaternion a,
const Quaternion b 
)
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!

Parameters
[in]athe first quaternion
[in]bthe second quaternion
Returns
the square of the distance between the two arguments.
183 {
184  return ((a.q0 - b.q0) * (a.q0 - b.q0) + (a.q1 - b.q1) * (a.q1 - b.q1) + (a.q2 - b.q2) * (a.q2 - b.q2) +
185  (a.q3 - b.q3) * (a.q3 - b.q3));
186 }

Referenced by Quaternion< float >::getLength().

◆ getEuler()

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

434 {
435  Mdouble sinp = 2 * (q0 * q2 - q3 * q1);
436  Mdouble pitch;
437 
438  if ((std::abs(sinp) < 1))
439  {
440  pitch = std::asin(sinp);
441  }
442  else
443  {
444  pitch = copysign(constants::pi, sinp);
445  }
446 
447  return Vec3D(
448  std::atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2)),
449  pitch,
450  std::atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3)));
451 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 asin(const bfloat16 &a)
Definition: BFloat16.h:634
const Mdouble pi
Definition: ExtendedMath.h:23

References abs(), Eigen::bfloat16_impl::asin(), atan2(), constants::pi, q0, q1, q2, and q3.

◆ getLength() [1/2]

Mdouble Quaternion::getLength ( ) const

Calculates the length of this Quaternion: \( \sqrt{a\cdot a} \).

Calculates the length of this quaternion

Returns
the (scalar) length of this quaternion
281 {
282  return std::sqrt(getLengthSquared());
283 }
Mdouble getLengthSquared() const
Calculates the squared length of this Quaternion: .
Definition: Quaternion.cc:203

Referenced by setLength().

◆ getLength() [2/2]

Mdouble Quaternion::getLength ( const Quaternion a)
static

Calculates the length of a Quaternion: \( \sqrt{a\cdot a} \).

Calculates the length of a given quaternion NB: this is a STATIC function!

Parameters
[in]aquaternion to be measured.
Returns
length of the argument.
292 {
293  return a.getLength();
294 }

◆ getLengthSquared() [1/2]

Mdouble Quaternion::getLengthSquared ( ) const

Calculates the squared length of this Quaternion: \( a\cdot a \).

Calculates the square of the length of itself

Returns
the square of the length of this quaternion
204 {
205  return (q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
206 }

References q0, q1, q2, and q3.

Referenced by Quaternion< float >::getAxis(), and normalise().

◆ getLengthSquared() [2/2]

Mdouble Quaternion::getLengthSquared ( const Quaternion a)
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!

Parameters
[in]athe quaternion.
Returns
the square of the length of the argument.
195 {
196  return (a.q0 * a.q0 + a.q1 * a.q1 + a.q2 * a.q2 + a.q3 * a.q3);
197 }

◆ getRotationMatrix()

void Quaternion::getRotationMatrix ( SmallMatrix< 3, 3 > &  A) const

Retrieves the rotation matrix

Parameters
[out]AThe rotation matrix
496 {
497 
498  Mdouble q00 = q0 * q0;
499  Mdouble q01 = 2 * q0 * q1;
500  Mdouble q02 = 2 * q0 * q2;
501  Mdouble q03 = 2 * q0 * q3;
502  Mdouble q11 = q1 * q1;
503  Mdouble q12 = 2 * q1 * q2;
504  Mdouble q13 = 2 * q1 * q3;
505  Mdouble q22 = q2 * q2;
506  Mdouble q23 = 2 * q2 * q3;
507  Mdouble q33 = q3 * q3;
508  A(0, 0) = q00 + q11 - q22 - q33;
509  A(1, 0) = q12 + q03;
510  A(2, 0) = q13 - q02;
511  A(0, 1) = q12 - q03;
512  A(1, 1) = q00 - q11 + q22 - q33;
513  A(2, 1) = q23 + q01;
514  A(0, 2) = q13 + q02;
515  A(1, 2) = q23 - q01;
516  A(2, 2) = q00 - q11 - q22 + q33;
517 }
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47

References q0, q1, q2, and q3.

Referenced by SuperQuadricParticle::computeHessianLabFixed(), rotate(), and rotateTensor().

◆ getUnitQuaternion()

Quaternion Quaternion::getUnitQuaternion ( const Quaternion a)
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!

Parameters
[in]athe quaternion of interest
Returns
unit quaternion in the direction of the argument (unless the argument has length zero; in that case a zero-quaternion).
306 {
307  Mdouble Length2 = a.getLengthSquared();
308  if (Length2 != 0.0)
309  return a / std::sqrt(Length2);
310  else
311  return Quaternion(1, 0, 0, 0);
312 }

◆ isEqualTo()

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).

Parameters
[in]otherthe 3D quaternion to check against
[in]tolthe tolerance
Returns
returns TRUE if the difference between the lengths of this quaternion and that given in the first argument (other) is smaller than the given tolerance.
272 {
273  return (getDistance(*this, other) <= tol * tol);
274 }
static Mdouble getDistance(const Quaternion &a, const Quaternion &b)
Calculates the distance between two Quaternion: .
Definition: Quaternion.cc:169

◆ isUnity()

bool Quaternion::isUnity ( ) const
inline

Checks if the quaternion value is (1,0,0,0)

Checks if ALL elements are zero

Returns
TRUE if q0 equals one and ALL other elements are zero
Bug:
use isEqual instead of ==
93  {
94  return q0 == 1.0 && q1 == 0.0 && q2 == 0.0 && q3 == 0.0;
95  };

References q0, q1, q2, and q3.

◆ multiplyQuaternions()

static Quaternion Quaternion::multiplyQuaternions ( const Quaternion q,
const Quaternion r 
)
inlinestatic
325  {
326  double w = q.q0 * r.q0 - q.q1 * r.q1 - q.q2 * r.q2 - q.q3 * r.q3;
327  double x = q.q0 * r.q1 + q.q1 * r.q0 + q.q2 * r.q3 - q.q3 * r.q2;
328  double y = q.q0 * r.q2 - q.q1 * r.q3 + q.q2 * r.q0 + q.q3 * r.q1;
329  double z = q.q0 * r.q3 + q.q1 * r.q2 - q.q2 * r.q1 + q.q3 * r.q0;
330  return {w, x, y, z};
331  }
RowVector3d w
Definition: Matrix_resize_int.cpp:3
Scalar * y
Definition: level1_cplx_impl.h:128
r
Definition: UniformPSDSelfTest.py:20
list x
Definition: plotDoE.py:28

References Eigen::numext::q, UniformPSDSelfTest::r, w, plotDoE::x, and y.

◆ normalise()

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).

143 {
144  const Mdouble length2 = getLengthSquared();
145  if (length2 == 0)
146  {
147  logger(ERROR, "Normalizing a quaternion of length 0");
148  }
149  *this /= sqrt(length2);
150 }

References getLengthSquared().

Referenced by setOrientationViaNormal(), and updateAngularDisplacement().

◆ operator*()

Quaternion Quaternion::operator* ( Mdouble  a) const

Multiplies by a scalar.

Multiplies each element with a scalar

Parameters
[in]athe scalar to be multiplied with
Returns
the resulting quaternion
68 {
69  return Quaternion(q0 * a, q1 * a, q2 * a, q3 * a);
70 }

References a, q0, q1, q2, q3, and Quaternion().

◆ operator*=()

Quaternion & Quaternion::operator*= ( Mdouble  a)

Multiplies *this by a scalar.

Multiplies each element by a scalar

Parameters
[in]ascalar to be multiplied by
Returns
(reference to) itself, i.e. resulting quaternion
116 {
117  q0 *= a;
118  q1 *= a;
119  q2 *= a;
120  q3 *= a;
121  return *this;
122 }

References a, q0, q1, and q2.

◆ operator+()

Quaternion Quaternion::operator+ ( const Quaternion a) const

Adds another quaternion and returns the result.

Adds quaternion to itself

Parameters
[in]aquaternion to be added
Returns
resulting 3D quaternion
48 {
49  return Quaternion(q0 + a.q0, q1 + a.q1, q2 + a.q2, q3 + a.q3);
50 }

References a, q0, q1, q2, q3, and Quaternion().

◆ operator+=()

Quaternion & Quaternion::operator+= ( const Quaternion a)

Adds another quaternion.

Adds a quaternion to itself

Parameters
[in]aquaternion to be added
Returns
(reference to) itself, i.e. resulting quaternion
88 {
89  q0 += a.q0;
90  q1 += a.q1;
91  q2 += a.q2;
92  q3 += a.q3;
93  return *this;
94 }

References a, q0, q1, q2, and q3.

◆ operator-()

Quaternion Quaternion::operator- ( const Quaternion a) const

Subtracts another quaternion and returns the result.

Subtracts quaternion from itself

Parameters
[in]aquaternion to be subtracted
Returns
resulting quaternion
58 {
59  return Quaternion(q0 - a.q0, q1 - a.q1, q2 - a.q2, q3 - a.q3);
60 }

References a, q0, q1, q2, q3, and Quaternion().

◆ operator-=()

Quaternion & Quaternion::operator-= ( const Quaternion a)

Subtracts another quaternion.

Subtracts a quaternion from itself

Parameters
[in]aquaternion to be subtracted
Returns
(reference to) itself, i.e. resulting quaternion
102 {
103  q0 -= a.q0;
104  q1 -= a.q1;
105  q2 -= a.q2;
106  q3 -= a.q3;
107  return *this;
108 }

References a, q0, and q1.

◆ operator/()

Quaternion Quaternion::operator/ ( Mdouble  a) const

Divides by a scalar.

Divides each element by a scalar

Parameters
[in]athe scalar to be divided by
Returns
resulting quaternion
78 {
79  return Quaternion(q0 / a, q1 / a, q2 / a, q3 / a);
80 }

References a, q0, q1, q2, q3, and Quaternion().

◆ operator/=()

Quaternion & Quaternion::operator/= ( Mdouble  a)

Divides by a scalar.

Divides each element by a scalar

Parameters
[in]ascalar to be divided by
Returns
(reference to) itself, i.e. resulting quaternion
130 {
131  q0 /= a;
132  q1 /= a;
133  q2 /= a;
134  q3 /= a;
135  return *this;
136 }

◆ rotate() [1/3]

Vec3D Quaternion::rotate ( const Vec3D position) const

Returns the rotated position

569 {
570  Mdouble q00 = q0 * q0;
571  Mdouble q01 = 2 * q0 * q1;
572  Mdouble q02 = 2 * q0 * q2;
573  Mdouble q03 = 2 * q0 * q3;
574  Mdouble q11 = q1 * q1;
575  Mdouble q12 = 2 * q1 * q2;
576  Mdouble q13 = 2 * q1 * q3;
577  Mdouble q22 = q2 * q2;
578  Mdouble q23 = 2 * q2 * q3;
579  Mdouble q33 = q3 * q3;
580  return Matrix3D(
581  q00 + q11 - q22 - q33, q12 - q03, q13 + q02,
582  q12 + q03, q00 - q11 + q22 - q33, q23 - q01,
583  q13 - q02, q23 + q01, q00 - q11 - q22 + q33) * position;
584 }
Implementation of a 3D matrix.
Definition: Kernel/Math/Matrix.h:17

References q0, q1, q2, and q3.

◆ rotate() [2/3]

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.

589 {
592  position = A * position;
593 }
void getRotationMatrix(SmallMatrix< 3, 3 > &A) const
Definition: Quaternion.cc:495
Data type for small dense matrix.
Definition: SmallMatrix.h:48

References getRotationMatrix().

◆ rotate() [3/3]

void Quaternion::rotate ( Vec3D position) const

◆ rotateBack() [1/2]

Vec3D Quaternion::rotateBack ( const Vec3D position) const

Returns the inverse rotated position

616 {
617  Mdouble q00 = q0 * q0;
618  Mdouble q01 = 2 * q0 * q1;
619  Mdouble q02 = 2 * q0 * q2;
620  Mdouble q03 = 2 * q0 * q3;
621  Mdouble q11 = q1 * q1;
622  Mdouble q12 = 2 * q1 * q2;
623  Mdouble q13 = 2 * q1 * q3;
624  Mdouble q22 = q2 * q2;
625  Mdouble q23 = 2 * q2 * q3;
626  Mdouble q33 = q3 * q3;
627  return Matrix3D(
628  q00 + q11 - q22 - q33, q12 + q03, q13 - q02,
629  q12 - q03, q00 - q11 + q22 - q33, q23 + q01,
630  q13 + q02, q23 - q01, q00 - q11 - q22 + q33) * position;
631 }

References q0, q1, q2, and q3.

◆ rotateBack() [2/2]

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}.

598 {
599  Mdouble q00 = q0 * q0;
600  Mdouble q01 = 2 * q0 * q1;
601  Mdouble q02 = 2 * q0 * q2;
602  Mdouble q03 = 2 * q0 * q3;
603  Mdouble q11 = q1 * q1;
604  Mdouble q12 = 2 * q1 * q2;
605  Mdouble q13 = 2 * q1 * q3;
606  Mdouble q22 = q2 * q2;
607  Mdouble q23 = 2 * q2 * q3;
608  Mdouble q33 = q3 * q3;
609  position = Matrix3D(
610  q00 + q11 - q22 - q33, q12 + q03, q13 - q02,
611  q12 - q03, q00 - q11 + q22 - q33, q23 + q01,
612  q13 + q02, q23 - q01, q00 - q11 - q22 + q33) * position;
613 }

References q0, q1, q2, and q3.

Referenced by SuperQuadricParticle::computeHessianLabFixed(), SuperQuadricParticle::computeShape(), SuperQuadricParticle::computeShapeGradientLabFixed(), NurbsWall::getDistanceAndNormal(), Screw::getDistanceAndNormal(), BasicIntersectionOfWalls::getDistanceAndNormal(), BasicUnionOfWalls::getDistanceAndNormal(), IntersectionOfWalls::getDistanceAndNormal(), LevelSetWall::getDistanceAndNormal(), and WearableNurbsWall::storeDebris().

◆ rotateInverseInertiaTensor()

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\).

Todo:
move link to where it belongs http://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another This is the same as rotateTensor, but than for MatrixSymmetric3D instead of SmallMatrix<3,3>.
658 {
659  Mdouble a = 1 - 2 * q2 * q2 - 2 * q3 * q3;
660  Mdouble b = 2 * q1 * q2 - 2 * q0 * q3;
661  Mdouble c = 2 * q1 * q3 + 2 * q0 * q2;
662  Mdouble d = 2 * q1 * q2 + 2 * q0 * q3;
663  Mdouble e = 1 - 2 * q1 * q1 - 2 * q3 * q3;
664  Mdouble f = 2 * q2 * q3 - 2 * q0 * q1;
665  Mdouble g = 2 * q1 * q3 - 2 * q0 * q2;
666  Mdouble h = 2 * q2 * q3 + 2 * q0 * q1;
667  Mdouble i = 1 - 2 * q1 * q1 - 2 * q2 * q2;
669  invI.XX * a * a + 2 * invI.XY * a * b + 2 * invI.XZ * a * c + invI.YY * b * b + 2 * invI.YZ * b * c +
670  invI.ZZ * c * c,
671  d * (invI.XX * a + invI.XY * b + invI.XZ * c) + e * (invI.XY * a + invI.YY * b + invI.YZ * c) +
672  f * (invI.XZ * a + invI.YZ * b + invI.ZZ * c),
673  g * (invI.XX * a + invI.XY * b + invI.XZ * c) + h * (invI.XY * a + invI.YY * b + invI.YZ * c) +
674  i * (invI.XZ * a + invI.YZ * b + invI.ZZ * c),
675  invI.XX * d * d + 2 * invI.XY * d * e + 2 * invI.XZ * d * f + invI.YY * e * e + 2 * invI.YZ * e * f +
676  invI.ZZ * f * f,
677  g * (invI.XX * d + invI.XY * e + invI.XZ * f) + h * (invI.XY * d + invI.YY * e + invI.YZ * f) +
678  i * (invI.XZ * d + invI.YZ * e + invI.ZZ * f),
679  invI.XX * g * g + 2 * invI.XY * g * h + 2 * invI.XZ * g * i + invI.YY * h * h + 2 * invI.YZ * h * i +
680  invI.ZZ * i * i
681  );
682  return ans;
683 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Implementation of a 3D symmetric matrix.
Definition: MatrixSymmetric.h:16
Mdouble ZZ
Definition: MatrixSymmetric.h:21
Mdouble YY
Definition: MatrixSymmetric.h:21
Mdouble XZ
Definition: MatrixSymmetric.h:21
Mdouble XY
Definition: MatrixSymmetric.h:21
Mdouble XX
The six distinctive matrix elements.
Definition: MatrixSymmetric.h:21
Mdouble YZ
Definition: MatrixSymmetric.h:21
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
Definition: cxx11_tensor_map.cpp:237
int c
Definition: calibrate.py:100

References a, b, calibrate::c, e(), f(), i, q0, q1, q2, q3, MatrixSymmetric3D::XX, MatrixSymmetric3D::XY, MatrixSymmetric3D::XZ, MatrixSymmetric3D::YY, MatrixSymmetric3D::YZ, and MatrixSymmetric3D::ZZ.

◆ rotateTensor()

void Quaternion::rotateTensor ( SmallMatrix< 3, 3 >  I) const
686 {
689  I = A * I * A.transpose();
690 }
#define I
Definition: main.h:127

References getRotationMatrix(), and I.

◆ setAngleZ()

void Quaternion::setAngleZ ( Mdouble  psi)

Converts the rotation angle in the XY plane into a quaternion (for Mercury2D). See Wikipedia for details.

473 {
474  //assuming theta=phi=0
475  q0 = std::cos(-0.5 * psi);
476  q1 = 0;
477  q2 = 0;
478  q3 = std::sin(-0.5 * psi);
479 }

References cos(), q0, q1, q2, q3, and sin().

◆ setComponent()

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).

Parameters
[in]indexindex of element of interest,
[in]valvalue to be set
240 {
241  switch (index)
242  {
243  case 0:
244  q0 = val;
245  break;
246  case 1:
247  q1 = val;
248  break;
249  case 2:
250  q2 = val;
251  break;
252  case 3:
253  q3 = val;
254  break;
255  default:
256  logger(ERROR,
257  "[Quaternion::setComponent] Index = %, which is too high for a 3D quaternion (should be 0-2).",
258  index);
259  }
260 }
val
Definition: calibrate.py:119

◆ setEuler()

void Quaternion::setEuler ( const Vec3D e)

Convert Euler angles to a quaternion. See Wikipedia for details.

454 {
455  Mdouble cp = std::cos(0.5 * e.Y);
456  Mdouble sp = std::sin(0.5 * e.Y);
457  Mdouble cr = std::cos(0.5 * e.X);
458  Mdouble sr = std::sin(0.5 * e.X);
459  Mdouble cy = std::cos(0.5 * e.Z);
460  Mdouble sy = std::sin(0.5 * e.Z);
461  q0 = cr * cp * cy + sr * sp * sy;
462  q1 = sr * cp * cy - cr * sp * sy;
463  q2 = cr * sp * cy + sr * cp * sy;
464  q3 = cr * cp * sy - sr * sp * cy;
465 }

References cos(), e(), q0, q1, q2, q3, and sin().

Referenced by BaseInteractable::setOrientationViaEuler().

◆ setLength()

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).

Parameters
[in]lengththe length to be set
158 {
159  *this /= this->getLength() * length;
160 }
Mdouble getLength() const
Calculates the length of this Quaternion: .
Definition: Quaternion.cc:280

References getLength().

◆ setOrientationViaNormal()

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

Parameters
[in]normalthe vector that the x-axis is rotated into.
524 {
525  //if the normal vector cannot be read properly
526 // if (normal.getLengthSquared() < 1e-20)
527 // {
528 // setUnity();
529 // return;
530 // }
531 
532  normal.normalise();
533 
534  if (normal.X <= -1 + 1e-14)
535  {
536  *this = Quaternion(0, 0, 1, 0);
537  return;
538  }
539 
540  Vec3D half = Vec3D(1, 0, 0) + normal;
541  q0 = half.X;
542  q1 = 0;
543  q2 = -half.Z;
544  q3 = half.Y;
545  normalise();
546  //note, it can technically happen that normalising a normalised vector slightly changes the result.
547 }
void normalise()
Makes this Quaternion unit length |q|=1.
Definition: Quaternion.cc:142

References e(), WallFunction::normal(), normalise(), q0, q1, q2, q3, Quaternion(), Vec3D::X, Vec3D::Y, and Vec3D::Z.

Referenced by Quaternion(), and BaseInteractable::setOrientationViaNormal().

◆ setUnity()

void Quaternion::setUnity ( )

Sets quaternion value to (1,0,0,0)

Sets q0 to 1, and all other elements to zero.

35 {
36  q0 = 1.0;
37  q1 = 0.0;
38  q2 = 0.0;
39  q3 = 0.0;
40 }

References q0, q1, q2, and q3.

Referenced by BaseInteractable::BaseInteractable(), and Quaternion().

◆ updateAngularDisplacement()

void Quaternion::updateAngularDisplacement ( Vec3D  angularVelocityDt)
413 {
414  *this += angularDisplacementTimeDerivative(angularVelocityDt);
415  //const Quaternion q = *this;
416  normalise();
417  //logger(INFO,"%|%",q,*this);
418 }
Quaternion angularDisplacementTimeDerivative(Vec3D v) const
Converts an angular momentum v=omega into a quaternion rate of change, q(t+dt)-q(t)/dt.
Definition: Quaternion.cc:403

References angularDisplacementTimeDerivative(), and normalise().

Referenced by BaseInteractable::rotate().

Friends And Related Function Documentation

◆ operator*

Quaternion operator* ( Mdouble  a,
const Quaternion b 
)
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.

Parameters
[in]athe scalar
[in]bthe quaternion
Returns
the resulting quaternion
387 {
388  return Quaternion(b.q0 * a, b.q1 * a, b.q2 * a, b.q3 * a);
389 }

◆ operator+

Quaternion operator+ ( Mdouble  a,
const Quaternion b 
)
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.

Parameters
[in]athe scalar to be added
[in]bthe quaternion the scalar gets added to.
Returns
the resulting quaternion.
349 {
350  return Quaternion(b.q0 + a, b.q1 + a, b.q2 + a, b.q3 + a);
351 }

◆ operator- [1/2]

Quaternion operator- ( const Quaternion a)
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.

Parameters
[in]athe quaternion to be negated
Returns
the negated quaternion
374 {
375  return Quaternion(-a.q0, -a.q1, -a.q2, -a.q3);
376 }

◆ operator- [2/2]

Quaternion operator- ( Mdouble  a,
const Quaternion b 
)
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.

Parameters
[in]athe scalar
[in]bthe quaternion to be subtracted the scalar gets subtracted from.
Returns
the resulting quaternion.
362 {
363  return Quaternion(a - b.q0, a - b.q1, a - b.q2, a - b.q3);
364 }

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Quaternion a 
)
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!

Parameters
[in]osthe output stream,
[in]aThe quaternion of interest
Returns
the output stream with quaternion elements added
322 {
323  os << a.q0 << ' ' << a.q1 << ' ' << a.q2 << ' ' << a.q3;
324  return os;
325 }

◆ operator>>

std::istream& operator>> ( std::istream &  is,
Quaternion a 
)
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!

Parameters
[in,out]isthe input stream
[in,out]athe quaternion to be read in
Returns
the input stream from which the quaternion elements were read
335 {
336  is >> a.q0 >> a.q1 >> a.q2 >> a.q3;
337  return is;
338 }

Member Data Documentation

◆ q0

◆ q1

◆ q2

◆ q3


The documentation for this class was generated from the following files: