NGL  6.5
The NCCA Graphics Library
ngl::Quaternion Class Reference

#include <Quaternion.h>

+ Collaboration diagram for ngl::Quaternion:

Public Member Functions

 Quaternion (const Real _s=0.0f, const Real _x=0.0f, const Real _y=0.0f, const Real _z=0.0f) noexcept
 constructor I use the format used in John Vinces bood where we have a scalar and a vector, some libs do this the otherway round and use the w component, make sure you check if using different libs More...
 
 Quaternion (const Mat4 &_m) noexcept
 constructor passing in a matrix More...
 
 Quaternion (const Vec3 &_rot) noexcept
 constructor passing in a Vec3 which represents the rolls around the x y and z axis More...
 
 Quaternion (const Quaternion &_q) noexcept
 copy constructor More...
 
void set (Real _s, Real _x, Real _y, Real _z) noexcept
 method to set the quaternion values More...
 
Real getS () const noexcept
 accesor for the scalar part More...
 
Real getX () const noexcept
 accesor for the x vector components More...
 
Real getY () const noexcept
 accesor for the y vector components More...
 
Real getZ () const noexcept
 accesor for the z vector components More...
 
Vec4 getVector () const noexcept
 accesor for the vector components as an Vec4 More...
 
void setVector (const Vec4 &_v) noexcept
 mutator for the vector components as an Vec4 More...
 
void setS (Real &_s) noexcept
 mutator for the scalar part More...
 
void setX (Real &_x) noexcept
 mutator for the x vector part More...
 
void setY (Real &_y) noexcept
 mutator for the y vector part More...
 
void setZ (Real &_z) noexcept
 mutator for the z vector part More...
 
Quaternion operator* (const Quaternion &_q) const noexcept
 Perform a multiplication between 2 quaternions. More...
 
void operator*= (const Quaternion &_q) noexcept
 Perform a multiplication this and another quaternions sets the current quat q1 = q1*q2. More...
 
Quaternion operator* (Real _s) const noexcept
 Perform a multiplication between a quaternion and a scalar. More...
 
void operator*= (Real _s) noexcept
 Perform a multiplication this and a real scalar sets the current quat to q=q*_s. More...
 
Quaternion operator+ (const Quaternion &_q) const noexcept
 add two quaternions More...
 
Quaternion operator- (const Quaternion &_q) const noexcept
 subtract two quaternions More...
 
void operator+= (const Quaternion &_q) noexcept
 add _q to the current quaternion More...
 
void operator-= (const Quaternion &_q) noexcept
 subtract _q from the current quaternion More...
 
void normalise () noexcept
 normalise this quaternion this sets each of the component parts by calculating the magnitude and dividing each component by this More...
 
Real magnitude () const noexcept
 returns the magnitude of the quaternion More...
 
Quaternion conjugate () const noexcept
 conjugate negate the vector part can also be done by the -() operator More...
 
Quaternion inverse () const noexcept
 conjugate negate the vector part can also be done by the -() operator More...
 
void operator- () noexcept
 conjugate negate the vector part but for the current vector - More...
 
Quaternion operator- () const noexcept
 returns the inverse of the quaternion (aka conjugate) the scalar part remains the same and we reverse the vector part More...
 
bool operator== (const Quaternion &_q) const noexcept
 test for equality More...
 
