00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _MATRIX_
00019 #define _MATRIX_
00020
00021 #include "util/Vector2.h"
00022
00023 class Matrix
00024 {
00025 public:
00026 float _00, _10, _20;
00027 float _01, _11, _21;
00028 float _02, _12, _22;
00029
00030 Matrix()
00031 :_00(1.0f), _10(0.0f), _20(0.0f), _01(0.0f), _11(1.0f), _21(0.0f),
00032 _02(0.0f), _12(0.0f), _22(1.0f)
00033 {
00034 }
00035
00036 Matrix(const float m00, const float m10, const float m20,
00037 const float m01, const float m11, const float m21,
00038 const float m02, const float m12, const float m22)
00039 :_00(m00), _10(m10), _20(m20), _01(m01), _11(m11), _21(m21), _02(m02), _12(m12), _22(m22)
00040 {
00041 }
00042
00043 Matrix(const Matrix & other)
00044 :_00(other._00), _10(other._10), _20(other._20), _01(other._01), _11(other._11), _21(other._21),
00045 _02(other._02), _12(other._12), _22(other._22)
00046 {
00047 }
00048
00049 Matrix operator * (const Matrix& other) const;
00050 Matrix & operator *= (const Matrix& other);
00051
00052 Matrix GetInverse() const;
00053 Matrix & Inverse();
00054 Matrix & Identity();
00055
00056 Matrix & Rotation(const float fRotation);
00057 Matrix & Translation(const float fTranslationX, const float fTranslationY);
00058 Matrix & Scaling(const float fScaleX, const float fScaleY);
00059
00060 float GetDeterminant() const;
00061 };
00062
00063 inline Matrix Matrix::operator * (const Matrix& other) const
00064 {
00065 Matrix result;
00066
00067 result._00 = _00 * other._00 + _10 * other._01 + _20 * other._02;
00068 result._10 = _00 * other._10 + _10 * other._11 + _20 * other._12;
00069 result._20 = _00 * other._20 + _10 * other._21 + _20 * other._22;
00070
00071 result._01 = _01 * other._00 + _11 * other._01 + _21 * other._02;
00072 result._11 = _01 * other._10 + _11 * other._11 + _21 * other._12;
00073 result._21 = _01 * other._20 + _11 * other._21 + _21 * other._22;
00074
00075 result._02 = _02 * other._00 + _12 * other._01 + _22 * other._02;
00076 result._12 = _02 * other._10 + _12 * other._11 + _22 * other._12;
00077 result._22 = _02 * other._20 + _12 * other._21 + _22 * other._22;
00078
00079 return result;
00080 }
00081
00082 inline Matrix & Matrix::operator *= (const Matrix& other)
00083 {
00084 *this = *this * other;
00085
00086 return *this;
00087 }
00088
00089 inline Matrix Matrix::GetInverse() const
00090 {
00091 float fDeterminant = GetDeterminant();
00092
00093 if(fDeterminant != 0)
00094 {
00095 float fInvDeterminant = 1 / fDeterminant;
00096 Matrix temp;
00097
00098 temp._00 = ((_11 * _22) - (_21 * _12)) * fInvDeterminant;
00099 temp._10 = ((_20 * _12) - (_10 * _22)) * fInvDeterminant;
00100 temp._20 = ((_20 * _21) - (_20 * _11)) * fInvDeterminant;
00101
00102 temp._01 = ((_21 * _02) - (_01 * _22)) * fInvDeterminant;
00103 temp._11 = ((_00 * _22) - (_20 * _02)) * fInvDeterminant;
00104 temp._21 = ((_20 * _01) - (_00 * _21)) * fInvDeterminant;
00105
00106 temp._02 = ((_01 * _12) - (_11 * _02)) * fInvDeterminant;
00107 temp._12 = ((_10 * _02) - (_00 * _12)) * fInvDeterminant;
00108 temp._22 = ((_00 * _11) - (_10 * _01)) * fInvDeterminant;
00109
00110 return temp;
00111 }
00112
00113 return Matrix();
00114 }
00115
00116 inline Matrix & Matrix::Inverse()
00117 {
00118 *this = GetInverse();
00119
00120 return *this;
00121 }
00122
00123 inline Matrix & Matrix::Identity()
00124 {
00125 memset(this, 0, sizeof(float) * 9);
00126
00127 _00 = _11 = _22 = 1.0f;
00128
00129 return *this;
00130 }
00131
00132 inline Matrix & Matrix::Rotation(const float fRotation)
00133 {
00134 float fCos = cosf(fRotation);
00135 float fSin = sinf(fRotation);
00136
00137 _00 = fCos;
00138 _10 = -fSin;
00139 _01 = fSin;
00140 _11 = fCos;
00141
00142 return *this;
00143 }
00144
00145 inline Matrix & Matrix::Translation(const float fTranslationX, const float fTranslationY)
00146 {
00147 _02 = fTranslationX;
00148 _12 = fTranslationY;
00149
00150 return *this;
00151 }
00152
00153 inline Matrix & Matrix::Scaling(const float fScaleX, const float fScaleY)
00154 {
00155 _00 = fScaleX;
00156 _11 = fScaleY;
00157
00158 return *this;
00159 }
00160
00161 inline float Matrix::GetDeterminant() const
00162 {
00163 float det1 = 0;
00164 float det2 = 0;
00165 float det3 = 0;
00166 float det4 = 0;
00167 float det5 = 0;
00168 float det6 = 0;
00169
00170 det1 = _00 * _11 * _22;
00171 det2 = _00 * _12 * _02;
00172 det3 = _10 * _21 * _02;
00173 det4 = _10 * _01 * _22;
00174 det5 = _20 * _11 * _02;
00175 det6 = _20 * _01 * _12;
00176
00177 return det1 - det2 + det3 - det4 + det5 - det6;
00178 }
00179
00180 inline Vector2 operator * (const Vector2 & v, const Matrix & m)
00181 {
00182 Vector2 temp;
00183
00184 temp._x = v._x * m._00 + v._y * m._01 + m._02;
00185 temp._y = v._x * m._10 + v._y * m._11 + m._12;
00186 float z = v._x * m._20 + v._y * m._21 + m._22;
00187
00188 if(z != 0)
00189 {
00190 temp._x /= z;
00191 temp._y /= z;
00192 }
00193
00194 return temp;
00195 }
00196
00197 inline Vector2 & operator *= (Vector2 & v, const Matrix & m)
00198 {
00199 Vector2 temp = v * m;
00200
00201 v = temp;
00202
00203 return v;
00204 }
00205
00206
00207 #endif