quaternion.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::quaternion
26 
27 Description
28  Quaternion class used to perform rotations in 3D space.
29 
30 SourceFiles
31  quaternionI.H
32  quaternion.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef quaternion_H
37 #define quaternion_H
38 
39 #include "scalar.H"
40 #include "vector.H"
41 #include "tensor.H"
42 #include "word.H"
43 #include "contiguous.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declaration of friend functions and operators
51 
52 class quaternion;
53 Istream& operator>>(Istream& is, quaternion&);
54 Ostream& operator<<(Ostream& os, const quaternion& C);
55 
56 
57 /*---------------------------------------------------------------------------*\
58  Class quaternion Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class quaternion
62 {
63  // private data
64 
65  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
66  scalar w_;
67 
68  //- Vector part of the quaternion ( = axis of rotation)
69  vector v_;
70 
71 
72  //- Multiply vector v by quaternion as if v is a pure quaternion
73  inline quaternion mulq0v(const vector& v) const;
74 
75 
76 public:
77 
78  //- Component type
79  typedef scalar cmptType;
80 
81  // Member constants
82 
83  enum
84  {
85  rank = 1 // Rank of quaternion is 1
86  };
87 
88 
89  // Static data members
90 
91  static const char* const typeName;
92 
93  static const quaternion zero;
94  static const quaternion I;
95 
96 
97  // Constructors
98 
99  //- Construct null
100  inline quaternion();
101 
102  //- Construct given scalar and vector parts
103  inline quaternion(const scalar w, const vector& v);
104 
105  //- Construct a rotation quaternion given the direction d
106  // and angle theta
107  inline quaternion(const vector& d, const scalar theta);
108 
109  //- Construct a rotation quaternion given the direction d
110  // and cosine angle cosTheta and a if d is normalized
111  inline quaternion
112  (
113  const vector& d,
114  const scalar cosTheta,
115  const bool normalized
116  );
117 
118  //- Construct given scalar part, the vector part = vector::zero
119  inline explicit quaternion(const scalar w);
120 
121  //- Construct a pure quaternion given the vector part, scalar part = 0
122  inline explicit quaternion(const vector& v);
123 
124  //- Construct a quaternion given the three Euler angles
125  inline quaternion
126  (
127  const scalar angleX,
128  const scalar angleY,
129  const scalar angleZ
130  );
131 
132  //- Construct a quaternion from a rotation tensor
133  inline explicit quaternion(const tensor& rotationTensor);
134 
135  //- Construct from Istream
137 
138 
139  // Member functions
140 
141  // Access
142 
143  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
144  inline scalar w() const;
145 
146  //- Vector part of the quaternion ( = axis of rotation)
147  inline const vector& v() const;
148 
149  //- The rotation tensor corresponding the quaternion
150  inline tensor R() const;
151 
152  //- Return a vector of euler angles (rotations in radians about
153  // the x, y and z axes.
154  inline vector eulerAngles(const quaternion& q) const;
155 
156  inline quaternion normalized() const;
157 
158 
159  // Edit
160 
161  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
162  inline scalar& w();
163 
164  //- Vector part of the quaternion ( = axis of rotation)
165  inline vector& v();
166 
167  inline void normalize();
168 
169 
170  // Transform
171 
172  //- Rotate the given vector
173  inline vector transform(const vector& v) const;
174 
175  //- Rotate the given vector anti-clockwise
176  inline vector invTransform(const vector& v) const;
177 
178  //- Rotate the given quaternion (and normalize)
179  inline quaternion transform(const quaternion& q) const;
180 
181  //- Rotate the given quaternion anti-clockwise (and normalize)
182  inline quaternion invTransform(const quaternion& q) const;
183 
184 
185  // Member operators
186 
187  inline void operator=(const quaternion&);
188  inline void operator+=(const quaternion&);
189  inline void operator-=(const quaternion&);
190  inline void operator*=(const quaternion&);
191  inline void operator/=(const quaternion&);
192 
193  inline void operator=(const scalar);
194 
195  inline void operator=(const vector&);
196 
197  inline void operator*=(const scalar);
198  inline void operator/=(const scalar);
199 
200 
201  // IOstream operators
202 
203  friend Istream& operator>>(Istream& is, quaternion&);
204  friend Ostream& operator<<(Ostream& os, const quaternion& C);
205 };
206 
207 
208 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
209 
210 inline scalar magSqr(const quaternion& q);
211 inline scalar mag(const quaternion& q);
212 
213 //- Return the conjugate of the given quaternion
214 inline quaternion conjugate(const quaternion& q);
215 
216 //- Return the normalized (unit) quaternion of the given quaternion
217 inline quaternion normalize(const quaternion& q);
218 
219 //- Return the inverse of the given quaternion
220 inline quaternion inv(const quaternion& q);
221 
222 //- Return a string representation of a quaternion
223 word name(const quaternion&);
224 
225 //- Spherical linear interpolation of quaternions
227 (
228  const quaternion& qa,
229  const quaternion& qb,
230  const scalar t
231 );
232 
233 //- Exponent of a quaternion
234 quaternion exp(const quaternion& q);
235 
236 //- Power of a quaternion
237 quaternion pow(const quaternion& q, const label power);
238 
239 //- Power of a quaternion
240 quaternion pow(const quaternion& q, const scalar power);
241 
242 //- Data associated with quaternion type are contiguous
243 template<>
244 inline bool contiguous<quaternion>() {return true;}
245 
246 
247 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
248 
249 inline bool operator==(const quaternion& q1, const quaternion& q2);
250 inline bool operator!=(const quaternion& q1, const quaternion& q2);
251 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
252 inline quaternion operator-(const quaternion& q);
253 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
254 inline scalar operator&(const quaternion& q1, const quaternion& q2);
255 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
256 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
257 inline quaternion operator*(const scalar s, const quaternion& q);
258 inline quaternion operator*(const quaternion& q, const scalar s);
259 inline quaternion operator/(const quaternion& q, const scalar s);
260 
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 } // End namespace Foam
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #include "quaternionI.H"
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #endif
273 
274 // ************************************************************************* //
void operator-=(const quaternion &)
Definition: quaternionI.H:301
tmp< fvMatrix< Type > > operator*(const DimensionedField< scalar, volMesh > &, const fvMatrix< Type > &)
static const char *const typeName
Definition: quaternion.H:90
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
vector eulerAngles(const quaternion &q) const
Return a vector of euler angles (rotations in radians about.
Definition: quaternionI.H:258
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject( name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE ))
static const quaternion zero
Definition: quaternion.H:92
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
dimensioned< scalar > mag(const dimensioned< Type > &)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void operator/=(const quaternion &)
Definition: quaternionI.H:314
dimensioned< scalar > magSqr(const dimensioned< Type > &)
tmp< fvMatrix< Type > > operator+(const fvMatrix< Type > &, const fvMatrix< Type > &)
friend Ostream & operator<<(Ostream &os, const quaternion &C)
const vector & v() const
Vector part of the quaternion ( = axis of rotation)
Definition: quaternionI.H:172
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:60
A class for handling words, derived from string.
Definition: word.H:59
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
dimensionedScalar exp(const dimensionedScalar &ds)
Graphite solid properties.
Definition: C.H:57
scalar cmptType
Component type.
Definition: quaternion.H:78
bool contiguous< quaternion >()
Data associated with quaternion type are contiguous.
Definition: quaternion.H:243
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:133
void operator*=(const quaternion &)
Definition: quaternionI.H:307
Namespace for OpenFOAM.
tensor R() const
The rotation tensor corresponding the quaternion.
Definition: quaternionI.H:235
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
vector transform(const vector &v) const
Rotate the given vector.
Definition: quaternionI.H:208
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
scalar w() const
Scalar part of the quaternion ( = cos(theta/2) for rotation)
Definition: quaternionI.H:166
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
quaternion()
Construct null.
Definition: quaternionI.H:28
bool operator!=(const particle &, const particle &)
Definition: particle.C:147
friend Istream & operator>>(Istream &is, quaternion &)
Template function to specify if the data of a type are contiguous.
void operator=(const quaternion &)
Definition: quaternionI.H:289
quaternion conjugate(const quaternion &q)
Return the conjugate of the given quaternion.
Definition: quaternionI.H:359
quaternion normalized() const
Definition: quaternionI.H:190
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
vector invTransform(const vector &v) const
Rotate the given vector anti-clockwise.
Definition: quaternionI.H:214
tensor rotationTensor(const vector &n1, const vector &n2)
Definition: transform.H:46
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition: quaternion.C:55
static const quaternion I
Definition: quaternion.H:93
void operator+=(const quaternion &)
Definition: quaternionI.H:295
volScalarField & C