CUGL 2.1
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
cugl::Quaternion Class Reference

#include <CUQuaternion.h>

Public Member Functions

 Quaternion ()
 
 Quaternion (float x, float y, float z, float w)
 
 Quaternion (float *array)
 
 Quaternion (const Vec3 axis, float angle)
 
Quaternionoperator= (const float *array)
 
Quaternionset (float x, float y, float z, float w)
 
Quaternionset (const float *array)
 
Quaternionset (const Vec3 axis, float angle)
 
Quaternionset (const Quaternion &q)
 
QuaternionsetIdentity ()
 
QuaternionsetZero ()
 
Quaternionadd (const Quaternion &q)
 
Quaternionsubtract (const Quaternion &q)
 
Quaternionmultiply (const Quaternion &q)
 
Quaterniondivide (const Quaternion &q)
 
Quaternionscale (float s)
 
Quaternionconjugate ()
 
Quaternion getConjugate () const
 
Quaternioninvert ()
 
Quaternion getInverse () const
 
Quaternionnormalize ()
 
Quaternion getNormalization () const
 
Quaternionnegate ()
 
Quaternion getNegation () const
 
float dot (const Quaternion &q) const
 
Quaternionoperator+= (const Quaternion &q)
 
Quaternionoperator-= (const Quaternion &q)
 
Quaternionoperator*= (float s)
 
Quaternionoperator*= (const Quaternion &q)
 
Quaternionoperator/= (float s)
 
Quaternionoperator/= (const Quaternion &q)
 
const Quaternion operator+ (const Quaternion &q) const
 
const Quaternion operator- (const Quaternion &q) const
 
const Quaternion operator- () const
 
const Quaternion operator* (float s) const
 
const Quaternion operator* (const Quaternion &q) const
 
const Quaternion operator/ (float s) const
 
const Quaternion operator/ (const Quaternion &q) const
 
bool operator== (const Quaternion &q) const
 
bool operator!= (const Quaternion &q) const
 
bool equals (const Quaternion &q, float variance=CU_MATH_EPSILON) const
 
float norm () const
 
float normSquared () const
 
bool isZero () const
 
bool isNearZero (float variance=CU_MATH_EPSILON) const
 
bool isIdentity () const
 
bool isNearIdentity (float variance=CU_MATH_EPSILON) const
 
bool isUnit (float variance=CU_MATH_EPSILON) const
 
float toAxisAngle (Vec3 *e) const
 
Quaternionlerp (const Quaternion &q, float t)
 
Quaternionslerp (const Quaternion &q, float t)
 
Quaternionnlerp (const Quaternion &q, float t)
 
Quaternion getLerp (const Quaternion &q, float t)
 
Quaternion getSlerp (const Quaternion &q, float t)
 
Quaternion getNlerp (const Quaternion &q, float t)
 
Vec3 getRotation (const Vec3 v)
 
std::string toString (bool verbose=false) const
 
 operator std::string () const
 
 operator Vec4 () const
 
 Quaternion (const Vec4 vector)
 
Quaternionoperator= (const Vec4 vector)
 
Quaternionset (const Vec4 vector)
 
 operator Mat4 () const
 
 Quaternion (const Mat4 &m)
 
Quaternionoperator= (const Mat4 &m)
 
Quaternionset (const Mat4 &m)
 

Static Public Member Functions

static QuaternioncreateFromRotationMatrix (const Mat4 &m, Quaternion *dst)
 
static QuaternioncreateFromAxisAngle (const Vec3 axis, float angle, Quaternion *dst)
 
static Quaternionadd (const Quaternion &q1, const Quaternion &q2, Quaternion *dst)
 
static Quaternionsubtract (const Quaternion &q1, const Quaternion &q2, Quaternion *dst)
 
static Quaternionmultiply (const Quaternion &q1, const Quaternion &q2, Quaternion *dst)
 
static Quaterniondivide (const Quaternion &q1, const Quaternion &q2, Quaternion *dst)
 
static Quaternionscale (const Quaternion &q1, float s, Quaternion *dst)
 
static Quaternionconjugate (const Quaternion &quat, Quaternion *dst)
 
