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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::quaternion
29 
30 Description
31  Quaternion class used to perform rotations in 3D space.
32 
33 SourceFiles
34  quaternionI.H
35  quaternion.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef quaternion_H
40 #define quaternion_H
41 
42 #include "scalar.H"
43 #include "vector.H"
44 #include "tensor.H"
45 #include "word.H"
46 #include "Enum.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class quaternion Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class quaternion
58 {
59  // Private Data
60 
61  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
62  scalar w_;
63 
64  //- Vector part of the quaternion ( = axis of rotation)
65  vector v_;
66 
67 
68  //- Multiply vector v by quaternion as if v is a pure quaternion
69  inline quaternion mulq0v(const vector& v) const;
70 
71  //- Conversion of two-axis rotation components into Euler-angles
72  inline static vector twoAxes
73  (
74  const scalar t11,
75  const scalar t12,
76  const scalar c2,
77  const scalar t31,
78  const scalar t32
79  );
80 
81  //- Conversion of three-axis rotation components into Euler-angles
82  inline static vector threeAxes
83  (
84  const scalar t11,
85  const scalar t12,
86  const scalar s2,
87  const scalar t31,
88  const scalar t32
89  );
90 
91 
92 public:
93 
94  // Public Types
95 
96  //- Component type
97  typedef scalar cmptType;
98 
99  //- Magnitude type
100  typedef scalar magType;
101 
102  //- Euler-angle rotation order
103  enum eulerOrder : unsigned char
104  {
105  // Proper Euler angles
106  XZX, XYX, YXY, YZY, ZYZ, ZXZ,
107 
108  // Tait-Bryan angles
109  XZY, XYZ, YXZ, YZX, ZYX, ZXY
110  };
111 
112  //- The names for Euler-angle rotation order
113  static const Enum<eulerOrder> eulerOrderNames;
114 
115 
116  // Member Constants
117 
118  //- Rank of quaternion is 1
119  static constexpr direction rank = 1;
120 
121 
122  // Static Data Members
123 
124  static constexpr const char* const typeName = "quaternion";
125 
126  static const quaternion zero;
127  static const quaternion I;
128 
129 
130  // Constructors
131 
132  //- Construct null
133  inline quaternion();
134 
135  //- Construct zero initialized
136  inline quaternion(const Foam::zero);
137 
138  //- Construct given scalar and vector parts
139  inline quaternion(const scalar w, const vector& v);
140 
141  //- Construct rotation quaternion given direction d and angle theta
142  inline quaternion(const vector& d, const scalar theta);
143 
144  //- Construct a rotation quaternion given direction d
145  //- and cosine angle cosTheta and flag if d is normalized
146  inline quaternion
147  (
148  const vector& d,
149  const scalar cosTheta,
150  const bool normalized
151  );
152 
153  //- Construct a real quaternion from the given scalar part,
154  //- the vector part = zero
155  inline explicit quaternion(const scalar w);
156 
157  //- Construct a pure imaginary quaternion given the vector part,
158  //- the scalar part = 0
159  inline explicit quaternion(const vector& v);
160 
161  //- Return the unit quaternion (versor) from the given vector
162  //- (w = sqrt(1 - |sqr(v)|))
163  static inline quaternion unit(const vector& v);
164 
165  //- Construct from three Euler rotation angles
166  inline quaternion(const eulerOrder order, const vector& angles);
167 
168  //- Construct from a rotation tensor
169  inline explicit quaternion(const tensor& rotationTensor);
170 
171  //- Construct from Istream
172  explicit quaternion(Istream& is);
173 
174 
175  // Member Functions
176 
177  // Access
178 
179  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
180  inline scalar w() const;
181 
182  //- Vector part of the quaternion ( = axis of rotation)
183  inline const vector& v() const;
184 
185  //- The rotation tensor corresponding to the quaternion
186  inline tensor R() const;
187 
188  //- Return the Euler rotation angles corresponding to the
189  //- specified rotation order
190  inline vector eulerAngles(const eulerOrder order) const;
191 
192  //- Return the quaternion normalized by its magnitude
193  inline quaternion normalized() const;
194 
195 
196  // Edit
197 
198  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
199  inline scalar& w();
200 
201  //- Vector part of the quaternion ( = axis of rotation)
202  inline vector& v();
203 
204  //- Normalize the quaternion by its magnitude
205  inline void normalize();
206 
207 
208  // Transform
209 
210  //- Rotate the given vector
211  inline vector transform(const vector& v) const;
212 
213  //- Rotate the given vector anti-clockwise
214  inline vector invTransform(const vector& v) const;
215 
216  //- Rotate the given quaternion (and normalize)
217  inline quaternion transform(const quaternion& q) const;
218 
219  //- Rotate the given quaternion anti-clockwise (and normalize)
220  inline quaternion invTransform(const quaternion& q) const;
221 
222 
223  // Member Operators
224 
225  inline void operator=(const quaternion& q);
226  inline void operator+=(const quaternion& q);
227  inline void operator-=(const quaternion& q);
228  inline void operator*=(const quaternion& q);
229  inline void operator/=(const quaternion& q);
230 
231  //- Change scalar portion
232  inline void operator=(const scalar s);
233 
234  //- Change vector portion
235  inline void operator=(const vector& v);
236 
237  inline void operator*=(const scalar s);
238  inline void operator/=(const scalar s);
239 };
240 
241 
242 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
243 
244 //- Contiguous data for quaternion
245 template<> struct is_contiguous<quaternion> : std::true_type {};
246 
247 //- Contiguous scalar data for quaternion
248 template<> struct is_contiguous_scalar<quaternion> : std::true_type {};
249 
250 
251 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
252 
253 inline scalar magSqr(const quaternion& q);
254 inline scalar mag(const quaternion& q);
255 
256 //- Return the conjugate of the given quaternion
257 inline quaternion conjugate(const quaternion& q);
258 
259 //- Return the normalized (unit) quaternion of the given quaternion
260 inline quaternion normalize(const quaternion& q);
261 
262 //- Return the inverse of the given quaternion
263 inline quaternion inv(const quaternion& q);
264 
265 //- Return a string representation of a quaternion
266 word name(const quaternion& q);
267 
268 //- Spherical linear interpolation of quaternions
270 (
271  const quaternion& qa,
272  const quaternion& qb,
273  const scalar t
274 );
275 
276 //- Simple weighted average with sign change
278 (
279  const UList<quaternion>& qs,
280  const UList<scalar> w
281 );
282 
283 //- Exponent of a quaternion
284 quaternion exp(const quaternion& q);
285 
286 //- Power of a quaternion
287 quaternion pow(const quaternion& q, const label power);
288 
289 //- Power of a quaternion
290 quaternion pow(const quaternion& q, const scalar power);
291 
292 
293 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
294 
296 Ostream& operator<<(Ostream& os, const quaternion& q);
297 
298 inline bool operator==(const quaternion& q1, const quaternion& q2);
299 inline bool operator!=(const quaternion& q1, const quaternion& q2);
300 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
301 inline quaternion operator-(const quaternion& q);
302 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
303 inline scalar operator&(const quaternion& q1, const quaternion& q2);
304 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
305 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
306 inline quaternion operator*(const scalar s, const quaternion& q);
307 inline quaternion operator*(const quaternion& q, const scalar s);
308 inline quaternion operator/(const quaternion& q, const scalar s);
309 
310 
311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 
313 } // End namespace Foam
314 
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
316 
317 #include "quaternionI.H"
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
321 #endif
322 
323 // ************************************************************************* //
Foam::Tensor< scalar >
Foam::quaternion::normalize
void normalize()
Normalize the quaternion by its magnitude.
Definition: quaternionI.H:319
Foam::Enum< eulerOrder >
Foam::quaternion::ZYZ
Definition: quaternion.H:105
Foam::quaternion::operator+=
void operator+=(const quaternion &q)
Definition: quaternionI.H:594
Foam::quaternion::v
const vector & v() const
Vector part of the quaternion ( = axis of rotation)
Definition: quaternionI.H:295
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.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))
Definition: gmvOutputSpray.H:25
Foam::quaternion::ZYX
Definition: quaternion.H:108
Foam::operator&
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
Foam::slerp
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition: quaternion.C:78
Foam::quaternion::operator/=
void operator/=(const quaternion &q)
Definition: quaternionI.H:613
Foam::quaternion::typeName
static constexpr const char *const typeName
Definition: quaternion.H:123
Foam::operator-
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Foam::quaternion::normalized
quaternion normalized() const
Return the quaternion normalized by its magnitude.
Definition: quaternionI.H:313
Foam::quaternion::operator-=
void operator-=(const quaternion &q)
Definition: quaternionI.H:600
Foam::quaternion::I
static const quaternion I
Definition: quaternion.H:126
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::quaternion::eulerAngles
vector eulerAngles(const eulerOrder order) const
Definition: quaternionI.H:408
Foam::quaternion::XZX
Definition: quaternion.H:105
Foam::exp
dimensionedScalar exp(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:261
Foam::quaternion::w
scalar w() const
Scalar part of the quaternion ( = cos(theta/2) for rotation)
Definition: quaternionI.H:289
tensor.H
Foam::quaternion::ZXY
Definition: quaternion.H:108
Foam::quaternion::YXZ
Definition: quaternion.H:108
Foam::quaternion
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:56
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
quaternionI.H
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
Foam::quaternion::rank
static constexpr direction rank
Rank of quaternion is 1.
Definition: quaternion.H:118
Foam::label
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:62
Foam::quaternion::ZXZ
Definition: quaternion.H:105
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::inv
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
Definition: dimensionedSphericalTensor.C:73
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::quaternion::eulerOrder
eulerOrder
Euler-angle rotation order.
Definition: quaternion.H:102
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam::quaternion::XZY
Definition: quaternion.H:108
Foam::quaternion::XYZ
Definition: quaternion.H:108
scalar.H
Foam::quaternion::quaternion
quaternion()
Construct null.
Definition: quaternionI.H:31
Foam::quaternion::unit
static quaternion unit(const vector &v)
Definition: quaternionI.H:91
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::constant::physicoChemical::c2
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
Foam::is_contiguous_scalar
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:91
Foam::operator/
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
Definition: dimensionedScalar.C:68
Foam::quaternion::YXY
Definition: quaternion.H:105
Foam::quaternion::YZY
Definition: quaternion.H:105
Foam::quaternion::operator=
void operator=(const quaternion &q)
Definition: quaternionI.H:588
Foam::quaternion::eulerOrderNames
static const Enum< eulerOrder > eulerOrderNames
The names for Euler-angle rotation order.
Definition: quaternion.H:112
Foam::quaternion::operator*=
void operator*=(const quaternion &q)
Definition: quaternionI.H:606
Foam::Vector< scalar >
Foam::operator+
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::quaternion::magType
scalar magType
Magnitude type.
Definition: quaternion.H:99
Foam::quaternion::cmptType
scalar cmptType
Component type.
Definition: quaternion.H:96
vector.H
Foam::quaternion::invTransform
vector invTransform(const vector &v) const
Rotate the given vector anti-clockwise.
Definition: quaternionI.H:337
Foam::quaternion::R
tensor R() const
The rotation tensor corresponding to the quaternion.
Definition: quaternionI.H:358
Foam::quaternion::zero
static const quaternion zero
Definition: quaternion.H:125
Foam::quaternion::YZX
Definition: quaternion.H:108
Foam::conjugate
quaternion conjugate(const quaternion &q)
Return the conjugate of the given quaternion.
Definition: quaternionI.H:658
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H
Foam::rotationTensor
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from vector n1 to n2.
Definition: transform.H:49
Foam::quaternion::XYX
Definition: quaternion.H:105
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::normalize
quaternion normalize(const quaternion &q)
Return the normalized (unit) quaternion of the given quaternion.
Definition: quaternionI.H:671
Foam::quaternion::transform
vector transform(const vector &v) const
Rotate the given vector.
Definition: quaternionI.H:331
Foam::average
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:328
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61
Enum.H