AaronCameron.net
No ads. No Profit. No Master, But Truth.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 02:11am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cQuaternion.h

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #ifndef _n2l4_cQuaternion_h
00027 #define _n2l4_cQuaternion_h
00028 
00029 #include <math.h>
00030 #include <GL/gl.h>
00031 
00032 /******************************************************************************/
00033 namespace n2l
00034 {
00037     template <class TComponent>
00038     class cQuaternion
00039     {
00040     public:
00041         typedef TComponent      tComponent;
00042 
00043         typedef cQuaternion<TComponent> tThisQuaternion;
00044 
00045         // Constructors
00046         /**********************************************************************/
00047         cQuaternion() {}
00048 
00049         cQuaternion( const tComponent *iComp )
00050         {
00051             set(iComp);
00052         }
00053 
00054         /**********************************************************************/
00055         cQuaternion( const tThisQuaternion & iQ )
00056         {
00057             set( iQ.data() );
00058         }
00059 
00060         /**********************************************************************/
00061         cQuaternion( const tComponent & iX, const tComponent & iY,
00062             const tComponent & iZ, const tComponent & iW )
00063         {
00064             mComp[0] = iX;
00065             mComp[1] = iY;
00066             mComp[2] = iZ;
00067             mComp[3] = iW;
00068         }
00069 
00070 
00071         /**********************************************************************/
00072         ~cQuaternion() {}
00073 
00074         /**********************************************************************/
00075         // Assignments
00076         tComponent & x( const tComponent iX ) {
00077             mComp[0] = iX; return mComp[0];
00078         }
00079         tComponent & y( const tComponent iY ) {
00080             mComp[1] = iY; return mComp[1];
00081         }
00082         tComponent & z( const tComponent iZ ) {
00083             mComp[2] = iZ; return mComp[2];
00084         }
00085         tComponent & w( const tComponent iW ) {
00086             mComp[3] = iW; return mComp[3];
00087         }
00088 
00089         void set( const tComponent *iComp ) {
00090             memmove( (void*)(mComp), (void*)(iComp), sizeof(tComponent)*4 );
00091         }
00092 
00093         /**********************************************************************/
00094         tThisQuaternion &operator =( const tThisQuaternion &iQ )
00095         {
00096             set( iQ.data() );
00097             return *this;
00098         }       
00099 
00100         /**********************************************************************/
00101         void set( const tComponent &iX, const tComponent &iY,
00102             const tComponent &iZ, const tComponent &iW )
00103         {
00104             mComp[0] = iX;
00105             mComp[1] = iY;
00106             mComp[2] = iZ;
00107             mComp[3] = iW;
00108         }
00109 
00110         /**********************************************************************/
00111         // Views
00112         inline const tComponent &x() const { return mComp[0]; }
00113         inline const tComponent &y() const { return mComp[1]; }
00114         inline const tComponent &z() const { return mComp[2]; }
00115         inline const tComponent &w() const { return mComp[3]; }
00116 
00117         inline tComponent &x() { return mComp[0]; }
00118         inline tComponent &y() { return mComp[1]; }
00119         inline tComponent &z() { return mComp[2]; }
00120         inline tComponent &w() { return mComp[3]; }
00121 
00122         inline tComponent &operator[]( const tUint iIndex ) {
00123             return mComp[iIndex];
00124         }
00125         inline const tComponent &operator[]( const tUint iIndex ) const {
00126             return mComp[iIndex];
00127         }
00128 
00129         /**********************************************************************/
00130         const tBool containsNaN() const
00131         {
00132             if (mComp[0]!=mComp[0] || mComp[1]!=mComp[1] ||
00133                 mComp[2]!=mComp[2] || mComp[3]!=mComp[3])
00134                 return true;
00135             else
00136                 return false;
00137         }
00138 
00139         /**********************************************************************/
00140         void get( tComponent &oX, tComponent &oY,
00141             tComponent &oZ, tComponent &oW ) const
00142         {
00143             oX = mComp[0];
00144             oY = mComp[1];
00145             oZ = mComp[2];
00146             oW = mComp[3];
00147         }
00148         
00149         /**********************************************************************/
00150         const tBool operator ==( const tThisQuaternion &iQ ) const
00151         {
00152             return (memcmp( (void*)(iQ.data()), (void*)(mComp),
00153                 sizeof(tComponent)*4)==0);
00154         }
00155 
00156         /**********************************************************************/
00157         const tBool operator !=( const tThisQuaternion &iQ ) const
00158         {
00159             return (memcmp( (void*)(iQ.data()), (void*)(mComp),
00160                 sizeof(tComponent)*4)!=0);
00161         }
00162 
00163         /**********************************************************************/
00164         const tString dump() const {
00165             tString str("tQuaternion(");
00166             str += asString(mComp[0]);
00167             str += ",";
00168             str += asString(mComp[1]);
00169             str += ",";
00170             str += asString(mComp[2]);
00171             str += ",";
00172             str += asString(mComp[3]);
00173             str += ")";
00174             return str;
00175         }
00176 
00177         /**********************************************************************/
00178         // Use this method at your own risk.
00179         tComponent *const data() { return mComp; }
00180 
00181     private:
00182         tComponent mComp[4];
00183 
00184     }; // class
00185 
00186 } // namespace n2l
00187 
00188 
00189 
00190 #endif
©2012 Aaron Cameron