static Quaternioninvert (const Quaternion &quat, Quaternion *dst)
 
static Quaternionnormalize (const Quaternion &quat, Quaternion *dst)
 
static Quaternionnegate (const Quaternion &quat, Quaternion *dst)
 
static float dot (const Quaternion &q1, const Quaternion &q2)
 
static Quaternionlerp (const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst)
 
static Quaternionslerp (const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst)
 
static Quaternionnlerp (const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst)
 
static Vec3rotate (const Vec3 v, const Quaternion &quat, Vec3 *dst)
 

Public Attributes

float x
 
float y
 
float z
 
float w
 

Static Public Attributes

static const Quaternion ZERO
 
static const Quaternion IDENTITY
 

Detailed Description

This class provides a quaternion that represents an object orientation.

Quaternions are typically used as a replacement for euler angles and rotation matrices as a way to achieve smooth interpolation and avoid gimbal lock.

Note that this quaternion class does not automatically keep the quaternion normalized. Therefore, care must be taken to normalize the quaternion when necessary, by calling the normalize method.

This class provides three methods for doing quaternion interpolation: lerp, slerp, and nlerp. The advatanges of each of these interpolation types are discussed at

https://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/

lerp (linear interpolation): The interpolation curve gives a straight line in quaternion space. It is simple and fast to compute. The only problem is that it does not provide constant angular velocity. Note that a constant velocity is not necessarily a requirement for a curve.

The lerp method provided here interpolates strictly in quaternion space. Note that the resulting path may pass through the origin if interpolating between a quaternion and its exact negative.

slerp (spherical linear interpolation): The interpolation curve forms a great arc on the quaternion unit sphere. Slerp provides constant angular velocity and is torque minimal. However, it is not commutative and is very computationally expensive.

The slerp method provided here is intended for interpolation of principal rotations. It treats +q and -q as the same principal rotation and is at liberty to use the negative of either input. The resulting path is always the shorter arc.

nlerp (normalized linear interpolation): The interpolation curve gives a straight line in quaternion space, but normalizes the result. When the input quaternions are themselves unit quaternions, this provides a fast alternative to slerp. Again, it does not provide constant angular velocity, but it is communitative and is torque minimal.

The nlerp method provided here interpolates uses lerp as its base.

This class is in standard layout with fields of uniform type. This means that it is safe to reinterpret_cast objects to float arrays.

Constructor & Destructor Documentation

◆ Quaternion() [1/6]

cugl::Quaternion::Quaternion ( )
inline

Constructs a quaternion initialized to (0, 0, 0, 1).

◆ Quaternion() [2/6]

cugl::Quaternion::Quaternion ( float  x,
float  y,
float  z,
float  w 
)
inline

Constructs a quaternion initialized to the specified values.

Parameters
xThe x component of the quaternion.
yThe y component of the quaternion.
zThe z component of the quaternion.
wThe w component of the quaternion.

◆ Quaternion() [3/6]

cugl::Quaternion::Quaternion ( float *  array)

Constructs a new quaternion from the values in the specified array.

The elements of the array are in the order x, y, z, and w.

Parameters
arrayAn array containing the elements of the quaternion.

◆ Quaternion() [4/6]

cugl::Quaternion::Quaternion ( const Vec3  axis,
float  angle 
)

Constructs a quaternion equal to the rotation from the specified axis and angle.

Parameters
axisA vector describing the axis of rotation.
angleThe angle of rotation (in radians).

◆ Quaternion() [5/6]

cugl::Quaternion::Quaternion ( const Vec4  vector)
explicit

Creates a quaternion from the given vector.

Parameters
vectorThe vector to convert

◆ Quaternion() [6/6]

cugl::Quaternion::Quaternion ( const Mat4 m)
explicit

Constructs a quaternion equal to the rotational part of the specified matrix.

This constructor may fail, particularly if the scale component of the matrix is too small. In that case, this method intializes this quaternion to the zero quaternion.

Parameters
mThe matrix to extract the rotation.

Member Function Documentation

◆ add() [1/2]

