CrystalSpace

Public API Reference

csgeom/matrix4.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1998,1999,2000 by Jorrit Tyberghein
00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 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     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_MATRIX4_H__
00021 #define __CS_MATRIX4_H__
00022 
00030 #include "csextern.h"
00031 #include "csgeom/math.h"
00032 #include "csgeom/transfrm.h"
00033 #include "csgeom/vector4.h"
00034 #include "csutil/csstring.h"
00035 
00036 namespace CS
00037 {
00038   namespace Math
00039   {
00043     class CS_CRYSTALSPACE_EXPORT Matrix4
00044     {
00045     public:
00046       float m11, m12, m13, m14;
00047       float m21, m22, m23, m24;
00048       float m31, m32, m33, m34;
00049       float m41, m42, m43, m44;
00050     
00051     public:
00053       Matrix4 ()
00054           : m11(1), m12(0), m13(0), m14(0),
00055             m21(0), m22(1), m23(0), m24(0),
00056             m31(0), m32(0), m33(1), m34(0),
00057             m41(0), m42(0), m43(0), m44(1)
00058       {}
00059     
00061       Matrix4 (float am11, float am12, float am13, float am14,
00062                float am21, float am22, float am23, float am24,
00063                float am31, float am32, float am33, float am34,
00064                float am41, float am42, float am43, float am44)
00065           : m11(am11), m12(am12), m13(am13), m14(am14),
00066             m21(am21), m22(am22), m23(am23), m24(am24),
00067             m31(am31), m32(am32), m33(am33), m34(am34),
00068             m41(am41), m42(am42), m43(am43), m44(am44)
00069       {}
00070     
00072       Matrix4 (Matrix4 const& o)
00073         : m11(o.m11), m12(o.m12), m13(o.m13), m14(o.m14),
00074           m21(o.m21), m22(o.m22), m23(o.m23), m24(o.m24),
00075           m31(o.m31), m32(o.m32), m33(o.m33), m34(o.m34),
00076           m41(o.m41), m42(o.m42), m43(o.m43), m44(o.m44)
00077       {
00078       }
00079       
00081       Matrix4 (csTransform const& o)
00082         : m11(o.GetO2T().m11), m12(o.GetO2T().m12), m13(o.GetO2T().m13),
00083           m21(o.GetO2T().m21), m22(o.GetO2T().m22), m23(o.GetO2T().m23),
00084           m31(o.GetO2T().m31), m32(o.GetO2T().m32), m33(o.GetO2T().m33), 
00085           m41(0), m42(0), m43(0), m44(1)
00086       {
00087         csVector3 o_t2o = -o.GetO2T()*o.GetO2TTranslation();
00088         m14 = o_t2o.x;
00089         m24 = o_t2o.y;
00090         m34 = o_t2o.z;
00091       }
00092     
00094       Matrix4 (csMatrix3 const& m)
00095         : m11(m.m11), m12(m.m12), m13(m.m13), m14 (0),
00096           m21(m.m21), m22(m.m22), m23(m.m23), m24 (0),
00097           m31(m.m31), m32(m.m32), m33(m.m33), m34 (0),
00098           m41(0), m42(0), m43(0), m44(1)
00099       {
00100       }
00101     
00103       csString Description() const;
00104         
00106           csTransform GetTransform() const;
00107       
00109       inline csVector4 Row1() const { return csVector4 (m11,m12,m13,m14); }
00110     
00112       inline csVector4 Row2() const { return csVector4 (m21,m22,m23,m24); }
00113     
00115       inline csVector4 Row3() const { return csVector4 (m31,m32,m33,m34); }
00116     
00118       inline csVector4 Row4() const { return csVector4 (m41,m42,m43,m44); }
00119     
00121       inline csVector4 Row(size_t n) const
00122       {
00123         switch (n)
00124         {
00125           case 0: return Row1();
00126           case 1: return Row2();
00127           case 2: return Row3();
00128           default:
00129           case 3: return Row4();
00130         }
00131       }
00132     
00134       inline csVector4 Col1() const { return csVector4 (m11,m21,m31,m41); }
00135     
00137       inline csVector4 Col2() const { return csVector4 (m12,m22,m32,m42); }
00138     
00140       inline csVector4 Col3() const { return csVector4 (m13,m23,m33,m43); }
00141     
00143       inline csVector4 Col4() const { return csVector4 (m14,m24,m34,m44); }
00144     
00146       inline csVector4 Col(size_t n) const
00147       {
00148         switch (n)
00149         {
00150           case 0: return Col1();
00151           case 1: return Col2();
00152           case 2: return Col3();
00153           default:
00154           case 3: return Col4();
00155         }
00156       }
00157     
00158       inline void Set (Matrix4 const &o)
00159       {
00160         m11 = o.m11; m12 = o.m12; m13 = o.m13; m14 = o.m14;
00161         m21 = o.m21; m22 = o.m22; m23 = o.m23; m24 = o.m24;
00162         m31 = o.m31; m32 = o.m32; m33 = o.m33; m34 = o.m34;
00163         m41 = o.m41; m42 = o.m42; m43 = o.m43; m44 = o.m44;
00164       }
00165     
00167       Matrix4& operator= (const Matrix4& o) { Set(o); return *this; }
00168     
00170       Matrix4& operator*= (float f)
00171       {
00172         m11 *= f; m12 *= f; m13 *= f; m14 *= f;
00173         m21 *= f; m22 *= f; m23 *= f; m24 *= f;
00174         m31 *= f; m32 *= f; m33 *= f; m34 *= f;
00175         m41 *= f; m42 *= f; m43 *= f; m44 *= f;
00176         return *this;
00177       }
00178     
00180       Matrix4& operator/= (float f) 
00181       {
00182         *this *= 1.0f/f;        
00183         return *this;
00184       }
00185     
00187       friend CS_CRYSTALSPACE_EXPORT Matrix4 operator* (const Matrix4& m1, 
00188         const Matrix4& m2);
00189       
00191       csVector4 operator* (const csVector4& v) const
00192       {
00193         return csVector4 (
00194           m11*v.x + m12*v.y + m13*v.z + m14*v.w,
00195           m21*v.x + m22*v.y + m23*v.z + m24*v.w,
00196           m31*v.x + m32*v.y + m33*v.z + m34*v.w,
00197           m41*v.x + m42*v.y + m43*v.z + m44*v.w);
00198       }
00199       
00201       float Determinant() const
00202       {
00203         return m14 * m23 * m32 * m41-m13 * m24 * m32 * m41-m14 * m22 * m33 * m41+m12 * m24 * m33 * m41
00204           + m13 * m22 * m34 * m41-m12 * m23 * m34 * m41-m14 * m23 * m31 * m42+m13 * m24 * m31 * m42
00205           + m14 * m21 * m33 * m42-m11 * m24 * m33 * m42-m13 * m21 * m34 * m42+m11 * m23 * m34 * m42
00206           + m14 * m22 * m31 * m43-m12 * m24 * m31 * m43-m14 * m21 * m32 * m43+m11 * m24 * m32 * m43
00207           + m12 * m21 * m34 * m43-m11 * m22 * m34 * m43-m13 * m22 * m31 * m44+m12 * m23 * m31 * m44
00208           + m13 * m21 * m32 * m44-m11 * m23 * m32 * m44-m12 * m21 * m33 * m44+m11 * m22 * m33 * m44;
00209       }
00210       
00212       Matrix4 GetInverse() const
00213       {
00214         Matrix4 m (
00215           m23*m34*m42 - m24*m33*m42 + m24*m32*m43 - m22*m34*m43 - m23*m32*m44 + m22*m33*m44,
00216           m14*m33*m42 - m13*m34*m42 - m14*m32*m43 + m12*m34*m43 + m13*m32*m44 - m12*m33*m44,
00217           m13*m24*m42 - m14*m23*m42 + m14*m22*m43 - m12*m24*m43 - m13*m22*m44 + m12*m23*m44,
00218           m14*m23*m32 - m13*m24*m32 - m14*m22*m33 + m12*m24*m33 + m13*m22*m34 - m12*m23*m34,
00219    
00220           m24*m33*m41 - m23*m34*m41 - m24*m31*m43 + m21*m34*m43 + m23*m31*m44 - m21*m33*m44,
00221           m13*m34*m41 - m14*m33*m41 + m14*m31*m43 - m11*m34*m43 - m13*m31*m44 + m11*m33*m44,
00222           m14*m23*m41 - m13*m24*m41 - m14*m21*m43 + m11*m24*m43 + m13*m21*m44 - m11*m23*m44,
00223           m13*m24*m31 - m14*m23*m31 + m14*m21*m33 - m11*m24*m33 - m13*m21*m34 + m11*m23*m34,
00224         
00225           m22*m34*m41 - m24*m32*m41 + m24*m31*m42 - m21*m34*m42 - m22*m31*m44 + m21*m32*m44,
00226           m14*m32*m41 - m12*m34*m41 - m14*m31*m42 + m11*m34*m42 + m12*m31*m44 - m11*m32*m44,
00227           m12*m24*m41 - m14*m22*m41 + m14*m21*m42 - m11*m24*m42 - m12*m21*m44 + m11*m22*m44,
00228           m14*m22*m31 - m12*m24*m31 - m14*m21*m32 + m11*m24*m32 + m12*m21*m34 - m11*m22*m34,
00229 
00230           m23*m32*m41 - m22*m33*m41 - m23*m31*m42 + m21*m33*m42 + m22*m31*m43 - m21*m32*m43,
00231           m12*m33*m41 - m13*m32*m41 + m13*m31*m42 - m11*m33*m42 - m12*m31*m43 + m11*m32*m43,
00232           m13*m22*m41 - m12*m23*m41 - m13*m21*m42 + m11*m23*m42 + m12*m21*m43 - m11*m22*m43,
00233           m12*m23*m31 - m13*m22*m31 + m13*m21*m32 - m11*m23*m32 - m12*m21*m33 + m11*m22*m33);
00234         m /= Determinant();
00235         return m;
00236       }
00237 
00239       void Invert() { *this = GetInverse(); }
00240     
00242       inline void Transpose ()
00243       {
00244         CS::Swap (m12, m21); CS::Swap (m13, m31); CS::Swap (m14, m41); 
00245         CS::Swap (m23, m32); CS::Swap (m24, m42); 
00246         CS::Swap (m34, m43); 
00247       }
00248     
00250       Matrix4 GetTranspose () const
00251       {
00252         return Matrix4 (
00253           m11, m21, m31, m41,
00254           m12, m22, m32, m42,
00255           m13, m23, m33, m43,
00256           m14, m24, m34, m44);
00257       }
00258     };
00259 
00260   } // namespace Math
00261 } // namespace CS
00262 
00265 #endif // __CS_MATRIX4_H__

Generated for Crystal Space 2.0 by doxygen 1.7.6.1