CrystalSpace

Public API Reference

csgeom/matrix2.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 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_MATRIX2_H__
00021 #define __CS_MATRIX2_H__
00022 
00023 #include "csgeom/vector2.h"
00024 
00035 class csMatrix2
00036 {
00037 public:
00038   float m11, m12;
00039   float m21, m22;
00040 
00041 public:
00043   csMatrix2 ()
00044     : m11 (1), m12 (0), m21 (0), m22 (1)
00045   {}
00046 
00048   csMatrix2 (float m11, float m12,
00049              float m21, float m22)
00050     : m11 (m11), m12 (m12), m21 (m21), m22 (m22)
00051   {}
00052 
00053 
00055   inline csVector2 Row1() const 
00056   { return csVector2 (m11,m12); }
00057 
00059   inline csVector2 Row2() const 
00060   { return csVector2 (m21,m22); }
00061 
00063   inline csVector2 Col1() const 
00064   { return csVector2 (m11,m21); }
00065 
00067   inline csVector2 Col2() const 
00068   { return csVector2 (m12,m22); }
00069 
00071   inline void Set (float m11, float m12,
00072                    float m21, float m22)
00073   {
00074     csMatrix2::m11 = m11; csMatrix2::m12 = m12;
00075     csMatrix2::m21 = m21; csMatrix2::m22 = m22;
00076   }
00077 
00079   inline csMatrix2& operator+= (const csMatrix2& m)
00080   {
00081     m11 += m.m11; m12 += m.m12;
00082     m21 += m.m21; m22 += m.m22;
00083     return *this;
00084   }
00085 
00087   inline csMatrix2& operator-= (const csMatrix2& m)
00088   {
00089     m11 -= m.m11; m12 -= m.m12;
00090     m21 -= m.m21; m22 -= m.m22;
00091     return *this;
00092   }
00093 
00095   inline csMatrix2& operator*= (const csMatrix2& m)
00096   {
00097     csMatrix2 r (*this);
00098     m11 = r.m11 * m.m11 + r.m12 * m.m21;
00099     m12 = r.m11 * m.m12 + r.m12 * m.m22;
00100     m21 = r.m21 * m.m11 + r.m22 * m.m21;
00101     m22 = r.m21 * m.m12 + r.m22 * m.m22;
00102     return *this;
00103   }
00104 
00106   inline csMatrix2& operator*= (float s)
00107   {
00108     m11 *= s; m12 *= s;
00109     m21 *= s; m22 *= s;
00110     return *this;
00111   }
00112 
00114   inline csMatrix2& operator/= (float s)
00115   {
00116     s=1.0f/s;
00117     m11 *= s; m12 *= s;
00118     m21 *= s; m22 *= s;
00119     return *this;
00120   }
00121 
00123   inline csMatrix2 operator+ () const 
00124   { return *this; }
00125 
00127   inline csMatrix2 operator- () const
00128   {
00129     return csMatrix2 (-m11,-m12, -m21,-m22);
00130   }
00131 
00133   inline void Transpose ()
00134   {
00135     float swap = m12;
00136     m12 = m21;
00137     m21 = swap;
00138   }
00139 
00141   inline csMatrix2 GetTranspose () const
00142   {
00143     return csMatrix2 (m11, m21, m12, m22);
00144   }
00145 
00147   inline csMatrix2 GetInverse () const
00148   {
00149     float inv_det = 1 / (m11 * m22 - m12 * m21);
00150     return csMatrix2 (m22 * inv_det, -m12 * inv_det,
00151                       -m21 * inv_det, m11 * inv_det);
00152   }
00153 
00155   inline void Invert () 
00156   { *this = GetInverse (); }
00157 
00159   inline float Determinant () const
00160   { return m11 * m22 - m12 * m21; }
00161 
00163   inline void Identity ()
00164   {
00165     m11 = m22 = 1;
00166     m12 = m21 = 0;
00167   }
00168 
00170   inline friend csMatrix2 operator+ (const csMatrix2& m1, const csMatrix2& m2)
00171   {
00172     return csMatrix2 (
00173       m1.m11 + m2.m11, m1.m12 + m2.m12,
00174       m1.m21 + m2.m21, m1.m22 + m2.m22);
00175   }
00176 
00178   inline friend csMatrix2 operator- (const csMatrix2& m1, const csMatrix2& m2)
00179   {
00180     return csMatrix2 (
00181       m1.m11 - m2.m11, m1.m12 - m2.m12,
00182       m1.m21 - m2.m21, m1.m22 - m2.m22);
00183   }
00184 
00186   inline friend csMatrix2 operator* (const csMatrix2& m1, const csMatrix2& m2)
00187   {
00188     return csMatrix2 (
00189       m1.m11 * m2.m11 + m1.m12 * m2.m21,
00190       m1.m11 * m2.m12 + m1.m12 * m2.m22,
00191       m1.m21 * m2.m11 + m1.m22 * m2.m21,
00192       m1.m21 * m2.m12 + m1.m22 * m2.m22);
00193   }
00194 
00196   inline friend csVector2 operator* (const csMatrix2& m, const csVector2& v)
00197   { return csVector2 (m.m11*v.x + m.m12*v.y, m.m21*v.x + m.m22*v.y); }
00198 
00200   inline friend csMatrix2 operator* (const csMatrix2& m, float f)
00201   { return csMatrix2 (m.m11 * f, m.m12 * f, m.m21 * f, m.m22 * f); }
00202 
00204   inline friend csMatrix2 operator* (float f, const csMatrix2& m)
00205   { return csMatrix2 (m.m11 * f, m.m12 * f, m.m21 * f, m.m22 * f); }
00206 
00208   inline friend csMatrix2 operator/ (const csMatrix2& m, float f)
00209   {
00210     float inv_f = 1 / f;
00211     return csMatrix2 (
00212       m.m11 * inv_f, m.m12 * inv_f,
00213       m.m21 * inv_f, m.m22 * inv_f);
00214   }
00215 };
00216 
00219 #endif // __CS_MATRIX2_H__
00220 

Generated for Crystal Space 2.0 by doxygen 1.7.6.1