Quaternion& cugl::Quaternion::add ( const Quaternion q)
inline

Adds the specified quaternion to this one in place.

Parameters
qThe quaternion to add.
Returns
this quaternion, after addition

◆ add() [2/2]

static Quaternion* cugl::Quaternion::add ( const Quaternion q1,
const Quaternion q2,
Quaternion dst 
)
static

Adds the specified quaternions and stores the result in dst.

Parameters
q1The first quaternion.
q2The second quaternion.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ conjugate() [1/2]

Quaternion& cugl::Quaternion::conjugate ( )
inline

Sets this quaternion to the conjugate of itself.

Returns
this quaternion, after conjugation

◆ conjugate() [2/2]

static Quaternion* cugl::Quaternion::conjugate ( const Quaternion quat,
Quaternion dst 
)
static

Conjugates the specified quaternion and stores the result in dst.

Parameters
quatThe quaternion to conjugate.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ createFromAxisAngle()

static Quaternion* cugl::Quaternion::createFromAxisAngle ( const Vec3  axis,
float  angle,
Quaternion dst 
)
static

Creates this quaternion equal to the rotation from the specified axis and angle.

The result is stored in dst.

Parameters
axisA vector describing the axis of rotation.
angleThe angle of rotation (in radians).
dstA quaternion to store the conjugate in.
Returns
A reference to dst for chaining

◆ createFromRotationMatrix()

static Quaternion* cugl::Quaternion::createFromRotationMatrix ( const Mat4 m,
Quaternion dst 
)
static

Creates a quaternion equal to the rotational part of the matrix.

The result is stored in dst.

Parameters
mThe matrix.
dstA quaternion to store the conjugate in.
Returns
A reference to dst for chaining

◆ divide() [1/2]

Quaternion& cugl::Quaternion::divide ( const Quaternion q)
inline

Divides this quaternion by the specified one in place.

Division is the same as multiplication by the inverse of q.

Parameters
qThe quaternion to divide by.
Returns
this quaternion, after division

◆ divide() [2/2]

static Quaternion* cugl::Quaternion::divide ( const Quaternion q1,
const Quaternion q2,
Quaternion dst 
)
static

Divides a quaternion by another and stores the result in dst.

This method performs standard quaternion division. That is, it multiplies the first quaternion by the inverse of the second.

Parameters
q1The initial quaternion.
q2The quaternion to divide by.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ dot() [1/2]

float cugl::Quaternion::dot ( const Quaternion q) const
inline

Returns the dot product of this quaternion with the specified one.

Parameters
qThe quaternion to compute the dot product with.
Returns
The dot product.

◆ dot() [2/2]

static float cugl::Quaternion::dot ( const Quaternion q1,
const Quaternion q2 
)
static

Returns the dot product of the two quaternions

Parameters
q1The first quaternion.
q2The second quaternion.
Returns
The dot product.

◆ equals()

bool cugl::Quaternion::equals ( const Quaternion q,
float  variance = CU_MATH_EPSILON 
) const

Returns true if the quaternions are within tolerance of each other.

The tolerance bounds the norm of the difference between the two quaternions

Parameters
qThe vector to compare against.
varianceThe comparison tolerance.
Returns
true if the quaternions are within tolerance of each other.

◆ getConjugate()

Quaternion cugl::Quaternion::getConjugate ( ) const
inline

Returns the conjugate of this quaternion.

Returns
the conjugate of this quaternion.

◆ getInverse()

Quaternion cugl::Quaternion::getInverse ( ) const
inline

Gets the inverse of this quaternion.

Note that the inverse of a quaternion is equal to its conjugate when the quaternion is unit-length. For this reason, it is more efficient to use the conjugate method directly when you know your quaternion is already unit-length.

If the inverse cannot be computed, this method returns a quaternion with all NaN values instead.

Returns
the inverse of this quaternion.

◆ getLerp()

Quaternion cugl::Quaternion::getLerp ( const Quaternion q,
float  t 
)
inline

Returns an interpolation between this quaternion and another.

The interpolation curve for linear interpolation between quaternions gives a straight line in quaternion space.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

