Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
67 is >> *this;
68}
69
70
71// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72
73Foam::word Foam::name(const quaternion& q)
75 OStringStream buf;
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
99Foam::quaternion Foam::average
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];
118 }
119
120 return qa;
121}
122
123
124Foam::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
137 expW*cos(magV),
138 expW*sin(magV)*q.v()/magV
139 );
140}
141
142
143Foam::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
160Foam::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
179Foam::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
190Foam::Ostream& Foam::operator<<(Ostream& os, const quaternion& q)
191{
193 << q.w() << token::SPACE << q.v()
195
196 return os;
197}
198
199
200// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Input/output from string buffers.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition Istream.C:152
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition Istream.C:134
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future).
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Quaternion class used to perform rotations in 3D space.
Definition quaternion.H:54
const vector & v() const noexcept
Vector part of the quaternion ( = axis of rotation).
scalar w() const noexcept
Scalar part of the quaternion ( = cos(theta/2) for rotation).
quaternion()=default
Default construct.
static const Enum< eulerOrder > eulerOrderNames
The names for Euler-angle and Tait-Bryan angles, including "rollPitchYaw" and "yawPitchRoll" aliases.
Definition quaternion.H:132
static const quaternion I
Definition quaternion.H:148
static const quaternion zero
Definition quaternion.H:147
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
@ SPACE
Space [isspace].
Definition token.H:144
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
dimensionedScalar exp(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar sin(const dimensionedScalar &ds)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Istream & operator>>(Istream &, directionInfo &)
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition quaternion.C:75
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &f1, const label comm)
dimensionedScalar cos(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)