World of Rigid Bodies (WoRB)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
WoRB::Quaternion Class Reference

Encapsulates Hamilton real quaternions. More...

#include <Quaternion.h>

Inheritance diagram for WoRB::Quaternion:

Public Member Functions

void Dump (const char *name) const
Indexing operators
double operator[] (unsigned index) const
double & operator[] (unsigned index)
Methods acting on quaterinion components
QuaternionNormalize (double length=1.0)
 Normalizes the quaternion to a given length.
QuaternionTrim (double size)
 Limits the size of the quaternion to the given maximum.
QuaternionZeroize (double eps=1e-4)
 Removes the quaternion components that are bellow the limit.
Unary operations
Quaternion operator- () const
 Flips signs of all the components of the quaternion.
Quaternion Conjugate () const
 Returns the conjugate of the quaternion.
double SquaredNorm () const
 Gets the squared magnitude of this quaternion.
double ReSquaredNorm () const
 Gets the squared magnitude of the scalar part of this quaternion.
double ImSquaredNorm () const
 Gets the squared magnitude of the vector part of this quaternion.
double Norm () const
 Gets the magnitude of this quaternion.
double ImNorm () const
 Gets the magnitude of the vector part of this quaternion.
Quaternion Unit (double length=1.0) const
 Returns the normalized version (versor) of a quaternion.
Binary operations
Quaternion operator+ (const Quaternion &q) const
 Returns the value of the given quaternion added to this.
Quaternionoperator+= (const Quaternion &q)
 Adds the given quaternion to this.
Quaternion operator- (const Quaternion &q) const
 Returns the value of the given quaternion subtracted from this.
Quaternion operator-= (const Quaternion &q)
 Subtracts the given quaternion from this.
Quaternion operator* (const Quaternion &q) const
 Multiplies the quaternion by the given quaternion.
Quaternionoperator*= (const Quaternion &right)
 Multiplies the quaternion by the given quaternion.
Quaternion operator* (double value) const
 Returns a copy of this quaternion scaled the given scalar value.
Quaternionoperator*= (double value)
 Multiplies this quaternion by the given scalar value.
Quaternion ComponentWiseProduct (const Quaternion &q) const
 Returns a component-wise product of this and another quaternion.
Quaternion Cross (const Quaternion &q) const
 Returns a vector product of the vector parts of this and another quaternion.
double Dot (const Quaternion &q) const
 Returns a scalar product of the vector parts of this and another quaternion.
Comparison operators
bool operator== (const Quaternion &q) const
 Checks if the two quaternions have identical components.
bool operator!= (const Quaternion &q) const
 Checks if the two vectors have non-identical components.
bool operator< (const Quaternion &q) const
 Checks if this quaternion is component-by-component less than the other.
bool operator> (const Quaternion &q) const
 Checks if this quaternion is component-by-component less than the other.
bool operator<= (const Quaternion &q) const
 Checks if this quaternion is component-by-component less than the other.
bool operator>= (const Quaternion &q) const
 Checks if this quaternion is component-by-component less than the other.

Data Fields

Quaternion components
  • w: holds the real (aka scalar) part of the quaternion.
  • x, y, z: holds the pure imaginary (vector) part of the quaternion.

See Quaternions on Wikipedia

double w
 Holds the real (scalar) part of the quaternion.
double x
 Holds i-component of the pure imaginary (vector) part.
double y
 Holds j-component of the pure imaginary (vector) part.
double z
 Holds k-component of the pure imaginary (vector) part.

Constructors and assignment operators

 Quaternion ()
 The default constructor; creates a zero quaternion.
 Quaternion (double scalar)
 Creates a quaternion having only the scalar (real) part.
 Quaternion (double w, double x, double y, double z)
 Creates a quaternion with the given components.
Quaternionoperator= (double scalar)
 Sets the scalar (real) part of the quaternion also clearing the vector part.
static Quaternion FromAxisAngle (double angle, double vx, double vy, double vz)
 Constructs a quaternion from axis angle representation of rotation.

Detailed Description

Encapsulates Hamilton real quaternions.

Note:
The Quaternion class is implemented with inline methods only.

Definition at line 25 of file Quaternion.h.


Constructor & Destructor Documentation

The default constructor; creates a zero quaternion.

Definition at line 50 of file Quaternion.h.

Referenced by ComponentWiseProduct(), Conjugate(), Cross(), FromAxisAngle(), operator*(), operator+(), operator-(), and Unit().

            : w(0), x(0), y(0), z(0) 
        {
        }
WoRB::Quaternion::Quaternion ( double  scalar) [inline]

Creates a quaternion having only the scalar (real) part.

Definition at line 57 of file Quaternion.h.

            : w(scalar), x(0), y(0), z(0) 
        {
        }
WoRB::Quaternion::Quaternion ( double  w,
double  x,
double  y,
double  z 
) [inline]

Creates a quaternion with the given components.

Definition at line 64 of file Quaternion.h.

            : w(w), x(x), y(y), z(z)
        {
        }

Member Function Documentation

Returns a component-wise product of this and another quaternion.