Parameters
qThe quaternion to interpolate.
tThe interpolation coefficient in [0,1].

Note: this does not modify this quaternion.

Returns
an interpolation between this quaternion and another.

◆ getNegation()

Quaternion cugl::Quaternion::getNegation ( ) const
inline

Returns a negated copy of this quaternion.

Note: This method does not modify the quaternion

Returns
a negated copy of this quaternion.

◆ getNlerp()

Quaternion cugl::Quaternion::getNlerp ( const Quaternion q,
float  t 
)
inline

Returns a normalized linear interpolation between this quaternion and another.

Normalized linear interpolation provides smooth transitions between different orientations and is often useful for animating models or cameras in 3D.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

In addition, the input quaternions must be at (or close to) unit length. When assertions are enabled, it will test this via the isUnit() method.

Note: this does not modify this quaternion.

Parameters
qThe quaternion to interpolate.
tThe interpolation coefficient in [0,1].
Returns
a spherical linear interpolation of this quaternion.

◆ getNormalization()

Quaternion cugl::Quaternion::getNormalization ( ) const
inline

Returns a normalized copy of this quaternion.

If the quaternion already has unit length or if the length of the quaternion is zero, this method simply copies this quaternion.

Note: This method does not modify the quaternion

Returns
a normalized copy of this quaternion.

◆ getRotation()

Vec3 cugl::Quaternion::getRotation ( const Vec3  v)
inline

Returns a copy of the vector rotated by this quaternion.

The rotation is defined by the matrix associated with the vector. *

Parameters
vThe vector to rotate.
Returns
a copy of the vector rotated by this quaternion.

◆ getSlerp()

Quaternion cugl::Quaternion::getSlerp ( const Quaternion q,
float  t 
)
inline

Returns a spherical linear interpolation between this quaternion and another.

Spherical linear interpolation provides smooth transitions between different orientations and is often useful for animating models or cameras in 3D.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

In addition, the input quaternions must be at (or close to) unit length. When assertions are enabled, it will test this via the isUnit() method.

Note: this does not modify this quaternion.

Parameters
qThe quaternion to interpolate.
tThe interpolation coefficient in [0,1].
Returns
a spherical linear interpolation of this quaternion.

◆ invert() [1/2]

Quaternion& cugl::Quaternion::invert ( )
inline

Sets this quaternion to the inverse of itself.

Note that the inverse of a quaternion is equal to its conjugate when the quaternion is unit-length. For this reason, it is more efficient to use the conjugate method directly when you know your quaternion is already unit-length.

If the inverse cannot be computed, this method sets the quaternion to have NaN values.

Returns
this quaternion, after inverting

◆ invert() [2/2]

static Quaternion* cugl::Quaternion::invert ( const Quaternion quat,
Quaternion dst 
)
static

Inverts the specified quaternion and stores the result in dst.

Note that the inverse of a quaternion is equal to its conjugate when the quaternion is unit-length. For this reason, it is more efficient to use the conjugate method directly when you know your quaternion is already unit-length.

If the inverse cannot be computed, this method stores a quaternion with NaN values in dst.

Parameters
quatThe quaternion to invert.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ isIdentity()

bool cugl::Quaternion::isIdentity ( ) const

Returns true if this quaternion is the identity.

Returns
true if this quaternion is the identity.

◆ isNearIdentity()

bool cugl::Quaternion::isNearIdentity ( float  variance = CU_MATH_EPSILON) const
inline

Returns true if this quaternion is near the identity.

The tolerance bounds the quaternion norm of the differences.

Parameters
varianceThe comparison tolerance
Returns
true if this quaternion is near the identity.

◆ isNearZero()

bool cugl::Quaternion::isNearZero ( float  variance = CU_MATH_EPSILON) const
inline

Returns true if this quaternion is with tolerance of the zero quaternion.

The tolerance bounds the quaternion norm.

Parameters
varianceThe comparison tolerance
Returns
true if this quaternion is with tolerance of the zero quaternion.

◆ isUnit()

bool cugl::Quaternion::isUnit ( float  variance = CU_MATH_EPSILON) const
inline

