OSGMatrix.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 #ifndef _OSGMATRIX_H_
00040 #define _OSGMATRIX_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044 
00045
00046 #include <cstdlib>
00047 #include <cstdio>
00048
00049 #include <iostream>
00050 #include <iomanip>
00051
00052 #include "OSGLog.h"
00053 #include "OSGVector.h"
00054
00055 OSG_BEGIN_NAMESPACE
00056
00057 #if defined(OSG_MICROSOFT_COMPILER_HACKS) || defined(__sun)
00058 static const UInt32 JacobiRank = 3;
00059 #endif
00060 
00061 template <class ValueTypeT> class QuaternionBase;
00062
00074 template<class ValueTypeT>
00075 class TransformationMatrix
00076 {
00077     /*==========================  PUBLIC  =================================*/
00078
00079   public:
00080
00081     /*---------------------------------------------------------------------*/
00085     typedef TransformationMatrix<ValueTypeT>    Self;
00086
00087     typedef                      ValueTypeT     ValueType;
00088     typedef Vector              <ValueTypeT, 4> VectorType;
00089
00090     typedef QuaternionBase      <ValueType>     QuaternionType;
00091
00092     typedef Vector              <ValueTypeT, 3> VectorType3f;
00093
00094     typedef Point               <ValueTypeT, 4> PointType;
00095     typedef Point               <ValueTypeT, 3> PointType3f;
00096
00098     /*---------------------------------------------------------------------*/
00102     static const TransformationMatrix &identity(void);
00103
00105     /*---------------------------------------------------------------------*/
00109     TransformationMatrix(void);
00110     TransformationMatrix(const TransformationMatrix &source );
00111
00112     TransformationMatrix(const VectorType3f         &vector1,
00113                          const VectorType3f         &vector2,
00114                          const VectorType3f         &vector3);
00115
00116     TransformationMatrix(const VectorType           &vector1,
00117                          const VectorType           &vector2,
00118                          const VectorType           &vector3,
00119                          const VectorType           &vector4 );
00120
00121     TransformationMatrix(const VectorType3f         &vector1,
00122                          const VectorType3f         &vector2,
00123                          const VectorType3f         &vector3,
00124                          const VectorType3f         &vector4);
00125
00126     TransformationMatrix(const ValueTypeT            rVal00,
00127                          const ValueTypeT            rVal10,
00128                          const ValueTypeT            rVal20,
00129                          const ValueTypeT            rVal30,
00130
00131                          const ValueTypeT            rVal01,
00132                          const ValueTypeT            rVal11,
00133                          const ValueTypeT            rVal21,
00134                          const ValueTypeT            rVal31,
00135
00136                          const ValueTypeT            rVal02,
00137                          const ValueTypeT            rVal12,
00138                          const ValueTypeT            rVal22,
00139                          const ValueTypeT            rVal32,
00140
00141                          const ValueTypeT            rVal03,
00142                          const ValueTypeT            rVal13,
00143                          const ValueTypeT            rVal23,
00144                          const ValueTypeT            rVal33);
00145
00147     /*---------------------------------------------------------------------*/
00151     ~TransformationMatrix(void);
00152
00154     /*---------------------------------------------------------------------*/
00158     void setIdentity       (void                                           );
00159
00160     void setValue          (const TransformationMatrix &mat                );
00161
00162     template<class ValueTypeR>
00163     void convertFrom       (const TransformationMatrix<ValueTypeR>& mat    );
00164
00165     void setValue          (const VectorType3f         &vector1,
00166                             const VectorType3f         &vector2,
00167                             const VectorType3f         &vector3            );
00168
00169     void setValue          (const VectorType3f         &vector1,
00170                             const VectorType3f         &vector2,
00171                             const VectorType3f         &vector3,
00172                             const VectorType3f         &vector4            );
00173
00174     void setValue          (const ValueTypeT            rVal00,
00175                             const ValueTypeT            rVal10,
00176                             const ValueTypeT            rVal20,
00177                             const ValueTypeT            rVal30,
00178
00179                             const ValueTypeT            rVal01,
00180                             const ValueTypeT            rVal11,
00181                             const ValueTypeT            rVal21,
00182                             const ValueTypeT            rVal31,
00183
00184                             const ValueTypeT            rVal02,
00185                             const ValueTypeT            rVal12,
00186                             const ValueTypeT            rVal22,
00187                             const ValueTypeT            rVal32,
00188
00189                             const ValueTypeT            rVal03,
00190                             const ValueTypeT            rVal13,
00191                             const ValueTypeT            rVal23,
00192                             const ValueTypeT            rVal33             );
00193
00194     void setValueTransposed(const ValueTypeT            rVal00,
00195                             const ValueTypeT            rVal01,
00196                             const ValueTypeT            rVal02,
00197                             const ValueTypeT            rVal03,
00198
00199                             const ValueTypeT            rVal10,
00200                             const ValueTypeT            rVal11,
00201                             const ValueTypeT            rVal12,
00202                             const ValueTypeT            rVal13,
00203
00204                             const ValueTypeT            rVal20,
00205                             const ValueTypeT            rVal21,
00206                             const ValueTypeT            rVal22,
00207                             const ValueTypeT            rVal23,
00208
00209                             const ValueTypeT            rVal30,
00210                             const ValueTypeT            rVal31,
00211                             const ValueTypeT            rVal32,
00212                             const ValueTypeT            rVal33             );
00213
00214     void setValue          (const ValueTypeT           *pMat,
00215                                   bool                  bTransposed = true );
00216
00217     void setValue          (const VectorType           *pMat               );
00218
00219     void setValue          (const VectorType3f         *pMat               );
00220
00221     void setValue           (const Char8               *string,
00222                                    bool                 bTransposed = true );
00223
00225     /*---------------------------------------------------------------------*/
00229           ValueTypeT *getValues(void);
00230     const ValueTypeT *getValues(void) const;
00231
00233     /*---------------------------------------------------------------------*/
00237     void setScale    (const ValueTypeT      s               );
00238
00239     void setScale    (const ValueTypeT      sx,
00240                       const ValueTypeT      sy,
00241                       const ValueTypeT      sz              );
00242
00243     void setScale    (const VectorType3f   &s               );
00244
00245
00246     void setTranslate(const ValueTypeT      tx,
00247                       const ValueTypeT      ty,
00248                       const ValueTypeT      tz              );
00249
00250     void setTranslate(const VectorType3f   &t               );
00251
00252     void setTranslate(const PointType3f    &t               );
00253
00254
00255     void setRotate   (const QuaternionType &q               );
00256
00257
00258     void setTransform(const VectorType3f   &t               );
00259
00260     void setTransform(const QuaternionType &r               );
00261
00262     void setTransform(const VectorType3f   &t,
00263                       const QuaternionType &r               );
00264
00265     void setTransform(const VectorType3f   &t,
00266                       const QuaternionType &r,
00267                       const VectorType3f   &s               );
00268
00269     void setTransform(const VectorType3f   &t,
00270                       const QuaternionType &r,
00271                       const VectorType3f   &s,
00272                       const QuaternionType &so              );
00273
00274     void setTransform(const VectorType3f   &translation,
00275                       const QuaternionType &rotation,
00276                       const VectorType3f   &scaleFactor,
00277                       const QuaternionType &scaleOrientation,
00278                       const VectorType3f   &center          );
00279
00281     /*---------------------------------------------------------------------*/
00285     void getTransform(      VectorType3f         &translation,
00286                             QuaternionType       &rotation,
00287                             VectorType3f         &scaleFactor,
00288                             QuaternionType       &scaleOrientation,
00289                       const VectorType3f         &center) const;
00290
00291     void getTransform(      VectorType3f         &translation,
00292                             QuaternionType       &rotation,
00293                             VectorType3f         &scaleFactor,
00294                             QuaternionType       &scaleOrientation) const;
00295
00296     bool factor      (      TransformationMatrix &r,
00297                             VectorType3f         &s,
00298                             TransformationMatrix &u,
00299                             VectorType3f         &t,
00300                             TransformationMatrix &proj) const;
00301
00303     /*---------------------------------------------------------------------*/
00307     void mult        (const PointType    &pntIn, PointType    &pntOut) const;
00308     void multFull    (const PointType3f  &pntIn, PointType3f  &pntOut) const;
00309     void mult        (const PointType3f  &pntIn, PointType3f  &pntOut) const;
00310
00311     void mult        (const VectorType   &vecIn, VectorType   &vecOut) const;
00312     void multFull    (const VectorType3f &vecIn, VectorType3f &vecOut) const;
00313     void mult        (const VectorType3f &vecIn, VectorType3f &vecOut) const;
00314
00315     void mult3x3     (const PointType3f  &pntIn, PointType3f  &pntOut) const;
00316     void mult3x3     (const VectorType3f &vecIn, VectorType3f &vecOut) const;
00317
00318     void multLeftFull(const PointType3f  &pntIn, PointType3f  &pntOut) const;
00319     void multLeft    (const PointType3f  &pntIn, PointType3f  &pntOut) const;
00320
00321     void multLeftFull(const VectorType3f &vecIn, VectorType3f &vecOut) const;
00322     void multLeft    (const VectorType3f &vecIn, VectorType3f &vecOut) const;
00323
00324     PointType    operator *(const PointType    &pntIn) const;
00325     PointType3f  operator *(const PointType3f  &pntIn) const;
00326
00327     VectorType   operator *(const VectorType   &vecIn) const;
00328     VectorType3f operator *(const VectorType3f &vecIn) const;
00329
00331     /*---------------------------------------------------------------------*/
00335     bool       equals       (const TransformationMatrix &matrix,
00336                              const ValueType             tol   ) const;
00337
00338     ValueTypeT det3         (      void                        ) const;
00339     ValueTypeT det          (      void                        ) const;
00340
00341     bool       inverse      (      TransformationMatrix &result) const;
00342     bool       invert       (      void                        );
00343     bool       invertFrom   (const TransformationMatrix &matrix);
00344
00345     bool       inverse3     (      TransformationMatrix &result) const;
00346     bool       invert3      (      void                        );
00347     bool       invertFrom3  (const TransformationMatrix &matrix);
00348
00349     bool       transposed   (      TransformationMatrix &result) const;
00350     bool       transpose    (      void                        );
00351     bool       transposeFrom(const TransformationMatrix &matrix);
00352
00353     template<class ValueTypeR>
00354     void       mult         (const TransformationMatrix<ValueTypeR>& mat);
00355     template<class ValueTypeR>
00356     void       multLeft     (const TransformationMatrix<ValueTypeR>& mat);
00357
00358     void       add          (const TransformationMatrix &matrix);
00359     void       scale        (      ValueTypeT            s     );
00360     void       addScaled    (const TransformationMatrix &matrix,
00361                                    ValueTypeT            s     );
00362     void       negate       (      void                        );
00363
00364     ValueTypeT norm1        (      void                        ) const;
00365     ValueTypeT norm2        (      void                        ) const;
00366     ValueTypeT normInfinity (      void                        ) const;
00367
00368     bool       sqrt         (      TransformationMatrix &result) const;
00369     bool       sqrtOf       (const TransformationMatrix &matrix);
00370     bool       sqrt         (      void                        );
00371
00372     bool       log          (      TransformationMatrix &result) const;
00373     bool       logOf        (const TransformationMatrix &matrix);
00374
00375     bool       exp          (      TransformationMatrix &result) const;
00376     bool       expOf        (const TransformationMatrix &matrix);
00377
00379     /*---------------------------------------------------------------------*/
00383           VectorType &operator [](UInt32 uiIndex);
00384     const VectorType &operator [](UInt32 uiIndex) const;
00385
00387     /*---------------------------------------------------------------------*/
00391     TransformationMatrix &operator =(const TransformationMatrix &source);
00392
00394     /*---------------------------------------------------------------------*/
00398     bool operator == (const TransformationMatrix &other) const;
00399     bool operator != (const TransformationMatrix &other) const;
00400
00402     /*---------------------------------------------------------------------*/
00406 #ifdef OSG_1_COMPAT
00407     void mult             (      PointType3f  &pnt) const;
00408     void multMatrixPnt    (      PointType3f  &pnt) const;
00409     void multMatrixPnt    (const PointType3f  &src,
00410                                  PointType3f  &dst) const;
00411     void multMatrixVec    (      VectorType3f &vec) const;
00412     void multMatrixVec    (const VectorType3f &src,
00413                                  VectorType3f &dst) const;
00414     void multFullMatrixPnt(      PointType3f  &pnt) const;
00415     void multFullMatrixPnt(const PointType3f  &src,
00416                                  PointType3f  &dst) const;
00417     void multPntFullMatrix(const PointType3f  &src,
00418                                  PointType3f  &dst) const;
00419 #endif
00420 
00422     /*=========================  PROTECTED  ===============================*/
00423
00424   protected:
00425
00426     /*---------------------------------------------------------------------*/
00430     VectorType _matrix[4];
00431
00433     /*---------------------------------------------------------------------*/
00437     template<class ValueTypeR, class ValueTypeS>
00438     ValueTypeT rowMulCol4(const TransformationMatrix<ValueTypeR> &gRowMat,
00439                                 UInt32                            iRow,
00440                           const TransformationMatrix<ValueTypeS> &gColMat,
00441                                 UInt32                            iColumn) const;
00442
00443     ValueTypeT det2_calc (const ValueTypeT            a1,
00444                           const ValueTypeT            a2,
00445                           const ValueTypeT            b1,
00446                           const ValueTypeT            b2     ) const;
00447     ValueTypeT det3_calc (const ValueTypeT            a1,
00448                           const ValueTypeT            a2,
00449                           const ValueTypeT            a3,
00450                           const ValueTypeT            b1,
00451                           const ValueTypeT            b2,
00452                           const ValueTypeT            b3,
00453                           const ValueTypeT            c1,
00454                           const ValueTypeT            c2,
00455                           const ValueTypeT            c3     ) const;
00456
00457
00458     bool       calcInverse (      TransformationMatrix *destM,
00459                             const TransformationMatrix *srcM  ) const;
00460     bool       calcInverse3(      TransformationMatrix *destM,
00461                             const TransformationMatrix *srcM  ) const;
00462
00463     // helper functions for matrix decomposition
00464     ValueType norm1_3x3   (void) const;
00465     ValueType normInf_3x3 (void) const;
00466
00467     void adjointT_3x3     (TransformationMatrix       &result) const;
00468     void polarDecompose   (TransformationMatrix       &Q,
00469                            TransformationMatrix       &S,
00470                            ValueType                  &det   ) const;
00471     void spectralDecompose(TransformationMatrix       &SO,
00472                            VectorType3f               &k     ) const;
00473     void decompose        (VectorType3f               &t,
00474                            ValueType                  &f,
00475                            QuaternionType             &r,
00476                            QuaternionType             &so,
00477                            VectorType3f               &s     ) const;
00478
00479 #ifdef __sgi
00480 #pragma set woff 1424
00481 #endif
00482 
00483 #if !defined(OSG_MICROSOFT_COMPILER_HACKS) && !defined(__sun)
00484     static const UInt32 JacobiRank = 3;
00485 #endif
00486 
00487     bool jacobi(ValueTypeT    evalues [JacobiRank],
00488                 VectorType3f  evectors[JacobiRank],
00489                 Int32        &rots);
00490
00491 #ifdef __sgi
00492 #pragma reset woff 1424
00493 #endif
00494 
00496     /*==========================  PRIVATE  ================================*/
00497
00498   private:
00499
00500     static TransformationMatrix _identityMatrix;
00501 };
00502
00503 /*---------------------------------------------------------------------*/
00512 typedef OSG::TransformationMatrix<OSG::Real32> Matrix4f;
00513
00514
00520 typedef OSG::TransformationMatrix<OSG::Real64> Matrix4d;
00521
00522
00528 typedef OSG::TransformationMatrix<OSG::Fixed32> Matrix4fx;
00529
00530
00536 typedef OSG::TransformationMatrix<OSG::Real32 > Matrix;
00537
00538
00544 typedef OSG::TransformationMatrix<OSG::Fixed32> Matrixfx;
00545
00547 /*---------------------------------------------------------------------*/
00551 #ifdef OSG_FLOAT_PROFILE
00552 
00558 typedef Matrix4f Matrixr;
00559
00560
00561 #else
00562 
00568 typedef Matrix4fx Matrixr;
00569
00570 #endif
00571 
00573 /*---------------------------------------------------------------------*/
00574
00577 template<class ValueTypeT>
00578 std::ostream &operator <<(      std::ostream                     &os,
00579                           const TransformationMatrix<ValueTypeT> &obj);
00580
00581 OSG_END_NAMESPACE
00582
00583 #include "OSGMatrix.inl"
00584
00585 #endif /* _OSGMATRIX_H_ */
00586
00587