quaternion.C
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-2022 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 \*---------------------------------------------------------------------------*/
28 
29 #include "quaternion.H"
30 #include "IOstreams.H"
31 #include "StringStream.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
37 
40 ({
41  // Proper Euler angles
42  { eulerOrder::XZX, "xzx" },
43  { eulerOrder::XYX, "xyx" },
44  { eulerOrder::YXY, "yxy" },
45  { eulerOrder::YZY, "yzy" },
46  { eulerOrder::ZYZ, "zyz" },
47  { eulerOrder::ZXZ, "zxz" },
48 
49  // Tait-Bryan angles
50  { eulerOrder::XZY, "xzy" },
51  { eulerOrder::XYZ, "xyz" },
52  { eulerOrder::YXZ, "yxz" },
53  { eulerOrder::YZX, "yzx" },
54  { eulerOrder::ZYX, "zyx" },
55  { eulerOrder::ZXY, "zxy" },
56 
57  // Aliases
58  { eulerOrder::XYZ, "rollPitchYaw" },
59  { eulerOrder::ZYX, "yawPitchRoll" },
60 });
61 
62 
63 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
64 
66 {
67  is >> *this;
68 }
69 
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 Foam::word Foam::name(const quaternion& q)
74 {
76  buf << '(' << q.w() << ',' << q.v() << ')';
77  return buf.str();
78 }
79 
80 
82 (
83  const quaternion& qa,
84  const quaternion& qb,
85  const scalar t
86 )
87 {
88  label sign = 1;
89 
90  if ((qa & qb) < 0)
91  {
92  sign = -1;
93  }
94 
95  return qa*pow((inv(qa)*sign*qb), t);
96 }
97 
98 
100 (
101  const UList<quaternion>& qs,
102  const UList<scalar> w
103 )
104 {
105  quaternion qa(w[0]*qs[0]);
106 
107  for (label i=1; i<qs.size(); i++)
108  {
109  // Invert quaternion if it has the opposite sign to the average
110  if ((qa & qs[i]) > 0)
111  {
112  qa += w[i]*qs[i];
113  }
114  else
115  {
116  qa -= w[i]*qs[i];
117  }
118  }
119 
120  return qa;
121 }
122 
123 
124 Foam::quaternion Foam::exp(const quaternion& q)
125 {
126  const scalar magV = mag(q.v());
127 
128  if (magV == 0)
129  {
130  return quaternion(1, Zero);
131  }
132 
133  const scalar expW = exp(q.w());
134 
135  return quaternion
136  (
137  expW*cos(magV),
138  expW*sin(magV)*q.v()/magV
139  );
140 }
141 
142 
143 Foam::quaternion Foam::pow(const quaternion& q, const label power)
144 {
145  const scalar magQ = mag(q);
146  const scalar magV = mag(q.v());
147 
148  quaternion powq(q.v());
149 
150  if (magV != 0 && magQ != 0)
151  {
152  powq /= magV;
153  powq *= power*acos(q.w()/magQ);
154  }
155 
156  return pow(magQ, power)*exp(powq);
157 }
158 
159 
160 Foam::quaternion Foam::pow(const quaternion& q, const scalar power)
161 {
162  const scalar magQ = mag(q);
163  const scalar magV = mag(q.v());
164 
165  quaternion powq(q.v());
166 
167  if (magV != 0 && magQ != 0)
168  {
169  powq /= magV;
170  powq *= power*acos(q.w()/magQ);
171  }
173  return pow(magQ, power)*exp(powq);
174 }
175 
176 
177 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
178 
179 Foam::Istream& Foam::operator>>(Istream& is, quaternion& q)
180 {
181  is.readBegin("quaternion");
182  is >> q.w() >> q.v();
183  is.readEnd("quaternion");
184 
185  is.check(FUNCTION_NAME);
186  return is;
187 }
188 
189 
190 Foam::Ostream& Foam::operator<<(Ostream& os, const quaternion& q)
191 {
193  << q.w() << token::SPACE << q.v()
194  << token::END_LIST;
195 
196  return os;
197 }
198 
199 
200 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &f1)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:96
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
Input/output from string buffers.
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Begin list [isseparator].
Definition: token.H:161
static const Enum< eulerOrder > eulerOrderNames
The names for Euler-angle and Tait-Bryan angles, including "rollPitchYaw" and "yawPitchRoll" aliases...
Definition: quaternion.H:132
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static const quaternion zero
Definition: quaternion.H:147
dimensionedScalar cos(const dimensionedScalar &ds)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
dimensionedScalar exp(const dimensionedScalar &ds)
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:131
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:53
End list [isseparator].
Definition: token.H:162
Vector< scalar > vector
Definition: vector.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
dimensionedScalar sin(const dimensionedScalar &ds)
quaternion()=default
Default construct.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
static const quaternion I
Definition: quaternion.H:148
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition: quaternion.C:75
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127