Returns true if this vector is a unit vector.

Parameters
varianceThe comparison tolerance
Returns
true if this vector is a unit vector.

◆ isZero()

bool cugl::Quaternion::isZero ( ) const

Returns true this quaternion contains all zeros.

Comparison is exact, which may be unreliable given that the attributes are floats.

Returns
true if this quaternion contains all zeros, false otherwise.

◆ lerp() [1/2]

Quaternion& cugl::Quaternion::lerp ( const Quaternion q,
float  t 
)
inline

Interpolates between this quaternion and another.

The interpolation curve for linear interpolation between quaternions gives a straight line in quaternion space.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

Parameters
qThe quaternion to interpolate.
tThe interpolation coefficient in [0,1].
Returns
this quaternion, after interpolation.

◆ lerp() [2/2]

static Quaternion* cugl::Quaternion::lerp ( const Quaternion q1,
const Quaternion q2,
float  t,
Quaternion dst 
)
static

Interpolates between two quaternions using linear interpolation.

The interpolation curve for linear interpolation between quaternions gives a straight line in quaternion space.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

Parameters
q1The first quaternion.
q2The second quaternion.
tThe interpolation coefficient in [0,1].
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ multiply() [1/2]

Quaternion& cugl::Quaternion::multiply ( const Quaternion q)
inline

Multiplies this quaternion by the specified one in place.

Parameters
qThe quaternion to multiply.
Returns
this quaternion, after multiplication

◆ multiply() [2/2]

static Quaternion* cugl::Quaternion::multiply ( const Quaternion q1,
const Quaternion q2,
Quaternion dst 
)
static

Multiplies the specified quaternions and stores the result in dst.

This method performs standard quaternion multiplication

Parameters
q1The first quaternion.
q2The second quaternion.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ negate() [1/2]

Quaternion& cugl::Quaternion::negate ( )
inline

Negates this quaternion in place.

Returns
this quaternion, after negation

◆ negate() [2/2]

static Quaternion* cugl::Quaternion::negate ( const Quaternion quat,
Quaternion dst 
)
static

Negates the specified quaternion and stores the result in dst.

Parameters
quatThe quaternion to negate.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ nlerp() [1/2]

Quaternion& cugl::Quaternion::nlerp ( const Quaternion q,
float  t 
)
inline

Interpolates between this quaternion and another with normalized linear interpolation.

Normalized linear interpolation provides smooth transitions between different orientations and is often useful for animating models or cameras in 3D.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

In addition, the input quaternions must be at (or close to) unit length. When assertions are enabled, it will test this via the isUnit() method.

Parameters
qThe quaternion to interpolate.
tThe interpolation coefficient in [0,1].
Returns
this quaternion, after interpolation.

◆ nlerp() [2/2]

static Quaternion* cugl::Quaternion::nlerp ( const Quaternion q1,
const Quaternion q2,
float  t,
Quaternion dst 
)
static

Interpolates between two quaternions using normalized linear interpolation.

Normalized linear interpolation provides smooth transitions between different orientations and is often useful for animating models or cameras in 3D.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

In addition, the input quaternions must be at (or close to) unit length. When assertions are enabled, it will test this via the isUnit() method.

Parameters
q1The first quaternion.
q2The second quaternion.
tThe interpolation coefficient in [0,1].
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ norm()

float cugl::Quaternion::norm ( ) const
inline

Returns the norm of this quaternion.

Returns
The of the quaternion.

{

See also
normSquared}

◆ normalize() [1/2]

Quaternion& cugl::Quaternion::normalize ( )
inline

Normalizes this quaternion to have unit length.

If the quaternion already has unit length or if the length of the quaternion is zero, this method does nothing.

Returns
this quaternion, after normalization

◆ normalize() [2/2]

static Quaternion* cugl::Quaternion::normalize ( const Quaternion quat,
Quaternion dst 
)
static

Normalizes the specified quaternion and stores the result in dst.

If the quaternion already has unit length or if the length of the quaternion is zero, this method copies quat into dst.

Parameters
quatThe quaternion to normalize.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ normSquared()

float cugl::Quaternion::normSquared ( ) const
inline

