# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

matrix.h

Go to the documentation of this file.
00001 
00008 /* Copyright, 2000 Nevrax Ltd.
00009  *
00010  * This file is part of NEVRAX NEL.
00011  * NEVRAX NEL is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015 
00016  * NEVRAX NEL is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * General Public License for more details.
00020 
00021  * You should have received a copy of the GNU General Public License
00022  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00024  * MA 02111-1307, USA.
00025  */
00026 
00027 #ifndef NL_MATRIX_H
00028 #define NL_MATRIX_H
00029 
00030 #include "nel/misc/vector.h"
00031 #include "nel/misc/vector_h.h"
00032 #include "nel/misc/quat.h"
00033 
00034 
00035 namespace       NLMISC
00036 {
00037 
00038 class   CPlane;
00039 
00040 
00041 // ======================================================================================================
00066 class CMatrix
00067 {
00068 public:
00070         enum    TRotOrder
00071         {
00072                 XYZ,
00073                 XZY,
00074                 YXZ,
00075                 YZX,
00076                 ZXY,
00077                 ZYX
00078         };
00079 
00081         static  const CMatrix   Identity;
00082 
00083 public:
00084 
00086 
00087 
00088         CMatrix()
00089         {
00090                 StateBit= 0;
00091                 // Init just Pos because must always be valid for faster getPos()
00092                 M[12]= M[13]= M[14]= 0;
00093         }
00095         CMatrix(const CMatrix &);
00097         CMatrix &operator=(const CMatrix &);
00099 
00100 
00101 
00103 
00104 
00105         void            identity();
00115         void            setRot(const CVector &i, const CVector &j, const CVector &k, bool hintNoScale=false);
00123         void            setRot(const float m33[9], bool hintNoScale=false);
00128         void            setRot(const CVector &v, TRotOrder ro);
00132         void            setRot(const CQuat &quat);
00136         void            setRot(const CMatrix &matrix);
00141         void            setPos(const CVector &v);
00145         void            movePos(const CVector &v);
00150         void            setProj(const float proj[4]);
00153         void            resetProj();
00160         void            set(const float m44[16]);
00162 
00163 
00164 
00166 
00167 
00172         void            getRot(CVector &i, CVector &j, CVector &k) const;
00176         void            getRot(float m33[9]) const;
00180         void            getRot(CQuat &quat) const;
00184         CQuat           getRot() const {CQuat   ret; getRot(ret); return ret;}
00188         void                    getPos(CVector &v) const        {v.x= M[12]; v.y= M[13]; v.z= M[14];}
00193         const CVector   &getPos() const                         {return *(CVector*)(M+12);}
00197         void            getProj(float proj[4]) const;
00199         CVector         getI() const;
00201         CVector         getJ() const;
00203         CVector         getK() const;
00207         void            get(float m44[16]) const;
00211         const float *get() const;
00217         //CVector               toEuler(TRotOrder ro) const;
00219 
00220 
00221 
00223 
00224 
00225         void            translate(const CVector &v);
00229         void            rotateX(float a);
00233         void            rotateY(float a);
00237         void            rotateZ(float a);
00242         void            rotate(const CVector &v, TRotOrder ro);
00245         void            rotate(const CQuat &quat);
00247         void            scale(float f);
00249         void            scale(const CVector &scale);
00251 
00252 
00253 
00255 
00256 
00260         void            setMulMatrix(const CMatrix &m1, const CMatrix &m2);
00262         CMatrix         operator*(const CMatrix &in) const
00263         {
00264                 CMatrix         ret;
00265                 ret.setMulMatrix(*this, in);
00266                 return ret;
00267         }
00269         CMatrix         &operator*=(const CMatrix &in)
00270         {
00271                 CMatrix         ret;
00272                 ret.setMulMatrix(*this, in);
00273                 *this= ret;
00274                 return *this;
00275         }
00281         void            setMulMatrixNoProj(const CMatrix &m1, const CMatrix &m2);
00284         void            transpose3x3();
00288         void            transpose();
00292         void            invert();
00296         CMatrix         inverted() const;
00303         bool            normalize(TRotOrder pref);
00305 
00306 
00307 
00309 
00310 
00311         CVector         mulVector(const CVector &v) const;
00313         CVector         mulPoint(const CVector &v) const;
00315         CVector         operator*(const CVector &v) const
00316         {
00317                 return mulPoint(v);
00318         }
00319 
00321         CVectorH        operator*(const CVectorH& v) const;
00323 
00325 
00326         void            serial(IStream &f);
00328         bool            hasScalePart() const;
00330         bool            hasScaleUniform() const;
00332         float           getScaleUniform() const;
00334         bool            hasProjectionPart() const;
00336 
00337         // Friend.
00339         friend CPlane           operator*(const CPlane &p, const CMatrix &m);
00340 
00341 
00342 private:
00343         float   M[16];
00344         float   Scale33;
00345         uint32  StateBit;       // BitVector. 0<=>identity.
00346 
00347         // Methods For inversion.
00348         void    fastInvert33(CMatrix &ret) const;
00349         bool    slowInvert33(CMatrix &ret) const;
00350         bool    slowInvert44(CMatrix &ret) const;
00351         // access to M, in math conventions (mat(1,1) ... mat(4,4)). Indices from 0 to 3.
00352         float   &mat(sint i, sint j)
00353         {
00354                 return M[ (j<<2) + i];
00355         }
00356         // access to M, in math conventions (mat(1,1) ... mat(4,4)). Indices from 0 to 3.
00357         const float     &mat(sint i, sint j) const
00358         {
00359                 return M[ (j<<2) + i];
00360         }
00361         // return the good 3x3 Id to compute the minor of (i,j);
00362         void    getCofactIndex(sint i, sint &l1, sint &l2, sint &l3) const
00363         {
00364                 switch(i)
00365                 {
00366                         case 0: l1=1; l2=2; l3=3; break;
00367                         case 1: l1=0; l2=2; l3=3; break;
00368                         case 2: l1=0; l2=1; l3=3; break;
00369                         case 3: l1=0; l2=1; l3=2; break;
00370                 }
00371         }
00372 
00373         // true if MAT_TRANS.
00374         // trans part is true means the right 3x1 translation part matrix is revelant.
00375         // Else it IS initialised to (0,0,0) (exception!!!)
00376         bool    hasTrans() const;
00377         // true if MAT_ROT | MAT_SCALEUNI | MAT_SCALEANY.
00378         // rot part is true means the 3x3 rot matrix AND Scale33 are revelant. 
00379         // Else they are not initialised but are supposed to represent identity and Scale33==1.
00380         bool    hasRot() const;
00381         // true if MAT_PROJ.
00382         // proj part is true means the bottom 1x4 projection part matrix is revelant.
00383         // Else it is not initialised but is supposed to represent the line vector (0,0,0,1).
00384         bool    hasProj() const;
00385         bool    hasAll() const;
00386 
00387         void    testExpandRot() const;
00388         void    testExpandProj() const;
00389 
00390 };
00391 
00392 
00393 }
00394 
00395 
00396 #endif // NL_MATRIX_H
00397 
00398 /* End of matrix.h */