Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

quat.H

Go to the documentation of this file.
00001 #ifndef QUAT_H_IS_INCLUDED
00002 #define QUAT_H_IS_INCLUDED
00003 
00004 /*!
00005  *  \file Quat.H
00006  *  \brief Contains the definition of the Quat class.  A quaternion class.
00007  *  \ingroup group_MLIB
00008  *
00009  */
00010 
00011 #include "mlib/global.H"
00012 
00013 namespace mlib {
00014 
00015 /*!
00016  *  \brief A quaternion class.
00017  *  \ingroup group_MLIB
00018  *
00019  *  The Quat class is designed to be the base class for more specific types of
00020  *  quaternions.  Specifically, quaternions in different 3D coordinates systems.
00021  *  The QUAT template argument is the type of the derived quaternion class.
00022  *  This allows the Quat class to return new quaternions of the same type as the
00023  *  derived quaternion class.  The M, P, V, and L template arguments are the
00024  *  types of the corresponding matrix, point, vector, and line (respectively)
00025  *  for the coordinate system of the derived quaternion class.
00026  *
00027  */
00028 template <class QUAT, class M, class P, class V, class L>
00029 class Quat
00030 {
00031    protected:
00032    
00033       V            _v;
00034       double       _w;
00035    
00036    public:
00037    
00038       //! \name Constructors
00039       //@{
00040    
00041       //! \brief Default constructor.  Creates a quaternion with a unit scalar
00042       //! component and a default constructed vector component.
00043       //! 
00044       //! This most likely creates a zero vector as the vector component.  So,
00045       //! the quaternion should be the identity quaternion.
00046       Quat() : _v(), _w(1) {}
00047       
00048       //! \brief Constructor that creates a quaternion with the scalar and vector
00049       //! components specified in the arguments.
00050       Quat(const V& v, double w)  : _v(v), _w(w) {}
00051       
00052       //! \brief Constructor that creates a quaternion with the specifeed scalar
00053       //! component and a default constructed vector component.
00054       Quat(double w)   : _w(w) {}
00055       
00056       //! \brief Constructor that creates a quaternion with the specified vector
00057       //! component and a zero scalar component.
00058       Quat(const V& v) : _v(v), _w(0) {}
00059       
00060       //! \brief Create quaternion from rotation matrix.
00061       Quat(const M& t);
00062       
00063       //! \brief Create quaternion to rotate from \p v1 to \p v2.
00064       Quat(const V& v1, const V& v2);
00065       
00066       //@}
00067       
00068       //! \name Accessor Functions
00069       //@{
00070       
00071       //! \brief Accesses the vector component.
00072       const V&     v()  const              { return _v; }
00073       //! \brief Accesses the vector component.
00074       V&           v()                     { return _v; }
00075       //! \brief Accesses the scalar component.
00076       double       w()  const              { return _w; }
00077       //! \brief Accesses the scalar component.
00078       double&      w()                     { return _w; }
00079       
00080       //@}
00081       
00082       //! \name Quaternion Operations
00083       //@{
00084    
00085       double norm()             const { return _v.length_sqrd() + _w*_w; }
00086    
00087       QUAT   conjugate()        const { return QUAT(-_v, _w); }
00088       
00089       QUAT   inverse()          const { return conjugate()/norm(); }
00090 
00091       QUAT   normalized()       const { return *this / norm(); }
00092 
00093       double dot(const QUAT& q) const { return v()*q.v() + w()*q.w(); }
00094 
00095       //@}
00096       
00097       //! \name Overloaded Arithmetic Operators
00098       //@{
00099       
00100       QUAT operator+(QUAT q) const { return QUAT(v()+q.v(), w()+q.w()); }
00101          
00102       QUAT operator*(QUAT q) const {
00103          return QUAT(cross(v(), q.v()) + w()*q.v() + q.w()*v(),
00104                      w()*q.w() - v()*q.v());
00105       }
00106 
00107       QUAT operator/(QUAT q) const { return operator*(q.inverse()); }
00108       
00109       //@}
00110       
00111       //! \name Scalar multiplication
00112       //@{
00113       QUAT operator*(double s) const { return QUAT(v()*s, w()*s); }
00114       QUAT operator/(double s) const { return *this * (1.0/s); }
00115 
00116       friend QUAT operator*(double s, const QUAT& q) { return q * s; }
00117 
00118       friend QUAT operator-(const QUAT& q) { return q * -1.0; }
00119 
00120       //@}
00121       
00122       //! \name Two Quaternion Operations
00123       //@{
00124    
00125       //! \brief Spherical linear interpolation.
00126       static QUAT  slerp(const QUAT& q1, const QUAT& q2, double u);
00127       
00128       //@}
00129    
00130 }; 
00131 
00132 } // namespace mlib
00133 
00134 namespace mlib {
00135 
00136 //! \brief Stream insertion operator for quaternions.
00137 //! \relates Quat
00138 template <class QUAT, class M, class P, class V, class L>
00139 inline ostream &
00140 operator<<(ostream &os, const Quat<QUAT,M,P,V,L> &p) 
00141 { 
00142    return os << "<" << p.v() << ", " << p.w() << ">";
00143 }
00144 
00145 } // namespace mlib
00146 
00147 #ifdef JOT_NEEDS_TEMPLATES_IN_H_FILE
00148 #include "quat.C"
00149 #endif
00150 
00151 #endif // QUAT_H_IS_INCLUDED

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4