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 // ************************************************************************* //
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar acos(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
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
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
scalar w() const
Scalar part of the quaternion ( = cos(theta/2) for rotation)
Definition: quaternionI.H:254
quaternion()
Construct null.
Definition: quaternionI.H:28
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
static const char *const typeName
Definition: quaternion.H:115
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:117
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
dimensionedScalar cos(const dimensionedScalar &ds)
dimensionedScalar exp(const dimensionedScalar &ds)
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
const vector & v() const
Vector part of the quaternion ( = axis of rotation)
Definition: quaternionI.H:260
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:60
static const zero Zero
Definition: zero.H:97
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
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:60
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
dimensionedScalar sin(const dimensionedScalar &ds)
static const quaternion I
Definition: quaternion.H:118
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition: quaternion.C:55
Ostream & operator<<(Ostream &, const ensightPart &)
string str() const
Return the string.
dimensioned< scalar > mag(const dimensioned< Type > &)
Output to memory buffer stream.
Definition: OStringStream.H:49