OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

quaternion.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::quaternion
00027 
00028 Description
00029     Quaternion class used to perform rotations in 3D space.
00030 
00031 SourceFiles
00032     quaternionI.H
00033     quaternion.C
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef quaternion_H
00038 #define quaternion_H
00039 
00040 #include "scalar.H"
00041 #include "vector.H"
00042 #include "tensor.H"
00043 #include "word.H"
00044 #include "contiguous.H"
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 namespace Foam
00049 {
00050 
00051 // Forward declaration of friend functions and operators
00052 
00053 class quaternion;
00054 Istream& operator>>(Istream& is, quaternion&);
00055 Ostream& operator<<(Ostream& os, const quaternion& C);
00056 
00057 
00058 /*---------------------------------------------------------------------------*\
00059                            Class quaternion Declaration
00060 \*---------------------------------------------------------------------------*/
00061 
00062 class quaternion
00063 {
00064     // private data
00065 
00066         //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
00067         scalar w_;
00068 
00069         //- Vector part of the quaternion ( = axis of rotation)
00070         vector v_;
00071 
00072 
00073         //- Multiply vector v by quaternion as if v is a pure quaternion
00074         inline quaternion mulq0v(const vector& v) const;
00075 
00076 
00077 public:
00078 
00079     // Static data members
00080 
00081         static const char* const typeName;
00082 
00083         static const quaternion zero;
00084         static const quaternion I;
00085 
00086 
00087     // Constructors
00088 
00089         //- Construct null
00090         inline quaternion();
00091 
00092         //- Construct given scalar and vector parts
00093         inline quaternion(const scalar w, const vector& v);
00094 
00095         //- Construct a rotation quaternion given the direction d
00096         //  and angle theta
00097         inline quaternion(const vector& d, const scalar theta);
00098 
00099         //- Construct given scalar part, the vector part = vector::zero
00100         inline explicit quaternion(const scalar w);
00101 
00102         //- Construct a pure quaternion given the vector part, scalar part = 0
00103         inline explicit quaternion(const vector& v);
00104 
00105         //- Construct a quaternion given the three Euler angles
00106         inline quaternion
00107         (
00108             const scalar angleX,
00109             const scalar angleY,
00110             const scalar angleZ
00111         );
00112 
00113         //- Construct from Istream
00114         quaternion(Istream&);
00115 
00116 
00117     // Member functions
00118 
00119            // Access
00120 
00121                //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
00122                inline scalar w() const;
00123 
00124                //- Vector part of the quaternion ( = axis of rotation)
00125                inline const vector& v() const;
00126 
00127                //- The rotation tensor corresponding the quaternion
00128                inline tensor R() const;
00129 
00130 
00131            // Edit
00132 
00133                //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
00134                inline scalar& w();
00135 
00136                //- Vector part of the quaternion ( = axis of rotation)
00137                inline vector& v();
00138 
00139                inline void normalize();
00140 
00141 
00142            // Transform
00143 
00144                //- Rotate the given vector
00145                inline vector transform(const vector& v) const;
00146 
00147                //- Rotate the given vector anti-clockwise
00148                inline vector invTransform(const vector& v) const;
00149 
00150                //- Rotate the given quaternion (and normalize)
00151                inline quaternion transform(const quaternion& q) const;
00152 
00153                //- Rotate the given quaternion anti-clockwise (and normalize)
00154                inline quaternion invTransform(const quaternion& q) const;
00155 
00156 
00157     // Member operators
00158 
00159         inline void operator=(const quaternion&);
00160         inline void operator+=(const quaternion&);
00161         inline void operator-=(const quaternion&);
00162         inline void operator*=(const quaternion&);
00163         inline void operator/=(const quaternion&);
00164 
00165         inline void operator=(const scalar);
00166 
00167         inline void operator=(const vector&);
00168 
00169         inline void operator*=(const scalar);
00170         inline void operator/=(const scalar);
00171 
00172 
00173     // IOstream operators
00174 
00175         friend Istream& operator>>(Istream& is, quaternion&);
00176         friend Ostream& operator<<(Ostream& os, const quaternion& C);
00177 };
00178 
00179 
00180 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00181 
00182 inline scalar magSqr(const quaternion& q);
00183 inline scalar mag(const quaternion& q);
00184 
00185 //- Return the conjugate of the given quaternion
00186 inline quaternion conjugate(const quaternion& q);
00187 
00188 //- Return the normailzed (unit) quaternion of the given quaternion
00189 inline quaternion normalize(const quaternion& q);
00190 
00191 //- Return the inverse of the given quaternion
00192 inline quaternion inv(const quaternion& q);
00193 
00194 //- Return a string representation of a quaternion
00195 word name(const quaternion&);
00196 
00197 //- Data associated with quaternion type are contiguous
00198 template<>
00199 inline bool contiguous<quaternion>() {return true;}
00200 
00201 
00202 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00203 
00204 inline bool operator==(const quaternion& q1, const quaternion& q2);
00205 inline bool operator!=(const quaternion& q1, const quaternion& q2);
00206 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
00207 inline quaternion operator-(const quaternion& q);
00208 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
00209 inline scalar operator&(const quaternion& q1, const quaternion& q2);
00210 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
00211 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
00212 inline quaternion operator*(const scalar s, const quaternion& q);
00213 inline quaternion operator*(const quaternion& q, const scalar s);
00214 inline quaternion operator/(const quaternion& q, const scalar s);
00215 
00216 
00217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00218 
00219 } // End namespace Foam
00220 
00221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00222 
00223 #include "quaternionI.H"
00224 
00225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00226 
00227 #endif
00228 
00229 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd