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-2020 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  // Generated Methods
131 
132  //- Default construct
133  quaternion() = default;
134 
135  //- Copy construct
136  quaternion(const quaternion&) = default;
137 
138  //- Copy assignment
139  quaternion& operator=(const quaternion&) = default;
140 
141 
142  // Constructors
143 
144  //- Construct zero initialized
145  inline quaternion(const Foam::zero);
146 
147  //- Construct given scalar and vector parts
148  inline quaternion(const scalar w, const vector& v);
149 
150  //- Construct rotation quaternion given direction d and angle theta
151  inline quaternion(const vector& d, const scalar theta);
152 
153  //- Construct a rotation quaternion given direction d
154  //- and cosine angle cosTheta and flag if d is normalized
155  inline quaternion
156  (
157  const vector& d,
158  const scalar cosTheta,
159  const bool normalized
160  );
161 
162  //- Construct a real quaternion from the given scalar part,
163  //- the vector part = zero
164  inline explicit quaternion(const scalar w);
165 
166  //- Construct a pure imaginary quaternion given the vector part,
167  //- the scalar part = 0
168  inline explicit quaternion(const vector& v);
169 
170  //- Return the unit quaternion (versor) from the given vector
171  //- (w = sqrt(1 - |sqr(v)|))
172  static inline quaternion unit(const vector& v);
173 
174  //- Construct from three Euler rotation angles
175  inline quaternion(const eulerOrder order, const vector& angles);
176 
177  //- Construct from a rotation tensor
178  inline explicit quaternion(const tensor& rotationTensor);
179 
180  //- Construct from Istream
181  explicit quaternion(Istream& is);
182 
183 
184  // Member Functions
185 
186  // Access
187 
188  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
189  inline scalar w() const;
190 
191  //- Vector part of the quaternion ( = axis of rotation)
192  inline const vector& v() const;
193 
194  //- The rotation tensor corresponding to the quaternion
195  inline tensor R() const;
196 
197  //- Return the Euler rotation angles corresponding to the
198  //- specified rotation order
199  inline vector eulerAngles(const eulerOrder order) const;
200 
201  //- Return the quaternion normalized by its magnitude
202  inline quaternion normalized() const;
203 
204 
205  // Edit
206 
207  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
208  inline scalar& w();
209 
210  //- Vector part of the quaternion ( = axis of rotation)
211  inline vector& v();
212 
213  //- Normalize the quaternion by its magnitude
214  inline void normalize();
215 
216 
217  // Transform
218 
219  //- Rotate the given vector
220  inline vector transform(const vector& v) const;
221 
222  //- Rotate the given vector anti-clockwise
223  inline vector invTransform(const vector& v) const;
224 
225  //- Rotate the given quaternion (and normalize)
226  inline quaternion transform(const quaternion& q) const;
227 
228  //- Rotate the given quaternion anti-clockwise (and normalize)
229  inline quaternion invTransform(const quaternion& q) const;
230 
231 
232  // Member Operators
233 
234  inline void operator+=(const quaternion& q);
235  inline void operator-=(const quaternion& q);
236  inline void operator*=(const quaternion& q);
237  inline void operator/=(const quaternion& q);
238 
239  //- Change scalar portion only
240  inline void operator=(const scalar s);
241 
242  //- Change vector portion only
243  inline void operator=(const vector& v);
244 
245  inline void operator*=(const scalar s);
246  inline void operator/=(const scalar s);
247 };
248 
249 
250 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
251 
252 //- Contiguous data for quaternion
253 template<> struct is_contiguous<quaternion> : std::true_type {};
254 
255 //- Contiguous scalar data for quaternion
256 template<> struct is_contiguous_scalar<quaternion> : std::true_type {};
257 
258 
259 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
260 
261 inline scalar magSqr(const quaternion& q);
262 inline scalar mag(const quaternion& q);
263 
264 //- Return the conjugate of the given quaternion
265 inline quaternion conjugate(const quaternion& q);
266 
267 //- Return the normalized (unit) quaternion of the given quaternion
268 inline quaternion normalize(const quaternion& q);
269 
270 //- Return the inverse of the given quaternion
271 inline quaternion inv(const quaternion& q);
272 
273 //- Return a string representation of a quaternion
274 word name(const quaternion& q);
275 
276 //- Spherical linear interpolation of quaternions
278 (
279  const quaternion& qa,
280  const quaternion& qb,
281  const scalar t
282 );
283 
284 //- Simple weighted average with sign change
286 (
287  const UList<quaternion>& qs,
288  const UList<scalar> w
289 );
290 
291 //- Exponent of a quaternion
292 quaternion exp(const quaternion& q);
293 
294 //- Power of a quaternion
295 quaternion pow(const quaternion& q, const label power);
296 
297 //- Power of a quaternion
298 quaternion pow(const quaternion& q, const scalar power);
299 
300 
301 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
302 
304 Ostream& operator<<(Ostream& os, const quaternion& q);
305 
306 inline bool operator==(const quaternion& q1, const quaternion& q2);
307 inline bool operator!=(const quaternion& q1, const quaternion& q2);
308 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
309 inline quaternion operator-(const quaternion& q);
310 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
311 inline scalar operator&(const quaternion& q1, const quaternion& q2);
312 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
313 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
314 inline quaternion operator*(const scalar s, const quaternion& q);
315 inline quaternion operator*(const quaternion& q, const scalar s);
316 inline quaternion operator/(const quaternion& q, const scalar s);
317 
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
321 } // End namespace Foam
322 
323 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
324 
325 #include "quaternionI.H"
326 
327 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328 
329 #endif
330 
331 // ************************************************************************* //
Foam::Tensor< scalar >
Foam::quaternion::normalize
void normalize()
Normalize the quaternion by its magnitude.
Definition: quaternionI.H:315
Foam::Enum< eulerOrder >
Foam::quaternion::ZYZ
Definition: quaternion.H:105
Foam::quaternion::operator+=
void operator+=(const quaternion &q)
Definition: quaternionI.H:584
Foam::quaternion::v
const vector & v() const
Vector part of the quaternion ( = axis of rotation)
Definition: quaternionI.H:291
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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=
quaternion & operator=(const quaternion &)=default
Copy assignment.
Foam::quaternion::operator/=
void operator/=(const quaternion &q)
Definition: quaternionI.H:603
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:309
Foam::quaternion::operator-=
void operator-=(const quaternion &q)
Definition: quaternionI.H:590
Foam::quaternion::I
static const quaternion I
Definition: quaternion.H:126
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::quaternion::eulerAngles
vector eulerAngles(const eulerOrder order) const
Definition: quaternionI.H:404
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:285
tensor.H
Foam::quaternion::quaternion
quaternion()=default
Default construct.
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:239
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::quaternion::rank
static constexpr direction rank
Rank of quaternion is 1.
Definition: quaternion.H:118
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::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
os
OBJstream os(runTime.globalPath()/outputName)
Foam::quaternion::unit
static quaternion unit(const vector &v)
Definition: quaternionI.H:87
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::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:596
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:52
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::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::quaternion::invTransform
vector invTransform(const vector &v) const
Rotate the given vector anti-clockwise.
Definition: quaternionI.H:333
Foam::quaternion::R
tensor R() const
The rotation tensor corresponding to the quaternion.
Definition: quaternionI.H:354
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:648
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:51
Foam::quaternion::XYX
Definition: quaternion.H:105
Foam::normalize
quaternion normalize(const quaternion &q)
Return the normalized (unit) quaternion of the given quaternion.
Definition: quaternionI.H:661
Foam::quaternion::transform
vector transform(const vector &v) const
Rotate the given vector.
Definition: quaternionI.H:327
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) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Enum.H