Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_DIMENSION_MATRIX2_H__
00009 #define __SP_DIMENSION_MATRIX2_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spMath.hpp"
00014
00015 #include <string.h>
00016
00017
00018 namespace sp
00019 {
00020 namespace dim
00021 {
00022
00023
00024 template <typename T> class matrix2
00025 {
00026
00027 public:
00028
00029
00030
00031 matrix2()
00032 {
00033 reset();
00034 }
00035 matrix2(const T (&other)[4])
00036 {
00037 *this = other;
00038 }
00039 matrix2(const matrix2<T> &other)
00040 {
00041 *this = other;
00042 }
00043 matrix2(
00044 T m1n1, T m2n1,
00045 T m1n2, T m2n2)
00046 {
00047 M[0] = m1n1; M[2] = m2n1;
00048 M[1] = m1n2; M[3] = m2n2;
00049 }
00050 ~matrix2()
00051 {
00052 }
00053
00054
00055
00056 inline const T operator () (u32 row, u32 col) const
00057 {
00058 return row < 2 && col < 2 ? M[(row << 1) + col] : (T)0;
00059 }
00060 inline T& operator () (u32 row, u32 col)
00061 {
00062 return M[(row << 1) + col];
00063 }
00064
00065 inline const T operator [] (u32 i) const
00066 {
00067 return i < 4 ? M[i] : (T)0;
00068 }
00069 inline T& operator [] (u32 i)
00070 {
00071 return M[i];
00072 }
00073
00074 inline bool operator == (const matrix2<T> &other)
00075 {
00076 for (s32 i = 0; i < 4; ++i)
00077 {
00078 if (M[i] != other.M[i])
00079 return false;
00080 }
00081 return true;
00082 }
00083 inline bool operator != (const matrix2<T> &other)
00084 {
00085 for (s32 i = 0; i < 4; ++i)
00086 {
00087 if (M[i] != other.M[i])
00088 return true;
00089 }
00090 return false;
00091 }
00092
00093 inline matrix2<T>& operator = (const T (&other)[4])
00094 {
00095 M[0] = other[0]; M[2] = other[2];
00096 M[1] = other[1]; M[3] = other[3];
00097 return *this;
00098 }
00099 inline matrix2<T>& operator = (const matrix2<T> &other)
00100 {
00101 M[0] = other[0]; M[2] = other[2];
00102 M[1] = other[1]; M[3] = other[3];
00103 return *this;
00104 }
00105 inline matrix2<T>& operator = (T Scalar)
00106 {
00107 memset(M, Scalar, sizeof(M));
00108 return *this;
00109 }
00110
00111 inline matrix2<T> operator + (const matrix2<T> &mltMatrix) const
00112 {
00113 matrix2<T> other;
00114
00115 other[0] = M[0] + mltMatrix[0]; other[2] = M[2] + mltMatrix[2];
00116 other[1] = M[1] + mltMatrix[1]; other[3] = M[3] + mltMatrix[3];
00117
00118 return other;
00119 }
00120
00121 inline matrix2<T>& operator += (const matrix2<T> &mltMatrix)
00122 {
00123 M[0] += mltMatrix[0]; M[2] += mltMatrix[2];
00124 M[1] += mltMatrix[1]; M[3] += mltMatrix[3];
00125 return *this;
00126 }
00127
00128 inline matrix2<T> operator - (const matrix2<T> &mltMatrix) const
00129 {
00130 matrix2<T> other;
00131 other[0] = M[0] - mltMatrix[0]; other[2] = M[2] - mltMatrix[2];
00132 other[1] = M[1] - mltMatrix[1]; other[3] = M[3] - mltMatrix[3];
00133 return other;
00134 }
00135 inline matrix2<T>& operator -= (const matrix2<T> &mltMatrix)
00136 {
00137 M[0] -= mltMatrix[0]; M[2] -= mltMatrix[2];
00138 M[1] -= mltMatrix[1]; M[3] -= mltMatrix[3];
00139 return *this;
00140 }
00141
00142 inline matrix2<T> operator * (const matrix2<T> &mltMatrix) const
00143 {
00144 matrix2<T> m3;
00145 const T* m1 = M;
00146 const T* m2 = mltMatrix.M;
00147
00148 m3[0] = m1[0]*m2[0] + m1[2]*m2[1];
00149 m3[1] = m1[1]*m2[0] + m1[3]*m2[1];
00150
00151 m3[2] = m1[0]*m2[2] + m1[2]*m2[3];
00152 m3[3] = m1[1]*m2[2] + m1[3]*m2[3];
00153
00154 return m3;
00155 }
00156
00157 inline matrix2<T> operator * (T Scalar) const
00158 {
00159 matrix2<T> other;
00160
00161 other[0] = M[0]*Scalar; other[2] = M[2]*Scalar;
00162 other[1] = M[1]*Scalar; other[3] = M[3]*Scalar;
00163
00164 return other;
00165 }
00166
00167 inline matrix2<T>& operator *= (const matrix2<T> &mltMatrix)
00168 {
00169 T m1[4];
00170 memcpy(m1, M, sizeof(M));
00171 const T* m2 = mltMatrix.M;
00172
00173 M[0] = m1[0]*m2[0] + m1[2]*m2[1];
00174 M[1] = m1[1]*m2[0] + m1[3]*m2[1];
00175
00176 M[2] = m1[0]*m2[2] + m1[2]*m2[3];
00177 M[3] = m1[1]*m2[2] + m1[3]*m2[3];
00178
00179 return *this;
00180 }
00181
00182 inline matrix2<T>& operator *= (T Scalar)
00183 {
00184 M[0] *= Scalar; M[2] *= Scalar;
00185 M[1] *= Scalar; M[3] *= Scalar;
00186 return *this;
00187 }
00188
00189 inline point2d<T> operator * (const point2d<T> &Vector) const
00190 {
00191 return point2d<T>(
00192 Vector.X*M[0] + Vector.Y*M[2],
00193 Vector.X*M[1] + Vector.Y*M[3]
00194 );
00195 }
00196
00197 inline void clear()
00198 {
00199 memset(M, 0, sizeof(M));
00200 }
00201
00202
00203
00204
00205
00206 inline matrix2<T>& reset()
00207 {
00208 M[0] = 1; M[2] = 0;
00209 M[1] = 0; M[3] = 1;
00210 return *this;
00211 }
00212
00213 inline void multiplySingleMatrix(const T (&Other)[2])
00214 {
00215 T Result[2];
00216
00217 Result[0] = M[0]*Other[0] + M[2]*Other[1];
00218 Result[1] = M[1]*Other[0] + M[3]*Other[1];
00219
00220 Other[0] = Result[0];
00221 Other[1] = Result[1];
00222 }
00223
00224 inline bool getInverse(matrix2<T> &InverseMat) const
00225 {
00226 T d = M[0]*M[3] - M[2]*M[1];
00227
00228 if (d == T(0))
00229 return false;
00230
00231 d = T(1) / d;
00232
00233 InverseMat[0] = d * ( M[3] );
00234 InverseMat[1] = d * ( -M[1] );
00235 InverseMat[2] = d * ( -M[2] );
00236 InverseMat[3] = d * ( M[0] );
00237
00238 return true;
00239 }
00240
00241 inline bool setInverse()
00242 {
00243 matrix2<T> Matrix;
00244
00245 if (getInverse(Matrix))
00246 {
00247 *this = Matrix;
00248 return true;
00249 }
00250
00251 return false;
00252 }
00253
00254 inline matrix2<T> getInverse() const
00255 {
00256 matrix2<T> Mat;
00257 getInverse(Mat);
00258 return Mat;
00259 }
00260
00261
00262
00263
00264
00265 inline matrix2<T>& scale(const point2d<T> &Vector)
00266 {
00267 matrix2<T> other;
00268
00269 other[0] = Vector.X;
00270 other[3] = Vector.Y;
00271
00272 return *this *= other;
00273 }
00274
00275
00276
00277
00278
00279 inline matrix2<T>& rotate(const T Angle)
00280 {
00281 matrix2<T> other;
00282 other.setRotation(Angle);
00283 return *this *= other;
00284 }
00285
00286 inline void setRotation(T Rotation, bool UseDegrees = true)
00287 {
00288 if (UseDegrees)
00289 Rotation = Rotation * static_cast<T>(M_PI) / T(180);
00290
00291 const T s = sin(Rotation);
00292 const T c = cos(Rotation);
00293
00294 M[0] = c; M[2] = s;
00295 M[1] = -s; M[3] = c;
00296 }
00297
00298
00299
00300 inline point2d<T> getRow(s32 Position) const
00301 {
00302 switch (Position) {
00303 case 0:
00304 return point2d<T>(M[0], M[2]);
00305 case 1:
00306 return point2d<T>(M[1], M[3]);
00307 }
00308 return point2d<T>();
00309 }
00310
00311 inline point2d<T> getColumn(s32 Position) const
00312 {
00313 switch (Position) {
00314 case 0:
00315 return point2d<T>(M[0], M[1]);
00316 case 1:
00317 return point2d<T>(M[2], M[3]);
00318 }
00319 return point2d<T>();
00320 }
00321
00322 inline void setScale(const point2d<T> &Scale)
00323 {
00324 M[0] = Scale.X; M[2] = Scale.Y;
00325 }
00326 inline point2d<T> getScale() const
00327 {
00328 if (math::Equal(M[1], 0.0f) && math::Equal(M[3], 0.0f))
00329 return point2d<T>(M[0], M[3]);
00330
00331 return point2d<T>(
00332 sqrtf(M[0]*M[0] + M[1]*M[1]),
00333 sqrtf(M[2]*M[2] + M[3]*M[3])
00334 );
00335 }
00336
00337 inline T getRotation() const
00338 {
00339 return 0.0f;
00340 }
00341
00342 inline matrix4<T> getTransposed() const
00343 {
00344 matrix4<T> Mat;
00345 getTransposed(Mat);
00346 return Mat;
00347 }
00348
00349 inline void getTransposed(matrix4<T> &other) const
00350 {
00351 other[0] = M[0]; other[2] = M[1];
00352 other[1] = M[2]; other[3] = M[3];
00353 }
00354
00355 inline point2d<T> interpolate(const point2d<T> &other, f32 seek) const
00356 {
00357 point2d<T> Mat;
00358
00359 for (s32 i = 0; i < 4; ++i)
00360 Mat.M[i] = M[i] + (other.M[i] - M[i]) * seek;
00361
00362 return Mat;
00363 }
00364
00365 inline bool isIdentity() const
00366 {
00367 return
00368 math::Equal(M[0], 1.0f) && math::Equal(M[1], 0.0f) &&
00369 math::Equal(M[2], 1.0f) && math::Equal(M[3], 0.0f);
00370 }
00371
00372 inline const T* getArray() const
00373 {
00374 return M;
00375 }
00376 inline T* getArray()
00377 {
00378 return M;
00379 }
00380
00381 template <typename B> inline matrix2<B> cast() const
00382 {
00383 B other[4];
00384
00385 for (s32 i = 0; i < 4; ++i)
00386 other[i] = static_cast<B>(M[i]);
00387
00388 return matrix2<B>(other);
00389 }
00390
00391 private:
00392
00393
00394
00395
00396
00397
00398
00399 T M[4];
00400
00401 };
00402
00403 typedef matrix2<s32> matrix2i;
00404 typedef matrix2<f32> matrix2f;
00405 typedef matrix2<f64> matrix2d;
00406
00407
00408 }
00409
00410 }
00411
00412
00413 #endif
00414
00415
00416
00417