quaternion.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 \*---------------------------------------------------------------------------*/
25 
26 #include "quaternion.H"
27 #include "IOstreams.H"
28 #include "OStringStream.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 const char* const Foam::quaternion::typeName = "quaternion";
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
39 {
40  is >> *this;
41 }
42 
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
47 {
48  OStringStream buf;
49  buf << '(' << q.w() << ',' << q.v() << ')';
50  return buf.str();
51 }
52 
53 
55 (
56  const quaternion& qa,
57  const quaternion& qb,
58  const scalar t
59 )
60 {
61  label sign = 1;
62 
63  if ((qa & qb) < 0)
64  {
65  sign = -1;
66  }
67 
68  return qa*pow((inv(qa)*sign*qb), t);
69 }
70 
71 
73 (
74  const UList<quaternion>& qs,
75  const UList<scalar> w
76 )
77 {
78  quaternion qa(w[0]*qs[0]);
79 
80  for (label i=1; i<qs.size(); i++)
81  {
82  // Invert quaternion if it has the opposite sign to the average
83  if ((qa & qs[i]) > 0)
84  {
85  qa += w[i]*qs[i];
86  }
87  else
88  {
89  qa -= w[i]*qs[i];
90  }
91  }
92 
93  return qa;
94 }
95 
96 
98 {
99  const scalar magV = mag(q.v());
100 
101  if (magV == 0)
102  {
103  return quaternion(1, Zero);
104  }
105 
106  const scalar expW = exp(q.w());
107 
108  return quaternion
109  (
110  expW*cos(magV),
111  expW*sin(magV)*q.v()/magV
112  );
113 }
114 
115 
117 {
118  const scalar magQ = mag(q);
119  const scalar magV = mag(q.v());
120 
121  quaternion powq(q.v());
122 
123  if (magV != 0 && magQ != 0)
124  {
125  powq /= magV;
126  powq *= power*acos(q.w()/magQ);
127  }
128 
129  return pow(magQ, power)*exp(powq);
130 }
131 
132 
133 Foam::quaternion Foam::pow(const quaternion& q, const scalar power)
134 {
135  const scalar magQ = mag(q);
136  const scalar magV = mag(q.v());
137 
138  quaternion powq(q.v());
139 
140  if (magV != 0 && magQ != 0)
141  {
142  powq /= magV;
143  powq *= power*acos(q.w()/magQ);
144  }
145 
146  return pow(magQ, power)*exp(powq);
147 }
148 
149 
150 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
151 
153 {
154  // Read beginning of quaternion
155  is.readBegin("quaternion");
156 
157  is >> q.w() >> q.v();
158 
159  // Read end of quaternion
160  is.readEnd("quaternion");
161 
162  // Check state of Istream
163  is.check("operator>>(Istream&, quaternion&)");
164 
165  return is;
166 }
167 
168 
170 {
171  os << token::BEGIN_LIST
172  << q.w() << token::SPACE << q.v()
173  << token::END_LIST;
174 
175  return os;
176 }
177 
178 
179 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
Output to memory buffer stream.
Definition: OStringStream.H:52
string str() const
Return the string.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
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:74
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:61
const vector & v() const
Vector part of the quaternion ( = axis of rotation)
Definition: quaternionI.H:260
quaternion()
Construct null.
Definition: quaternionI.H:28
scalar w() const
Scalar part of the quaternion ( = cos(theta/2) for rotation)
Definition: quaternionI.H:254
static const quaternion I
Definition: quaternion.H:118
static const quaternion zero
Definition: quaternion.H:117
static const char *const typeName
Definition: quaternion.H:115
@ BEGIN_LIST
Definition: token.H:106
@ END_LIST
Definition: token.H:107
A class for handling words, derived from string.
Definition: word.H:62
static const zero Zero
Definition: zero.H:97
dimensionedScalar exp(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
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
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
dimensionedScalar sin(const dimensionedScalar &ds)
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Istream & operator>>(Istream &, directionInfo &)
dimensioned< scalar > mag(const dimensioned< Type > &)
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
Ostream & operator<<(Ostream &, const ensightPart &)
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition: quaternion.C:55
dimensionedScalar cos(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)