CUGL 2.3
Cornell University Game Library
|
#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) | |
Quaternion & | operator= (const float *array) |
Quaternion & | set (float x, float y, float z, float w) |
Quaternion & | set (const float *array) |
Quaternion & | set (const Vec3 axis, float angle) |
Quaternion & | set (const Quaternion &q) |
Quaternion & | setIdentity () |
Quaternion & | setZero () |
Quaternion & | add (const Quaternion &q) |
Quaternion & | subtract (const Quaternion &q) |
Quaternion & | multiply (const Quaternion &q) |
Quaternion & | divide (const Quaternion &q) |
Quaternion & | scale (float s) |
Quaternion & | conjugate () |
Quaternion | getConjugate () const |
Quaternion & | invert () |
Quaternion | getInverse () const |
Quaternion & | normalize () |
Quaternion | getNormalization () const |
Quaternion & | negate () |
Quaternion | getNegation () const |
float | dot (const Quaternion &q) const |
Quaternion & | operator+= (const Quaternion &q) |
Quaternion & | operator-= (const Quaternion &q) |
Quaternion & | operator*= (float s) |
Quaternion & | operator*= (const Quaternion &q) |
Quaternion & | operator/= (float s) |
Quaternion & | operator/= (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 epsilon=CU_MATH_EPSILON) const |
float | norm () const |
float | normSquared () const |
bool | isZero () const |
bool | isNearZero (float epsilon=CU_MATH_EPSILON) const |
bool | isIdentity () const |
bool | isNearIdentity (float epsilon=CU_MATH_EPSILON) const |
bool | isUnit (float epsilon=CU_MATH_EPSILON) const |
float | toAxisAngle (Vec3 *e) const |
Quaternion & | lerp (const Quaternion &q, float t) |
Quaternion & | slerp (const Quaternion &q, float t) |
Quaternion & | nlerp (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) | |
Quaternion & | operator= (const Vec4 vector) |
Quaternion & | set (const Vec4 vector) |
operator Mat4 () const | |
Quaternion (const Mat4 &m) | |
Quaternion & | operator= (const Mat4 &m) |
Quaternion & | set (const Mat4 &m) |
Static Public Member Functions | |
static Quaternion * | createFromRotationMatrix (const Mat4 &m, Quaternion *dst) |
static Quaternion * | createFromAxisAngle (const Vec3 axis, float angle, Quaternion *dst) |
static Quaternion * | add (const Quaternion &q1, const Quaternion &q2, Quaternion *dst) |
static Quaternion * | subtract (const Quaternion &q1, const Quaternion &q2, Quaternion *dst) |
static Quaternion * | multiply (const Quaternion &q1, const Quaternion &q2, Quaternion *dst) |
static Quaternion * | divide (const Quaternion &q1, const Quaternion &q2, Quaternion *dst) |
static Quaternion * | scale (const Quaternion &q1, float s, Quaternion *dst) |
static Quaternion * | conjugate (const Quaternion &quat, Quaternion *dst) |
static Quaternion * | invert (const Quaternion &quat, Quaternion *dst) |
static Quaternion * | normalize (const Quaternion &quat, Quaternion *dst) |
static Quaternion * | negate (const Quaternion &quat, Quaternion *dst) |
static float | dot (const Quaternion &q1, const Quaternion &q2) |
static Quaternion * | lerp (const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst) |
static Quaternion * | slerp (const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst) |
static Quaternion * | nlerp (const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst) |
static Vec3 * | rotate (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 |
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.
|
inline |
Constructs a quaternion initialized to (0, 0, 0, 1).
|
inline |
Constructs a quaternion initialized to the specified values.
x | The x component of the quaternion. |
y | The y component of the quaternion. |
z | The z component of the quaternion. |
w | The w component of the quaternion. |
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.
array | An array containing the elements of the quaternion. |
cugl::Quaternion::Quaternion | ( | const Vec3 | axis, |
float | angle | ||
) |
Constructs a quaternion equal to the rotation from the specified axis and angle.
axis | A vector describing the axis of rotation. |
angle | The angle of rotation (in radians). |
|
explicit |
Creates a quaternion from the given vector.
vector | The vector to convert |
|
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.
m | The matrix to extract the rotation. |
|
inline |
Adds the specified quaternion to this one in place.
q | The quaternion to add. |
|
static |
Adds the specified quaternions and stores the result in dst.
q1 | The first quaternion. |
q2 | The second quaternion. |
dst | A quaternion to store the result in. |
|
inline |
Sets this quaternion to the conjugate of itself.
|
static |
Conjugates the specified quaternion and stores the result in dst.
quat | The quaternion to conjugate. |
dst | A quaternion to store the result in. |
|
static |
Creates this quaternion equal to the rotation from the specified axis and angle.
The result is stored in dst.
axis | A vector describing the axis of rotation. |
angle | The angle of rotation (in radians). |
dst | A quaternion to store the conjugate in. |
|
static |
Creates a quaternion equal to the rotational part of the matrix.
The result is stored in dst.
m | The matrix. |
dst | A quaternion to store the conjugate in. |
|
inline |
Divides this quaternion by the specified one in place.
Division is the same as multiplication by the inverse of q.
q | The quaternion to divide by. |
|
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.
q1 | The initial quaternion. |
q2 | The quaternion to divide by. |
dst | A quaternion to store the result in. |
|
inline |
Returns the dot product of this quaternion with the specified one.
q | The quaternion to compute the dot product with. |
|
static |
Returns the dot product of the two quaternions
q1 | The first quaternion. |
q2 | The second quaternion. |
bool cugl::Quaternion::equals | ( | const Quaternion & | q, |
float | epsilon = 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
q | The vector to compare against. |
epsilon | The comparison tolerance. |
|
inline |
Returns the conjugate of this quaternion.
|
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.
|
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.
q | The quaternion to interpolate. |
t | The interpolation coefficient in [0,1]. |
Note: this does not modify this quaternion.
|
inline |
Returns a negated copy of this quaternion.
Note: This method does not modify the quaternion
|
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.
q | The quaternion to interpolate. |
t | The interpolation coefficient in [0,1]. |
|
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 copy of the vector rotated by this quaternion.
The rotation is defined by the matrix associated with the vector. *
v | The vector to rotate. |
|
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.
q | The quaternion to interpolate. |
t | The interpolation coefficient in [0,1]. |
|
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.
|
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.
quat | The quaternion to invert. |
dst | A quaternion to store the result in. |
bool cugl::Quaternion::isIdentity | ( | ) | const |
Returns true if this quaternion is the identity.
|
inline |
Returns true if this quaternion is near the identity.
The tolerance bounds the quaternion norm of the differences.
epsilon | The comparison tolerance |
|
inline |
Returns true if this quaternion is with tolerance of the zero quaternion.
The tolerance bounds the quaternion norm.
epsilon | The comparison tolerance |
|
inline |
Returns true if this vector is a unit vector.
epsilon | The comparison tolerance |
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.
|
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.
q | The quaternion to interpolate. |
t | The interpolation coefficient in [0,1]. |
|
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.
q1 | The first quaternion. |
q2 | The second quaternion. |
t | The interpolation coefficient in [0,1]. |
dst | A quaternion to store the result in. |
|
inline |
Multiplies this quaternion by the specified one in place.
q | The quaternion to multiply. |
|
static |
Multiplies the specified quaternions and stores the result in dst.
This method performs standard quaternion multiplication
q1 | The first quaternion. |
q2 | The second quaternion. |
dst | A quaternion to store the result in. |
|
inline |
Negates this quaternion in place.
|
static |
Negates the specified quaternion and stores the result in dst.
quat | The quaternion to negate. |
dst | A quaternion to store the result in. |
|
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.
q | The quaternion to interpolate. |
t | The interpolation coefficient in [0,1]. |
|
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.
q1 | The first quaternion. |
q2 | The second quaternion. |
t | The interpolation coefficient in [0,1]. |
dst | A quaternion to store the result in. |
|
inline |
|
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.
|
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.
quat | The quaternion to normalize. |
dst | A quaternion to store the result in. |
|
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).
{
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.
|
inline |
Cast from Quaternion to a string.
cugl::Quaternion::operator Vec4 | ( | ) | const |
Cast from Quaternion to a Vec4.
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.
q | The quaternion to compare quaternion. |
|
inline |
Returns the product of this quaternion with the given quaternion.
This method performs standard quaternion multiplication
Note: this does not modify this quaternion.
q | The quaternion to multiply by. |
|
inline |
Returns the scalar product of this quaternion with the given value.
Note: this does not modify this quaternion.
s | The value to scale by. |
|
inline |
Multiplies this quaternion in place by the given quaternion.
This method performs standard quaternion multiplication
q | The quaternion to multiply by |
|
inline |
Scales this quaternion in place by the given factor.
s | The value to scale by |
|
inline |
Returns the sum of this quaternion with the given quaternion.
Note: this does not modify this quaternion.
q | The quaternion to add. |
|
inline |
Adds the given quaternion to this one in place.
q | The quaternion to add |
|
inline |
Returns the negation of this quaternion.
Note: this does not modify this quaternion.
|
inline |
Returns the difference of this quaternion with the given quaternion.
Note: this does not modify this quaternion.
q | The quaternion to subtract. |
|
inline |
Subtracts the given quaternion from this one in place.
q | The quaternion to subtract |
|
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.
q | The quaternion to divide by |
|
inline |
Returns a copy of this quaternion divided by the given constant
Note: this does not modify this quaternion.
s | the constant to divide this quaternion with |
|
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.
q | The quaternion to divide by |
|
inline |
Divides this quaternion in place by the given factor.
s | The scalar to divide by |
|
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.
array | An array containing the elements of the quaternion. |
|
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.
m | The matrix to extract the rotation. |
Quaternion & cugl::Quaternion::operator= | ( | const Vec4 | vector | ) |
Sets the coordinates of this quaternion to those of the given vector.
vector | The vector to convert |
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.
q | The quaternion to compare against. |
|
static |
Rotates the vector by this quaternion and stores the result in dst.
The rotation is defined by the matrix associated with the vector.
v | The vector to rotate. |
quat | The rotation quaternion. |
dst | A vector to store the result in. |
|
static |
Scales the specified quaternion by s and stores the result in dst.
q1 | The first quaternion. |
s | The scalar value. |
dst | A quaternion to store the result in. |
|
inline |
Scales this quaternion by the specified value in place.
s | The value to scale the quaternion. |
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.
array | An array containing the elements of the quaternion. |
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.
m | The matrix to extract the rotation. |
|
inline |
Sets the elements of this quaternion to those in the specified quaternion.
q | The quaternion to copy. |
Quaternion & cugl::Quaternion::set | ( | const Vec3 | axis, |
float | angle | ||
) |
Sets the quaternion equal to the rotation from the specified axis and angle.
axis | The axis of rotation. |
angle | The angle of rotation (in radians). |
Quaternion & cugl::Quaternion::set | ( | const Vec4 | vector | ) |
Sets the coordinates of this quaternion to those of the given vector.
vector | The vector to convert |
|
inline |
Sets the elements of the quaternion to the specified values.
x | The x component of the quaternion. |
y | The y component of the quaternion. |
z | The z component of the quaternion. |
w | The w component of the quaternion. |
|
inline |
Sets this quaternion to be equal to the identity quaternion.
|
inline |
Sets this quaternion to be equal to the zero quaternion.
|
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.
q | The quaternion to interpolate. |
t | The interpolation coefficient in [0,1]. |
|
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.
q1 | The first quaternion. |
q2 | The second quaternion. |
t | The interpolation coefficient in [0,1]. |
dst | A quaternion to store the result in. |
|
inline |
Subtracts the specified quaternion from this one in place.
q | The quaternion to subtract. |
|
static |
Subtacts the quaternions q2 from q1 and stores the result in dst.
q1 | The first quaternion. |
q2 | The second quaternion. |
dst | A quaternion to store the result in. |
float cugl::Quaternion::toAxisAngle | ( | Vec3 * | e | ) | const |
Converts this Quaternion4f to axis-angle notation.
The angle is in radians. The axis is normalized.
e | The Vec3f which stores the axis. |
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.
verbose | Whether to include class information |
|
static |
The identity Quaternion(0,0,0,1)
float cugl::Quaternion::w |
The scalar component of the quaternion.
float cugl::Quaternion::x |
The x-value of the quaternion's vector component.
float cugl::Quaternion::y |
The y-value of the quaternion's vector component.
float cugl::Quaternion::z |
The z-value of the quaternion's vector component.
|
static |
The zero to Quaternion(0,0,0,0)