Returns the squared norm of this quaternion.

This method is faster than norm because it does not need to compute a square root. Hence it is best to us this method when it is not necessary to get the exact length of a quaternions (e.g. when simply comparing the norm to a threshold value).

Returns
The squared norm of the quaternion.

{

See also
length}

◆ operator Mat4()

cugl::Quaternion::operator Mat4 ( ) const

Cast from Quaternion to a Matrix.

The matrix is a rotation matrix equivalent to the rotation represented by this quaternion.

◆ operator std::string()

cugl::Quaternion::operator std::string ( ) const
inline

Cast from Quaternion to a string.

◆ operator Vec4()

cugl::Quaternion::operator Vec4 ( ) const

Cast from Quaternion to a Vec4.

◆ operator!=()

bool cugl::Quaternion::operator!= ( const Quaternion q) const

Returns true if this quaternion is not equal to the given quaternion.

Comparison is exact, which may be unreliable given that the attributes are floats.

Parameters
qThe quaternion to compare quaternion.
Returns
True if this quaternion is not equal to the given quaternion.

◆ operator*() [1/2]

const Quaternion cugl::Quaternion::operator* ( const Quaternion q) const
inline

Returns the product of this quaternion with the given quaternion.

This method performs standard quaternion multiplication

Note: this does not modify this quaternion.

Parameters
qThe quaternion to multiply by.
Returns
the product of this quaternion with the given quaternion.

◆ operator*() [2/2]

const Quaternion cugl::Quaternion::operator* ( float  s) const
inline

Returns the scalar product of this quaternion with the given value.

Note: this does not modify this quaternion.

Parameters
sThe value to scale by.
Returns
The scalar product of this quaternion with the given value.

◆ operator*=() [1/2]

Quaternion& cugl::Quaternion::operator*= ( const Quaternion q)
inline

Multiplies this quaternion in place by the given quaternion.

This method performs standard quaternion multiplication

Parameters
qThe quaternion to multiply by
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator*=() [2/2]

Quaternion& cugl::Quaternion::operator*= ( float  s)
inline

Scales this quaternion in place by the given factor.

Parameters
sThe value to scale by
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator+()

const Quaternion cugl::Quaternion::operator+ ( const Quaternion q) const
inline

Returns the sum of this quaternion with the given quaternion.

Note: this does not modify this quaternion.

Parameters
qThe quaternion to add.
Returns
The sum of this quaternion with the given quaternion.

◆ operator+=()

Quaternion& cugl::Quaternion::operator+= ( const Quaternion q)
inline

Adds the given quaternion to this one in place.

Parameters
qThe quaternion to add
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator-() [1/2]

const Quaternion cugl::Quaternion::operator- ( ) const
inline

Returns the negation of this quaternion.

Note: this does not modify this quaternion.

Returns
The negation of this quaternion.

◆ operator-() [2/2]

const Quaternion cugl::Quaternion::operator- ( const Quaternion q) const
inline

Returns the difference of this quaternion with the given quaternion.

Note: this does not modify this quaternion.

Parameters
qThe quaternion to subtract.
Returns
The difference of this quaternion with the given quaternion.

◆ operator-=()

Quaternion& cugl::Quaternion::operator-= ( const Quaternion q)
inline

Subtracts the given quaternion from this one in place.

Parameters
qThe quaternion to subtract
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator/() [1/2]

const Quaternion cugl::Quaternion::operator/ ( const Quaternion q) const
inline

Returns a copy of this quaternion divided by the given quaternion.

This method is the same as multiplying by the inverse of the given quaternion. If the quaternion is not invertible this method will store NaN in the current quaternion.

Note: this does not modify this quaternion.

Parameters
qThe quaternion to divide by
Returns
a copy of this quaternion divided by the given quaternion.

◆ operator/() [2/2]

const Quaternion cugl::Quaternion::operator/ ( float  s) const
inline

Returns a copy of this quaternion divided by the given constant

Note: this does not modify this quaternion.

Parameters
sthe constant to divide this quaternion with
Returns
A copy of this vector quaternion by the given constant

