1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
28 inline Foam::quaternion::quaternion()
31 inline Foam::quaternion::quaternion(const scalar w, const vector& v)
37 inline Foam::quaternion::quaternion(const vector& d, const scalar theta)
40 v_((sin(0.5*theta)/magSqr(d))*d)
45 inline Foam::quaternion::quaternion(const scalar w)
51 inline Foam::quaternion::quaternion(const vector& v)
57 inline Foam::quaternion::quaternion
64 operator=(quaternion(vector(1, 0, 0), angleX));
65 operator*=(quaternion(vector(0, 1, 0), angleY));
66 operator*=(quaternion(vector(0, 0, 1), angleZ));
70 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 inline Foam::scalar Foam::quaternion::w() const
78 inline const Foam::vector& Foam::quaternion::v() const
84 inline Foam::scalar& Foam::quaternion::w()
90 inline Foam::vector& Foam::quaternion::v()
96 inline void Foam::quaternion::normalize()
98 operator/=(mag(*this));
102 inline Foam::quaternion Foam::quaternion::mulq0v(const vector& u) const
104 return quaternion(-(v() & u), w()*u + (v() ^ u));
108 inline Foam::vector Foam::quaternion::transform(const vector& u) const
110 return (mulq0v(u)*conjugate(*this)).v();
114 inline Foam::vector Foam::quaternion::invTransform(const vector& u) const
116 return (conjugate(*this).mulq0v(u)*(*this)).v();
120 inline Foam::quaternion Foam::quaternion::transform(const quaternion& q) const
122 return Foam::normalize((*this)*q);
126 inline Foam::quaternion Foam::quaternion::invTransform
131 return Foam::normalize(conjugate(*this)*q);
135 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
137 inline void Foam::quaternion::operator=(const quaternion& q)
143 inline void Foam::quaternion::operator+=(const quaternion& q)
149 inline void Foam::quaternion::operator-=(const quaternion& q)
155 inline void Foam::quaternion::operator*=(const quaternion& q)
158 w() = w()*q.w() - (v() & q.v());
159 v() = w0*q.v() + q.w()*v() + (v() ^ q.v());
162 inline void Foam::quaternion::operator/=(const quaternion& q)
164 return operator*=(inv(q));
168 inline void Foam::quaternion::operator=(const scalar s)
174 inline void Foam::quaternion::operator=(const vector& v)
180 inline void Foam::quaternion::operator*=(const scalar s)
186 inline void Foam::quaternion::operator/=(const scalar s)
193 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
195 inline Foam::scalar Foam::magSqr(const quaternion& q)
197 return magSqr(q.w()) + magSqr(q.v());
201 inline Foam::scalar Foam::mag(const quaternion& q)
203 return sqrt(magSqr(q));
207 inline Foam::quaternion Foam::conjugate(const quaternion& q)
209 return quaternion(q.w(), -q.v());
213 inline Foam::quaternion Foam::inv(const quaternion& q)
215 scalar magSqrq = magSqr(q);
216 return quaternion(q.w()/magSqrq, -q.v()/magSqrq);
220 inline Foam::quaternion Foam::normalize(const quaternion& q)
226 inline Foam::tensor Foam::quaternion::R() const
228 scalar w2 = sqr(w());
229 scalar x2 = sqr(v().x());
230 scalar y2 = sqr(v().y());
231 scalar z2 = sqr(v().z());
233 scalar txy = 2*v().x()*v().y();
234 scalar twz = 2*w()*v().z();
235 scalar txz = 2*v().x()*v().z();
236 scalar twy = 2*w()*v().y();
237 scalar tyz = 2*v().y()*v().z();
238 scalar twx = 2*w()*v().x();
242 w2 + x2 - y2 - z2, txy - twz, txz + twy,
243 txy + twz, w2 - x2 + y2 - z2, tyz - twx,
244 txz - twy, tyz + twx, w2 - x2 - y2 + z2
249 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
251 inline bool Foam::operator==(const quaternion& q1, const quaternion& q2)
253 return (equal(q1.w(), q2.w()) && equal(q1.v(), q2.v()));
257 inline bool Foam::operator!=(const quaternion& q1, const quaternion& q2)
259 return !operator==(q1, q2);
263 inline Foam::quaternion Foam::operator+
265 const quaternion& q1,
269 return quaternion(q1.w() + q2.w(), q1.v() + q2.v());
273 inline Foam::quaternion Foam::operator-(const quaternion& q)
275 return quaternion(-q.w(), -q.v());
279 inline Foam::quaternion Foam::operator-
281 const quaternion& q1,
285 return quaternion(q1.w() - q2.w(), q1.v() - q2.v());
289 inline Foam::scalar Foam::operator&(const quaternion& q1, const quaternion& q2)
291 return q1.w()*q2.w() + (q1.v() & q2.v());
295 inline Foam::quaternion Foam::operator*
297 const quaternion& q1,
303 q1.w()*q2.w() - (q1.v() & q2.v()),
304 q1.w()*q2.v() + q2.w()*q1.v() + (q1.v() ^ q2.v())
309 inline Foam::quaternion Foam::operator/
311 const quaternion& q1,
319 inline Foam::quaternion Foam::operator*(const scalar s, const quaternion& q)
321 return quaternion(s*q.w(), s*q.v());
325 inline Foam::quaternion Foam::operator*(const quaternion& q, const scalar s)
327 return quaternion(s*q.w(), s*q.v());
331 inline Foam::quaternion Foam::operator/(const quaternion& q, const scalar s)
333 return quaternion(q.w()/s, q.v()/s);
337 // ************************************************************************* //