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 
00071 template<class ValueTypeT>
00072 class TransformationMatrix
00073 {
00074     /*==========================  PUBLIC  =================================*/
00075 
00076   public:
00077 
00078     /*---------------------------------------------------------------------*/
00082     typedef                 ValueTypeT                ValueType;
00083     typedef VectorInterface<ValueTypeT,
00084                             VecStorage4<ValueTypeT> > VectorType;
00085 
00086     typedef QuaternionBase <ValueType>                QuaternionType;
00087 
00088     typedef VectorInterface<ValueTypeT,
00089                             VecStorage3<ValueTypeT> > VectorType3f;
00090 
00091     typedef PointInterface<ValueTypeT,
00092                            VecStorage3<ValueTypeT>  > PointType3f;
00093 
00095     /*---------------------------------------------------------------------*/
00099     static const TransformationMatrix &identity(void);
00100 
00102     /*---------------------------------------------------------------------*/
00106     TransformationMatrix(void);
00107     TransformationMatrix(const TransformationMatrix &source );
00108 
00109     TransformationMatrix(const VectorType3f         &vector1,
00110                          const VectorType3f         &vector2,
00111                          const VectorType3f         &vector3);
00112 
00113     TransformationMatrix(const VectorType3f         &vector1,
00114                          const VectorType3f         &vector2,
00115                          const VectorType3f         &vector3,
00116                          const VectorType3f         &vector4);
00117 
00118     TransformationMatrix(const ValueTypeT            rVal00,
00119                          const ValueTypeT            rVal10,
00120                          const ValueTypeT            rVal20,
00121                          const ValueTypeT            rVal30,
00122 
00123                          const ValueTypeT            rVal01,
00124                          const ValueTypeT            rVal11,
00125                          const ValueTypeT            rVal21,
00126                          const ValueTypeT            rVal31,
00127 
00128                          const ValueTypeT            rVal02,
00129                          const ValueTypeT            rVal12,
00130                          const ValueTypeT            rVal22,
00131                          const ValueTypeT            rVal32,
00132 
00133                          const ValueTypeT            rVal03,
00134                          const ValueTypeT            rVal13,
00135                          const ValueTypeT            rVal23,
00136                          const ValueTypeT            rVal33);
00137 
00139     /*---------------------------------------------------------------------*/
00143     ~TransformationMatrix(void);
00144 
00146     /*---------------------------------------------------------------------*/
00150     void setIdentity       (void                                           );
00151 
00152     void setValue          (const TransformationMatrix &mat                );
00153     
00154     template<class ValueTypeR>
00155     void convertFrom       (const TransformationMatrix<ValueTypeR>& mat    );
00156 
00157     void setValue          (const VectorType3f         &vector1,
00158                             const VectorType3f         &vector2,
00159                             const VectorType3f         &vector3            );
00160 
00161     void setValue          (const VectorType3f         &vector1,
00162                             const VectorType3f         &vector2,
00163                             const VectorType3f         &vector3,
00164                             const VectorType3f         &vector4            );
00165 
00166     void setValue          (const ValueTypeT            rVal00,
00167                             const ValueTypeT            rVal10,
00168                             const ValueTypeT            rVal20,
00169                             const ValueTypeT            rVal30,
00170 
00171                             const ValueTypeT            rVal01,
00172                             const ValueTypeT            rVal11,
00173                             const ValueTypeT            rVal21,
00174                             const ValueTypeT            rVal31,
00175 
00176                             const ValueTypeT            rVal02,
00177                             const ValueTypeT            rVal12,
00178                             const ValueTypeT            rVal22,
00179                             const ValueTypeT            rVal32,
00180 
00181                             const ValueTypeT            rVal03,
00182                             const ValueTypeT            rVal13,
00183                             const ValueTypeT            rVal23,
00184                             const ValueTypeT            rVal33             );
00185 
00186     void setValueTransposed(const ValueTypeT            rVal00,
00187                             const ValueTypeT            rVal01,
00188                             const ValueTypeT            rVal02,
00189                             const ValueTypeT            rVal03,
00190 
00191                             const ValueTypeT            rVal10,
00192                             const ValueTypeT            rVal11,
00193                             const ValueTypeT            rVal12,
00194                             const ValueTypeT            rVal13,
00195 
00196                             const ValueTypeT            rVal20,
00197                             const ValueTypeT            rVal21,
00198                             const ValueTypeT            rVal22,
00199                             const ValueTypeT            rVal23,
00200 
00201                             const ValueTypeT            rVal30,
00202                             const ValueTypeT            rVal31,
00203                             const ValueTypeT            rVal32,
00204                             const ValueTypeT            rVal33             );
00205 
00206     void setValue          (const ValueTypeT           *pMat,
00207                                   bool                  bTransposed = true );
00208 
00209     void setValue          (const VectorType           *pMat               );
00210 
00211 #ifndef WIN32
00212     void setValue          (const VectorType3f         *pMat               );
00213 #endif
00214 
00215     void setValue           (const Char8               *string,
00216                                    bool                 bTransposed = true );
00217     
00219     /*---------------------------------------------------------------------*/
00223           ValueTypeT *getValues(void);
00224     const ValueTypeT *getValues(void) const;
00225 
00227     /*---------------------------------------------------------------------*/
00231     void setScale    (const ValueTypeT      s               );
00232 
00233     void setScale    (const ValueTypeT      sx,
00234                       const ValueTypeT      sy,
00235                       const ValueTypeT      sz              );
00236 
00237     void setScale    (const VectorType3f   &s               );
00238 
00239 
00240     void setTranslate(const ValueTypeT      tx,
00241                       const ValueTypeT      ty,
00242                       const ValueTypeT      tz              );
00243 
00244     void setTranslate(const VectorType3f   &t               );
00245 
00246     void setTranslate(const PointType3f    &t               );
00247 
00248 
00249     void setRotate   (const QuaternionType &q               );
00250 
00251 
00252     void setTransform(const VectorType3f   &t               );
00253 
00254     void setTransform(const QuaternionType &r               );
00255 
00256     void setTransform(const VectorType3f   &t,
00257                       const QuaternionType &r               );
00258 
00259     void setTransform(const VectorType3f   &t,
00260                       const QuaternionType &r,
00261                       const VectorType3f   &s               );
00262 
00263     void setTransform(const VectorType3f   &t,
00264                       const QuaternionType &r,
00265                       const VectorType3f   &s,
00266                       const QuaternionType &so              );
00267 
00268     void setTransform(const VectorType3f   &translation,
00269                       const QuaternionType &rotation,
00270                       const VectorType3f   &scaleFactor,
00271                       const QuaternionType &scaleOrientation,
00272                       const VectorType3f   &center          );
00273 
00275     /*---------------------------------------------------------------------*/
00279     void getTransform(      VectorType3f         &translation,
00280                             QuaternionType       &rotation,
00281                             VectorType3f         &scaleFactor,
00282                             QuaternionType       &scaleOrientation,
00283                       const VectorType3f         &center) const;
00284 
00285     void getTransform(      VectorType3f         &translation,
00286                             QuaternionType       &rotation,
00287                             VectorType3f         &scaleFactor,
00288                             QuaternionType       &scaleOrientation) const;
00289 
00290     bool factor      (      TransformationMatrix &r,
00291                             VectorType3f         &s,
00292                             TransformationMatrix &u,
00293                             VectorType3f         &t,
00294                             TransformationMatrix &proj) const;
00295 
00297     /*---------------------------------------------------------------------*/
00301     void multMatrixPnt    (const PointType3f  &src,
00302                                  PointType3f  &dst) const;
00303     void multMatrixPnt    (      PointType3f  &pnt) const;
00304     void multFullMatrixPnt(const PointType3f  &src,
00305                                  PointType3f  &dst) const;
00306     void multFullMatrixPnt(      PointType3f  &pnt) const;
00307 
00308     void multMatrixVec    (const VectorType3f &src,
00309                                  VectorType3f &dst) const;
00310     void multMatrixVec    (      VectorType3f &vec) const;
00311 
00312     void mult             (const PointType3f  &src,
00313                                  PointType3f  &dst) const;
00314     void mult             (      PointType3f  &vec) const;
00315     void mult             (const VectorType3f &src,
00316                                  VectorType3f &dst) const;
00317     void mult             (      VectorType3f &vec) const;
00318 
00319 
00320     void multPntMatrix    (const PointType3f  &src,
00321                                  PointType3f  &dst) const;
00322     void multPntMatrix    (      PointType3f  &pnt) const;
00323     void multPntFullMatrix(const PointType3f  &src,
00324                                  PointType3f  &dst) const;
00325     void multPntFullMatrix(      PointType3f  &pnt) const;
00326 
00327     void multVecMatrix    (const VectorType3f &src,
00328                                  VectorType3f &dst) const;
00329     void multVecMatrix    (      VectorType3f &vec) const;
00330 
00331 
00332     void multMatrixVec    (const VectorType &src,
00333                                  VectorType &dst) const;
00334     void multMatrixVec    (      VectorType &vec) const;
00335 
00337     /*---------------------------------------------------------------------*/
00341     bool       equals       (const TransformationMatrix &matrix,
00342                              const ValueType             tol   ) const;
00343 
00344     ValueTypeT det3         (      void                        ) const;
00345     ValueTypeT det          (      void                        ) const;
00346 
00347     bool       inverse      (      TransformationMatrix &result) const;
00348     bool       invert       (      void                        );
00349     bool       invertFrom   (const TransformationMatrix &matrix);
00350 
00351     bool       inverse3     (      TransformationMatrix &result) const;
00352     bool       invert3      (      void                        );
00353     bool       invertFrom3  (const TransformationMatrix &matrix);
00354 
00355     bool       transposed   (      TransformationMatrix &result) const;
00356     bool       transpose    (      void                        );
00357     bool       transposeFrom(const TransformationMatrix &matrix);
00358 
00359     void       mult         (const TransformationMatrix &matrix);
00360     void       multLeft     (const TransformationMatrix &matrix);
00361 
00362     void       add          (const TransformationMatrix &matrix);
00363     void       scale        (      ValueTypeT            s     );
00364     void       addScaled    (const TransformationMatrix &matrix,
00365                                    ValueTypeT            s     );
00366     void       negate       (      void                        );
00367 
00368     ValueTypeT norm1        (      void                        ) const;
00369     ValueTypeT norm2        (      void                        ) const;
00370     ValueTypeT normInfinity (      void                        ) const;
00371 
00372     bool       sqrt         (      TransformationMatrix &result) const;
00373     bool       sqrtOf       (const TransformationMatrix &matrix);
00374     bool       sqrt         (      void                        );
00375 
00376     bool       log          (      TransformationMatrix &result) const;
00377     bool       logOf        (const TransformationMatrix &matrix);
00378 
00379     bool       exp          (      TransformationMatrix &result) const;
00380     bool       expOf        (const TransformationMatrix &matrix);
00381 
00383     /*---------------------------------------------------------------------*/
00387           VectorType &operator [](UInt32 uiIndex);
00388     const VectorType &operator [](UInt32 uiIndex) const;
00389 
00391     /*---------------------------------------------------------------------*/
00395     TransformationMatrix &operator =(const TransformationMatrix &source);
00396 
00398     /*---------------------------------------------------------------------*/
00402     bool operator == (const TransformationMatrix &other) const;
00403     bool operator != (const TransformationMatrix &other) const;
00404 
00406     /*=========================  PROTECTED  ===============================*/
00407 
00408   protected:
00409 
00410     /*---------------------------------------------------------------------*/
00414     VectorType _matrix[4];
00415 
00417     /*---------------------------------------------------------------------*/
00421     ValueTypeT rowMulCol4(const TransformationMatrix &gRowMat,
00422                                 UInt32                iRow,
00423                           const TransformationMatrix &gColMat,
00424                                 UInt32                iColumn) const;
00425 
00426     ValueTypeT det2_calc (const ValueTypeT            a1,
00427                           const ValueTypeT            a2,
00428                           const ValueTypeT            b1,
00429                           const ValueTypeT            b2     ) const;
00430     ValueTypeT det3_calc (const ValueTypeT            a1,
00431                           const ValueTypeT            a2,
00432                           const ValueTypeT            a3,
00433                           const ValueTypeT            b1,
00434                           const ValueTypeT            b2,
00435                           const ValueTypeT            b3,
00436                           const ValueTypeT            c1,
00437                           const ValueTypeT            c2,
00438                           const ValueTypeT            c3     ) const;
00439 
00440 #ifdef __sgi
00441 #pragma set woff 1424
00442 #endif
00443 
00444 #if !defined(OSG_MICROSOFT_COMPILER_HACKS) && !defined(__sun)
00445     static const UInt32 JacobiRank = 3;
00446 #endif
00447 
00448     bool jacobi(ValueTypeT    evalues [JacobiRank],
00449                 VectorType3f  evectors[JacobiRank],
00450                 Int32        &rots);
00451 
00452 #ifdef __sgi
00453 #pragma reset woff 1424
00454 #endif
00455 
00457     /*==========================  PRIVATE  ================================*/
00458 
00459   private:
00460 
00461     static TransformationMatrix _identityMatrix;
00462 };
00463 
00469 typedef TransformationMatrix<Real32> Matrix4f;
00470 
00476 typedef TransformationMatrix<Real64> Matrix4d;
00477 
00483 typedef TransformationMatrix<Fixed32> Matrix4fx;
00484 
00490 typedef TransformationMatrix<Real32 > Matrix;
00491 
00497 typedef TransformationMatrix<Fixed32> Matrix4fx;
00498 
00499 #ifdef OSG_FLOAT_PROFILE
00500 
00506 typedef Matrix4f Matrixr;
00507 
00508 #else
00509 
00515 typedef Matrix4fx Matrixr;
00516 
00517 #endif
00518 
00519 template<class ValueTypeT>
00520 std::ostream &operator <<(      std::ostream                     &os,
00521                           const TransformationMatrix<ValueTypeT> &obj);
00522 
00523 OSG_END_NAMESPACE
00524 
00525 #include "OSGMatrix.inl"
00526 
00527 #endif /* _OSGMATRIX_H_ */
00528 
00529