MercuryTypes.h

Go to the documentation of this file.
00001 #ifndef _MERCURYTYPES_H
00002 #define _MERCURYTYPES_H
00003 
00004 #include "global.h"
00005 #include "MercuryMath.h"
00006 #include "MercuryMatrix.h"
00007 
00009 class MercuryPoint
00010 {
00011 public:
00012     MercuryPoint() : x(0), y(0), z(0) { };
00013     MercuryPoint( float ix, float iy, float iz ) : x(ix), y(iy), z(iz) { };
00014     MercuryPoint( const float * in ) : x(in[0]), y(in[1]), z(in[2]) { };
00015 
00017     operator float* () { return &x; }
00019     operator const float* () const { return &x; }
00020 
00022     inline const float GetX() const { return x; }
00024     inline const float GetY() const { return y; }
00026     inline const float GetZ() const { return z; }
00028     inline bool SetX(const float ix) { if (ix == x) { return false; } x = ix; return true; }
00030     inline bool SetY(const float iy) { if (iy == y) { return false; } y = iy; return true; }
00032     inline bool SetZ(const float iz) { if (iz == z) { return false; } z = iz; return true; }
00034     inline void Clear() { x = 0; y = 0; z = 0; }
00035     
00036     //allow [] to access
00037     float & operator[] ( const int rhs );
00038     const float operator[] ( const int rhs ) const;
00039 
00041     void NormalizeSelf();
00043     const MercuryPoint Normalize() const;
00045     float Magnitude() const;
00046 
00047     float GetBiggestElement() const { if( x > y ) return (x>z)?x:z; else return (y>z)?y:z; }
00048 
00050     inline void ConvertToVector3( float* out ) const { out[0] = x; out[1] = y; out[2] = z; }
00052     inline void ConvertToVector4( float* out ) const { out[0] = x; out[1] = y; out[2] = z; out[3] = 0; }
00054     inline void ConvertToIVector4( float* out ) const { out[0] = -x; out[1] = -y; out[2] = -z; out[3] = 0; }
00055 
00057     MercuryPoint operator*(const MercuryPoint& p) const;
00059     MercuryPoint operator/(const MercuryPoint& p) const;
00060 
00061     inline MercuryPoint& operator += ( const MercuryPoint& other )      { x+=other.x; y+=other.y; z+=other.z; return *this; }
00062     inline MercuryPoint& operator -= ( const MercuryPoint& other )      { x-=other.x; y-=other.y; z-=other.z; return *this; }
00063     inline MercuryPoint& operator *= ( float f )                        { x*=f; y*=f; z*=f; return *this; }
00064     inline MercuryPoint& operator /= ( float f )                        { x/=f; y/=f; z/=f; return *this; }
00065 
00066     inline MercuryPoint operator + ( const MercuryPoint& other ) const  { return MercuryPoint( x+other.x, y+other.y, z+other.z ); }
00067     inline MercuryPoint operator - ( const MercuryPoint& other ) const  { return MercuryPoint( x-other.x, y-other.y, z-other.z ); }
00068     inline MercuryPoint operator * ( float f ) const                    { return MercuryPoint( x*f, y*f, z*f ); }
00069     inline MercuryPoint operator / ( float f ) const                    { return MercuryPoint( x/f, y/f, z/f ); }
00070 
00071     friend MercuryPoint operator * ( float f, const MercuryPoint& other )   { return other*f; }
00072 
00073     bool operator==(const MercuryPoint& p) const;
00074     inline bool operator!=(const MercuryPoint& p) const { return !(*this == p); }
00075 
00076     bool operator==(const float f) const;
00077     inline bool operator!=(const float f) const { return !(*this == f); }
00078 
00080     MercuryPoint CrossProduct(const MercuryPoint& p) const;
00081 
00082     float x;
00083     float y;
00084     float z;
00085 };
00086 
00088 extern const MercuryPoint gpZero;
00090 extern const MercuryPoint gpOne;
00091 
00092 
00093 
00095 MercuryPoint Rotate2DPoint( float fAngle, MercuryPoint pIn );
00096 
00098 void AngleMatrix (const MercuryPoint & angles, MercuryMatrix & mat );
00100 void TranslationMatrix( const MercuryPoint & position, MercuryMatrix & mat );
00102 void R_ConcatTransforms3 (  MercuryMatrix in1,  MercuryMatrix in2, MercuryMatrix & out );
00103 
00105 void VectorIRotate (const MercuryPoint & in1, MercuryMatrix &in2, MercuryPoint & out);
00106 void VectorRotate (const MercuryPoint & in1, const MercuryMatrix &in2, MercuryPoint & out);
00107 
00109 void VectorMultiply( MercuryMatrix &m, const MercuryPoint &p, MercuryPoint &out );
00110 
00112 void InvertMatrix( MercuryMatrix &in, MercuryMatrix & out );
00113 
00115 class MQuaternion {
00116 public:
00117     //Defines a Quaternion such that q = w + xi + yj + zk
00118     float w,x,y,z;
00119     MQuaternion() : w(0), x(0), y(0), z(0) { };
00120     MQuaternion(float W, float X, float Y, float Z) : w(W), x(X), y(Y), z(Z) { };
00121     MQuaternion(float* wxyz) : w(wxyz[0]), x(wxyz[1]), y(wxyz[2]), z(wxyz[3]) { };
00122     MQuaternion(const MercuryPoint& p) : w(0), x(p.GetX()), y(p.GetY()), z(p.GetZ()) {};
00123 
00125     void SetEuler(const MercuryPoint& angles);
00126 
00128     void CreateFromAxisAngle(const MercuryPoint& p, const float radians);
00129 
00131     float & operator[] ( const int rhs );
00132     const float & operator[] ( const int rhs ) const;
00133 
00135     float magnitude() const;
00137     MQuaternion normalize() const;
00139     MQuaternion conjugate() const;
00141     MQuaternion reciprocal() const;
00143     MQuaternion rotateAbout(const MQuaternion &spinAxis) const;
00145     void toMatrix( MercuryMatrix & mat ) const;
00147     void toMatrix4( MercuryMatrix & mat ) const;
00149     MercuryPoint ToPoint() { return MercuryPoint( x,y,z ); }
00150     /******************************************************
00151      * NOTE: Quaternion multipication is not commutative  *
00152      *       Therefore the / operator could imply for a/b *
00153      *       a*b.reciprocal() or b.reciprocal()*a         *
00154      ******************************************************/
00155     MQuaternion operator + (const MQuaternion &rhs) const;
00156     MQuaternion operator - (const MQuaternion &rhs) const;
00157     MQuaternion operator * (const MQuaternion &rhs) const;
00158     MQuaternion& operator =  (const MQuaternion &rhs);
00159     MQuaternion& operator += (const MQuaternion &rhs);
00160     MQuaternion& operator -= (const MQuaternion &rhs);
00161     MQuaternion& operator *= (const MQuaternion &rhs);
00162     MQuaternion operator * (const float &rhs) const;
00163     MQuaternion operator / (const float &rhs) const;
00164     MQuaternion& operator *= (const float &rhs);
00165     MQuaternion& operator /= (const float &rhs);
00166 } M_ALIGN(32);
00167 
00169 float innerProduct( const MQuaternion &a, const MQuaternion &b );
00170 
00172 MercuryPoint outerProduct( const MQuaternion &a, const MQuaternion &b );
00173 
00175 MQuaternion evenProduct(const MQuaternion &a, const MQuaternion &b );
00176 
00178 MercuryPoint oddProduct( const MQuaternion &a, const MQuaternion &b );
00179 
00181 MQuaternion SLERP( const MQuaternion &a, const MQuaternion &b, float t );
00182 
00183 #endif
00184 
00185 
00186 /* 
00187  * Copyright (c) 2005-2006, Joshua Allen, Charles Lohr, Adam Lowman
00188  * All rights reserved.
00189  *
00190  * Redistribution and use in source and binary forms, with or
00191  * without modification, are permitted provided that the following
00192  * conditions are met:
00193  *  -   Redistributions of source code must retain the above
00194  *      copyright notice, this list of conditions and the following disclaimer.
00195  *  -   Redistributions in binary form must reproduce the above copyright
00196  *      notice, this list of conditions and the following disclaimer in
00197  *      the documentation and/or other materials provided with the distribution.
00198  *  -   Neither the name of the Mercury Engine nor the names of its
00199  *      contributors may be used to endorse or promote products derived from
00200  *      this software without specific prior written permission.
00201  *
00202  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00203  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00204  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00205  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00206  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00207  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00208  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00209  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00210  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00211  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00212  */

Hosted by SourceForge.net Logo