VTK
dox/Common/Math/vtkQuaternion.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkQuaternion.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00032 #ifndef __vtkQuaternion_h
00033 #define __vtkQuaternion_h
00034 
00035 #include "vtkTuple.h"
00036 
00037 template<typename T> class vtkQuaternion : public vtkTuple<T, 4>
00038 {
00039 public:
00041   vtkQuaternion();
00042 
00044   explicit vtkQuaternion(const T& scalar) : vtkTuple<T, 4>(scalar) {}
00045 
00050   explicit vtkQuaternion(const T* init) : vtkTuple<T, 4>(init) {}
00051 
00053   vtkQuaternion(const T& w, const T& x, const T& y, const T& z);
00054 
00056   T SquaredNorm() const;
00057 
00059   T Norm() const;
00060 
00062   void ToIdentity();
00063 
00066   static vtkQuaternion<T> Identity();
00067 
00069   T Normalize();
00070 
00072   vtkQuaternion<T> Normalized() const;
00073 
00075   void Conjugate();
00076 
00078   vtkQuaternion<T> Conjugated() const;
00079 
00082   void Invert();
00083 
00085   vtkQuaternion<T> Inverse() const;
00086 
00089   void ToUnitLog();
00090 
00093   vtkQuaternion<T> UnitLog() const;
00094 
00098   void ToUnitExp();
00099 
00103   vtkQuaternion<T> UnitExp() const;
00104 
00107   void NormalizeWithAngleInDegrees();
00108 
00111   vtkQuaternion<T> NormalizedWithAngleInDegrees() const;
00112 
00114 
00115   void Set(const T& w, const T& x, const T& y, const T& z);
00116   void Set(T quat[4]);
00117   void Get(T quat[4]) const;
00119 
00121 
00122   void SetW(const T& w);
00123   const T& GetW() const;
00125 
00127 
00128   void SetX(const T& x);
00129   const T& GetX() const;
00131 
00133 
00134   void SetY(const T& y);
00135   const T& GetY() const;
00137 
00139 
00140   void SetZ(const T& z);
00141   const T& GetZ() const;
00143 
00145 
00147   T GetRotationAngleAndAxis(T axis[3]) const;
00148   void SetRotationAngleAndAxis(T angle, T axis[3]);
00149   void SetRotationAngleAndAxis(
00150     const T& angle, const T& x, const T& y, const T& z);
00152 
00154   template<typename CastTo> vtkQuaternion<CastTo> Cast() const;
00155 
00158   void ToMatrix3x3(T A[3][3]) const;
00159 
00163   void FromMatrix3x3(const T A[3][3]);
00164 
00169   vtkQuaternion<T> Slerp(T t, const vtkQuaternion<T>& q) const;
00170 
00172 
00174   vtkQuaternion<T> InnerPoint(const vtkQuaternion<T>& q1,
00175     const vtkQuaternion<T>& q2) const;
00177 
00179   void operator=(const vtkQuaternion<T>& q);
00180 
00182   vtkQuaternion<T> operator+(const vtkQuaternion<T>& q) const;
00183 
00185   vtkQuaternion<T> operator-(const vtkQuaternion<T>& q) const;
00186 
00188   vtkQuaternion<T> operator*(const vtkQuaternion<T>& q) const;
00189 
00191   vtkQuaternion<T> operator*(const T& scalar) const;
00192 
00195   void operator*=(const T& scalar) const;
00196 
00198   vtkQuaternion<T> operator/(const vtkQuaternion<T>& q) const;
00199 
00201   vtkQuaternion<T> operator/(const T& scalar) const;
00202 
00204 
00205   void operator/=(const T& scalar);
00206 };
00208 
00210 
00213 #define vtkQuaternionIdentity(quaternionType, type) \
00214 quaternionType Identity() const \
00215 { \
00216   return quaternionType(vtkQuaternion<type>::Identity().GetData()); \
00217 }
00218 #define vtkQuaternionNormalized(quaternionType, type) \
00219 quaternionType Normalized() const \
00220 { \
00221   return quaternionType(vtkQuaternion<type>::Normalized().GetData()); \
00222 }
00223 #define vtkQuaternionConjugated(quaternionType, type) \
00224 quaternionType Conjugated() const \
00225 { \
00226   return quaternionType(vtkQuaternion<type>::Conjugated().GetData()); \
00227 }
00228 #define vtkQuaternionInverse(quaternionType, type) \
00229 quaternionType Inverse() const \
00230 { \
00231   return quaternionType(vtkQuaternion<type>::Inverse().GetData()); \
00232 }
00233 #define vtkQuaternionUnitLog(quaternionType, type) \
00234 quaternionType UnitLog() const \
00235 { \
00236   return quaternionType( \
00237     vtkQuaternion<type>::UnitLog().GetData()); \
00238 }
00239 #define vtkQuaternionUnitExp(quaternionType, type) \
00240 quaternionType UnitExp() const \
00241 { \
00242   return quaternionType( \
00243     vtkQuaternion<type>::UnitExp().GetData()); \
00244 }
00245 #define vtkQuaternionNormalizedWithAngleInDegrees(quaternionType, type) \
00246 quaternionType NormalizedWithAngleInDegrees() const \
00247 { \
00248   return quaternionType( \
00249     vtkQuaternion<type>::NormalizedWithAngleInDegrees().GetData()); \
00250 }
00251 #define vtkQuaternionSlerp(quaternionType, type) \
00252 quaternionType Slerp(type t, const quaternionType& q) const \
00253 { \
00254   return quaternionType( \
00255     vtkQuaternion<type>::Slerp(t, q).GetData()); \
00256 }
00257 #define vtkQuaternionInnerPoint(quaternionType, type) \
00258 quaternionType InnerPoint(const quaternionType& q1, \
00259                           const quaternionType& q2) const \
00260 { \
00261   return quaternionType( \
00262     vtkQuaternion<type>::InnerPoint(q1, q2).GetData()); \
00263 }
00264 #define vtkQuaternionOperatorPlus(quaternionType, type) \
00265 inline quaternionType operator+(const quaternionType& q) const \
00266 { \
00267   return quaternionType( ( \
00268     static_cast< vtkQuaternion<type> > (*this) + \
00269     static_cast< vtkQuaternion<type> > (q)).GetData()); \
00270 }
00271 #define vtkQuaternionOperatorMinus(quaternionType, type) \
00272 inline quaternionType operator-(const quaternionType& q) const \
00273 { \
00274   return quaternionType( ( \
00275     static_cast< vtkQuaternion<type> > (*this) - \
00276     static_cast< vtkQuaternion<type> > (q)).GetData()); \
00277 }
00278 #define vtkQuaternionOperatorMultiply(quaternionType, type) \
00279 inline quaternionType operator*(const quaternionType& q) const \
00280 { \
00281   return quaternionType( ( \
00282     static_cast< vtkQuaternion<type> > (*this) * \
00283     static_cast< vtkQuaternion<type> > (q)).GetData()); \
00284 }
00285 #define vtkQuaternionOperatorMultiplyScalar(quaternionType, type) \
00286 inline quaternionType operator*(const type& scalar) const \
00287 { \
00288   return quaternionType( ( \
00289     static_cast< vtkQuaternion<type> > (*this) * \
00290     scalar).GetData()); \
00291 }
00292 #define vtkQuaternionOperatorDivide(quaternionType, type) \
00293 inline quaternionType operator/(const quaternionType& q) const \
00294 { \
00295   return quaternionType( ( \
00296     static_cast< vtkQuaternion<type> > (*this) / \
00297     static_cast< vtkQuaternion<type> > (q)).GetData()); \
00298 }
00299 #define vtkQuaternionOperatorDivideScalar(quaternionType, type) \
00300 inline quaternionType operator/(const type& scalar) const \
00301 { \
00302   return quaternionType( ( \
00303     static_cast< vtkQuaternion<type> > (*this) / \
00304     scalar).GetData()); \
00305 }
00306 
00307 
00308 #define vtkQuaternionOperatorMacro(quaternionType, type) \
00309 vtkQuaternionIdentity(quaternionType, type) \
00310 vtkQuaternionNormalized(quaternionType, type) \
00311 vtkQuaternionConjugated(quaternionType, type) \
00312 vtkQuaternionInverse(quaternionType, type) \
00313 vtkQuaternionUnitLog(quaternionType, type) \
00314 vtkQuaternionUnitExp(quaternionType, type) \
00315 vtkQuaternionNormalizedWithAngleInDegrees(quaternionType, type) \
00316 vtkQuaternionSlerp(quaternionType, type) \
00317 vtkQuaternionInnerPoint(quaternionType, type) \
00318 vtkQuaternionOperatorPlus(quaternionType, type) \
00319 vtkQuaternionOperatorMinus(quaternionType, type) \
00320 vtkQuaternionOperatorMultiply(quaternionType, type) \
00321 vtkQuaternionOperatorMultiplyScalar(quaternionType, type) \
00322 vtkQuaternionOperatorDivide(quaternionType, type) \
00323 vtkQuaternionOperatorDivideScalar(quaternionType, type)
00324 
00325 // .NAME vtkQuaternionf - Float quaternion type.
00326 //
00327 // .SECTION Description
00328 // This class is uses vtkQuaternion with float type data.
00329 // For futher description, see the templated class vtkQuaternion.
00330 // @sa vtkQuaterniond vtkQuaternion
00331 class vtkQuaternionf : public vtkQuaternion<float>
00332 {
00333 public:
00334   vtkQuaternionf() {}
00335   explicit vtkQuaternionf(float w, float x, float y, float z)
00336     : vtkQuaternion<float>(w, x, y, z) {}
00337   explicit vtkQuaternionf(float scalar) : vtkQuaternion<float>(scalar) {}
00338   explicit vtkQuaternionf(const float *init) : vtkQuaternion<float>(init) {}
00339   vtkQuaternionOperatorMacro(vtkQuaternionf, float)
00340 };
00341 
00342 // .NAME vtkQuaterniond - Double quaternion type.
00343 //
00344 // .SECTION Description
00345 // This class is uses vtkQuaternion with double type data.
00346 // For futher description, seethe templated class vtkQuaternion.
00347 // @sa vtkQuaternionf vtkQuaternion
00348 class vtkQuaterniond : public vtkQuaternion<double>
00349 {
00350 public:
00351   vtkQuaterniond() {}
00352   explicit vtkQuaterniond(double w, double x, double y, double z)
00353     : vtkQuaternion<double>(w, x, y, z) {}
00354   explicit vtkQuaterniond(double scalar) : vtkQuaternion<double>(scalar) {}
00355   explicit vtkQuaterniond(const double *init) : vtkQuaternion<double>(init) {}
00356   vtkQuaternionOperatorMacro(vtkQuaterniond, double);
00357 };
00358 
00359 #include "vtkQuaternion.txx"
00360 
00361 #endif // __vtkQuaternion_h
00362 // VTK-HeaderTest-Exclude: vtkQuaternion.h