FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

quaternionI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00027 
00028 inline Foam::quaternion::quaternion()
00029 {}
00030 
00031 inline Foam::quaternion::quaternion(const scalar w, const vector& v)
00032 :
00033     w_(w),
00034     v_(v)
00035 {}
00036 
00037 inline Foam::quaternion::quaternion(const vector& d, const scalar theta)
00038 :
00039     w_(cos(0.5*theta)),
00040     v_((sin(0.5*theta)/magSqr(d))*d)
00041 {
00042     normalize();
00043 }
00044 
00045 inline Foam::quaternion::quaternion(const scalar w)
00046 :
00047     w_(w),
00048     v_(vector::zero)
00049 {}
00050 
00051 inline Foam::quaternion::quaternion(const vector& v)
00052 :
00053     w_(0),
00054     v_(v)
00055 {}
00056 
00057 inline Foam::quaternion::quaternion
00058 (
00059     const scalar angleX,
00060     const scalar angleY,
00061     const scalar angleZ
00062 )
00063 {
00064     operator=(quaternion(vector(1, 0, 0), angleX));
00065     operator*=(quaternion(vector(0, 1, 0), angleY));
00066     operator*=(quaternion(vector(0, 0, 1), angleZ));
00067 }
00068 
00069 
00070 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00071 
00072 inline Foam::scalar Foam::quaternion::w() const
00073 {
00074     return w_;
00075 }
00076 
00077 
00078 inline const Foam::vector& Foam::quaternion::v() const
00079 {
00080     return v_;
00081 }
00082 
00083 
00084 inline Foam::scalar& Foam::quaternion::w()
00085 {
00086     return w_;
00087 }
00088 
00089 
00090 inline Foam::vector& Foam::quaternion::v()
00091 {
00092     return v_;
00093 }
00094 
00095 
00096 inline void Foam::quaternion::normalize()
00097 {
00098     operator/=(mag(*this));
00099 }
00100 
00101 
00102 inline Foam::quaternion Foam::quaternion::mulq0v(const vector& u) const
00103 {
00104     return quaternion(-(v() & u), w()*u + (v() ^ u));
00105 }
00106 
00107 
00108 inline Foam::vector Foam::quaternion::transform(const vector& u) const
00109 {
00110     return (mulq0v(u)*conjugate(*this)).v();
00111 }
00112 
00113 
00114 inline Foam::vector Foam::quaternion::invTransform(const vector& u) const
00115 {
00116     return (conjugate(*this).mulq0v(u)*(*this)).v();
00117 }
00118 
00119 
00120 inline Foam::quaternion Foam::quaternion::transform(const quaternion& q) const
00121 {
00122     return Foam::normalize((*this)*q);
00123 }
00124 
00125 
00126 inline Foam::quaternion Foam::quaternion::invTransform
00127 (
00128     const quaternion& q
00129 ) const
00130 {
00131     return Foam::normalize(conjugate(*this)*q);
00132 }
00133 
00134 
00135 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00136 
00137 inline void Foam::quaternion::operator=(const quaternion& q)
00138 {
00139     w_ = q.w_;
00140     v_ = q.v_;
00141 }
00142 
00143 inline void Foam::quaternion::operator+=(const quaternion& q)
00144 {
00145     w_ += q.w_;
00146     v_ += q.v_;
00147 }
00148 
00149 inline void Foam::quaternion::operator-=(const quaternion& q)
00150 {
00151     w_ -= q.w_;
00152     v_ -= q.v_;
00153 }
00154 
00155 inline void Foam::quaternion::operator*=(const quaternion& q)
00156 {
00157     scalar w0 = w();
00158     w() = w()*q.w() - (v() & q.v());
00159     v() = w0*q.v() + q.w()*v() + (v() ^ q.v());
00160 }
00161 
00162 inline void Foam::quaternion::operator/=(const quaternion& q)
00163 {
00164     return operator*=(inv(q));
00165 }
00166 
00167 
00168 inline void Foam::quaternion::operator=(const scalar s)
00169 {
00170     w_ = s;
00171 }
00172 
00173 
00174 inline void Foam::quaternion::operator=(const vector& v)
00175 {
00176     v_ = v;
00177 }
00178 
00179 
00180 inline void Foam::quaternion::operator*=(const scalar s)
00181 {
00182     w_ *= s;
00183     v_ *= s;
00184 }
00185 
00186 inline void Foam::quaternion::operator/=(const scalar s)
00187 {
00188     w_ /= s;
00189     v_ /= s;
00190 }
00191 
00192 
00193 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00194 
00195 inline Foam::scalar Foam::magSqr(const quaternion& q)
00196 {
00197     return magSqr(q.w()) + magSqr(q.v());
00198 }
00199 
00200 
00201 inline Foam::scalar Foam::mag(const quaternion& q)
00202 {
00203     return sqrt(magSqr(q));
00204 }
00205 
00206 
00207 inline Foam::quaternion Foam::conjugate(const quaternion& q)
00208 {
00209     return quaternion(q.w(), -q.v());
00210 }
00211 
00212 
00213 inline Foam::quaternion Foam::inv(const quaternion& q)
00214 {
00215     scalar magSqrq = magSqr(q);
00216     return quaternion(q.w()/magSqrq, -q.v()/magSqrq);
00217 }
00218 
00219 
00220 inline Foam::quaternion Foam::normalize(const quaternion& q)
00221 {
00222     return q/mag(q);
00223 }
00224 
00225 
00226 inline Foam::tensor Foam::quaternion::R() const
00227 {
00228     scalar w2 = sqr(w());
00229     scalar x2 = sqr(v().x());
00230     scalar y2 = sqr(v().y());
00231     scalar z2 = sqr(v().z());
00232 
00233     scalar txy = 2*v().x()*v().y();
00234     scalar twz = 2*w()*v().z();
00235     scalar txz = 2*v().x()*v().z();
00236     scalar twy = 2*w()*v().y();
00237     scalar tyz = 2*v().y()*v().z();
00238     scalar twx = 2*w()*v().x();
00239 
00240     return tensor
00241     (
00242         w2 + x2 - y2 - z2,  txy - twz,          txz + twy,
00243         txy + twz,          w2 - x2 + y2 - z2,  tyz - twx,
00244         txz - twy,          tyz + twx,          w2 - x2 - y2 + z2
00245     );
00246 }
00247 
00248 
00249 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00250 
00251 inline bool Foam::operator==(const quaternion& q1, const quaternion& q2)
00252 {
00253     return (equal(q1.w(), q2.w()) && equal(q1.v(), q2.v()));
00254 }
00255 
00256 
00257 inline bool Foam::operator!=(const quaternion& q1, const quaternion& q2)
00258 {
00259     return !operator==(q1, q2);
00260 }
00261 
00262 
00263 inline Foam::quaternion Foam::operator+
00264 (
00265     const quaternion& q1,
00266     const quaternion& q2
00267 )
00268 {
00269     return quaternion(q1.w() + q2.w(), q1.v() + q2.v());
00270 }
00271 
00272 
00273 inline Foam::quaternion Foam::operator-(const quaternion& q)
00274 {
00275     return quaternion(-q.w(), -q.v());
00276 }
00277 
00278 
00279 inline Foam::quaternion Foam::operator-
00280 (
00281     const quaternion& q1,
00282     const quaternion& q2
00283 )
00284 {
00285     return quaternion(q1.w() - q2.w(), q1.v() - q2.v());
00286 }
00287 
00288 
00289 inline Foam::scalar Foam::operator&(const quaternion& q1, const quaternion& q2)
00290 {
00291     return q1.w()*q2.w() + (q1.v() & q2.v());
00292 }
00293 
00294 
00295 inline Foam::quaternion Foam::operator*
00296 (
00297     const quaternion& q1,
00298     const quaternion& q2
00299 )
00300 {
00301     return quaternion
00302     (
00303         q1.w()*q2.w() - (q1.v() & q2.v()),
00304         q1.w()*q2.v() + q2.w()*q1.v() + (q1.v() ^ q2.v())
00305     );
00306 }
00307 
00308 
00309 inline Foam::quaternion Foam::operator/
00310 (
00311     const quaternion& q1,
00312     const quaternion& q2
00313 )
00314 {
00315     return q1*inv(q2);
00316 }
00317 
00318 
00319 inline Foam::quaternion Foam::operator*(const scalar s, const quaternion& q)
00320 {
00321     return quaternion(s*q.w(), s*q.v());
00322 }
00323 
00324 
00325 inline Foam::quaternion Foam::operator*(const quaternion& q, const scalar s)
00326 {
00327     return quaternion(s*q.w(), s*q.v());
00328 }
00329 
00330 
00331 inline Foam::quaternion Foam::operator/(const quaternion& q, const scalar s)
00332 {
00333     return quaternion(q.w()/s, q.v()/s);
00334 }
00335 
00336 
00337 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines