OSGQuaternion.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2003 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 _OSGQUATERNION_H_
00040 #define _OSGQUATERNION_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044 
00045 #include "OSGBaseFunctions.h"
00046 
00047 #include "OSGVectorFwd.h"
00048 #include "OSGMatrixFwd.h"
00049 
00050 #include <iostream>
00051 #include <iomanip>
00052 
00053 OSG_BEGIN_NAMESPACE
00054 
00058 template <class ValueTypeT>
00059 class QuaternionBase
00060 {
00061     /*==========================  PUBLIC  =================================*/
00062 
00063   public:
00064 
00065     /*---------------------------------------------------------------------*/
00069     typedef VectorInterface     <ValueTypeT,
00070                                  VecStorage3<ValueTypeT> > VectorType;
00071     typedef TransformationMatrix<ValueTypeT>               MatrixType;
00072 
00073     typedef ValueTypeT                                     ValueType;
00074 
00075     static const UInt32 _uiSize = 4;
00076 
00078     /*---------------------------------------------------------------------*/
00082     static const QuaternionBase &identity(void                          );
00083 
00084     static       QuaternionBase slerp    (const QuaternionBase &rot0,
00085                                           const QuaternionBase &rot1,
00086                                           const ValueTypeT      t       );
00087 
00089     /*---------------------------------------------------------------------*/
00093                 QuaternionBase(      void                               );
00094                 QuaternionBase(const QuaternionBase &source             );
00095     explicit    QuaternionBase(const MatrixType     &matrix             );
00096                 QuaternionBase(const VectorType     &axis,
00097                                const ValueTypeT      angle              );
00098                 QuaternionBase(const VectorType     &rotateFrom,
00099                                const VectorType     &rotateTo           );
00100 
00102     /*---------------------------------------------------------------------*/
00106     virtual ~QuaternionBase(void);
00107 
00109     /*---------------------------------------------------------------------*/
00113     void setIdentity      (       void                  );
00114 
00115     void setValueAsAxisRad(const  ValueTypeT *valsP     );
00116     void setValueAsAxisDeg(const  ValueTypeT *valsP     );
00117     void setValueAsQuat   (const  ValueTypeT *valsP     );
00118 
00119     void setValueAsAxisRad(const  ValueTypeT  x,
00120                            const  ValueTypeT  y,
00121                            const  ValueTypeT  z,
00122                            const  ValueTypeT  w         );
00123     void setValueAsAxisDeg(const  ValueTypeT  x,
00124                            const  ValueTypeT  y,
00125                            const  ValueTypeT  z,
00126                            const  ValueTypeT  w         );
00127     void setValueAsQuat   (const  ValueTypeT  x,
00128                            const  ValueTypeT  y,
00129                            const  ValueTypeT  z,
00130                            const  ValueTypeT  w         );
00131 
00132     void setValue         (const  MatrixType &matrix    );
00133 
00134     void setValueAsAxisRad(const  VectorType &axis,    
00135                                   ValueTypeT  angle     );
00136     void setValueAsAxisDeg(const  VectorType &axis,
00137                                   ValueTypeT  angle     );
00138 
00139     void setValue         (const  VectorType &rotateFrom,
00140                            const  VectorType &rotateTo  );
00141 
00142     void setValueAsAxisRad(const  Char8       *str      );
00143     void setValueAsAxisDeg(const  Char8       *str      );
00144     void setValueAsQuat   (const  Char8       *str      );
00145 
00146     void setValue         (const  ValueTypeT alpha,
00147                            const  ValueTypeT beta,
00148                            const  ValueTypeT gamma      );
00149 
00150 
00151     void setValueFromCString(const Char8          *szString);
00152     void setValueFromCString(      Char8          *szString);
00153 
00154 #ifndef OSG_DISABLE_DEPRECATED
00155     void setValue           (const Char8          *szString);
00156     void setValue           (      Char8          *szString);
00157 #endif
00158 
00160     /*---------------------------------------------------------------------*/
00164     const ValueTypeT *getValues        (void               ) const;
00165 
00166           void        getValueAsAxisDeg(ValueTypeT &x,
00167                                         ValueTypeT &y,
00168                                         ValueTypeT &z,
00169                                         ValueTypeT &w      ) const;
00170           void        getValueAsAxisRad(ValueTypeT &x,
00171                                         ValueTypeT &y,
00172                                         ValueTypeT &z,
00173                                         ValueTypeT &w      ) const;
00174           void        getValueAsQuat   (ValueTypeT &x,
00175                                         ValueTypeT &y,
00176                                         ValueTypeT &z,
00177                                         ValueTypeT &w      ) const;
00178 
00179           void       getValueAsAxisRad (VectorType &axis, 
00180                                         ValueTypeT &radians) const;
00181           void       getValueAsAxisDeg (VectorType &axis, 
00182                                         ValueTypeT &degrees) const;
00183           void       getValue          (MatrixType &matrix ) const;
00184           void       getValuesOnly     (MatrixType &matrix ) const;
00185 
00186           ValueTypeT x                 (void               ) const;
00187           ValueTypeT y                 (void               ) const;
00188           ValueTypeT z                 (void               ) const;
00189           ValueTypeT w                 (void               ) const;
00190 
00192     /*---------------------------------------------------------------------*/
00196           ValueTypeT      length    (void                        ) const;
00197           void            normalize (void                        );
00198 
00199           void            invert    (void                        );
00200     const QuaternionBase  inverse   (void                        ) const;
00201 
00202           void            multVec   (const VectorType &src,
00203                                            VectorType &dst       ) const;
00204 
00205           void            scaleAngle(      ValueTypeT scaleFactor);
00206 
00207           void            slerpThis (const QuaternionBase &rot0,
00208                                      const QuaternionBase &rot1,
00209                                      const ValueTypeT      t     );
00210 
00211           void            mult      (const QuaternionBase &other );
00212           void            multLeft  (const QuaternionBase &other );
00213 
00214           bool            equals    (const QuaternionBase &rot,
00215                                      const ValueTypeT tolerance  ) const;
00216 
00218     /*---------------------------------------------------------------------*/
00222           ValueTypeT &operator [](const UInt32 index);
00223     const ValueTypeT &operator [](const UInt32 index) const;
00224 
00226     /*---------------------------------------------------------------------*/
00230     void operator *=(const QuaternionBase &other);
00231 
00233     /*---------------------------------------------------------------------*/
00237     const QuaternionBase& operator = (const QuaternionBase &source);
00238 
00240     /*---------------------------------------------------------------------*/
00244     bool operator == (const QuaternionBase &other) const;
00245     bool operator != (const QuaternionBase &other) const;
00246 
00248     /*=========================  PROTECTED  ===============================*/
00249 
00250   protected:
00251 
00252     static void slerp(const QuaternionBase &rot0,
00253                       const QuaternionBase &rot1,
00254                             QuaternionBase &result,
00255                       const ValueTypeT      t);
00256 
00257            void mult (const ValueTypeT rVal1[4],
00258                       const ValueTypeT rVal2[4]);
00259 
00260     /*==========================  PRIVATE  ================================*/
00261 
00262   private:
00263 
00264     enum ElementIndices
00265     {
00266         Q_X = 0,
00267         Q_Y = 1,
00268         Q_Z = 2,
00269         Q_W = 3
00270     };
00271 
00272     static QuaternionBase _identity;
00273 
00274            ValueTypeT     _quat[4];
00275 };
00276 
00277 template <class ValueTypeT> 
00278 std::ostream &operator <<(      std::ostream               &os, 
00279                           const QuaternionBase<ValueTypeT> &obj);
00280 
00286 typedef QuaternionBase<Real32> Quaternion;
00287 
00293 typedef QuaternionBase<Fixed32> Quaternionfx;
00294 
00295 #ifdef OSG_FLOAT_PROFILE
00296 
00302 typedef QuaternionBase<Real32> Quaternionr;
00303 
00304 #else
00305 
00311 typedef QuaternionBase<Fixed32> Quaternionr;
00312 
00313 #endif
00314 
00315 OSG_END_NAMESPACE
00316 
00317 #include "OSGQuaternion.inl"
00318 
00319 #endif /* _OSGQUATERNION_H_ */