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