Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Quaternion.h

00001 /*
00002   File: Quaternion.h
00003 
00004   Copyright(C) C. Kotterink, Computed Graphics
00005 */
00006 #ifndef QUATERNION_H
00007 #define QUATERNION_H
00008 
00009 #include<Vertex3.h>
00010 #include<Matrix3.h>
00011 
00012 #include<real.h>
00013 #include<iostream.h>
00014 
00015 class ObjectFile;
00016 
00019 class Quaternion
00020 {
00021     friend ostream &operator<<(ostream &stream, Quaternion &q);
00022     friend ObjectFile &operator>>(ObjectFile &f, Quaternion &q);
00023 public:
00024     // Constructors
00025     Quaternion() {};
00026     Quaternion(real xx, real yy, real zz, real ww)
00027         : x(xx), y(yy), z(zz), w(ww) {}
00028     Quaternion(const Vertex3 &v, real angle);
00029     Quaternion(const Quaternion &q) : x(q.x), y(q.y), z(q.z), w(q.w) {}
00030 
00031     void identity();
00032     void trackball(real px, real py, real qx, real qy);
00033 
00034     // Get/Set
00035     real &operator [](unsigned int i) {
00036         ((real*)(this))[i];
00037     }
00038     void setRotation(const Vertex3 &v, real angle);
00039     void getRotation(Vertex3 &v, real &angle);
00040 
00041     // Assignment operators
00042     Quaternion &operator =(const Quaternion &q) {
00043         x = q.x;
00044         y = q.y;
00045         z = q.z;
00046         w = q.w;
00047         return *this;
00048     }
00049     void operator -=(const Quaternion &q) {
00050         x -= q.x;
00051         y -= q.y;
00052         z -= q.z;
00053         w -= q.w;
00054     }
00055     void operator += (const Quaternion &q) {
00056         x += q.x;
00057         y += q.y;
00058         z += q.z;
00059         w += q.w;
00060     }
00061     void operator *= (const Quaternion &q) {
00062         real xx = w*q.x + x*q.w + y*q.z - z*q.y;
00063         real yy = w*q.y - x*q.z + y*q.w + z*q.x;
00064         real zz = w*q.z + x*q.y - y*q.x + z*q.w;
00065         w = w*q.w - x*q.x - y*q.y - z*q.z;
00066         x = xx;
00067         y = yy;
00068         z = zz;
00069     }
00070     void operator *=(real r) {
00071         x *= r;
00072         y *= r;
00073         z *= r;
00074         w *= r;
00075     }
00076     void operator /=(Quaternion q) {
00077         *this *= !q;
00078     }
00079     void operator /=(real r) {
00080         x /= r;
00081         y /= r;
00082         z /= r;
00083         w /= r;
00084     }
00085 
00086     // Linear interpolation
00087     void slerp(const Quaternion &p,const Quaternion &q, double t);
00088     
00089     // Operators
00090     Quaternion operator ~() const {             // Conjugate
00091         Quaternion q(-x, -y, -z, w);
00092         return q;
00093     }
00094     Quaternion operator !() const {             // Multiplicative inverse
00095         Quaternion q;
00096         real n = x*x + y*y + z*z + w*w;
00097         q.x = x/n;
00098         q.y = y/n;
00099         q.z = z/n;
00100         q.w = w/n;
00101         return q;
00102     }
00103     Quaternion operator -(const Quaternion &q) const {
00104         Quaternion qq(x - q.x, y - q.y, z - q.z, w);
00105         return qq;
00106     }
00107     Quaternion operator +(const Quaternion &q) const {
00108         Quaternion qq(x + q.x, y + q.y, z + q.z, w + q.w);
00109         return qq;
00110     }
00111     Quaternion operator *(const Quaternion &q) const {
00112         Quaternion qq;
00113         qq.x = w*q.x + x*q.w + y*q.z - z*q.y;
00114         qq.y = w*q.y - x*q.z + y*q.w + z*q.x;
00115         qq.z = w*q.z + x*q.y - y*q.x + z*q.w;
00116         qq.w  = w*q.w - x*q.x - y*q.y - z*q.w;
00117         return qq;
00118     }
00119     Quaternion operator *(real r) const {
00120         Quaternion qq(x * r, y * r, z * r, w * r);
00121         return qq;
00122     }
00123     Quaternion operator /(const Quaternion &q) const {
00124         Quaternion qq;
00125         qq = *this * !q;
00126         return qq;
00127     }
00128     Quaternion operator /(real r) const {
00129         Quaternion qq(x / r, y / r, z / r, w / r);
00130         return qq;
00131     }
00132 
00133     real norm() const;
00134     Quaternion &normalize() {
00135         *this /= norm();
00136         return *this;
00137     }
00138 
00139     real x, y, z, w;
00140 };
00141 #endif

This documentation was generated using doxygen. If you have any comments or additions please mail me.