Back to index

libsfml  1.6+dfsg2
Matrix3.inl
Go to the documentation of this file.
00001 
00002 //
00003 // SFGE - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00025 
00029 inline Matrix3::Matrix3()
00030 {
00031     myData[0] = 1.f; myData[4] = 0.f; myData[8]  = 0.f; myData[12] = 0.f;
00032     myData[1] = 0.f; myData[5] = 1.f; myData[9]  = 0.f; myData[13] = 0.f;
00033     myData[2] = 0.f; myData[6] = 0.f; myData[10] = 1.f; myData[14] = 0.f;
00034     myData[3] = 0.f; myData[7] = 0.f; myData[11] = 0.f; myData[15] = 1.f;
00035 }
00036 
00037 
00041 inline Matrix3::Matrix3(float a00, float a01, float a02,
00042                         float a10, float a11, float a12,
00043                         float a20, float a21, float a22)
00044 {
00045     myData[0] = a00; myData[4] = a01; myData[8]  = 0.f; myData[12] = a02;
00046     myData[1] = a10; myData[5] = a11; myData[9]  = 0.f; myData[13] = a12;
00047     myData[2] = 0.f; myData[6] = 0.f; myData[10] = 1.f; myData[14] = 0.f;
00048     myData[3] = a20; myData[7] = a21; myData[11] = 0.f; myData[15] = a22;
00049 }
00050 
00051 
00055 inline void Matrix3::SetFromTransformations(const Vector2f& Center, const Vector2f& Translation, float Rotation, const Vector2f& Scale)
00056 {
00057     float Angle = Rotation * 3.141592654f / 180.f;
00058     float Cos   = static_cast<float>(cos(Angle));
00059     float Sin   = static_cast<float>(sin(Angle));
00060     float SxCos = Scale.x * Cos;
00061     float SyCos = Scale.y * Cos;
00062     float SxSin = Scale.x * Sin;
00063     float SySin = Scale.y * Sin;
00064     float Tx    = -Center.x * SxCos - Center.y * SySin + Translation.x;
00065     float Ty    =  Center.x * SxSin - Center.y * SyCos + Translation.y;
00066 
00067     myData[0] =  SxCos; myData[4] = SySin; myData[8]  = 0.f; myData[12] = Tx;
00068     myData[1] = -SxSin; myData[5] = SyCos; myData[9]  = 0.f; myData[13] = Ty;
00069     myData[2] =  0.f;   myData[6] = 0.f;   myData[10] = 1.f; myData[14] = 0.f;
00070     myData[3] =  0.f;   myData[7] = 0.f;   myData[11] = 0.f; myData[15] = 1.f;
00071 }
00072 
00073 
00077 inline Vector2f Matrix3::Transform(const Vector2f& Point) const
00078 {
00079     return Vector2f(myData[0] * Point.x + myData[4] * Point.y + myData[12],
00080                     myData[1] * Point.x + myData[5] * Point.y + myData[13]);
00081 }
00082 
00083 
00087 inline Matrix3 Matrix3::GetInverse() const
00088 {
00089     // Compute the determinant
00090     float Det = myData[0] * (myData[15] * myData[5] - myData[7] * myData[13]) -
00091                 myData[1] * (myData[15] * myData[4] - myData[7] * myData[12]) +
00092                 myData[3] * (myData[13] * myData[4] - myData[5] * myData[12]);
00093 
00094     // Compute the inverse if determinant is not zero
00095     if ((Det < -1E-7f) || (Det > 1E-7f))
00096     {
00097         return Matrix3( (myData[15] * myData[5] - myData[7] * myData[13]) / Det,
00098                        -(myData[15] * myData[4] - myData[7] * myData[12]) / Det,
00099                         (myData[13] * myData[4] - myData[5] * myData[12]) / Det,
00100                        -(myData[15] * myData[1] - myData[3] * myData[13]) / Det,
00101                         (myData[15] * myData[0] - myData[3] * myData[12]) / Det,
00102                        -(myData[13] * myData[0] - myData[1] * myData[12]) / Det,
00103                         (myData[7]  * myData[1] - myData[3] * myData[5])  / Det,
00104                        -(myData[7]  * myData[0] - myData[3] * myData[4])  / Det,
00105                         (myData[5]  * myData[0] - myData[1] * myData[4])  / Det);
00106     }
00107     else
00108     {
00109         return Identity;
00110     }
00111 }
00112 
00113 
00118 inline const float* Matrix3::Get4x4Elements() const
00119 {
00120     return myData;
00121 }
00122 
00123 
00127 inline float Matrix3::operator ()(unsigned int Row, unsigned int Col) const
00128 {
00129     switch (Row + Col * 3)
00130     {
00131         case 0 : return myData[0];
00132         case 1 : return myData[1];
00133         case 2 : return myData[3];
00134         case 3 : return myData[4];
00135         case 4 : return myData[5];
00136         case 5 : return myData[7];
00137         case 6 : return myData[12];
00138         case 7 : return myData[13];
00139         case 8 : return myData[15];
00140 
00141         default : return myData[0];
00142     }
00143 }
00144 inline float& Matrix3::operator ()(unsigned int Row, unsigned int Col)
00145 {
00146     switch (Row + Col * 3)
00147     {
00148         case 0 : return myData[0];
00149         case 1 : return myData[1];
00150         case 2 : return myData[3];
00151         case 3 : return myData[4];
00152         case 4 : return myData[5];
00153         case 5 : return myData[7];
00154         case 6 : return myData[12];
00155         case 7 : return myData[13];
00156         case 8 : return myData[15];
00157 
00158         default : return myData[0];
00159     }
00160 }
00161 
00162 
00166 inline Matrix3 Matrix3::operator *(const Matrix3& Mat) const
00167 {
00168     return Matrix3(myData[0] * Mat.myData[0]  + myData[4] * Mat.myData[1]  + myData[12] * Mat.myData[3],
00169                    myData[0] * Mat.myData[4]  + myData[4] * Mat.myData[5]  + myData[12] * Mat.myData[7],
00170                    myData[0] * Mat.myData[12] + myData[4] * Mat.myData[13] + myData[12] * Mat.myData[15],
00171                    myData[1] * Mat.myData[0]  + myData[5] * Mat.myData[1]  + myData[13] * Mat.myData[3],
00172                    myData[1] * Mat.myData[4]  + myData[5] * Mat.myData[5]  + myData[13] * Mat.myData[7],
00173                    myData[1] * Mat.myData[12] + myData[5] * Mat.myData[13] + myData[13] * Mat.myData[15],
00174                    myData[3] * Mat.myData[0]  + myData[7] * Mat.myData[1]  + myData[15] * Mat.myData[3],
00175                    myData[3] * Mat.myData[4]  + myData[7] * Mat.myData[5]  + myData[15] * Mat.myData[7],
00176                    myData[3] * Mat.myData[12] + myData[7] * Mat.myData[13] + myData[15] * Mat.myData[15]);
00177 }
00178 
00179 
00183 inline Matrix3& Matrix3::operator *=(const Matrix3& Mat)
00184 {
00185     return *this = *this * Mat;
00186 }