00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _SB_MATRIX_
00024 #define _SB_MATRIX_
00025
00026 #include <Inventor/SbBase.h>
00027 #include <Inventor/STL/iostream>
00028 #include <Inventor/SbVec.h>
00029
00030 class SbLine;
00031 class SbLined;
00032 class SbRotation;
00033 class SbRotationd;
00034 class SbMatrixd;
00035
00036 typedef float SbMat[4][4];
00037 typedef float SbMat3[3][3];
00038 typedef double SbMatd[4][4];
00039
00041
00042
00043
00044
00045
00047
00048
00049
00309 class SbMatrix {
00310 public:
00311
00315 SbMatrix() { }
00316
00320 SbMatrix(float a11, float a12, float a13, float a14,
00321 float a21, float a22, float a23, float a24,
00322 float a31, float a32, float a33, float a34,
00323 float a41, float a42, float a43, float a44);
00324
00328 SbMatrix(const SbMat &m);
00329
00333 void setValue(const SbMat &m);
00334
00339 void setValue(const float *pMat)
00340 {
00341 matrix[0][0] = *pMat++ ;
00342 matrix[0][1] = *pMat++ ;
00343 matrix[0][2] = *pMat++ ;
00344 matrix[0][3] = *pMat++ ;
00345
00346 matrix[1][0] = *pMat++ ;
00347 matrix[1][1] = *pMat++ ;
00348 matrix[1][2] = *pMat++ ;
00349 matrix[1][3] = *pMat++ ;
00350
00351 matrix[2][0] = *pMat++ ;
00352 matrix[2][1] = *pMat++ ;
00353 matrix[2][2] = *pMat++ ;
00354 matrix[2][3] = *pMat++ ;
00355
00356 matrix[3][0] = *pMat++ ;
00357 matrix[3][1] = *pMat++ ;
00358 matrix[3][2] = *pMat++ ;
00359 matrix[3][3] = *pMat++ ;
00360 } ;
00361
00365 void setValue(const SbMatrixd &md) ;
00366
00367 public:
00368
00372 void makeIdentity();
00373
00377 static SbMatrix identity();
00378
00382 void setRotate(const SbRotation &q);
00383
00387 void setScale(float s);
00388
00392 void setScale(const SbVec3f &s);
00393
00397 void setTranslate(const SbVec3f &t);
00398
00404 void setTransform(const SbVec3f &translation,
00405 const SbRotation &rotation,
00406 const SbVec3f &scaleFactor,
00407 const SbRotation &scaleOrientation,
00408 const SbVec3f ¢er);
00409
00414 void setTransform(const SbVec3f &t, const SbRotation &r, const SbVec3f &s);
00415
00421 void setTransform(const SbVec3f &t, const SbRotation &r,
00422 const SbVec3f &s, const SbRotation &so);
00423
00433 void getTransform(SbVec3f &translation,
00434 SbRotation &rotation,
00435 SbVec3f &scaleFactor,
00436 SbRotation &scaleOrientation,
00437 const SbVec3f ¢er) const;
00438
00444 void getTransform(SbVec3f &t, SbRotation &r,
00445 SbVec3f &s, SbRotation &so) const;
00446
00447
00448
00452 void getValue(SbMat &m) const;
00453
00454 #ifdef _WIN32
00455
00458 SbMat &getValue() { SbMat &rMat = matrix; return rMat; }
00459 #endif
00460
00464 const SbMat &getValue() const { return matrix; }
00465
00470 float det3(int r1, int r2, int r3, int c1, int c2, int c3) const;
00471
00475 float det3() const { return det3(0, 1, 2, 0, 1, 2); }
00476
00480 float det4() const;
00481
00488 SbBool factor(SbMatrix &r, SbVec3f &s, SbMatrix &u,
00489 SbVec3f &t, SbMatrix &proj) const;
00490
00496 SbMatrix inverse() const;
00497
00499 void translate(const SbVec3f& translation);
00500
00502 void scale(const SbVec3f& scaleFactor);
00503
00508 SbBool LUDecomposition(int index[4], float &d);
00509
00514 void LUBackSubstitution(int index[4], float b[4]) const;
00515
00520 SbMatrix transpose() const;
00521
00522
00523
00524
00529 SbMatrix &multRight(const SbMatrix &m);
00530
00535 SbMatrix &multLeft(const SbMatrix &m);
00536
00544 void multMatrixVec(const SbVec3f &src, SbVec3f &dst) const;
00545
00556 inline void multVecMatrix(const SbVec3f &src, SbVec3f &dst) const
00557 {
00558 float x,y,z,w;
00559
00560 x = src[0]*matrix[0][0] + src[1]*matrix[1][0] +
00561 src[2]*matrix[2][0] + matrix[3][0];
00562 y = src[0]*matrix[0][1] + src[1]*matrix[1][1] +
00563 src[2]*matrix[2][1] + matrix[3][1];
00564 z = src[0]*matrix[0][2] + src[1]*matrix[1][2] +
00565 src[2]*matrix[2][2] + matrix[3][2];
00566 w = src[0]*matrix[0][3] + src[1]*matrix[1][3] +
00567 src[2]*matrix[2][3] + matrix[3][3];
00568
00569 w = 1.f/w;
00570 dst.setValue(x*w, y*w, z*w);
00571 }
00572
00578 void multMatrixVec(const SbVec3f &src, SbVec4f &dst) const;
00579
00586 void multVecMatrix(const SbVec3f &src, SbVec4f &dst) const;
00587
00600 void multDirMatrix(const SbVec3f &src, SbVec3f &dst) const;
00601
00607 void multLineMatrix(const SbLine &src, SbLine &dst) const;
00608
00609
00610
00611
00615 void print(FILE *fp) const;
00616
00617
00621 operator float *() { return &matrix[0][0]; }
00622
00626 operator SbMat &() { return matrix; }
00627
00631 float * operator [](int i) { return &matrix[i][0]; }
00632
00636 const float * operator [](int i) const { return &matrix[i][0]; }
00637
00641 SbMatrix & operator =(const SbMat &m);
00642
00646 SbMatrix & operator =(const SbMatrix &m);
00647
00651 SbMatrix & operator =(const SbRotation &q) { setRotate(q); return *this; }
00652
00657 SbMatrix & operator *=(const SbMatrix &m) { return multRight(m); }
00658
00662 friend SbMatrix operator *(const SbMatrix &m1, const SbMatrix &m2);
00663
00667 friend int operator ==(const SbMatrix &m1, const SbMatrix &m2);
00671 friend int operator !=(const SbMatrix &m1, const SbMatrix &m2)
00672 { return !(m1 == m2); }
00673
00677 SbBool equals(const SbMatrix &m, float tolerance) const;
00678
00682 bool isInvertible() const;
00683
00684 private:
00688 void jacobi3(float evalues[3], SbVec3f evectors[3], int &rots) const;
00689
00690 private:
00691
00692 SbMat matrix;
00693
00694 SbBool affine_inverse(const SbMatrix &in, SbMatrix &out) const;
00695
00696
00697 };
00698
00700
00701
00702
00703
00704
00706
00745 class SbMatrixd {
00746 public:
00747
00751 SbMatrixd() { }
00752
00756 SbMatrixd(double a11, double a12, double a13, double a14,
00757 double a21, double a22, double a23, double a24,
00758 double a31, double a32, double a33, double a34,
00759 double a41, double a42, double a43, double a44);
00760
00764 SbMatrixd(const SbMatd &m);
00765
00769 void setValue(const SbMatd &m);
00770
00775 void setValue(const double *pMat)
00776 {
00777 matrix[0][0] = *pMat++ ;
00778 matrix[0][1] = *pMat++ ;
00779 matrix[0][2] = *pMat++ ;
00780 matrix[0][3] = *pMat++ ;
00781
00782 matrix[1][0] = *pMat++ ;
00783 matrix[1][1] = *pMat++ ;
00784 matrix[1][2] = *pMat++ ;
00785 matrix[1][3] = *pMat++ ;
00786
00787 matrix[2][0] = *pMat++ ;
00788 matrix[2][1] = *pMat++ ;
00789 matrix[2][2] = *pMat++ ;
00790 matrix[2][3] = *pMat++ ;
00791
00792 matrix[3][0] = *pMat++ ;
00793 matrix[3][1] = *pMat++ ;
00794 matrix[3][2] = *pMat++ ;
00795 matrix[3][3] = *pMat++ ;
00796 } ;
00797
00801 void setValue(const SbMatrix &m) ;
00802
00803 public:
00804
00808 void makeIdentity();
00809
00813 static SbMatrixd identity();
00814
00818 void setRotate(const SbRotationd &q);
00819
00823 void setScale(double s);
00824
00828 void setScale(const SbVec3d &s);
00829
00833 void setTranslate(const SbVec3d &t);
00834
00840 void setTransform(const SbVec3d &translation,
00841 const SbRotationd &rotation,
00842 const SbVec3d &scaleFactor,
00843 const SbRotationd &scaleOrientation,
00844 const SbVec3d ¢er);
00845
00850 void setTransform(const SbVec3d &t, const SbRotationd &r, const SbVec3d &s);
00851
00857 void setTransform(const SbVec3d &t, const SbRotationd &r,
00858 const SbVec3d &s, const SbRotationd &so);
00859
00869 void getTransform(SbVec3d &translation,
00870 SbRotationd &rotation,
00871 SbVec3d &scaleFactor,
00872 SbRotationd &scaleOrientation,
00873 const SbVec3d ¢er) const;
00874
00880 void getTransform(SbVec3d &t, SbRotationd &r,
00881 SbVec3d &s, SbRotationd &so) const;
00882
00883
00884
00885
00889 void getValue(SbMatd &m) const;
00890
00891 #ifdef _WIN32
00892
00896 SbMatd &getValue() { SbMatd &rMat = matrix; return rMat; }
00897 #endif
00898
00902 const SbMatd &getValue() const { return matrix; }
00903
00908 double det3(int r1, int r2, int r3, int c1, int c2, int c3) const;
00909
00913 double det3() const { return det3(0, 1, 2, 0, 1, 2); }
00914
00918 double det4() const;
00919
00926 SbBool factor(SbMatrixd &r, SbVec3d &s, SbMatrixd &u,
00927 SbVec3d &t, SbMatrixd &proj) const;
00928
00934 SbMatrixd inverse() const;
00935
00937 void translate(const SbVec3d& translation);
00938
00940 void scale(const SbVec3d& scaleFactor);
00941
00946 SbBool LUDecomposition(int index[4], double &d);
00947
00952 void LUBackSubstitution(int index[4], double b[4]) const;
00953
00958 SbMatrixd transpose() const;
00959
00960
00961
00962
00967 SbMatrixd &multRight(const SbMatrixd &m);
00968
00973 SbMatrixd &multLeft(const SbMatrixd &m);
00974
00982 void multMatrixVec(const SbVec3d &src, SbVec3d &dst) const;
00983
00994 void multVecMatrix(const SbVec3d &src, SbVec3d &dst) const;
00995
01008 void multDirMatrix(const SbVec3d &src, SbVec3d &dst) const;
01009
01015 void multLineMatrix(const SbLined &src, SbLined &dst) const;
01016
01017
01018
01019
01023 void print(FILE *fp) const;
01024
01025
01029 operator double *() { return &matrix[0][0]; }
01030
01034 operator SbMatd &() { return matrix; }
01035
01039 double * operator [](int i) { return &matrix[i][0]; }
01040
01044 const double * operator [](int i) const { return &matrix[i][0]; }
01045
01049 SbMatrixd & operator =(const SbMatd &m);
01050
01054 SbMatrixd & operator =(const SbMatrixd &m);
01055
01059 SbMatrixd & operator =(const SbRotationd &q) { setRotate(q); return *this; }
01060
01065 SbMatrixd & operator *=(const SbMatrixd &m) { return multRight(m); }
01066
01070 friend SbMatrixd operator *(const SbMatrixd &m1, const SbMatrixd &m2);
01071
01075 friend int operator ==(const SbMatrixd &m1, const SbMatrixd &m2);
01079 friend int operator !=(const SbMatrixd &m1, const SbMatrixd &m2)
01080 { return !(m1 == m2); }
01081
01085 SbBool equals(const SbMatrixd &m, double tolerance) const;
01086
01090 bool isInvertible() const;
01091
01092 private:
01093
01097 void jacobi3(double evalues[3], SbVec3d evectors[3], int &rots) const;
01098
01099 private:
01100
01101 SbMatd matrix;
01102
01103 SbBool affine_inverse(const SbMatrixd &in, SbMatrixd &out) const;
01104
01105
01106 };
01107
01109
01110
01111
01112
01113
01115
01116
01117
01153 class SbMatrix3 {
01154 public:
01155
01159 SbMatrix3() { }
01160
01164 SbMatrix3(float a11, float a12, float a13,
01165 float a21, float a22, float a23,
01166 float a31, float a32, float a33);
01167
01171 SbMatrix3(const SbMat3 &m);
01172
01176 void setValue(const SbMat3 &m);
01177
01182 void setValue(const float *pMat)
01183 {
01184 matrix[0][0] = *pMat++ ;
01185 matrix[0][1] = *pMat++ ;
01186 matrix[0][2] = *pMat++ ;
01187
01188 matrix[1][0] = *pMat++ ;
01189 matrix[1][1] = *pMat++ ;
01190 matrix[1][2] = *pMat++ ;
01191
01192 matrix[2][0] = *pMat++ ;
01193 matrix[2][1] = *pMat++ ;
01194 matrix[2][2] = *pMat++ ;
01195 } ;
01196
01200 void makeIdentity();
01201
01205 static SbMatrix3 identity();
01206
01210 void setRotate(const SbRotation &q);
01211
01215 void setScale(float s);
01216
01220 void setScale(const SbVec3f &s);
01221
01225 void getValue(SbMat3 &m) const;
01226
01230 const SbMat3 &getValue() const { return matrix; }
01231
01236 SbMatrix3 &multRight(const SbMatrix3 &m);
01237
01242 SbMatrix3 &multLeft(const SbMatrix3 &m);
01243
01248 void multMatrixVec(const SbVec3f &src, SbVec3f &dst) const;
01249
01254 void multVecMatrix(const SbVec3f &src, SbVec3f &dst) const;
01255
01259 void print(FILE *fp) const;
01260
01264 operator float *() { return &matrix[0][0]; }
01265
01269 operator SbMat3 &() { return matrix; }
01270
01274 float * operator [](int i) { return &matrix[i][0]; }
01275
01279 const float * operator [](int i) const { return &matrix[i][0]; }
01280
01284 SbMatrix3 & operator =(const SbMat3 &m);
01285
01289 SbMatrix3 & operator =(const SbMatrix3 &m);
01290
01294 SbMatrix3 & operator =(const SbRotation &q) { setRotate(q); return *this; }
01295
01300 SbMatrix3 & operator *=(const SbMatrix3 &m) { return multRight(m); }
01301
01305 friend SbMatrix3 operator *(const SbMatrix3 &m1, const SbMatrix3 &m2);
01306
01310 friend int operator ==(const SbMatrix3 &m1, const SbMatrix3 &m2);
01314 friend int operator !=(const SbMatrix3 &m1, const SbMatrix3 &m2)
01315 { return !(m1 == m2); }
01316
01317 private:
01318 SbMat3 matrix;
01319 };
01320
01321 inline std::ostream& operator << (std::ostream& os, const SbMatrix& mat)
01322 {
01323 for ( int j = 0; j < 4; j++ )
01324 {
01325 os << "[ ";
01326 for ( int i = 0; i < 4; i++ )
01327 os << mat[j][i] << " ";
01328 os << "]" << std::endl;
01329 }
01330 return os;
01331 }
01332
01333 #endif
01334
01335
01336