00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef HxMatrix_h
00013 #define HxMatrix_h
00014
00015 #include "HxStd.h"
00016 #include "HxIo.h"
00017 #include "HxVec3Double.h"
00018
00019 class L_HXBASIS HxVector;
00020
00025 class L_HXBASIS HxMatrix {
00026 public:
00027
00029
00032 HxMatrix();
00033
00035 HxMatrix(int nrow, int ncol);
00036
00038 HxMatrix(int nrow, int ncol, double a);
00039
00041 HxMatrix(int nrow, int ncol, double* data);
00042
00044 HxMatrix(const HxMatrix& m);
00045
00047 HxMatrix(const HxVector& v);
00048
00050 HxMatrix(const HxVector& v1, const HxVector& v2);
00051
00053 HxMatrix(const HxVector& v1, const HxVector& v2,
00054 const HxVector& v3);
00055
00057 HxMatrix(const HxVector& v1, const HxVector& v2,
00058 const HxVector& v3, const HxVector& v4);
00059
00061 HxMatrix(const HxVector& v1, const HxVector& v2,
00062 const HxVector& v3, const HxVector& v4,
00063 const HxVector& v5);
00064
00066 HxMatrix(const HxVector& v1, const HxVector& v2,
00067 const HxVector& v3, const HxVector& v4,
00068 const HxVector& v5, const HxVector& v6);
00070
00071 ~HxMatrix();
00072
00074
00078 static HxMatrix translate2d(double x, double y);
00079
00081 static HxMatrix scale2d(double sx, double sy);
00082
00084 static HxMatrix rotate2d(double alpha);
00085
00087 static HxMatrix rotate2dDeg(double alpha);
00088
00090 static HxMatrix reflect2d(int doX, int doY);
00091
00093 static HxMatrix shear2d(double sx, double sy);
00094
00096 static HxMatrix translate3d(double x, double y, double z);
00097
00099 static HxMatrix scale3d(double sx, double sy, double sz);
00100
00102 static HxMatrix rotateX3d(double alpha);
00103
00105 static HxMatrix rotateX3dDeg(double alpha);
00106
00108 static HxMatrix rotateY3d(double alpha);
00109
00111 static HxMatrix rotateY3dDeg(double alpha);
00112
00114 static HxMatrix rotateZ3d(double alpha);
00115
00117 static HxMatrix rotateZ3dDeg(double alpha);
00118
00120 static HxMatrix reflect3d(int doX, int doY, int doZ);
00121
00123 static HxMatrix projection(double f);
00124
00126 static HxMatrix camera(double f);
00127
00129 static HxMatrix lift2dTo3dXY();
00131
00133
00135 int nRow() const;
00136
00138 int nCol() const;
00139
00141 int nElem() const;
00142
00144 int valid() const;
00146
00148
00150 HxMatrix& operator=(double a);
00151
00153 HxMatrix& operator=(const HxMatrix& m);
00154
00155
00157 double* operator[](int i) const;
00158
00160 HxMatrix operator-() const;
00161
00163 friend HxMatrix operator*(const HxMatrix& a, double b);
00164
00166 friend HxMatrix operator*(double a, const HxMatrix& b);
00167
00169 friend L_HXBASIS HxMatrix operator*(const HxMatrix& a, const HxMatrix& b);
00170
00172 friend L_HXBASIS HxVector operator*(const HxVector& a, const HxMatrix& b);
00173
00175 friend L_HXBASIS HxVector operator*(const HxMatrix& a, const HxVector& b);
00176
00178 friend HxMatrix operator/(const HxMatrix& a, double b);
00179
00181 friend HxMatrix operator/(double a, const HxMatrix& b);
00182
00184 friend L_HXBASIS HxMatrix operator+(const HxMatrix& a, const HxMatrix& b);
00185
00187 friend HxMatrix operator+(const HxMatrix& a, double b);
00188
00190 friend HxMatrix operator+(double a, const HxMatrix& b);
00191
00193 friend L_HXBASIS HxMatrix operator-(const HxMatrix& a, const HxMatrix& b);
00194
00196 friend HxMatrix operator-(const HxMatrix& a, double b);
00197
00199 friend HxMatrix operator-(double a, const HxMatrix& b);
00200
00202 friend L_HXBASIS int operator==(const HxMatrix& a,const HxMatrix& b);
00203
00205 friend int operator!=(const HxMatrix& a,const HxMatrix& b);
00206
00207
00211 friend L_HXBASIS HxVec3Double operator*(const HxVec3Double& a, const HxMatrix& b);
00212
00216 friend L_HXBASIS HxVec3Double operator*(const HxMatrix& a, const HxVec3Double& b);
00218
00220
00222 HxMatrix i() const;
00223
00225 HxMatrix t() const;
00226
00230 HxMatrix svd(HxVector& W, HxMatrix& V) const;
00231
00235 HxMatrix add(const HxMatrix& b) const;
00236
00240 HxMatrix add(const double val) const;
00241
00245 HxMatrix sub(const HxMatrix& b) const;
00246
00250 HxMatrix sub(const double val) const;
00251
00255 HxMatrix mul(const HxMatrix& b) const;
00256
00260 HxMatrix mul(const HxVector& v) const;
00261
00265 HxMatrix mul(const double val) const;
00266
00270 HxMatrix div(const double val) const;
00271
00273 HxMatrix sin() const;
00274
00276 HxMatrix cos() const;
00277
00279 HxMatrix tan() const;
00280
00282 HxMatrix sinh() const;
00283
00285 HxMatrix cosh() const;
00286
00288 HxMatrix tanh() const;
00289
00291 HxMatrix exp() const;
00292
00294 HxMatrix log() const;
00295
00297 HxMatrix sqrt() const;
00298
00300 HxMatrix abs() const;
00301
00303 HxMatrix sgn() const;
00304
00306 HxMatrix map(double (*f)(double)) const;
00308
00309 STD_OSTREAM& put(STD_OSTREAM& os) const;
00310 private:
00311 friend class HxVector;
00312
00313 int _nr;
00314 int _nc;
00315 double* _data;
00316 };
00317
00318 #include "HxVector.h"
00319
00320
00321 inline
00322 HxMatrix::HxMatrix()
00323 {
00324 _nr = 0;
00325 _nc = 0;
00326 _data = 0;
00327 }
00328
00329 inline
00330 HxMatrix::HxMatrix(int nRow, int nCol)
00331 {
00332 _nr = nRow;
00333 _nc = nCol;
00334 _data = new double[_nr * _nc];
00335 }
00336
00337 inline
00338 HxMatrix::HxMatrix(int nRow, int nCol, double a)
00339 {
00340 _nr = nRow;
00341 _nc = nCol;
00342 _data = new double[_nr * _nc];
00343
00344 double* t = _data;
00345 int i = nElem();
00346 while (--i >= 0)
00347 *t++ = a;
00348 }
00349
00350 inline
00351 HxMatrix::HxMatrix(int nRow, int nCol, double *data)
00352 {
00353 _nr = nRow;
00354 _nc = nCol;
00355 _data = data;
00356 }
00357
00358 inline HxMatrix::HxMatrix(const HxMatrix& m)
00359 {
00360 _nr = m.nRow();
00361 _nc = m.nCol();
00362 _data = new double[_nr * _nc];
00363
00364 double* t = m._data;
00365 double* u = _data;
00366 int i = m.nElem();
00367 while (--i >= 0)
00368 *u++ = *t++;
00369 }
00370
00371 inline HxMatrix::~HxMatrix()
00372 {
00373 delete [] _data;
00374 }
00375
00376 inline int
00377 HxMatrix::nRow() const
00378 {
00379 return _nr;
00380 }
00381
00382 inline int
00383 HxMatrix::nCol() const
00384 {
00385 return _nc;
00386 }
00387
00388 inline int
00389 HxMatrix::nElem() const
00390 {
00391 return _nr * _nc;
00392 }
00393
00394 inline int
00395 HxMatrix::valid() const
00396 {
00397 return ((_nc != 0) && (_nr != 0));
00398 }
00399
00400 inline HxMatrix&
00401 HxMatrix::operator=(double a)
00402 {
00403 int i = nElem();
00404 double *t = _data;
00405 while (--i >= 0)
00406 *t++ = a;
00407 return *this;
00408 }
00409
00410 inline HxMatrix&
00411 HxMatrix::operator=(const HxMatrix& m)
00412 {
00413 if (this != &m) {
00414 delete [] _data;
00415 _nr = m.nRow();
00416 _nc = m.nCol();
00417 _data = new double [_nr * _nc];
00418 double *t = _data;
00419 double *u = m._data;
00420 int i = m.nElem();
00421 while (--i >= 0)
00422 *t++ = *u++;
00423 }
00424 return *this;
00425 }
00426
00427 inline double*
00428 HxMatrix::operator[](int i) const
00429 {
00430 return &_data[i*_nc];
00431 }
00432
00433 inline HxMatrix
00434 HxMatrix::operator-() const
00435 {
00436 HxMatrix m(*this);
00437 double* t = m._data;
00438 double* u = _data;
00439 int i = nElem();
00440 while (--i >= 0)
00441 *t++ = -(*u++);
00442 return m;
00443 }
00444
00445 inline HxMatrix
00446 operator*(const HxMatrix& a, double b)
00447 {
00448 HxMatrix m(a);
00449 double* t = m._data;
00450 double* u = a._data;
00451 int i = a.nElem();
00452 while (--i >= 0)
00453 *t++ = *u++ * b;
00454 return m;
00455 }
00456
00457 inline HxMatrix
00458 operator*(double a, const HxMatrix &b)
00459 {
00460 HxMatrix m(b);
00461 double* t = m._data;
00462 double* u = b._data;
00463 int i = b.nElem();
00464 while (--i >= 0)
00465 *t++ = a * *u++;
00466 return m;
00467 }
00468
00469 inline HxMatrix
00470 operator/(const HxMatrix &a, double b)
00471 {
00472 HxMatrix m(a);
00473 double* t = m._data;
00474 double* u = a._data;
00475 int i = a.nElem();
00476 while (--i >= 0)
00477 *t++ = *u++ / b;
00478 return m;
00479 }
00480
00481 inline HxMatrix
00482 operator/(double a, const HxMatrix &b)
00483 {
00484 HxMatrix m(b);
00485 double* t = m._data;
00486 double* u = b._data;
00487 int i = b.nElem();
00488 while (--i >= 0)
00489 *t++ = a / *u++;
00490 return m;
00491 }
00492
00493 inline HxMatrix
00494 operator+(const HxMatrix &a, double b)
00495 {
00496 HxMatrix m(a);
00497 double* t = m._data;
00498 double* u = a._data;
00499 int i = a.nElem();
00500 while (--i >= 0)
00501 *t++ = *u++ + b;
00502 return m;
00503 }
00504
00505 inline HxMatrix
00506 operator+(double a, const HxMatrix &b)
00507 {
00508 HxMatrix m(b);
00509 double* t = m._data;
00510 double* u = b._data;
00511 int i = b.nElem();
00512 while (--i >= 0)
00513 *t++ = a + *u++;
00514 return m;
00515 }
00516
00517 inline HxMatrix
00518 operator-(const HxMatrix &a, double b)
00519 {
00520 HxMatrix m(a);
00521 double* t = m._data;
00522 double* u = a._data;
00523 int i = a.nElem();
00524 while (--i >= 0)
00525 *t++ = *u++ - b;
00526 return m;
00527 }
00528
00529 inline HxMatrix
00530 operator-(double a, const HxMatrix &b)
00531 {
00532 HxMatrix m(b);
00533 double* t = m._data;
00534 double* u = b._data;
00535 int i = b.nElem();
00536 while (--i >= 0)
00537 *t++ = a - *u++;
00538 return m;
00539 }
00540
00541 inline int
00542 operator!=(const HxMatrix &a, const HxMatrix &b)
00543 {
00544 return !(a == b);
00545 }
00546
00547 inline HxMatrix
00548 HxMatrix::t() const
00549 {
00550 HxMatrix m(nCol(), nRow());
00551 int i, j;
00552 for (i=0 ; i<nRow() ; i++)
00553 for (j=0 ; j<nCol() ; j++)
00554 m[j][i] = (*this)[i][j];
00555 return m;
00556 }
00557
00558 inline STD_OSTREAM&
00559 operator<<(STD_OSTREAM& os, const HxMatrix v)
00560 {
00561 return v.put(os);
00562 }
00563
00564 inline HxMatrix
00565 HxMatrix::map(double (*f)(double)) const
00566 {
00567 HxMatrix m(*this);
00568 double* t = m._data;
00569 double* u = _data;
00570 int i = nElem();
00571 while (--i >= 0)
00572 *t++ = f(*u++);
00573 return m;
00574 }
00575
00576 L_HXBASIS HxMatrix operator*(const HxMatrix& a, const HxMatrix& b);
00577 L_HXBASIS HxVector operator*(const HxVector& a, const HxMatrix& b);
00578 L_HXBASIS HxVector operator*(const HxMatrix& a, const HxVector& b);
00579 L_HXBASIS HxMatrix operator+(const HxMatrix& a, const HxMatrix& b);
00580 L_HXBASIS HxMatrix operator-(const HxMatrix& a, const HxMatrix& b);
00581 L_HXBASIS int operator==(const HxMatrix& a, const HxMatrix& b);
00582 L_HXBASIS HxVec3Double operator*(const HxVec3Double& a, const HxMatrix& b);
00583 L_HXBASIS HxVec3Double operator*(const HxMatrix& a, const HxVec3Double& b);
00584
00585 #endif