Quaternion
A quaternion is commonly used to represent a 3D rotation. Like Float4
, it consists of four
floating-point components x
, y
, z
and w
.
Only unit quaternions (ie. quaternions with unit length) represent valid 3D rotations. The x
, y
and z
components give the direction of the rotation axis, while the w
component is the cosine of
half the rotation angle. Rotation follows the right-hand
rule in a right-handed coordinate system.
When working with quaternions and vectors, the *
operator behaves differently depending on the
type of the operands:
- If
a
andb
are bothQuaternion
,a * b
returns their Hamilton product (composition). - If
q
is a unitQuaternion
andv
is aFloat3
,q * v
rotatesv
usingq
. - If
a
andb
are bothFloat4
,a * b
performs componentwise multiplication.
Header File
#include <ply-math/Matrix.h>
Also included from <ply-math/Base.h>
.
Data Members
-
w
is the fourth component. It followsz
sequentially in memory.
Constructors
-
Constructs an uninitialized
Quaternion
. -
Constructs a
Quaternion
from the given components.Quaternion q = {0, 0, 0, 1};
-
Constructs a
Quaternion
from aFloat3
and a fourth component. -
Returns a const reference to the first three components as a
Float3
using type punning. This should only be used as a temporary expression. -
Casts to
Float4
using type punning. This should only be used as a temporary expression. -
Returns the identity quaternion
{0, 0, 0, 1}
. -
Returns a quaternion that represents a rotation around the given axis.
unitAxis
must have unit length. The angle is specified in radians. Rotation follows the right-hand rule in a right-handed coordinate system. -
Returns a unit quaternion that transforms
start
toend
. Both input vectors must have unit length. -
Converts a rotation matrix to a unit quaternion.
-
Given a unit quaternion, returns a unit quaternion that represents its inverse rotation.
This function actually returns the conjugate of the given quaternion by negating its
x
,y
andz
components, with the understanding that the conjugate of a unit quaternion is also its inverse. -
Rotates the special vectors (1, 0, 0), (0, 1, 0) and (0, 0, 1) by the given quaternion using fewer instructions than
operator*()
.q.rotateUnitX()
is equivalent toq * Float3{1, 0, 0}
q.rotateUnitY()
is equivalent toq * Float3{0, 1, 0}
q.rotateUnitZ()
is equivalent toq * Float3{0, 0, 1}
This function will probably be replaced by an overload of
operator*()
that acceptsAxis3
values. -
Rotates the vector
v
by the quaternionq
.q
must have unit length. Quaternion operator*(const Quaternion& a, const Quaternion& b)
[code]-
Composes two quaternions using the Hamilton product. If both quaternions have unit length, the resulting quaternion represents a rotation by
b
followed by a rotation bya
. -
Returns a unit quaternion having the same direction as
this
. The input quaternion must not be zero. When many unit quaternions are composed together, it's a good idea to occasionally re-normalize the result to compensate for any drift caused by floating-point imprecision. Quaternion operator-(const Quaternion& q)
[code]-
Unary negation. All components of the original quaternion are negated. If the quaternion has unit length, the resulting quaternion represents the same 3D rotation as the original quaternion.
-
Returns either
this
or-this
, whichever is closer toother
. Ifthis
has unit length, the resulting quaternion represents the same 3D rotation asthis
.In general, when interpolating between arbitrary quaternions
a
andb
, it's best to interpolate froma
tob.negatedIfCloserTo(a)
since this form takes the shortest path. -
Interpolates between two quaternions. Performs a linear interpolation on the components of
a.negatedIfCloserTo(b)
andb
, then normalizes the result.