1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 inline Foam::quaternion::quaternion()
32 inline Foam::quaternion::quaternion(const scalar w, const vector& v)
38 inline Foam::quaternion::quaternion(const vector& d, const scalar theta)
41 v_((sin(0.5*theta)/magSqr(d))*d)
46 inline Foam::quaternion::quaternion(const scalar w)
52 inline Foam::quaternion::quaternion(const vector& v)
58 inline Foam::quaternion::quaternion
65 operator=(quaternion(vector(1, 0, 0), angleX));
66 operator*=(quaternion(vector(0, 1, 0), angleY));
67 operator*=(quaternion(vector(0, 0, 1), angleZ));
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
73 inline Foam::scalar Foam::quaternion::w() const
79 inline const Foam::vector& Foam::quaternion::v() const
85 inline Foam::scalar& Foam::quaternion::w()
91 inline Foam::vector& Foam::quaternion::v()
97 inline void Foam::quaternion::normalize()
99 operator/=(mag(*this));
103 inline Foam::quaternion Foam::quaternion::mulq0v(const vector& u) const
105 return quaternion(-(v() & u), w()*u + (v() ^ u));
109 inline Foam::vector Foam::quaternion::transform(const vector& u) const
111 return (mulq0v(u)*conjugate(*this)).v();
115 inline Foam::vector Foam::quaternion::invTransform(const vector& u) const
117 return (conjugate(*this).mulq0v(u)*(*this)).v();
121 inline Foam::quaternion Foam::quaternion::transform(const quaternion& q) const
123 return Foam::normalize((*this)*q);
127 inline Foam::quaternion Foam::quaternion::invTransform
132 return Foam::normalize(conjugate(*this)*q);
136 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
138 inline void Foam::quaternion::operator=(const quaternion& q)
144 inline void Foam::quaternion::operator+=(const quaternion& q)
150 inline void Foam::quaternion::operator-=(const quaternion& q)
156 inline void Foam::quaternion::operator*=(const quaternion& q)
159 w() = w()*q.w() - (v() & q.v());
160 v() = w0*q.v() + q.w()*v() + (v() ^ q.v());
163 inline void Foam::quaternion::operator/=(const quaternion& q)
165 return operator*=(inv(q));
169 inline void Foam::quaternion::operator=(const scalar s)
175 inline void Foam::quaternion::operator=(const vector& v)
181 inline void Foam::quaternion::operator*=(const scalar s)
187 inline void Foam::quaternion::operator/=(const scalar s)
194 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
196 inline Foam::scalar Foam::magSqr(const quaternion& q)
198 return magSqr(q.w()) + magSqr(q.v());
202 inline Foam::scalar Foam::mag(const quaternion& q)
204 return sqrt(magSqr(q));
208 inline Foam::quaternion Foam::conjugate(const quaternion& q)
210 return quaternion(q.w(), -q.v());
214 inline Foam::quaternion Foam::inv(const quaternion& q)
216 scalar magSqrq = magSqr(q);
217 return quaternion(q.w()/magSqrq, -q.v()/magSqrq);
221 inline Foam::quaternion Foam::normalize(const quaternion& q)
227 inline Foam::tensor Foam::quaternion::R() const
229 scalar w2 = sqr(w());
230 scalar x2 = sqr(v().x());
231 scalar y2 = sqr(v().y());
232 scalar z2 = sqr(v().z());
234 scalar txy = 2*v().x()*v().y();
235 scalar twz = 2*w()*v().z();
236 scalar txz = 2*v().x()*v().z();
237 scalar twy = 2*w()*v().y();
238 scalar tyz = 2*v().y()*v().z();
239 scalar twx = 2*w()*v().x();
243 w2 + x2 - y2 - z2, txy - twz, txz + twy,
244 txy + twz, w2 - x2 + y2 - z2, tyz - twx,
245 txz - twy, tyz + twx, w2 - x2 - y2 + z2
250 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
252 inline bool Foam::operator==(const quaternion& q1, const quaternion& q2)
254 return (equal(q1.w(), q2.w()) && equal(q1.v(), q2.v()));
258 inline bool Foam::operator!=(const quaternion& q1, const quaternion& q2)
260 return !operator==(q1, q2);
264 inline Foam::quaternion Foam::operator+
266 const quaternion& q1,
270 return quaternion(q1.w() + q2.w(), q1.v() + q2.v());
274 inline Foam::quaternion Foam::operator-(const quaternion& q)
276 return quaternion(-q.w(), -q.v());
280 inline Foam::quaternion Foam::operator-
282 const quaternion& q1,
286 return quaternion(q1.w() - q2.w(), q1.v() - q2.v());
290 inline Foam::scalar Foam::operator&(const quaternion& q1, const quaternion& q2)
292 return q1.w()*q2.w() + (q1.v() & q2.v());
296 inline Foam::quaternion Foam::operator*
298 const quaternion& q1,
304 q1.w()*q2.w() - (q1.v() & q2.v()),
305 q1.w()*q2.v() + q2.w()*q1.v() + (q1.v() ^ q2.v())
310 inline Foam::quaternion Foam::operator/
312 const quaternion& q1,
320 inline Foam::quaternion Foam::operator*(const scalar s, const quaternion& q)
322 return quaternion(s*q.w(), s*q.v());
326 inline Foam::quaternion Foam::operator*(const quaternion& q, const scalar s)
328 return quaternion(s*q.w(), s*q.v());
332 inline Foam::quaternion Foam::operator/(const quaternion& q, const scalar s)
334 return quaternion(q.w()/s, q.v()/s);
338 // ************************************************************************* //