Definition at line 290 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

Referenced by WoRB::Cuboid::Check(), WoRB::Cuboid::RegisterContactOnAxis_Thorough(), and WoRB::Cuboid::SetMass().

        {
            return Quaternion( w * q.w, x * q.x, y * q.y, z * q.z);
        }

Returns the conjugate of the quaternion.

Definition at line 173 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

        {
            return Quaternion( w, -x, -y, -z );
        }
void Quaternion::Dump ( const char *  name) const

Definition at line 19 of file WoRB.cpp.

References WoRB::Printf(), w, x, y, and z.

Referenced by WoRB::Collision::Dump().

{
    Printf( "%10s : %12.4lf %12.4lf %12.4lf | %12.4lf\n", name, x, y, z, w );
}
static Quaternion WoRB::Quaternion::FromAxisAngle ( double  angle,
double  vx,
double  vy,
double  vz 
) [inline, static]

Constructs a quaternion from axis angle representation of rotation.

Definition at line 80 of file Quaternion.h.

References Quaternion().

Referenced by WoRB_TestBed::ReconfigureTestBed().

        {
            double norm = sqrt( vx * vx + vy * vy + vz * vz );

            if ( norm == 0 ) {
                return Quaternion( 1 );
            }

            double Re = cos( angle/2 );
            double Im = sin( angle/2 ) / norm;

            return Quaternion( Re, Im * vx, Im * vy, Im * vz );
        }
double WoRB::Quaternion::ImNorm ( ) const [inline]

Gets the magnitude of the vector part of this quaternion.

Definition at line 208 of file Quaternion.h.

References ImSquaredNorm().

Referenced by WoRB::Sphere::Check(), and WoRB::Collision::PositionProjection().

        {
            return sqrt( ImSquaredNorm () );
        }
double WoRB::Quaternion::ImSquaredNorm ( ) const [inline]

Gets the squared magnitude of the vector part of this quaternion.

Definition at line 194 of file Quaternion.h.

References x, y, and z.

Referenced by WoRB::Cuboid::CheckOverlapOnAxis(), WoRB::Cuboid::FindContactPointOnEdges(), ImNorm(), WoRB::Sphere::Intersects(), WoRB::Cuboid::IsOverlapOnAxis(), WoRB::RenderStateVariables(), and Trim().

        {
            return x * x + y * y + z * z;
        }
double WoRB::Quaternion::Norm ( ) const [inline]

Gets the magnitude of this quaternion.

Definition at line 201 of file Quaternion.h.

References SquaredNorm().

Referenced by Normalize().

        {
            return sqrt( SquaredNorm () );
        }
Quaternion& WoRB::Quaternion::Normalize ( double  length = 1.0) [inline]

Normalizes the quaternion to a given length.

Definition at line 121 of file Quaternion.h.

References Norm(), and w.

Referenced by WoRB::RigidBody::CalculateDerivedQuantities(), WoRB::Collision::FindOrthonormalBasisAtContactPoint(), WoRB::RandomQuaternion(), WoRB::RenderStateVariables(), and Trim().

        {
            double norm = Norm ();

            // Handle zero length quaternion. 
            // Note that norm == 0 if and only if w == x == y == z == 0
            //
            if ( norm == 0 ) {
                w = length;   
                return *this;
            }

            return *this *= length / norm;
        }
bool WoRB::Quaternion::operator!= ( const Quaternion q) const [inline]

Checks if the two vectors have non-identical components.

Definition at line 325 of file Quaternion.h.

        {
            return !( *this == q );
        }
Quaternion WoRB::Quaternion::operator* ( const Quaternion q) const [inline]

Multiplies the quaternion by the given quaternion.

Definition at line 256 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

        {
            return Quaternion(
                w * q.w - x * q.x - y * q.y - z * q.z,
                w * q.x + x * q.w + y * q.z - z * q.y,
                w * q.y + y * q.w + z * q.x - x * q.z,
                w * q.z + z * q.w + x * q.y - y * q.x
                );
        }
Quaternion WoRB::Quaternion::operator* ( double  value) const [inline]

Returns a copy of this quaternion scaled the given scalar value.

Definition at line 275 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

        {
            return Quaternion( w * value, x * value, y * value, z * value);
        }
Quaternion& WoRB::Quaternion::operator*= ( const Quaternion right) [inline]

Multiplies the quaternion by the given quaternion.

Definition at line 268 of file Quaternion.h.

        {
            return *this = *this * right;
        }
Quaternion& WoRB::Quaternion::operator*= ( double  value) [inline]

Multiplies this quaternion by the given scalar value.

Definition at line 282 of file Quaternion.h.

References w, x, y, and z.

        {
            w *= value;  x *= value;  y *= value;  z *= value;
            return *this;
        }
Quaternion WoRB::Quaternion::operator+ ( const Quaternion q) const [inline]

Returns the value of the given quaternion added to this.

Definition at line 226 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

        {
            return Quaternion( w + q.w, x + q.x, y + q.y, z + q.z );
        }
Quaternion& WoRB::Quaternion::operator+= ( const Quaternion q) [inline]

