FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::quaternion
00026 
00027 Description
00028     Quaternion class used to perform rotations in 3D space.
00029 
00030 SourceFiles
00031     quaternionI.H
00032     quaternion.C
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #ifndef quaternion_H
00037 #define quaternion_H
00038 
00039 #include <OpenFOAM/scalar.H>
00040 #include <OpenFOAM/vector.H>
00041 #include <OpenFOAM/tensor.H>
00042 #include <OpenFOAM/word.H>
00043 #include <OpenFOAM/contiguous.H>
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049 
00050 // Forward declaration of friend functions and operators
00051 
00052 class quaternion;
00053 Istream& operator>>(Istream& is, quaternion&);
00054 Ostream& operator<<(Ostream& os, const quaternion& C);
00055 
00056 
00057 /*---------------------------------------------------------------------------*\
00058                            Class quaternion Declaration
00059 \*---------------------------------------------------------------------------*/
00060 
00061 class quaternion
00062 {
00063     // private data
00064 
00065         //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
00066         scalar w_;
00067 
00068         //- Vector part of the quaternion ( = axis of rotation)
00069         vector v_;
00070 
00071 
00072         //- Multiply vector v by quaternion as if v is a pure quaternion
00073         inline quaternion mulq0v(const vector& v) const;
00074 
00075 
00076 public:
00077 
00078     // Static data members
00079 
00080         static const char* const typeName;
00081 
00082         static const quaternion zero;
00083         static const quaternion I;
00084 
00085 
00086     // Constructors
00087 
00088         //- Construct null
00089         inline quaternion();
00090 
00091         //- Construct given scalar and vector parts
00092         inline quaternion(const scalar w, const vector& v);
00093 
00094         //- Construct a rotation quaternion given the direction d
00095         //  and angle theta
00096         inline quaternion(const vector& d, const scalar theta);
00097 
00098         //- Construct given scalar part, the vector part = vector::zero
00099         inline explicit quaternion(const scalar w);
00100 
00101         //- Construct a pure quaternion given the vector part, scalar part = 0
00102         inline explicit quaternion(const vector& v);
00103 
00104         //- Construct a quaternion given the three Euler angles
00105         inline quaternion
00106         (
00107             const scalar angleX,
00108             const scalar angleY,
00109             const scalar angleZ
00110         );
00111 
00112         //- Construct from Istream
00113         quaternion(Istream&);
00114 
00115 
00116     // Member functions
00117 
00118            // Access
00119 
00120                //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
00121                inline scalar w() const;
00122 
00123                //- Vector part of the quaternion ( = axis of rotation)
00124                inline const vector& v() const;
00125 
00126                //- The rotation tensor corresponding the quaternion
00127                inline tensor R() const;
00128 
00129 
00130            // Edit
00131 
00132                //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
00133                inline scalar& w();
00134 
00135                //- Vector part of the quaternion ( = axis of rotation)
00136                inline vector& v();
00137 
00138                inline void normalize();
00139 
00140 
00141            // Transform
00142 
00143                //- Rotate the given vector
00144                inline vector transform(const vector& v) const;
00145 
00146                //- Rotate the given vector anti-clockwise
00147                inline vector invTransform(const vector& v) const;
00148 
00149                //- Rotate the given quaternion (and normalize)
00150                inline quaternion transform(const quaternion& q) const;
00151 
00152                //- Rotate the given quaternion anti-clockwise (and normalize)
00153                inline quaternion invTransform(const quaternion& q) const;
00154 
00155 
00156     // Member operators
00157 
00158         inline void operator=(const quaternion&);
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 
00164         inline void operator=(const scalar);
00165 
00166         inline void operator=(const vector&);
00167 
00168         inline void operator*=(const scalar);
00169         inline void operator/=(const scalar);
00170 
00171 
00172     // IOstream operators
00173 
00174         friend Istream& operator>>(Istream& is, quaternion&);
00175         friend Ostream& operator<<(Ostream& os, const quaternion& C);
00176 };
00177 
00178 
00179 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00180 
00181 inline scalar magSqr(const quaternion& q);
00182 inline scalar mag(const quaternion& q);
00183 
00184 //- Return the conjugate of the given quaternion
00185 inline quaternion conjugate(const quaternion& q);
00186 
00187 //- Return the normailzed (unit) quaternion of the given quaternion
00188 inline quaternion normalize(const quaternion& q);
00189 
00190 //- Return the inverse of the given quaternion
00191 inline quaternion inv(const quaternion& q);
00192 
00193 //- Return a string representation of a quaternion
00194 word name(const quaternion&);
00195 
00196 //- Data associated with quaternion type are contiguous
00197 template<>
00198 inline bool contiguous<quaternion>() {return true;}
00199 
00200 
00201 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00202 
00203 inline bool operator==(const quaternion& q1, const quaternion& q2);
00204 inline bool operator!=(const quaternion& q1, const quaternion& q2);
00205 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
00206 inline quaternion operator-(const quaternion& q);
00207 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
00208 inline scalar operator&(const quaternion& q1, const quaternion& q2);
00209 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
00210 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
00211 inline quaternion operator*(const scalar s, const quaternion& q);
00212 inline quaternion operator*(const quaternion& q, const scalar s);
00213 inline quaternion operator/(const quaternion& q, const scalar s);
00214 
00215 
00216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00217 
00218 } // End namespace Foam
00219 
00220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00221 
00222 #include <OpenFOAM/quaternionI.H>
00223 
00224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00225 
00226 #endif
00227 
00228 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines