Operations for quaternions.
Quaternions extend complex numbers and are widely used in fields like 3D graphics and physics for representing rotations.
In fastmath
, quaternions are represented as 4-dimensional vectors ([[Vec4]]) where the components correspond to the scalar part and the three imaginary parts ($i$, $j$, $k$): $q = a + bi + cj + dk$ is (Vec4. a b c d)
.
The namespace provides functions for creating quaternions, accessing scalar and vector parts, predicates (e.g., real?
, zero?
, inf?
, nan?
), and fundamental properties (magnitude, argument, normalization).
A comprehensive set of operations is included:
rotate
), spherical linear interpolation (SLERP), and conversions between quaternions, Euler angles (ZYX body 3-2-1 and z-y'-x''), and rotation matrices.The implementation correctly handles floating-point special values, including ##Inf
and ##NaN
.
Operations for quaternions. Quaternions extend complex numbers and are widely used in fields like 3D graphics and physics for representing rotations. In `fastmath`, quaternions are represented as 4-dimensional vectors ([[Vec4]]) where the components correspond to the scalar part and the three imaginary parts ($i$, $j$, $k$): $q = a + bi + cj + dk$ is `(Vec4. a b c d)`. The namespace provides functions for creating quaternions, accessing scalar and vector parts, predicates (e.g., [[real?]], [[zero?]], [[inf?]], [[nan?]]), and fundamental properties (magnitude, argument, normalization). A comprehensive set of operations is included: - **Arithmetic:** Addition, subtraction, multiplication, division, negation, square, reciprocal, scaling, conjugation. - **Transcendental Functions:** Extensions of standard complex functions like exponential, logarithm, power, trigonometric, hyperbolic functions, and their inverses. - **Rotations:** Functions for creating rotation quaternions, rotating 3D vectors ([[rotate]]), spherical linear interpolation (SLERP), and conversions between quaternions, Euler angles (ZYX body 3-2-1 and z-y'-x''), and rotation matrices. The implementation correctly handles floating-point special values, including `##Inf` and `##NaN`.
(arg quaternion)
Argument of quaternion, atan2(|vector(q)|, re(q))
Argument of quaternion, atan2(|vector(q)|, re(q))
(complex->quaternion z)
Create quaternion from complex number
Create quaternion from complex number
(conjugate quaternion)
Conjugate of the quaternion
Conjugate of the quaternion
(delta-eq q1 q2)
(delta-eq q1 q2 accuracy)
Compare quaternions with given accuracy (10e-6 by default)
Compare quaternions with given accuracy (10e-6 by default)
(ensure-quaternion q)
Convert possible number, complex number to a quaternion
Convert possible number, complex number to a quaternion
(from-angles [x y z])
(from-angles x y z)
Converts Tait–Bryan angles (z-y′-x'' intrinsic rotation sequence) to a quaternion.
The angles [x y z]
correspond to rotations around the local (body) x-axis, followed by the intermediate local y'-axis, and finally the local z''-axis.
Parameters:
x
: Rotation around the x-axis (radians).y
: Rotation around the y'-axis (radians).z
: Rotation around the z''-axis (radians).Can accept individual double values or a Vec3
containing [x y z]
.
Returns A quaternion Vec4
representing the rotation.
Converts Tait–Bryan angles (z-y′-x'' intrinsic rotation sequence) to a quaternion. The angles `[x y z]` correspond to rotations around the local (body) x-axis, followed by the intermediate local y'-axis, and finally the local z''-axis. Parameters: * `x`: Rotation around the x-axis (radians). * `y`: Rotation around the y'-axis (radians). * `z`: Rotation around the z''-axis (radians). Can accept individual double values or a `Vec3` containing `[x y z]`. Returns A quaternion `Vec4` representing the rotation.
(from-euler [roll pitch yaw])
(from-euler roll pitch yaw)
Converts Euler angles (ZYX body 3-2-1 convention) to a quaternion.
The rotation sequence is intrinsic Z-Y'-X'' (yaw, pitch, roll), applied to the body frame. The order of input parameters or vector components is [roll pitch yaw].
Parameters:
roll
: Rotation around the x-axis (radians), expected range [-pi, pi]
.pitch
: Rotation around the y'-axis (radians), expected range [-pi/2, pi/2]
.yaw
: Rotation around the z''-axis (radians), expected range [-pi, pi]
.Can accept individual double values or a Vec3
containing [roll pitch yaw].
Returns A quaternion Vec4
representing the rotation.
Converts Euler angles (ZYX body 3-2-1 convention) to a quaternion. The rotation sequence is intrinsic Z-Y'-X'' (yaw, pitch, roll), applied to the body frame. The order of input parameters or vector components is [roll pitch yaw]. Parameters: * `roll`: Rotation around the x-axis (radians), expected range `[-pi, pi]`. * `pitch`: Rotation around the y'-axis (radians), expected range `[-pi/2, pi/2]`. * `yaw`: Rotation around the z''-axis (radians), expected range `[-pi, pi]`. Can accept individual double values or a `Vec3` containing [roll pitch yaw]. Returns A quaternion `Vec4` representing the rotation.
(from-rotation-matrix m)
Converts a 3x3 rotation matrix to a quaternion.
Takes a [[Mat3x3]] rotation matrix as input. Returns a [[Vec4]] representing the quaternion that represents the same rotation.
The resulting quaternion is a unit quaternion if the input matrix is a valid rotation matrix. The method handles numerical stability and normalization.
Converts a 3x3 rotation matrix to a quaternion. Takes a [[Mat3x3]] rotation matrix as input. Returns a [[Vec4]] representing the quaternion that represents the same rotation. The resulting quaternion is a unit quaternion if the input matrix is a valid rotation matrix. The method handles numerical stability and normalization.
(imaginary? quaternion)
Is q is a pure imaginary number?
Is q is a pure imaginary number?
(mult q1 q2)
Multiplication of two quaternions.
Multiplication of two quaternions.
(norm quaternion)
Norm of the quaternion, length of the vector
Norm of the quaternion, length of the vector
(normalize quaternion)
Normalize quaternion, unit of quaternion.
Normalize quaternion, unit of quaternion.
(qsgn q)
(qsgn re im-i im-j im-k)
Computes the signum of a quaternion.
Returns 0.0
for the zero quaternion ($0+0i+0j+0k$). For any other quaternion, returns the sign of its scalar part.
Computes the signum of a quaternion. Returns `0.0` for the zero quaternion ($0+0i+0j+0k$). For any other quaternion, returns the sign of its scalar part.
(quaternion a)
(quaternion scalar [i j k])
(quaternion a b c d)
Create quaternion from individual values or scalar and vector parts, reprezented as Vec4
.
Create quaternion from individual values or scalar and vector parts, reprezented as `Vec4`.
(re quaternion)
Returns scalar part of quaternion
Returns scalar part of quaternion
(reciprocal quaternion)
Reciprocal of the quaternion
Reciprocal of the quaternion
(rotate in rotq)
(rotate in angle u)
Rotate 3d in
vector around axis u
, the same as fastmath.vector/axis-rotate
.
Rotate 3d `in` vector around axis `u`, the same as `fastmath.vector/axis-rotate`.
(rotation-quaternion angle u)
Creates a unit quaternion representing a rotation.
The rotation is defined by an angle
(in radians) and a 3D vector u
specifying the axis of rotation. The axis vector u
is normalized
internally to ensure a unit quaternion result.
Parameters:
angle
: The rotation angle in radians (double).u
: The axis of rotation (Vec3). It will be normalized before use.Returns A unit quaternion (Vec4) representing the rotation.
Creates a unit quaternion representing a rotation. The rotation is defined by an `angle` (in radians) and a 3D vector `u` specifying the axis of rotation. The axis vector `u` is normalized internally to ensure a unit quaternion result. Parameters: * `angle`: The rotation angle in radians (double). * `u`: The axis of rotation (Vec3). It will be normalized before use. Returns A unit quaternion (Vec4) representing the rotation.
(scalar quaternion)
Returns scalar part of quaternion, double
Returns scalar part of quaternion, double
(slerp q1 q2 t)
Performs Spherical Linear Interpolation (SLERP) between two quaternions.
SLERP interpolates along the shortest arc on the unit sphere, providing smooth interpolation between rotations represented by unit quaternions.
Parameters:
q1
: The starting quaternion (Vec4).q2
: The ending quaternion (Vec4).t
: The interpolation parameter (double). Should be in the range [0.0, 1.0].
t=0.0
, returns q1
.t=1.0
, returns q2
.0.0 < t < 1.0
, returns an interpolated quaternion.
The parameter t
is internally constrained to [0.0, 1.0].Note: This function is typically used with unit quaternions for rotation interpolation.
Performs Spherical Linear Interpolation (SLERP) between two quaternions. SLERP interpolates along the shortest arc on the unit sphere, providing smooth interpolation between rotations represented by unit quaternions. Parameters: * `q1`: The starting quaternion (Vec4). * `q2`: The ending quaternion (Vec4). * `t`: The interpolation parameter (double). Should be in the range [0.0, 1.0]. - If `t=0.0`, returns `q1`. - If `t=1.0`, returns `q2`. - For `0.0 < t < 1.0`, returns an interpolated quaternion. The parameter `t` is internally constrained to [0.0, 1.0]. Note: This function is typically used with unit quaternions for rotation interpolation.
(to-angles q)
Converts a quaternion q
to Tait–Bryan angles using the z-y′-x'' intrinsic rotation sequence.
The input quaternion is normalized before calculation.
Returns a Vec3
representing the angles [x y z]
. These angles correspond to rotations around the local (body) x-axis, followed by the intermediate local y'-axis, and finally the local z''-axis.
Output angles are typically in the range [-pi, pi]
for x and z, and [-pi/2, pi/2]
for y.
Converts a quaternion `q` to Tait–Bryan angles using the z-y′-x'' intrinsic rotation sequence. The input quaternion is normalized before calculation. Returns a `Vec3` representing the angles `[x y z]`. These angles correspond to rotations around the local (body) x-axis, followed by the intermediate local y'-axis, and finally the local z''-axis. Output angles are typically in the range `[-pi, pi]` for x and z, and `[-pi/2, pi/2]` for y.
(to-euler q)
Converts a quaternion q
to ZYX (body 3-2-1) Euler angles.
The input quaternion is normalized before calculation.
Returns a Vec3
representing the angles [roll pitch yaw]
.
Roll is the angle around the x-axis, pitch around the y'-axis, and yaw around the z''-axis.
Output angles are typically in [-pi, pi] for roll/yaw and [-pi/2, pi/2] for pitch.
Converts a quaternion `q` to ZYX (body 3-2-1) Euler angles. The input quaternion is normalized before calculation. Returns a `Vec3` representing the angles `[roll pitch yaw]`. Roll is the angle around the x-axis, pitch around the y'-axis, and yaw around the z''-axis. Output angles are typically in [-pi, pi] for roll/yaw and [-pi/2, pi/2] for pitch.
(to-rotation-matrix q)
Converts a quaternion to a 3x3 rotation matrix.
The input quaternion is normalized internally to ensure a valid rotation matrix. Unit quaternions are used to represent rotations.
Parameters:
q
: The quaternion (Vec4) to convert.Returns A 3x3 rotation matrix (Mat3x3).
Converts a quaternion to a 3x3 rotation matrix. The input quaternion is normalized internally to ensure a valid rotation matrix. Unit quaternions are used to represent rotations. Parameters: * `q`: The quaternion (Vec4) to convert. Returns A 3x3 rotation matrix (Mat3x3).
(valid? z)
Is valid complex (not NaN or Inf)?
Is valid complex (not NaN or Inf)?
(vector quaternion)
Returns vector part of quaternion, Vec3
type
Returns vector part of quaternion, `Vec3` type
cljdoc builds & hosts documentation for Clojure/Script libraries
Ctrl+k | Jump to recent docs |
← | Move to previous article |
→ | Move to next article |
Ctrl+/ | Jump to the search field |