Quaternions

This package provides the basic mathematical tools necessary for manipulating quaternions. To begin using it, you must first include the header file:

    #include <gfx/quat.h>

For more information on the definition of quaternions and their associated mathematics, you might try the informative Web pages provided by MathWorld, David Eberly, or UC Davis.

class Quat

The interface presented by the Quat class treats quaternions as consisting of a real scalar part and a complex 3-vector part.

Constructor Methods

The Quat class defines the following set of constructors:

    Quat();                                        // Initializes to identity quaternion
    Quat(double x, double y, double z, double w);  // Specify vector & scalar part.
    Quat(const Vec3& a, double b);                 // Specify vector & scalar part.
    Quat(const Quat& q);                           // Copy values from q
    Quat::ident();                                 // Return the identity quaternion

Arithmetic Methods

The quaternion class supports all the usual arithmetic methods, both as in-place assignment operators:

    Quat& operator=(const Quat& q);
    Quat& operator+=(const Quat& q);
    Quat& operator-=(const Quat& q);
    Quat& operator=(double d);
    Quat& operator*=(double d);
    Quat& operator/=(double d);
and as normal binary operations:
    Quat operator+(const Quat& q, const Quat& r);
    Quat operator*(const Quat& q, const Quat& r);
    Quat operator*(const Quat& q, double s);
    Quat operator*(double s, const Quat& q);
    Quat operator/(const Quat& q, double s);

Quaternion Operations

Using the fundamental arithmetic operations defined above, this package also provides the following operations on quaternions:

    double norm(const Quat& q);     // Return the length of q
    Quat conjugate(const Quat& q);  // Return conjugate of q
    Quat inverse(const Quat& q);    // Return multiplicative inverse of q
    void unitize(Quat& q);          // Convert to unit quaternion
    Quat exp(const Quat& q);        // Exponential of a unit quaternion 
    Quat log(const Quat& q);        // Natural logarithm of a unit quaternion 

Quaternions can be constructed from an axis/angle rotation specification using the function:

    Quat axis_to_quat(const Vec3& a, double phi);
At is also generally useful to be able to convert a quaternion into a rotation matrix:
    Mat4 quat_to_matrix(const Quat& q);
    Mat4 unit_quat_to_matrix(const Quat& q);

    Quat slerp(const Quat& from, const Quat& to, double t);

Quaternions can also be read from and written to C++ iostreams using the standard << and >> operators.