◆ operator/=() [1/2]

Quaternion& cugl::Quaternion::operator/= ( const Quaternion q)
inline

Divides this quaternion in place by the given quaternion.

This method is the same as multiplying by the inverse of the given quaternion. If the quaternion is not invertible this method will store NaN in the current quaternion.

Parameters
qThe quaternion to divide by
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator/=() [2/2]

Quaternion& cugl::Quaternion::operator/= ( float  s)
inline

Divides this quaternion in place by the given factor.

Parameters
sThe scalar to divide by
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator=() [1/3]

Quaternion& cugl::Quaternion::operator= ( const float *  array)
inline

Sets the elements of this quaternion from the values in the specified array.

The elements of the array are in the order x, y, z, and w.

Parameters
arrayAn array containing the elements of the quaternion.
Returns
A reference to this (modified) Quaternion for chaining.

◆ operator=() [2/3]

Quaternion& cugl::Quaternion::operator= ( const Mat4 m)
inline

Sets quaternion equal to the rotational part of the specified matrix.

This constructor may fail, particularly if the scale component of the matrix is too small. In that case, this method intializes this quaternion to the zero quaternion.

Parameters
mThe matrix to extract the rotation.
Returns
A reference to this (modified) Qauternion for chaining.

◆ operator=() [3/3]

Quaternion& cugl::Quaternion::operator= ( const Vec4  vector)

Sets the coordinates of this quaternion to those of the given vector.

Parameters
vectorThe vector to convert
Returns
A reference to this (modified) Qauternion for chaining.

◆ operator==()

bool cugl::Quaternion::operator== ( const Quaternion q) const

Returns true if this quaternion is equal to the given quaternion.

Comparison is exact, which may be unreliable given that the attributes are floats.

Parameters
qThe quaternion to compare against.
Returns
True if this quaternion is equal to the given quaternion.

◆ rotate()

static Vec3* cugl::Quaternion::rotate ( const Vec3  v,
const Quaternion quat,
Vec3 dst 
)
static

Rotates the vector by this quaternion and stores the result in dst.

The rotation is defined by the matrix associated with the vector.

Parameters
vThe vector to rotate.
quatThe rotation quaternion.
dstA vector to store the result in.
Returns
A reference to dst for chaining

◆ scale() [1/2]

static Quaternion* cugl::Quaternion::scale ( const Quaternion q1,
float  s,
Quaternion dst 
)
static

Scales the specified quaternion by s and stores the result in dst.

Parameters
q1The first quaternion.
sThe scalar value.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ scale() [2/2]

Quaternion& cugl::Quaternion::scale ( float  s)
inline

Scales this quaternion by the specified value in place.

Parameters
sThe value to scale the quaternion.
Returns
this quaternion, after multiplication

◆ set() [1/6]

Quaternion& cugl::Quaternion::set ( const float *  array)

Sets the elements of this quaternion from the values in the specified array.

The elements of the array are in the order x, y, z, and w.

Parameters
arrayAn array containing the elements of the quaternion.
Returns
A reference to this (modified) Quaternion for chaining.

◆ set() [2/6]

Quaternion& cugl::Quaternion::set ( const Mat4 m)

Sets quaternion equal to the rotational part of the specified matrix.

This constructor may fail, particularly if the scale component of the matrix is too small. In that case, this method intializes this quaternion to the zero quaternion.

Parameters
mThe matrix to extract the rotation.
Returns
A reference to this (modified) Qauternion for chaining.

◆ set() [3/6]

Quaternion& cugl::Quaternion::set ( const Quaternion q)
inline

Sets the elements of this quaternion to those in the specified quaternion.

Parameters
qThe quaternion to copy.
Returns
A reference to this (modified) Quaternion for chaining.

◆ set() [4/6]

Quaternion& cugl::Quaternion::set ( const Vec3  axis,
float  angle 
)

Sets the quaternion equal to the rotation from the specified axis and angle.

Parameters
axisThe axis of rotation.
angleThe angle of rotation (in radians).
Returns
A reference to this (modified) Quaternion for chaining.

◆ set() [5/6]