Vec4 operator* (const Vec4 &_vec) const noexcept
 operator to allow a quat to be multiplied by a vector V2=Q*V1 this is formed by sandwiching the vector between the current quat and the inverse of the current quat (conjugate) so we get q*_vec*q.conjugate() this is the main way to do quaternion rotation on points (or can use the rotatePoint method which does the same thing) we must ensure that the quat has been set to the correct values for rotation (i.e. set the axis and the rotation values this can be done using the rotateX/Y/Z or fromAxisAngle or fromEulerAngle methods More...
 
void rotateX (Real _angle) noexcept
 set the current quaternion as a rotation around the X cartesian axis [1,0,0] More...
 
void rotateY (Real _angle) noexcept
 set the current quaternion as a rotation around the Y cartesian axis [0,1,0] More...
 
void rotateZ (Real _angle) noexcept
 set the current quaternion as a rotation around the Z cartesian axis [0,0,1] More...
 
void fromAxisAngle (const Vec3 &_axis, Real _angle) noexcept
 set the current quaternion as a rotation around the vector _axis More...
 
void fromEulerAngles (const Real _x, const Real _y, const Real _z) noexcept
 set the current quaternion as a rotation based on 3 Euler angles this will create the quat as a product of 3 seprate quats More...
 
void rotatePoint (const Quaternion &_r, Vec3 &io_p) noexcept
 rotate our point by a quat (but can also be done using the * operator) More...
 
void toAxisAngle (Vec3 &o_axis, Real &o_angle) noexcept
 return the axis and angle of the current quat (angle in degrees) More...
 
Mat4 toMat4 () const noexcept
 return the current quat as a 4x4 transform matrix More...
 
Mat4 toMat4Transpose () const noexcept
 return the current quat as a 4x4 transform matrix transposed More...
 

Static Public Member Functions

static Quaternion slerp (const Quaternion &_q1, const Quaternion &_q2, const Real &_t) noexcept
 this function spherically interpolates between two quaternions with respect to t More...
 

Protected Attributes

Real m_s
 the quaternion data for the scalar real part More...
 
Real m_x
 the quaternion data for x More...
 
Real m_y
 the quaternion data for y More...
 
Real m_z
 the quaternion data for z More...
 

Detailed Description

Definition at line 38 of file Quaternion.h.

Constructor & Destructor Documentation

ngl::Quaternion::Quaternion ( const Real  _s = 0.0f,
const Real  _x = 0.0f,
const Real  _y = 0.0f,
const Real  _z = 0.0f 
)
inlinenoexcept

constructor I use the format used in John Vinces bood where we have a scalar and a vector, some libs do this the otherway round and use the w component, make sure you check if using different libs

Parameters
[in]_s- the s component of the quaternion
[in]_x- the x component of the quaternion
[in]_y- the y component of the quaternion
[in]_z- the z component of the quaternion

Definition at line 51 of file Quaternion.h.

Quaternion::Quaternion ( const Mat4 _m)
noexcept

constructor passing in a matrix

Parameters
_mthe matrix to build the quat from this is useful when using the slerp so we can interpolate between two rotation matrices

Definition at line 30 of file Quaternion.cpp.

Quaternion::Quaternion ( const Vec3 _rot)
noexcept

constructor passing in a Vec3 which represents the rolls around the x y and z axis

Parameters
_rotthe roatation to build the quat from this is useful when using the slerp so we can interpolate between two rotation matrices

Definition at line 70 of file Quaternion.cpp.

References ngl::radians().

+ Here is the call graph for this function:

ngl::Quaternion::Quaternion ( const Quaternion _q)
inlinenoexcept

copy constructor

Parameters
[in]_q- the quaternion to copy

Definition at line 75 of file Quaternion.h.

Member Function Documentation

Quaternion ngl::Quaternion::conjugate ( ) const
inlinenoexcept

conjugate negate the vector part can also be done by the -() operator

Returns
the conjugate of the current quaternion

Definition at line 210 of file Quaternion.h.

void Quaternion::fromAxisAngle ( const Vec3 _axis,
Real  _angle 
)
noexcept

set the current quaternion as a rotation around the vector _axis

[in] _axis the axis to rotate around (will be normalized)

Parameters
[in]_anglethe angle of rotation around the Z axis in degrees

Definition at line 245 of file Quaternion.cpp.

References m_s, m_x, ngl::Vec3::m_x, m_y, ngl::Vec3::m_y, ngl::Vec3::m_z, m_z, ngl::Vec3::normalize(), and ngl::radians().

+ Here is the call graph for this function:

void Quaternion::fromEulerAngles ( const Real  _x,
const Real  _y,
const Real  _z 
)
noexcept

set the current quaternion as a rotation based on 3 Euler angles this will create the quat as a product of 3 seprate quats

[in] _x the rotation in degrees around the x axis [in] _y the rotation in degrees around the y axis [in] _z the rotation in degrees around the z axis

Definition at line 259 of file Quaternion.cpp.

References m_s, m_x, m_y, m_z, and ngl::radians().

+ Here is the call graph for this function:

Real ngl::Quaternion::getS ( ) const
inlinenoexcept

accesor for the scalar part

Returns
m_s the scalar part of the quaternion

Definition at line 98 of file Quaternion.h.

+ Here is the caller graph for this function:

Vec4 ngl::Quaternion::getVector ( ) const
inlinenoexcept

accesor for the vector components as an Vec4

Returns
a vector

Definition at line 118 of file Quaternion.h.

Real ngl::Quaternion::getX ( ) const
inlinenoexcept

accesor for the x vector components

Returns
m_x the scalar part of the quaternion

Definition at line 103 of file Quaternion.h.

+ Here is the caller graph for this function:

Real ngl::Quaternion::getY ( ) const
inlinenoexcept

accesor for the y vector components

Returns
m_y the scalar part of the quaternion

Definition at line 108 of file Quaternion.h.

+ Here is the caller graph for this function:

Real ngl::Quaternion::getZ ( ) const
inlinenoexcept

accesor for the z vector components

Returns
m_z the scalar part of the quaternion

Definition at line 113 of file Quaternion.h.

+ Here is the caller graph for this function:

Quaternion ngl::Quaternion::inverse ( ) const
inlinenoexcept

conjugate negate the vector part can also be done by the -() operator

Returns
the conjugate of the current quaternion

Definition at line 215 of file Quaternion.h.

Real Quaternion::magnitude ( ) const
noexcept

returns the magnitude of the quaternion

Returns
The magnitude of the quaternion

Definition at line 182 of file Quaternion.cpp.

References m_s, m_x, m_y, and m_z.

+ Here is the caller graph for this function:

void Quaternion::normalise ( )
noexcept

normalise this quaternion this sets each of the component parts by calculating the magnitude and dividing each component by this

Definition at line 171 of file Quaternion.cpp.

References m_s, m_x, m_y, m_z, and magnitude().

+ Here is the call graph for this function:

Quaternion Quaternion::operator* ( const Quaternion _q) const
noexcept

Perform a multiplication between 2 quaternions.

Parameters
[in]_qthe rhs quaternion argument
Returns
the result of the mutliplication (product)

Definition at line 88 of file Quaternion.cpp.

References ngl::Vec4::cross(), m_s, m_x, ngl::Vec4::m_x, m_y, ngl::Vec4::m_y, m_z, and ngl::Vec4::m_z.

+ Here is the call graph for this function:

Quaternion Quaternion::operator* ( Real  _s) const
noexcept

Perform a multiplication between a quaternion and a scalar.

Parameters
[in]_sthe rhs scalar argument
Returns
the result of the mutliplication q*s

Definition at line 153 of file Quaternion.cpp.

Vec4 Quaternion::operator* ( const Vec4 _vec) const
noexcept

operator to allow a quat to be multiplied by a vector V2=Q*V1 this is formed by sandwiching the vector between the current quat and the inverse of the current quat (conjugate) so we get q*_vec*q.conjugate() this is the main way to do quaternion rotation on points (or can use the rotatePoint method which does the same thing) we must ensure that the quat has been set to the correct values for rotation (i.e. set the axis and the rotation values this can be done using the rotateX/Y/Z or fromAxisAngle or fromEulerAngle methods

Parameters
[in]_vecthe vector to be multiplied
Returns
a vector formed from q*_vec*q^-1 (conjugate)

Definition at line 200 of file Quaternion.cpp.

void Quaternion::operator*= ( const Quaternion _q)
noexcept

Perform a multiplication this and another quaternions sets the current quat q1 = q1*q2.

Parameters
[in]_qthe rhs quaternion argument

Definition at line 110 of file Quaternion.cpp.

void Quaternion::operator*= ( Real  _s)
noexcept

Perform a multiplication this and a real scalar sets the current quat to q=q*_s.

Parameters
[in]_sthe rhs quaternion argument

Definition at line 160 of file Quaternion.cpp.

Quaternion Quaternion::operator+ ( const Quaternion _q) const
noexcept

add two quaternions

Parameters
[in]_qthe rhs quaternion argument
Returns
the result of the addition

Definition at line 117 of file Quaternion.cpp.

References m_s, m_x, m_y, and m_z.

void Quaternion::operator+= ( const Quaternion _q)
noexcept

add _q to the current quaternion

Parameters
[in]_qthe rhs quaternion argument

Definition at line 140 of file Quaternion.cpp.

Quaternion Quaternion::operator- ( const Quaternion _q) const
noexcept

subtract two quaternions

Parameters
[in]_qthe rhs quaternion argument
Returns
the result of the subtraction

Definition at line 128 of file Quaternion.cpp.

References m_s, m_x, m_y, and m_z.

void ngl::Quaternion::operator- ( )
inlinenoexcept

conjugate negate the vector part but for the current vector -

Returns
the conjugate of the current quaternion

Definition at line 220 of file Quaternion.h.

Quaternion ngl::Quaternion::operator- ( ) const
inlinenoexcept

returns the inverse of the quaternion (aka conjugate) the scalar part remains the same and we reverse the vector part

Returns
the conjugate of the quaternion

Definition at line 226 of file Quaternion.h.

References ngl::operator*(), and ngl::operator==().

+ Here is the call graph for this function:

void Quaternion::operator-= ( const Quaternion _q)
noexcept

subtract _q from the current quaternion

Parameters
[in]_qthe rhs quaternion argument

Definition at line 146 of file Quaternion.cpp.

bool Quaternion::operator== ( const Quaternion _q) const
noexcept

test for equality

Parameters
[in]_qthe quaternion to test against
Returns
true if the same (based on EPSILON test range) or false

Definition at line 188 of file Quaternion.cpp.

References FCompare, m_s, m_x, m_y, and m_z.

void Quaternion::rotatePoint ( const Quaternion _r,
Vec3 io_p 
)
noexcept

rotate our point by a quat (but can also be done using the * operator)

[in] _r the rotation Quat [inout] io_p the point to be rotated (result we be set in this point)

Definition at line 274 of file Quaternion.cpp.

References m_x, m_y, m_z, and set().

+ Here is the call graph for this function:

void Quaternion::rotateX ( Real  _angle)
noexcept

set the current quaternion as a rotation around the X cartesian axis [1,0,0]

Parameters
[in]_anglethe angle of rotation around the x axis in degrees

Definition at line 211 of file Quaternion.cpp.

References m_s, m_x, m_y, m_z, and ngl::radians().

+ Here is the call graph for this function:

void Quaternion::rotateY ( Real  _angle)
noexcept

set the current quaternion as a rotation around the Y cartesian axis [0,1,0]

Parameters
[in]_anglethe angle of rotation around the y axis in degrees

Definition at line 221 of file Quaternion.cpp.

References m_s, m_x, m_y, m_z, and ngl::radians().

+ Here is the call graph for this function:

void Quaternion::rotateZ ( Real  _angle)
noexcept

set the current quaternion as a rotation around the Z cartesian axis [0,0,1]

Parameters
[in]_anglethe angle of rotation around the Z axis in degrees

Definition at line 232 of file Quaternion.cpp.

References m_s, m_x, m_y, m_z, and ngl::radians().

+ Here is the call graph for this function:

void ngl::Quaternion::set ( Real  _s,
Real  _x,
Real  _y,
Real  _z 
)
inlinenoexcept

method to set the quaternion values

Parameters
[in]_xthe x value
[in]_ythe y value
[in]_zthe z value
[in]_wthe w value

Definition at line 87 of file Quaternion.h.

+ Here is the caller graph for this function:

void ngl::Quaternion::setS ( Real _s)
inlinenoexcept

mutator for the scalar part

Parameters
[in]_sthe scalar part of the quaternion to set

Definition at line 133 of file Quaternion.h.

void ngl::Quaternion::setVector ( const Vec4 _v)
inlinenoexcept

mutator for the vector components as an Vec4

Parameters
[in]_vthe vector to set the quat vector components from

Definition at line 123 of file Quaternion.h.

void ngl::Quaternion::setX ( Real _x)
inlinenoexcept

mutator for the x vector part

Parameters
[in]_xthe x vector part of the quaternion to set

Definition at line 138 of file Quaternion.h.

void ngl::Quaternion::setY ( Real _y)
inlinenoexcept

mutator for the y vector part

Parameters
[in]_xthe x vector part of the quaternion to set

Definition at line 143 of file Quaternion.h.

void ngl::Quaternion::setZ ( Real _z)
inlinenoexcept

mutator for the z vector part

Parameters
[in]_zthe z vector part of the quaternion to set

Definition at line 148 of file Quaternion.h.

References ngl::operator*().

+ Here is the call graph for this function:

Quaternion Quaternion::slerp ( const Quaternion _q1,
const Quaternion _q2,
const Real _t 
)
staticnoexcept

this function spherically interpolates between two quaternions with respect to t

Parameters
[in]_q1- the first quaternion
[in]_q2- the second quaternion
[in]_t- the interpolating t value

Definition at line 295 of file Quaternion.cpp.

References m_s, m_x, m_y, and m_z.

void Quaternion::toAxisAngle ( Vec3 o_axis,
Real o_angle 
)
noexcept

return the axis and angle of the current quat (angle in degrees)

[out] o_axis the axis of rotation ( [out] o_angle the angle of rotation about the quat angle in degrees

Definition at line 282 of file Quaternion.cpp.

References ngl::degrees(), m_s, m_x, m_y, and m_z.

+ Here is the call graph for this function:

Mat4 Quaternion::toMat4 ( ) const
noexcept

return the current quat as a 4x4 transform matrix

Returns
the quat as a matrix

Definition at line 340 of file Quaternion.cpp.

References ngl::Mat4::m_openGL, m_s, m_x, m_y, and m_z.

Mat4 Quaternion::toMat4Transpose ( ) const
noexcept

return the current quat as a 4x4 transform matrix transposed

Returns
the quat as a matrix

Definition at line 379 of file Quaternion.cpp.

References ngl::Mat4::m_openGL, m_s, m_x, m_y, and m_z.

Member Data Documentation

Real ngl::Quaternion::m_s
protected

the quaternion data for the scalar real part

Definition at line 306 of file Quaternion.h.

Real ngl::Quaternion::m_x
protected

the quaternion data for x

Definition at line 308 of file Quaternion.h.

Real ngl::Quaternion::m_y
protected

the quaternion data for y

Definition at line 310 of file Quaternion.h.

Real ngl::Quaternion::m_z
protected

the quaternion data for z

Definition at line 312 of file Quaternion.h.


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