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 | Copyright (C) 2011-2013 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 scalar magV = mag(q.v());
75 
76  if (magV == 0)
77  {
78  return quaternion(1, vector::zero);
79  }
80 
81  const scalar expW = exp(q.w());
82 
83  return quaternion
84  (
85  expW*cos(magV),
86  expW*sin(magV)*q.v()/magV
87  );
88 }
89 
90 
92 {
93  const scalar magQ = mag(q);
94  const scalar magV = mag(q.v());
95 
96  quaternion powq(q.v());
97 
98  if (magV != 0 && magQ != 0)
99  {
100  powq /= magV;
101  powq *= power*acos(q.w()/magQ);
102  }
103 
104  return pow(magQ, power)*exp(powq);
105 }
106 
107 
108 Foam::quaternion Foam::pow(const quaternion& q, const scalar power)
109 {
110  const scalar magQ = mag(q);
111  const scalar magV = mag(q.v());
112 
113  quaternion powq(q.v());
114 
115  if (magV != 0 && magQ != 0)
116  {
117  powq /= magV;
118  powq *= power*acos(q.w()/magQ);
119  }
120 
121  return pow(magQ, power)*exp(powq);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
126 
128 {
129  // Read beginning of quaternion
130  is.readBegin("quaternion");
131 
132  is >> q.w() >> q.v();
133 
134  // Read end of quaternion
135  is.readEnd("quaternion");
136 
137  // Check state of Istream
138  is.check("operator>>(Istream&, quaternion&)");
139 
140  return is;
141 }
142 
143 
145 {
146  os << token::BEGIN_LIST
147  << q.w() << token::SPACE << q.v()
148  << token::END_LIST;
149 
150  return os;
151 }
152 
153 
154 // ************************************************************************* //
Foam::Istream::readEnd
Istream & readEnd(const char *funcName)
Definition: Istream.C:105
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::Vector< scalar >::zero
static const Vector zero
Definition: Vector.H:80
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::slerp
quaternion slerp(const quaternion &qa, const quaternion &qb, const scalar t)
Spherical linear interpolation of quaternions.
Definition: quaternion.C:55
quaternion.H
Foam::sin
dimensionedScalar sin(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:255
Foam::quaternion::typeName
static const char *const typeName
Definition: quaternion.H:90
Foam::quaternion::I
static const quaternion I
Definition: quaternion.H:93
Foam::exp
dimensionedScalar exp(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:252
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::sign
dimensionedScalar sign(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:179
Foam::OStringStream::str
string str() const
Return the string.
Definition: OStringStream.H:107
Foam::quaternion
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:60
Foam::label
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
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::inv
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
Definition: dimensionedSphericalTensor.C:71
OStringStream.H
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:73
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Foam::quaternion::quaternion
quaternion()
Construct null.
Definition: quaternionI.H:28
Foam::quaternion::w
scalar w() const
Scalar part of the quaternion ( = cos(theta/2) for rotation)
Definition: quaternionI.H:166
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::acos
dimensionedScalar acos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:259
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::quaternion::v
const vector & v() const
Vector part of the quaternion ( = axis of rotation)
Definition: quaternionI.H:172
Foam::quaternion::zero
static const quaternion zero
Definition: quaternion.H:92
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::Istream::readBegin
Istream & readBegin(const char *funcName)
Definition: Istream.C:88
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
Foam::token::SPACE
@ SPACE
Definition: token.H:95
Foam::cos
dimensionedScalar cos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:256