00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
#ifndef _ZFXMATH_INCLUDE_MATRIX3X3_H_
00014
#define _ZFXMATH_INCLUDE_MATRIX3X3_H_
00015
00016
namespace ZFXMath
00017 {
00027
template<
class T>
00028 class TMatrix3x3
00029 {
00030
00031
public:
00032
00033
union
00034
{
00035 T m[9];
00036
struct
00037
{
00038 T _11; T _12; T _13;
00039 T _21; T _22; T _23;
00040 T _31; T _32; T _33;
00041 };
00042 };
00043
00044
TMatrix3x3()
00045 {
00046 (*this) = Identity();
00047 };
00048
00049
TMatrix3x3(
const T& val )
00050 {
00051 _11 = _12 = _13 =
00052 _21 = _22 = _23 =
00053 _31 = _32 = _33 = val;
00054 }
00055
00056
TMatrix3x3(
const T& _11,
const T& _12,
const T& _13,
00057
const T& _21,
const T& _22,
const T& _23,
00058
const T& _31,
const T& _32,
const T& _33 )
00059 {
00060 this->_11 = _11; this->_12 = _12; this->_13 = _13;
00061 this->_21 = _21; this->_22 = _22; this->_23 = _23;
00062 this->_31 = _31; this->_32 = _32; this->_33 = _33;
00063 }
00064
00065
TMatrix3x3(
const T& sx,
const T& sy,
const T& sz = (T)1.0 )
00066 {
00067 (*this) =
TMatrix3x3( sx, 0.0, 0.0,
00068 0.0, sy, 0.0,
00069 0.0, 0.0, sz );
00070 }
00071
00072
TMatrix3x3(
const T* _m )
00073 {
00074 memcpy( m, _m,
sizeof(
TMatrix3x3 ) );
00075 }
00076
00077
TMatrix3x3(
const TMatrix3x3& _m )
00078 {
00079 memcpy( m, _m,
sizeof(
TMatrix3x3 ) );
00080 }
00081
00082
TMatrix3x3(
const T& rad,
const T& scale,
const TVector2D<T>& p )
00083 {
00084 (*this) =
Rotation2D( rad );
00085
Scale( scale );
00086
Translate( p );
00087 }
00088
00089
00090
TMatrix3x3(
const TVector3D<T>& v1,
const TVector3D<T>& v2,
const TVector3D<T>& v3 )
00091 {
00092 _11 = v1.
x; _12 = v1.
y; _13 = v1.
z;
00093 _21 = v2.
x; _22 = v2.
y; _23 = v2.
z;
00094 _31 = v3.
x; _32 = v3.
y; _33 = v3.
z;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00139 operator T* () {
return m; }
00140 operator const T* ()
const {
return m; }
00141
00147 T
operator () (
const int i,
const int j )
const
00148
{
00149
return m[ Check( i ) * 3 + Check( j ) ];
00150 }
00151
00157 T&
operator () (
const int i,
const int j )
00158 {
00159
return m[ Check( i ) * 3 + Check( j ) ];
00160 }
00161
00162
TMatrix3x3 operator + (
const TMatrix3x3& _m )
const {
return TMatrix3x3( _11 + _m.
_11, _12 + _m.
_12, _13 + _m.
_13,
00163 _21 + _m.
_21, _22 + _m.
_22, _23 + _m.
_23,
00164 _31 + _m.
_31, _32 + _m.
_32, _33 + _m.
_33 ); }
00165 TMatrix3x3 operator - (
const TMatrix3x3& _m )
const {
return (*this) + _m.Negate(); }
00166 TMatrix3x3 operator * (
const TMatrix3x3& _m )
const {
return Multiply( _m ); }
00167 TMatrix3x3 operator * (
const T& s )
const {
return Scale( s ); }
00168
00169
00170 TMatrix3x3& operator += (
const TMatrix3x3& _m ) { *
this = (*this) + _m;
return (*this); }
00171 TMatrix3x3& operator -= (
const TMatrix3x3& _m ) { *
this = (*this) - _m;
return (*this); }
00172 TMatrix3x3& operator *= (
const TMatrix3x3& _m ) { *
this = (*this) * _m;
return (*this); }
00173 TMatrix3x3& operator *= (
const T& s ) { *
this = (*this) * s;
return (*this); }
00174
00175
friend TVector3D<T> operator * (
const TVector3D<T>& v,
const TMatrix3x3& m )
00176 {
00177
return TVector3D<T>( TVector3D<T>( m._11, m._21, m._31 ).DotProduct( v ),
00178 TVector3D<T>( m._12, m._22, m._32 ).DotProduct( v ),
00179 TVector3D<T>( m._13, m._23, m._33 ).DotProduct( v ) );
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
static TMatrix3x3 Zero()
00193 {
00194
return TMatrix3x3( 0.0 );
00195 }
00196
00197
static TMatrix3x3 Identity()
00198 {
00199
return TMatrix3x3( 1.0, 0.0, 0.0,
00200 0.0, 1.0, 0.0,
00201 0.0, 0.0, 1.0 );
00202 }
00203
00209 TMatrix3x3&
Scale(
const T& s )
00210 {
00211 _11 *= s; _12 *= s; _13 *= s;
00212 _21 *= s; _22 *= s; _23 *= s;
00213 _31 *= s; _32 *= s; _33 *= s;
00214
00215
return (*this);
00216 }
00217
00223 TMatrix3x3&
Scale(
const T& sx,
const T& sy,
const T& sz = (T)1.0 )
00224 {
00225 _11 *= sx; _12 *= sy; _13 *= sz;
00226 _21 *= sx; _22 *= sy; _23 *= sz;
00227 _31 *= sx; _32 *= sy; _33 *= sz;
00228
00229
return (*this);
00230 }
00231
00237 TMatrix3x3&
Scale(
const TVector3D<T>& sv )
00238 {
00239
return Scale( sv.
x, sv.
y, sv.
z );
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00258 static TMatrix3x3 Translation(
const TVector2D<T>& p )
00259 {
00260
return Translation( p.
x, p.
y );
00261 }
00262
00268 static TMatrix3x3 Translation(
const T& x,
const T& y )
00269 {
00270
TMatrix3x3 newM = TMatrix3x3::Identity();
00271 newM.
_31 = x;
00272 newM.
_32 = y;
00273
00274
return newM;
00275 }
00276
00282 TMatrix3x3&
Translate(
const TVector2D<T>& p )
00283 {
00284
return Translate( p.
x, p.
y );
00285 }
00286
00292 TMatrix3x3&
Translate(
const T& x,
const T& y )
00293 {
00294 _31 += x;
00295 _32 += y;
00296
return (*this);
00297 }
00298
00304 TMatrix3x3 Multiply(
const TMatrix3x3& _m )
const
00305
{
00306
TMatrix3x3 newM = TMatrix3x3::Zero();
00307
00308
for (
int i = 0; i < 3; i++ )
00309 {
00310
for (
int j = 0; j < 3; j++ )
00311 {
00312
for (
int k = 0; k < 3; k++ )
00313 {
00314 newM.
m[i * 3 + j] += m[i * 3 + k] * _m.
m[k * 3 + j];
00315 }
00316 }
00317 }
00318
00319
return newM;
00320 }
00321
00327 static TMatrix3x3 RotationX(
const T& rad )
00328 {
00329 T sin, cos;
00330
00331
SinCos( rad, sin, cos );
00332
00333
return TMatrix3x3( 1.0, 0.0, 0.0,
00334 0.0, cos, sin,
00335 0.0, -sin, cos );
00336 }
00337
00343 TMatrix3x3&
RotateX(
const T& rad )
00344 {
00345
return (*this) *=
RotationX( rad );
00346 }
00347
00353 static TMatrix3x3 RotationY(
const T& rad )
00354 {
00355 T sin, cos;
00356
00357
SinCos( rad, sin, cos );
00358
00359
return TMatrix3x3( cos, 0.0, -sin,
00360 0.0, 1.0, 0.0,
00361 sin, 0.0, cos );
00362 }
00363
00369 TMatrix3x3&
RotateY(
const T& rad )
00370 {
00371
return (*this) *=
RotationY( rad );
00372 }
00373
00379 static TMatrix3x3 RotationZ(
const T& rad )
00380 {
00381 T sin, cos;
00382
00383
SinCos( rad, sin, cos );
00384
00385
return TMatrix3x3( cos, sin, 0.0,
00386 -sin, cos, 0.0,
00387 0.0, 0.0, 1.0 );
00388 }
00389
00395 TMatrix3x3&
RotateZ(
const T& rad )
00396 {
00397
return (*this) *=
RotationZ( rad );
00398 }
00399
00405 static TMatrix3x3 Rotation2D(
const T& rad )
00406 {
00407
return RotationZ( rad );
00408 }
00409
00415 TMatrix3x3&
Rotate2D(
const T& rad )
00416 {
00417
return (*this) *=
Rotation2D( rad );
00418 }
00419
00420
00426 TMatrix3x3 Transpose()
const
00427
{
00428
return TMatrix3x3( _11, _21, _31,
00429 _12, _22, _32,
00430 _13, _23, _33 );
00431 }
00432
00438 TMatrix3x3 Negate()
const
00439
{
00440
return TMatrix3x3( -_11, -_12, -_13,
00441 -_21, -_22, -_23,
00442 -_31, -_32, -_33 );
00443 }
00444
00450 TMatrix3x3 GetSwitchedHand()
const
00451
{
00452
TMatrix3x3 newM(
Transpose() );
00453 newM.
_32 = -newM.
_32;
00454 newM.
_23 = -newM.
_23;
00455 newM.
_13 = -newM.
_13;
00456 newM.
_31 = -newM.
_31;
00457
00458
return newM;
00459 }
00460
00461
private:
00462
inline int Check(
const int index )
const
00463
{
00464
00465 assert( index >= 0 && index < 3 );
00466
00467
return index;
00468 }
00469
00470
00471 };
00472 }
00473
00474
#endif //_ZFXMATH_INCLUDE_MATRIX3X3_H_
00475
00476
00477
00478