Quaternion& cugl::Quaternion::set ( const Vec4  vector)

Sets the coordinates of this quaternion to those of the given vector.

Parameters
vectorThe vector to convert
Returns
A reference to this (modified) Qauternion for chaining.

◆ set() [6/6]

Quaternion& cugl::Quaternion::set ( float  x,
float  y,
float  z,
float  w 
)
inline

Sets the elements of the quaternion to the specified values.

Parameters
xThe x component of the quaternion.
yThe y component of the quaternion.
zThe z component of the quaternion.
wThe w component of the quaternion.
Returns
A reference to this (modified) Quaternion for chaining.

◆ setIdentity()

Quaternion& cugl::Quaternion::setIdentity ( )
inline

Sets this quaternion to be equal to the identity quaternion.

Returns
A reference to this (modified) Quaternion for chaining.

◆ setZero()

Quaternion& cugl::Quaternion::setZero ( )
inline

Sets this quaternion to be equal to the zero quaternion.

Returns
A reference to this (modified) Quaternion for chaining.

◆ slerp() [1/2]

Quaternion& cugl::Quaternion::slerp ( const Quaternion q,
float  t 
)
inline

Interpolates between this quaternion and another with spherical linear interpolation.

Spherical linear interpolation provides smooth transitions between different orientations and is often useful for animating models or cameras in 3D.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

In addition, the input quaternions must be at (or close to) unit length. When assertions are enabled, it will test this via the isUnit() method.

Parameters
qThe quaternion to interpolate.
tThe interpolation coefficient in [0,1].
Returns
this quaternion, after interpolation.

◆ slerp() [2/2]

static Quaternion* cugl::Quaternion::slerp ( const Quaternion q1,
const Quaternion q2,
float  t,
Quaternion dst 
)
static

Interpolates between two quaternions using spherical linear interpolation.

Spherical linear interpolation provides smooth transitions between different orientations and is often useful for animating models or cameras in 3D.

The interpolation coefficient MUST be between 0 and 1 (inclusive). Any other values will cause an error.

In addition, the input quaternions must be at (or close to) unit length. When assertions are enabled, it will test this via the isUnit() method.

Parameters
q1The first quaternion.
q2The second quaternion.
tThe interpolation coefficient in [0,1].
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ subtract() [1/2]

Quaternion& cugl::Quaternion::subtract ( const Quaternion q)
inline

Subtracts the specified quaternion from this one in place.

Parameters
qThe quaternion to subtract.
Returns
this quaternion, after subtraction

◆ subtract() [2/2]

static Quaternion* cugl::Quaternion::subtract ( const Quaternion q1,
const Quaternion q2,
Quaternion dst 
)
static

Subtacts the quaternions q2 from q1 and stores the result in dst.

Parameters
q1The first quaternion.
q2The second quaternion.
dstA quaternion to store the result in.
Returns
A reference to dst for chaining

◆ toAxisAngle()

float cugl::Quaternion::toAxisAngle ( Vec3 e) const

Converts this Quaternion4f to axis-angle notation.

The angle is in radians. The axis is normalized.

Parameters
eThe Vec3f which stores the axis.
Returns
The angle (in radians).

◆ toString()

std::string cugl::Quaternion::toString ( bool  verbose = false) const

Returns a string representation of this quaternion for debuggging purposes.

If verbose is true, the string will include class information. This allows us to unambiguously identify the class.

Parameters
verboseWhether to include class information
Returns
a string representation of this vector for debuggging purposes.

Member Data Documentation

◆ IDENTITY

const Quaternion cugl::Quaternion::IDENTITY
static

The identity Quaternion(0,0,0,1)

◆ w

float cugl::Quaternion::w

The scalar component of the quaternion.

◆ x

float cugl::Quaternion::x

The x-value of the quaternion's vector component.

◆ y

float cugl::Quaternion::y

The y-value of the quaternion's vector component.

◆ z

float cugl::Quaternion::z

The z-value of the quaternion's vector component.

◆ ZERO

const Quaternion cugl::Quaternion::ZERO
static

The zero to Quaternion(0,0,0,0)


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