Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

vnl_quaternion.h

Go to the documentation of this file.
00001 #ifndef vnl_quaternion_h_
00002 #define vnl_quaternion_h_
00003 // This is vxl/vnl/vnl_quaternion.h
00004 
00005 //: \file
00006 //  \brief Unit quaternion represents rotation in 3D.
00007 //  \author awf@robots.ox.ac.uk 16 Mar 00
00008 //
00009 
00010  
00011 //  Modifications
00012 //  20-05-2000 fsm@robots. changed FLOAT to T since gcc will barf at
00013 //            the very reasonable forward declaration
00014 //            template <class T> class vnl_quaternion;
00015 //  LSB (Manchester) 23/3/01 Tidied documentation
00016 
00017 #include <vnl/vnl_vector_fixed.h>
00018 #include <vnl/vnl_matrix_fixed.h>
00019 //: 4-element vector that represents rotation in 3D.
00020 // Quaternion is a 4-element vector with 1 real and 3 imaginary
00021 // components:
00022 // \verbatim
00023 //    q = r + (i*x + j*y + k*z)
00024 //    r = cos(theta/2)
00025 //    (x, y, z) = sin(theta/2) (kx, ky, kz) 
00026 // \endverbatim
00027 // where theta and k are  respectively the angle and axis of rotation. 
00028 // 3D vectors can be  thought  of  as  imaginary  quaternions, and  so  a
00029 // quaternion is represented as a Vector<T> with the imaginary
00030 // part before the real part for 1-1 alignment.
00031 // 
00032 // Unit quaternion provides a more efficient representation for
00033 // rotation, than  the usual orthonormal matrix that has nine
00034 // parameters  and  six  orthonormal  constraints.   The   unit
00035 // quaternion  has only one unit magnitude constraint. Composing
00036 // rotations with quaternions results in fewer multiplications
00037 // and less error. To insure valid rotation result, the
00038 // nearest unit quaternion is computed, and this is much easier
00039 // than  finding  the  nearest orthonormal matrix. Transforming
00040 // vectors with a quaternion requires more operations  compared
00041 // to multiplication with the equivalent orthonormal matrix.
00042 //
00043 // See also
00044 // Vector<Type> and Matrix<Type> for basic operations on vectors and matrices.
00045 // 
00046 // Transform for coordinate transformations.
00047 // 
00048 // Envelope for envelope-letter scheme that avoids deep copy on
00049 // return by value in arithmetic expressions like: q1 * q2 * q3 *...
00050 //
00051 
00052 //what was this for? #include <vcl_functional.h>
00053 #include <vnl/vnl_vector_fixed.h>
00054 #include <vnl/vnl_matrix_fixed.h>
00055 
00056 export template <class T>
00057 class vnl_quaternion : public vnl_vector_fixed<T, 4> { // public for IS-A relation
00058   typedef vnl_vector_fixed<T, 4> Base;
00059 public:
00060 
00061  //: Constructor for null quaternion
00062   vnl_quaternion () {}
00063 
00064  //: Construct quaternion from components x,y,z,r
00065   vnl_quaternion (T x, T y, T z, T r);
00066 
00067  //: Construct quaternion from axis and angle of rotation
00068   vnl_quaternion (const vnl_vector<T>& axis, T angle); 
00069 
00070  //: Construct quaternion from from 3-4 square row-major
00071   explicit vnl_quaternion (const vnl_matrix<T>& transform); // from 3-4 square row-major
00072 
00073  //: Construct quaternion from from from 3-4D vector
00074   vnl_quaternion (const vnl_vector<T>& vec); 
00075 
00076  //: Copy constructor
00077   inline vnl_quaternion (const vnl_quaternion<T>& from);
00078 
00079  //: Free internal array
00080   inline ~vnl_quaternion();
00081 
00082  //: q1 = q2
00083   inline vnl_quaternion& operator= (const vnl_quaternion<T>& rhs);
00084 
00085   inline T& x ();
00086   inline T& y ();
00087 //: Imaginary components, parallel to axis of rotation
00088   inline T& z ();
00089 
00090 //: Real component
00091   inline T& r ();
00092 
00093   inline T x () const;
00094   inline T y () const;
00095   inline T z () const;
00096   inline T r () const;
00097   inline T real () const;
00098   
00099  //: Imaginary vector part
00100   inline vnl_vector<T> imaginary () const;
00101 
00102  //: Axis of rotation
00103   vnl_vector<T> axis () const;
00104 
00105  //: Angle of rotation
00106   T angle () const;
00107   
00108  //: 3x3 rotation matrix
00109   vnl_matrix_fixed<T,3,3> rotation_matrix () const;
00110 
00111  //: 4x4 rotation matrix
00112   vnl_matrix_fixed<T,4,4> rotation_matrix_4 () const;
00113 
00114  //: Same real, opposite img part 
00115   vnl_quaternion<T> conjugate () const;
00116 
00117  //: Inverse for nonzero quat
00118   vnl_quaternion<T> inverse () const;
00119   
00120   vnl_quaternion<T> operator* (const vnl_quaternion<T>&) const; 
00121 
00122   //: Rotate 3D v
00123   vnl_vector<T> rotate (const vnl_vector<T>& v) const;
00124 };
00125 
00126 
00127 //: Quaternion -- Creates a copy of from quaternion.
00128 template <class T>
00129 inline
00130 vnl_quaternion<T>::vnl_quaternion (const vnl_quaternion<T>& from) :
00131   Base(from)
00132 {    // 1-1 layout between vector&quat
00133 }
00134 
00135 //: ~Quaternion -- Frees space allocated for quaternion.
00136 
00137 template <class T>
00138 inline
00139 vnl_quaternion<T>::~vnl_quaternion () {} // Vector will free data array
00140 
00141 
00142 //: x
00143 
00144 template <class T>
00145 inline T& vnl_quaternion<T>::x () {
00146   return this->operator()(0);
00147 }
00148 
00149 //: y 
00150 
00151 template <class T>
00152 inline T& vnl_quaternion<T>::y () {
00153   return this->operator()(1);
00154 }
00155 
00156 //:  z 
00157 
00158 template <class T>
00159 inline T& vnl_quaternion<T>::z () {
00160   return this->operator()(2);
00161 }
00162 
00163 //: r
00164 // Accessors for the imaginary and real components of  the
00165 // quaternion. Use these accessors to both get
00166 // and set the components.
00167 
00168 template <class T>
00169 inline T& vnl_quaternion<T>::r () {
00170   return this->operator()(3);
00171 }
00172 
00173 
00174 //: x
00175 template <class T>
00176 inline T vnl_quaternion<T>::x () const {
00177   return this->operator()(0);
00178 }
00179 
00180 //: y 
00181 template <class T>
00182 inline T vnl_quaternion<T>::y () const {
00183   return this->operator()(1);
00184 }
00185 
00186 //: z 
00187 template <class T>
00188 inline T vnl_quaternion<T>::z () const {
00189   return this->operator()(2);
00190 }
00191 
00192 //: r
00193 // Accessors for the imaginary and real components of  the
00194 // quaternion. Use these accessors to both get
00195 // and set the components.
00196 template <class T>
00197 inline T vnl_quaternion<T>::r () const {
00198   return this->operator()(3);
00199 }
00200 
00201 
00202 //: Copies and returns the imaginary part.
00203 template <class T>
00204 inline vnl_vector<T> vnl_quaternion<T>::imaginary () const {
00205   return this->extract(3,0);
00206 }
00207 
00208 //: Copies and returns the real part.
00209 template <class T>
00210 inline T vnl_quaternion<T>::real () const {
00211   return this->get(3);
00212 }
00213 
00214 //: operator=
00215 //  Overloads assignment operator to  copy rhs quaternion
00216 //  into lhs quaternion.
00217 template <class T>
00218 inline vnl_quaternion<T>& vnl_quaternion<T>::operator= (const vnl_quaternion<T>& rhs) {
00219   Base::operator=(rhs);   // same as copy vector part
00220   return *this;
00221 }
00222 
00223 
00224 //: operator<<
00225 template <class T>
00226 inline vcl_ostream& operator<< (vcl_ostream& os, const vnl_quaternion<T>& q) {
00227   return os << *((vnl_vector<T>*) &q);
00228 }
00229 
00230 // operator<<  -- Print the components of Quaternion.
00231 //  awf removed : pointers should never be printed dereffed.
00232 // template <class T>
00233 // inline ostream& operator<< (ostream& os, const vnl_quaternion<T>* q) {
00234 //   return os << *((vnl_vector<T>*) q);
00235 // }
00236 
00237 #define VNL_QUATERNION_INSTANTIATE(T) extern "Error, include vnl/vnl_quaternion.txx";
00238 
00239 #endif // vnl_quaternion_h_

Generated at Fri May 21 01:15:52 2004 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000