00001 #ifndef __SSMATRIX_HPP
00002 #define __SSMATRIX_HPP
00003
00004 #if !defined (__SSVECTOR_HPP)
00005 # include "ssVector.hpp"
00006 #endif
00007
00008 namespace SS
00009 {
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 class Matrix4x4;
00020 class MatrixFactory
00021 {
00022 public:
00023 static Matrix4x4 rotateX (float angle);
00024 static Matrix4x4 rotateY (float angle);
00025 static Matrix4x4 rotateZ (float angle);
00026 };
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class Matrix3x4
00037 {
00038 private:
00039 float m[3][4];
00040 public:
00041 enum Empty
00042 {
00043 NO_INIT
00044 };
00045
00046 inline const Vector4& operator[] (int i) const { SS_ASSERT (i>=0 && i < 3); return (const Vector4&)(m[i][0]); }
00047 inline Vector4& operator[] (int i) { SS_ASSERT (i>=0 && i < 3); return (Vector4&)(m[i][0]); }
00048
00049 inline Matrix3x4 (void) { ident(); }
00050 inline Matrix3x4 (Empty) { }
00051 inline Matrix3x4 (const Matrix3x4& n) { *this=n; }
00052 inline Matrix3x4 (float e00, float e01, float e02, float e03,
00053 float e10, float e11, float e12, float e13,
00054 float e20, float e21, float e22, float e23) { m[0][0] = e00; m[0][1] = e01; m[0][2] = e02; m[0][3] = e03; m[1][0] = e10; m[1][1] = e11; m[1][2] = e12; m[1][3] = e13; m[2][0] = e20; m[2][1] = e21; m[2][2] = e22; m[2][3] = e23; }
00055 inline void operator= (const Matrix3x4&);
00056
00057 void clear (void);
00058 void flushToZero (void);
00059 void ident (void);
00060
00061 bool invert (void);
00062
00063 inline Vector3 getColumn (int i) const { SS_ASSERT (i>=0 && i < 4); return Vector3(m[0][i], m[1][i], m[2][i]);}
00064 inline void setColumn (int i, const Vector3& v) { SS_ASSERT (i>=0 && i < 4); m[0][i] = v.x; m[1][i] = v.y; m[2][i] = v.z; }
00065
00066 bool operator== (const Matrix3x4& n) const;
00067 inline bool operator!= (const Matrix3x4& n) const { return !(*this == n); }
00068
00069 void operator*= (const Matrix3x4& n);
00070 void operator*= (float f);
00071 inline void scale (const Vector3& v);
00072 };
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 class Matrix4x4
00084 {
00085 private:
00086 float m[4][4];
00087 public:
00088 enum Empty
00089 {
00090 NO_INIT
00091 };
00092
00093 inline const Vector4& operator[] (int i) const { SS_ASSERT (i>=0 && i < 4); return (const Vector4&)(m[i][0]); }
00094 inline Vector4& operator[] (int i) { SS_ASSERT (i>=0 && i < 4); return (Vector4&)(m[i][0]); }
00095
00096
00097 inline Matrix4x4 (void) { ident(); }
00098 inline Matrix4x4 (Empty) { }
00099 inline Matrix4x4 (const Matrix4x4& n) { *this=n; }
00100 inline Matrix4x4 (float e00, float e01, float e02, float e03,
00101 float e10, float e11, float e12, float e13,
00102 float e20, float e21, float e22, float e23,
00103 float e30, float e31, float e32, float e33) { m[0][0] = e00; m[0][1] = e01; m[0][2] = e02; m[0][3] = e03; m[1][0] = e10; m[1][1] = e11; m[1][2] = e12; m[1][3] = e13; m[2][0] = e20; m[2][1] = e21; m[2][2] = e22; m[2][3] = e23; m[3][0] = e30; m[3][1] = e31; m[3][2] = e32; m[3][3] = e33; }
00104 inline void operator= (const Matrix4x4&);
00105
00106 inline bool is3x4Matrix (void) const { return m[3][0] == 0.0f && m[3][1] == 0.0f && m[3][2] == 0.0f && m[3][3] == 1.0f; }
00107 inline const Matrix3x4& get3x4Matrix (void) const { return reinterpret_cast<const Matrix3x4&>(*this); }
00108
00109 void clear (void);
00110 void flushToZero (void);
00111 void ident (void);
00112
00113 bool invert (void);
00114
00115 inline Vector4 getColumn (int i) const { SS_ASSERT (i>=0 && i < 4); return Vector4(m[0][i], m[1][i], m[2][i],m[3][i]); }
00116 inline void setColumn (int i, const Vector4& v) { SS_ASSERT (i>=0 && i < 4); m[0][i] = v.x; m[1][i] = v.y; m[2][i] = v.z; m[3][i] = v.w; }
00117
00118 bool operator== (const Matrix4x4& n) const;
00119 inline bool operator!= (const Matrix4x4& n) const { return !(*this == n); }
00120
00121 void operator*= (const Matrix4x4& n);
00122 void operator*= (float f);
00123 void transpose (void);
00124 void transpose (const Matrix4x4& src);
00125 float det (void) const;
00126
00127 private:
00128 float det3x3 (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3) const;
00129 float det2x2 (float a, float b, float c, float d) const;
00130 };
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 class Matrix3x3
00142 {
00143 private:
00144 float m[3][3];
00145 public:
00146 enum Empty
00147 {
00148 NO_INIT
00149 };
00150
00151 inline const Vector3& operator[] (int i) const { SS_ASSERT (i>=0 && i < 3); return (const Vector3&)(m[i][0]); }
00152 inline Vector3& operator[] (int i) { SS_ASSERT (i>=0 && i < 3); return (Vector3&)(m[i][0]); }
00153
00154
00155 inline Matrix3x3 (void) { ident(); }
00156 inline Matrix3x3 (Empty) { }
00157 inline Matrix3x3 (float e00, float e01, float e02,
00158 float e10, float e11, float e12,
00159 float e20, float e21, float e22) { m[0][0] = e00; m[0][1] = e01; m[0][2] = e02; m[1][0] = e10; m[1][1] = e11; m[1][2] = e12; m[2][0] = e20; m[2][1] = e21; m[2][2] = e22; }
00160 inline void operator= (const Matrix3x3&);
00161
00162 void clear (void);
00163 void flushToZero (void);
00164 void ident (void);
00165
00166 bool invert (void);
00167
00168 inline Vector3 getColumn (int i) const { SS_ASSERT (i>=0 && i < 3); return Vector3(m[0][i], m[1][i], m[2][i]); }
00169 inline void setColumn (int i, const Vector3& v) { SS_ASSERT (i>=0 && i < 3); m[0][i] = v.x; m[1][i] = v.y; m[2][i] = v.z; }
00170
00171 bool operator== (const Matrix3x3& n) const;
00172 inline bool operator!= (const Matrix3x3& n) const { return !(*this == n); }
00173
00174 void operator*= (const Matrix3x3& n);
00175 void operator*= (float f);
00176 void transpose (void);
00177 void transpose (const Matrix3x3& src);
00178 float det (void);
00179 };
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 class Matrix2x2
00191 {
00192 private:
00193 float m[2][2];
00194 public:
00195 enum Empty
00196 {
00197 NO_INIT
00198 };
00199
00200 inline const Vector2& operator[] (int i) const { SS_ASSERT (i>=0 && i < 2); return (const Vector2&)(m[i][0]); }
00201 inline Vector2& operator[] (int i) { SS_ASSERT (i>=0 && i < 2); return (Vector2&)(m[i][0]); }
00202
00203
00204 inline Matrix2x2 (void) { ident(); }
00205 inline Matrix2x2 (Empty) { }
00206 inline Matrix2x2 (float e00, float e01,
00207 float e10, float e11) { m[0][0] = e00; m[0][1] = e01; m[1][0] = e10; m[1][1] = e11; }
00208 inline void operator= (const Matrix2x2&);
00209
00210 void clear (void);
00211 void flushToZero (void);
00212 void ident (void);
00213
00214 bool invert (void);
00215
00216 inline Vector2 getColumn (int i) const { SS_ASSERT (i>=0 && i < 2); return Vector2(m[0][i], m[1][i]); }
00217 inline void setColumn (int i, const Vector2& v) { SS_ASSERT (i>=0 && i < 2); m[0][i] = v.x; m[1][i] = v.y; }
00218
00219 bool operator== (const Matrix2x2& n) const;
00220 inline bool operator!= (const Matrix2x2& n) const { return !(*this == n); }
00221
00222 void operator*= (const Matrix2x2& n);
00223 void operator*= (float f);
00224 void transpose (void);
00225 void transpose (const Matrix2x2& src);
00226 float det (void);
00227 };
00228
00229 Matrix4x4 operator* (const Matrix4x4& m1, const Matrix4x4& m2);
00230 Matrix3x3 operator* (const Matrix3x3& m1, const Matrix3x3& m2);
00231 Matrix2x2 operator* (const Matrix2x2& m1, const Matrix2x2& m2);
00232
00233
00234
00235
00236
00237 inline void Matrix3x4::operator= (const Matrix3x4& ss)
00238 {
00239 float* d = &(*this)[0][0];
00240 const float* s = &ss[0][0];
00241 for (int i = 0 ; i < 4*3; i++)
00242 d[i] = s[i];
00243 }
00244
00245
00246
00247
00248
00249 inline Vector4& Vector4::operator*= (const Matrix4x4& m)
00250 {
00251 Vector4 v = *this;
00252 x = m[0][0]*v.x + m[1][0]*v.y + m[2][0]*v.z + m[3][0]*v.w;
00253 y = m[0][1]*v.x + m[1][1]*v.y + m[2][1]*v.z + m[3][1]*v.w;
00254 z = m[0][2]*v.x + m[1][2]*v.y + m[2][2]*v.z + m[3][2]*v.w;
00255 w = m[0][3]*v.x + m[1][3]*v.y + m[2][3]*v.z + m[3][3]*v.w;
00256 return *this;
00257 }
00258
00259 inline void Matrix4x4::operator= (const Matrix4x4& ss)
00260 {
00261 float* d = &(*this)[0][0];
00262 const float* s = &ss[0][0];
00263 for (int i = 0 ; i < 4*4; i++)
00264 d[i] = s[i];
00265 }
00266
00267
00268
00269
00270
00271 inline Vector3& Vector3::operator*= (const Matrix3x3& m)
00272 {
00273 Vector3 v = *this;
00274 x = m[0][0]*v.x + m[1][0]*v.y + m[2][0]*v.z;
00275 y = m[0][1]*v.x + m[1][1]*v.y + m[2][1]*v.z;
00276 z = m[0][2]*v.x + m[1][2]*v.y + m[2][2]*v.z;
00277 return *this;
00278 }
00279
00280 inline void Matrix3x3::operator= (const Matrix3x3& ss)
00281 {
00282 float* d = &(*this)[0][0];
00283 const float* s = &ss[0][0];
00284 for (int i = 0 ; i < 3*3; i++)
00285 d[i] = s[i];
00286 }
00287
00288
00289
00290
00291
00292 inline Vector2& Vector2::operator*= (const Matrix2x2& m)
00293 {
00294 Vector2 v = *this;
00295 x = m[0][0]*v.x + m[1][0]*v.y;
00296 y = m[0][1]*v.x + m[1][1]*v.y;
00297 return *this;
00298 }
00299
00300 inline void Matrix2x2::operator= (const Matrix2x2& ss)
00301 {
00302 float* d = &(*this)[0][0];
00303 const float* s = &ss[0][0];
00304 for (int i = 0 ; i < 2*2; i++)
00305 d[i] = s[i];
00306 }
00307
00308 }
00309
00310
00311 #endif // __SSMATRIX_HPP