1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 Quaternion class used to perform rotations in 3D space.
34 \*---------------------------------------------------------------------------*/
43 #include "contiguous.H"
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 // Forward declaration of friend functions and operators
53 Istream& operator>>(Istream& is, quaternion&);
54 Ostream& operator<<(Ostream& os, const quaternion& C);
57 /*---------------------------------------------------------------------------*\
58 Class quaternion Declaration
59 \*---------------------------------------------------------------------------*/
65 //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
68 //- Vector part of the quaternion ( = axis of rotation)
72 //- Multiply vector v by quaternion as if v is a pure quaternion
73 inline quaternion mulq0v(const vector& v) const;
78 // Static data members
80 static const char* const typeName;
82 static const quaternion zero;
83 static const quaternion I;
91 //- Construct given scalar and vector parts
92 inline quaternion(const scalar w, const vector& v);
94 //- Construct a rotation quaternion given the direction d
96 inline quaternion(const vector& d, const scalar theta);
98 //- Construct given scalar part, the vector part = vector::zero
99 inline explicit quaternion(const scalar w);
101 //- Construct a pure quaternion given the vector part, scalar part = 0
102 inline explicit quaternion(const vector& v);
104 //- Construct a quaternion given the three Euler angles
112 //- Construct from Istream
113 quaternion(Istream&);
120 //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
121 inline scalar w() const;
123 //- Vector part of the quaternion ( = axis of rotation)
124 inline const vector& v() const;
126 //- The rotation tensor corresponding the quaternion
127 inline tensor R() const;
132 //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
135 //- Vector part of the quaternion ( = axis of rotation)
138 inline void normalize();
143 //- Rotate the given vector
144 inline vector transform(const vector& v) const;
146 //- Rotate the given vector anti-clockwise
147 inline vector invTransform(const vector& v) const;
149 //- Rotate the given quaternion (and normalize)
150 inline quaternion transform(const quaternion& q) const;
152 //- Rotate the given quaternion anti-clockwise (and normalize)
153 inline quaternion invTransform(const quaternion& q) const;
158 inline void operator=(const quaternion&);
159 inline void operator+=(const quaternion&);
160 inline void operator-=(const quaternion&);
161 inline void operator*=(const quaternion&);
162 inline void operator/=(const quaternion&);
164 inline void operator=(const scalar);
166 inline void operator=(const vector&);
168 inline void operator*=(const scalar);
169 inline void operator/=(const scalar);
172 // IOstream operators
174 friend Istream& operator>>(Istream& is, quaternion&);
175 friend Ostream& operator<<(Ostream& os, const quaternion& C);
179 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
181 inline scalar magSqr(const quaternion& q);
182 inline scalar mag(const quaternion& q);
184 //- Return the conjugate of the given quaternion
185 inline quaternion conjugate(const quaternion& q);
187 //- Return the normailzed (unit) quaternion of the given quaternion
188 inline quaternion normalize(const quaternion& q);
190 //- Return the inverse of the given quaternion
191 inline quaternion inv(const quaternion& q);
193 //- Return a string representation of a quaternion
194 word name(const quaternion&);
196 //- Data associated with quaternion type are contiguous
198 inline bool contiguous<quaternion>() {return true;}
201 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
203 inline bool operator==(const quaternion& q1, const quaternion& q2);
204 inline bool operator!=(const quaternion& q1, const quaternion& q2);
205 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
206 inline quaternion operator-(const quaternion& q);
207 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
208 inline scalar operator&(const quaternion& q1, const quaternion& q2);
209 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
210 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
211 inline quaternion operator*(const scalar s, const quaternion& q);
212 inline quaternion operator*(const quaternion& q, const scalar s);
213 inline quaternion operator/(const quaternion& q, const scalar s);
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
218 } // End namespace Foam
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 #include "quaternionI.H"
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 // ************************************************************************* //