Adds the given quaternion to this.

Definition at line 233 of file Quaternion.h.

References w, x, y, and z.

        {
            w += q.w;  x += q.x;  y += q.y;  z += q.z;
            return *this;
        }
Quaternion WoRB::Quaternion::operator- ( ) const [inline]

Flips signs of all the components of the quaternion.

Definition at line 166 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

        {
            return Quaternion( -w, -x, -y, -z );
        }
Quaternion WoRB::Quaternion::operator- ( const Quaternion q) const [inline]

Returns the value of the given quaternion subtracted from this.

Definition at line 241 of file Quaternion.h.

References Quaternion(), w, x, y, and z.

        {
            return Quaternion( w - q.w, x - q.x, y - q.y, z - q.z );
        }
Quaternion WoRB::Quaternion::operator-= ( const Quaternion q) [inline]

Subtracts the given quaternion from this.

Definition at line 248 of file Quaternion.h.

References w, x, y, and z.

        {
            w -= q.w;  x -= q.x;  y -= q.y;  z -= q.z;
            return *this;
        }
bool WoRB::Quaternion::operator< ( const Quaternion q) const [inline]

Checks if this quaternion is component-by-component less than the other.

Definition at line 332 of file Quaternion.h.

References w, x, y, and z.

        {
            return w < q.w && x < q.x && y < q.y && z < q.z;
        }
bool WoRB::Quaternion::operator<= ( const Quaternion q) const [inline]

Checks if this quaternion is component-by-component less than the other.

Definition at line 346 of file Quaternion.h.

References w, x, y, and z.

        {
            return w <= q.w && x <= q.x && y <= q.y && z <= q.z;
        }
Quaternion& WoRB::Quaternion::operator= ( double  scalar) [inline]

Sets the scalar (real) part of the quaternion also clearing the vector part.

Definition at line 71 of file Quaternion.h.

References w, x, y, and z.

        {
            w = scalar;
            x = y = z = 0;
            return *this;
        }
bool WoRB::Quaternion::operator== ( const Quaternion q) const [inline]

Checks if the two quaternions have identical components.

Definition at line 318 of file Quaternion.h.

References w, x, y, and z.

        {
            return w == q.w && x == q.x && y == q.y && z == q.z;
        }
bool WoRB::Quaternion::operator> ( const Quaternion q) const [inline]

Checks if this quaternion is component-by-component less than the other.

Definition at line 339 of file Quaternion.h.

References w, x, y, and z.

        {
            return w > q.w && x > q.x && y > q.y && z > q.z;
        }
bool WoRB::Quaternion::operator>= ( const Quaternion q) const [inline]

Checks if this quaternion is component-by-component less than the other.

Definition at line 353 of file Quaternion.h.

References w, x, y, and z.

        {
            return w >= q.w && x >= q.x && y >= q.y && z >= q.z;
        }
double WoRB::Quaternion::operator[] ( unsigned  index) const [inline]

Definition at line 99 of file Quaternion.h.

References w, and x.

        {
            if ( index <= 2 ) {
                return *(&x + index);
            }
            return w;
        }
double& WoRB::Quaternion::operator[] ( unsigned  index) [inline]

Definition at line 107 of file Quaternion.h.

References w, and x.

        {
            if ( index <= 2 ) {
                return *(&x + index);
            }
            return w;
        }
double WoRB::Quaternion::ReSquaredNorm ( ) const [inline]

Gets the squared magnitude of the scalar part of this quaternion.

Definition at line 187 of file Quaternion.h.

References w.

        {
            return w * w;
        }
double WoRB::Quaternion::SquaredNorm ( ) const [inline]

Gets the squared magnitude of this quaternion.

Definition at line 180 of file Quaternion.h.

References w, x, y, and z.

Referenced by Norm().

        {
            return w * w + x * x + y * y + z * z;
        }
Quaternion& WoRB::Quaternion::Trim ( double  size) [inline]

Limits the size of the quaternion to the given maximum.

Definition at line 138 of file Quaternion.h.

References ImSquaredNorm(), and Normalize().

        {
            if ( ImSquaredNorm () > size * size )
            {
                return Normalize( size );
            }

            return *this;
        }
Quaternion WoRB::Quaternion::Unit ( double  length = 1.0) const [inline]

Returns the normalized version (versor) of a quaternion.

Definition at line 215 of file Quaternion.h.

References Quaternion().

Referenced by WoRB::Cuboid::Check(), and WoRB::Cuboid::GetPenetrationOnAxis().

        {
            return Quaternion( *this ).Normalize( length );
        }
Quaternion& WoRB::Quaternion::Zeroize ( double  eps = 1e-4) [inline]

Removes the quaternion components that are bellow the limit.

Definition at line 150 of file Quaternion.h.

References w, x, y, and z.

        {
            if ( fabs( x ) < eps ) x = 0;
            if ( fabs( y ) < eps ) y = 0;
            if ( fabs( z ) < eps ) z = 0;
            if ( fabs( w ) < eps ) w = 0;

            return *this;
        }

Field Documentation


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