qtom, mtoq, qadd, qsub, qneg, qmul, qdiv, qunit, qinv, qlen, slerp, qmid, qsqrt
- Quaternion arithmetic
Quaternion qadd(Quaternion q, Quaternion r)
Quaternion qsub(Quaternion q, Quaternion r)
Quaternion qneg(Quaternion q)
Quaternion qmul(Quaternion q, Quaternion r)
Quaternion qdiv(Quaternion q, Quaternion r)
Quaternion qinv(Quaternion q)
double qlen(Quaternion p)
Quaternion qunit(Quaternion q)
void qtom(Matrix m, Quaternion q)
Quaternion mtoq(Matrix mat)
Quaternion slerp(Quaternion q, Quaternion r, double a)
Quaternion qmid(Quaternion q, Quaternion r)
Quaternion qsqrt(Quaternion q)
The Quaternions are a non-commutative extension field of the Real numbers,
designed to do for rotations in 3-space what the complex numbers do for
rotations in 2-space. Quaternions have a real component r
imaginary vector component v
add componentwise and multiply according to the rule (
), where . and × are the
ordinary vector dot and cross products. The multiplicative inverse of a
non-zero quaternion ( r
) is (
The following routines do arithmetic on quaternions, represented as
typedef struct Quaternion Quaternion;
double r, i, j, k;
- Add two quaternions.
- Subtract two quaternions.
- Negate a quaternion.
- Multiply two quaternions.
- Divide two quaternions.
- Return the multiplicative inverse of a quaternion.
- Return sqrt(q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k), the length of a
- Return a unit quaternion (length=1) with components proportional to
A rotation by angle θ
about axis A
unit vector) can be represented by the unit quaternion q
/2). The same rotation is
represented by − q
; a rotation by −θ
is the same as a rotation by θ
The quaternion q
transforms points by (0, x',y',z'
. Quaternion multiplication composes
rotations. The orientation of an object in 3-space can be represented by a
quaternion giving its rotation relative to some `standard' orientation.
The following routines operate on rotations or orientations represented as unit
- Convert a rotation matrix (see matrix(3)) to a unit
- Convert a unit quaternion to a rotation matrix.
- Spherical lerp. Interpolate between two orientations. The rotation that
carries q to r is q-1r, so slerp(q, r,
t) is q(q-1r)t.
- slerp(q, r, .5)
- The square root of q. This is just a rotation about the same axis
by half the angle.
To avoid name conflicts with NetBSD, qdiv
is a preprocessor macro defined
; see intro