00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef MATRIX3_H
00033 #define MATRIX3_H
00034
00037 #include <iostream>
00038
00040 class Vector2 {
00041 public:
00043 double x[2];
00044
00046 Vector2() {
00047 x[0] = x[1] = 0.0;
00048 }
00049
00051
00054 inline double &operator[](int idx) {
00055 return x[idx];
00056 }
00057
00059
00062 inline double operator[](int idx) const {
00063 return x[idx];
00064 }
00065
00067
00070 inline double &operator()(int idx) {
00071 return x[idx];
00072 }
00073
00075
00078 Vector2 operator*(double v) {
00079 Vector2 res;
00080 for (int i=0;i<2;i++)
00081 res.x[i] = x[i]*v;
00082 return res;
00083 }
00084
00086
00089 Vector2 &operator=(double v) {
00090 for (int i=0;i<2;i++)
00091 x[i] = v;
00092 return *this;
00093 }
00094
00096
00099 Vector2 &operator*=(double v) {
00100 for (int i=0;i<2;i++)
00101 x[i] *= v;
00102 return *this;
00103 }
00104
00106
00109 Vector2 &operator+=(double v) {
00110 for (int i=0;i<2;i++)
00111 x[i] += v;
00112 return *this;
00113 }
00114
00116
00119 Vector2 &operator+=(const Vector2 &v) {
00120 for (int i=0;i<2;i++)
00121 x[i] += v[i];
00122 return *this;
00123 }
00124
00126
00130 friend std::ostream &operator<<(std::ostream &os, const Vector2 &v) {
00131 os << "[ " << v[0] << " " << v[1] << " ]";
00132 return os;
00133 }
00134 };
00135
00137 class Vector3 {
00138 public:
00140 double x[3];
00141
00143 Vector3() {
00144 x[0] = x[1] = x[2] = 0.0;
00145 }
00146
00148
00151 inline double &operator[](int idx) {
00152 return x[idx];
00153 }
00154
00156
00159 inline double operator[](int idx) const {
00160 return x[idx];
00161 }
00162
00164
00167 inline double &operator()(int idx) {
00168 return x[idx];
00169 }
00170
00172
00175 Vector3 operator*(double v) {
00176 Vector3 res;
00177 for (int i=0;i<3;i++)
00178 res.x[i] = x[i]*v;
00179 return res;
00180 }
00181
00183
00186 Vector3 &operator=(double v) {
00187 for (int i=0;i<3;i++)
00188 x[i] = v;
00189 return *this;
00190 }
00191
00193
00196 Vector3 &operator*=(double v) {
00197 for (int i=0;i<3;i++)
00198 x[i] *= v;
00199 return *this;
00200 }
00201
00203
00206 Vector3 &operator+=(double v) {
00207 for (int i=0;i<3;i++)
00208 x[i] += v;
00209 return *this;
00210 }
00211
00213
00216 Vector3 &operator+=(const Vector3 &v) {
00217 for (int i=0;i<3;i++)
00218 x[i] += v[i];
00219 return *this;
00220 }
00221
00223
00227 friend std::ostream &operator<<(std::ostream &os, const Vector3 &v) {
00228 os << "[ " << v[0] << " " << v[1] << " " << v[2] << " ]";
00229 return os;
00230 }
00231 };
00232
00234 class Matrix2 {
00235 public:
00237 Vector2 x[2];
00238
00240 Matrix2() {
00241 identity();
00242 }
00243
00245
00247 Matrix2(const Matrix2 &m) {
00248 for (int i=0;i<2;i++)
00249 x[i] = m.x[i];
00250 }
00251
00253
00255 Matrix2 &operator=(const Matrix2 &m) {
00256 for (int i=0;i<2;i++)
00257 x[i] = m.x[i];
00258 return *this;
00259 }
00260
00262 void identity() {
00263 x[0][0] = x[1][1] = 1.0;
00264 x[0][1] = x[1][0] = 0.0;
00265 }
00266
00268
00271 inline Vector2 &operator[](int idx) {
00272 return x[idx];
00273 }
00274
00276
00279 inline Vector2 operator[](int idx) const {
00280 return x[idx];
00281 }
00282
00284
00288 inline double &operator()(int r, int c) {
00289 return x[r][c];
00290 }
00291
00293
00294 double determinant() {
00295 return x[0][0]*x[1][1] - x[0][1]*x[1][0];
00296 }
00297
00299
00300 Matrix2 invert() {
00301 double det = determinant();
00302 Matrix2 res;
00303 if (det!=0.0) {
00304 double idet = 1.0/det;
00305 res.x[0][0] = idet*x[1][1];
00306 res.x[0][1] = -idet*x[0][1];
00307 res.x[1][0] = -idet*x[1][0];
00308 res.x[1][1] = idet*x[0][0];
00309 }
00310 return res;
00311 }
00312
00314
00316 Matrix2 operator*(const Matrix2 &m) {
00317 Matrix2 res;
00318 for (int j=0;j<2;j++)
00319 for (int i=0;i<2;i++)
00320 res.x[j][i] = x[j][0]*m.x[0][i] + x[j][1]*m.x[1][i];
00321 return res;
00322 }
00323
00325
00327 Vector2 operator*(const Vector2 &v) {
00328 Vector2 res;
00329 for (int i=0;i<2;i++)
00330 res.x[i] = x[i][0]*v.x[0] + x[i][1]*v.x[1];
00331 return res;
00332 }
00333
00335
00337 Matrix2 operator*(double v) {
00338 Matrix2 res;
00339 for (int i=0;i<2;i++)
00340 res.x[i] = x[i]*v;
00341 return res;
00342 }
00343
00345
00347 Matrix2 &operator=(double v) {
00348 for (int i=0;i<2;i++)
00349 x[i] = v;
00350 return *this;
00351 }
00352
00354
00356 Matrix2 &operator*=(double v) {
00357 for (int i=0;i<2;i++)
00358 x[i] *= v;
00359 return *this;
00360 }
00361
00363
00365 Matrix2 &operator+=(const Matrix2 &m) {
00366 for (int i=0;i<2;i++)
00367 x[i] += m[i];
00368 return *this;
00369 }
00370
00372
00374 Matrix2 &operator+=(double v) {
00375 for (int i=0;i<2;i++)
00376 x[i] += v;
00377 return *this;
00378 }
00379
00381
00385 friend std::ostream &operator<<(std::ostream &os, const Matrix2 &m) {
00386 os << "[ " << m[0][0] << " " << m[0][1] << " ;";
00387 os << " " << m[1][0] << " " << m[1][1] << " ]";
00388 return os;
00389 }
00390 };
00391
00393 class Matrix3 {
00394 public:
00396 Vector3 x[3];
00397
00399 Matrix3() {
00400 identity();
00401 }
00402
00404
00406 Matrix3(const Matrix3 &m) {
00407 for (int i=0;i<3;i++)
00408 x[i] = m.x[i];
00409 }
00410
00412
00414 Matrix3 &operator=(const Matrix3 &m) {
00415 for (int i=0;i<3;i++)
00416 x[i] = m.x[i];
00417 return *this;
00418 }
00419
00421 void identity() {
00422 x[0][0] = x[1][1] = x[2][2] = 1.0;
00423 x[0][1] = x[0][2] = x[1][0] = 0.0;
00424 x[1][0] = x[2][0] = x[2][1] = 0.0;
00425 }
00426
00428
00431 inline Vector3 &operator[](int idx) {
00432 return x[idx];
00433 }
00434
00436
00439 inline Vector3 operator[](int idx) const {
00440 return x[idx];
00441 }
00442
00444
00448 inline double &operator()(int r, int c) {
00449 return x[r][c];
00450 }
00451
00453
00454 double determinant() {
00455 return (x[0][0]*(x[1][1]*x[2][2] - x[2][1]*x[1][2]) -
00456 x[1][0]*(x[2][1]*x[0][2] - x[0][1]*x[2][2]) +
00457 x[2][0]*(x[0][1]*x[1][2] - x[1][1]*x[0][2]));
00458 }
00459
00461
00462 Matrix3 invert() {
00463 double m00 = x[1][1]*x[2][2] - x[2][1]*x[1][2];
00464 double m01 = x[2][1]*x[0][2] - x[0][1]*x[2][2];
00465 double m02 = x[0][1]*x[1][2] - x[1][1]*x[0][2];
00466 double det = x[0][0]*m00 + x[1][0]*m01 + x[2][0]*m02;
00467 Matrix3 res;
00468 if (det!=0.0) {
00469 double idet = 1.0/det;
00470 res.x[0][0] = idet*m00;
00471 res.x[0][1] = idet*m01;
00472 res.x[0][2] = idet*m02;
00473 res.x[1][0] = idet*(x[2][0]*x[1][2] - x[1][0]*x[2][2]);
00474 res.x[1][1] = idet*(x[0][0]*x[2][2] - x[2][0]*x[0][2]);
00475 res.x[1][2] = idet*(x[1][0]*x[0][2] - x[0][0]*x[1][2]);
00476 res.x[2][0] = idet*(x[1][0]*x[2][1] - x[2][0]*x[1][1]);
00477 res.x[2][1] = idet*(x[2][0]*x[0][1] - x[0][0]*x[2][1]);
00478 res.x[2][2] = idet*(x[0][0]*x[1][1] - x[1][0]*x[0][1]);
00479 }
00480 return res;
00481 }
00482
00484
00486 Matrix3 operator*(const Matrix3 &m) {
00487 Matrix3 res;
00488 for (int j=0;j<3;j++)
00489 for (int i=0;i<3;i++)
00490 res.x[j][i] = x[j][0]*m.x[0][i] + x[j][1]*m.x[1][i] + x[j][2]*m.x[2][i];
00491 return res;
00492 }
00493
00495
00497 Vector3 operator*(const Vector3 &v) {
00498 Vector3 res;
00499 for (int i=0;i<3;i++)
00500 res.x[i] = x[i][0]*v.x[0] + x[i][1]*v.x[1] + x[i][2]*v.x[2];
00501 return res;
00502 }
00503
00505
00507 Matrix3 operator*(double v) {
00508 Matrix3 res;
00509 for (int i=0;i<3;i++)
00510 res.x[i] = x[i]*v;
00511 return res;
00512 }
00513
00515
00517 Matrix3 &operator=(double v) {
00518 for (int i=0;i<3;i++)
00519 x[i] = v;
00520 return *this;
00521 }
00522
00524
00526 Matrix3 &operator*=(double v) {
00527 for (int i=0;i<3;i++)
00528 x[i] *= v;
00529 return *this;
00530 }
00531
00533
00535 Matrix3 &operator+=(const Matrix3 &m) {
00536 for (int i=0;i<3;i++)
00537 x[i] += m[i];
00538 return *this;
00539 }
00540
00542
00544 Matrix3 &operator+=(double v) {
00545 for (int i=0;i<3;i++)
00546 x[i] += v;
00547 return *this;
00548 }
00549
00551
00555 Matrix2 submatrix(int r, int c) {
00556 Matrix2 res;
00557 res.x[0][0] = x[r][c];
00558 res.x[0][1] = x[r][c+1];
00559 res.x[1][0] = x[r+1][c];
00560 res.x[1][1] = x[r+1][c+1];
00561 return res;
00562 }
00563
00564
00566
00570 friend std::ostream &operator<<(std::ostream &os, const Matrix3 &m) {
00571 os << "[ " << m[0][0] << " " << m[0][1] << " " << m[0][2] << " ;";
00572 os << " " << m[1][0] << " " << m[1][1] << " " << m[1][2] << " ;";
00573 os << " " << m[2][0] << " " << m[2][1] << " " << m[2][2] << " ]";
00574 return os;
00575 }
00576 };
00577
00578 #endif
00579
00580