00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __Matrix_h__
00011 #define __Matrix_h__
00012
00013 #include "Vector3.h"
00014
00015
00016
00017
00018
00019 template <class V> class Matrix3x3 {
00020 public:
00021
00022
00023
00024 Vector3<V> c[3];
00025
00026
00027
00028
00029 Matrix3x3<V>()
00030 {
00031 c[0]=Vector3<V>(1,0,0);
00032 c[1]=Vector3<V>(0,1,0);
00033 c[2]=Vector3<V>(0,0,1);
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043 Matrix3x3<V>(
00044 const Vector3<V>& c0,
00045 const Vector3<V>& c1,
00046 const Vector3<V>& c2
00047 )
00048 {
00049 c[0] = c0;
00050 c[1] = c1;
00051 c[2] = c2;
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 Matrix3x3<V>& operator=(const Matrix3x3<V>& other)
00061 {
00062 c[0] = other.c[0];
00063 c[1] = other.c[1];
00064 c[2] = other.c[2];
00065 return *this;
00066 }
00067
00068
00069
00070
00071
00072
00073 Matrix3x3<V>(const Matrix3x3<V>& other)
00074 {
00075 *this = other;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 Matrix3x3<V> operator+(const Matrix3x3<V>& other) const
00086 {
00087 return Matrix3x3<V>(
00088 (*this).c[0]+other.c[0],
00089 (*this).c[1]+other.c[1],
00090 (*this).c[2]+other.c[2]
00091 );
00092 }
00093
00094
00095
00096
00097
00098
00099 Matrix3x3<V>& operator+=(const Matrix3x3<V>& other)
00100 {
00101 (*this).c[0]+=other.c[0];
00102 (*this).c[1]+=other.c[1];
00103 (*this).c[2]+=other.c[2];
00104 return *this;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 Vector3<V> operator*(const Vector3<V>& vector) const
00114 {
00115 return (c[0]*vector.x + c[1]*vector.y + c[2]*vector.z);
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125 Matrix3x3<V> operator*(const Matrix3x3<V>& other) const
00126 {
00127 return Matrix3x3<V>(
00128 (*this)*other.c[0],
00129 (*this)*other.c[1],
00130 (*this)*other.c[2]
00131 );
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 Matrix3x3<V>& operator*=(const Matrix3x3<V>& other)
00141 {
00142 return *this = *this * other;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 Matrix3x3<V>& operator*=(const V& factor)
00152 {
00153 c[0] *= factor;
00154 c[1] *= factor;
00155 c[2] *= factor;
00156 return *this;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 Matrix3x3<V>& operator/=(const V& factor)
00166 {
00167 *this *= 1 / factor;
00168 return *this;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 Matrix3x3<V> operator*(const V& factor) const
00178 {
00179 return Matrix3x3<V>(*this) *= factor;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 Matrix3x3<V> operator/(const V& factor) const
00189 {
00190 return Matrix3x3<V>(*this) /= factor;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 bool operator==(const Matrix3x3<V>& other) const
00200 {
00201 return (
00202 c[0] == other.c[0] &&
00203 c[1] == other.c[1] &&
00204 c[2] == other.c[2]
00205 );
00206 }
00207
00208
00209
00210
00211
00212
00213
00214 bool operator!=(const Matrix3x3<V>& other) const
00215 {
00216 return !(*this == other);
00217 }
00218
00219
00220
00221
00222
00223
00224 Vector3<V>& operator[](int i)
00225 {
00226 return c[i];
00227 }
00228
00229
00230
00231
00232
00233
00234 Matrix3x3<V> transpose() const
00235 {
00236 return Matrix3x3<V>(
00237 Vector3<V>(c[0].x, c[1].x, c[2].x),
00238 Vector3<V>(c[0].y, c[1].y, c[2].y),
00239 Vector3<V>(c[0].z, c[1].z, c[2].z)
00240 );
00241 }
00242
00243
00244
00245
00246
00247
00248 V det() const
00249 {
00250 return
00251 c[0].x * (c[1].y * c[2].z - c[1].z * c[2].y) +
00252 c[0].y * (c[1].z * c[2].x - c[1].x * c[2].z) +
00253 c[0].z * (c[1].x * c[2].y - c[1].y * c[2].x);
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263 static V det2(V a, V b, V c, V d)
00264 {
00265 return a*d - b*c;
00266 }
00267
00268
00269
00270
00271
00272
00273 Matrix3x3<V> adjoint() const
00274 {
00275 return Matrix3x3<V>(
00276 Vector3<V>(
00277 det2(c[1].y, c[2].y, c[1].z, c[2].z),
00278 det2(c[2].x, c[1].x, c[2].z, c[1].z),
00279 det2(c[1].x, c[2].x, c[1].y, c[2].y)
00280 ),
00281 Vector3<V>(
00282 det2(c[2].y, c[0].y, c[2].z, c[0].z),
00283 det2(c[0].x, c[2].x, c[0].z, c[2].z),
00284 det2(c[2].x, c[0].x, c[2].y, c[0].y)
00285 ),
00286 Vector3<V>(
00287 det2(c[0].y, c[1].y, c[0].z, c[1].z),
00288 det2(c[1].x, c[0].x, c[1].z, c[0].z),
00289 det2(c[0].x, c[1].x, c[0].y, c[1].y)
00290 )
00291 );
00292
00293 }
00294
00295
00296
00297
00298
00299
00300 Matrix3x3<V> invert() const
00301 {
00302 return adjoint().transpose() / det();
00303 }
00304 };
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 template <class V> In& operator>>(In& stream, Matrix3x3<V>& matrix3x3);
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 template <class V> Out& operator<<(Out& stream, const Matrix3x3<V>& matrix3x3);
00325
00326
00327
00328
00329
00330 class RotationMatrix : public Matrix3x3<double> {
00331 public:
00332
00333
00334
00335 RotationMatrix() {}
00336
00337
00338
00339
00340
00341
00342
00343
00344 RotationMatrix(
00345 const Vector3<double>& c0,
00346 const Vector3<double>& c1,
00347 const Vector3<double>& c2
00348 )
00349 : Matrix3x3<double>(c0,c1,c2)
00350 {
00351 }
00352
00353
00354
00355
00356
00357
00358
00359 RotationMatrix& operator=(const Matrix3x3<double>& other)
00360 {
00361 c[0] = other.c[0];
00362 c[1] = other.c[1];
00363 c[2] = other.c[2];
00364 return *this;
00365 }
00366
00367
00368
00369
00370
00371
00372 RotationMatrix(const Matrix3x3<double>& other)
00373 {
00374 *this = other;
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 RotationMatrix& fromKardanRPY(const double yaw, const double pitch, const double roll);
00389
00390
00391
00392
00393
00394
00395 RotationMatrix invert()
00396 {
00397 return transpose();
00398 }
00399
00400
00401
00402
00403
00404
00405
00406 RotationMatrix& rotateX(const double angle);
00407
00408
00409
00410
00411
00412
00413
00414 RotationMatrix& rotateY(const double angle);
00415
00416
00417
00418
00419
00420
00421
00422 RotationMatrix& rotateZ(const double angle);
00423
00424
00425
00426
00427
00428
00429
00430 double getXAngle() const;
00431
00432
00433
00434
00435
00436
00437
00438 double getYAngle() const;
00439
00440
00441
00442
00443
00444
00445
00446 double getZAngle() const;
00447
00448
00449
00450
00451
00452
00453
00454 static RotationMatrix getRotationX(const double angle)
00455 {
00456 return RotationMatrix().rotateX(angle);
00457 }
00458
00459
00460
00461
00462
00463
00464
00465 static RotationMatrix getRotationY(const double angle)
00466 {
00467 return RotationMatrix().rotateY(angle);
00468 }
00469
00470
00471
00472
00473
00474
00475
00476 static RotationMatrix getRotationZ(const double angle)
00477 {
00478 return RotationMatrix().rotateZ(angle);
00479 }
00480 };
00481
00482
00483
00484
00485
00486
00487
00488
00489 In& operator>>(In& stream, RotationMatrix& rotationMatrix);
00490
00491
00492
00493
00494
00495
00496
00497
00498 Out& operator<<(Out& stream, const RotationMatrix& rotationMatrix);
00499
00500
00501
00502 #endif // __Matrix_h__