QuaternionT.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS Quaternion
00048 //
00049 //=============================================================================
00050 
00051 #ifndef ACG_QUATERNION_HH
00052 #define ACG_QUATERNION_HH
00053 
00054 
00055 //== INCLUDES =================================================================
00056 
00057 #include "VectorT.hh"
00058 #include "Matrix4x4T.hh"
00059 
00060 
00061 //== NAMESPACES  ==============================================================
00062 
00063 namespace ACG {
00064 
00065 
00066 //== CLASS DEFINITION =========================================================
00067 
00068 
00073 template <class Scalar>
00074 class QuaternionT : public VectorT<Scalar,4>
00075 {
00076 public:
00077 
00078 #define W Base::values_[0]
00079 #define X Base::values_[1]
00080 #define Y Base::values_[2]
00081 #define Z Base::values_[3]
00082 
00083 
00084   typedef VectorT<Scalar,4>    Base;
00085   typedef QuaternionT<Scalar>  Quaternion;
00086   typedef VectorT<Scalar,3>    Vec3;
00087   typedef VectorT<Scalar,4>    Vec4;
00088   typedef Matrix4x4T<Scalar>   Matrix;
00089 
00090 
00092   QuaternionT(Scalar _w=1.0, Scalar _x=0.0, Scalar _y=0.0, Scalar _z=0.0)
00093     : Vec4(_w, _x, _y, _z) {}
00094 
00096   QuaternionT(const Vec3& _p)
00097     : Vec4(0, _p[0], _p[1], _p[2]) {}
00098 
00100   QuaternionT(const Vec4& _p)
00101     : Vec4(_p[0], _p[1], _p[2], _p[3]) {}
00102 
00104   QuaternionT(Vec3 _axis, Scalar _angle)
00105   {
00106     _axis.normalize();
00107     Scalar theta = 0.5 * _angle;
00108     Scalar sin_theta = sin(theta);
00109     W = cos(theta);
00110     X = sin_theta * _axis[0];
00111     Y = sin_theta * _axis[1];
00112     Z = sin_theta * _axis[2];
00113   }
00114 
00115 
00117   void identity() { W=1.0; X=Y=Z=0.0; }
00118 
00119 
00121   Quaternion conjugate() const
00122   { return Quaternion(W, -X, -Y, -Z); }
00123 
00124 
00126   Quaternion invert() const
00127   { return conjugate()/Base::sqrnorm(); }
00128 
00129 
00131   Quaternion operator*(const Quaternion& _q) const
00132   { return Quaternion(W*_q.W - X*_q.X - Y*_q.Y - Z*_q.Z,
00133                       W*_q.X + X*_q.W + Y*_q.Z - Z*_q.Y,
00134                       W*_q.Y - X*_q.Z + Y*_q.W + Z*_q.X,
00135                       W*_q.Z + X*_q.Y - Y*_q.X + Z*_q.W); }
00136 
00137 
00139   Quaternion& operator*=(const Quaternion& _q)
00140   { return *this = *this * _q; }
00141 
00142 
00144   Vec3 rotate(const Vec3& _v)
00145   { 
00146     Quaternion q = *this * Quaternion(_v) * conjugate();
00147     return Vec3(q[1], q[2], q[3]);
00148   }
00149 
00150 
00152   void axis_angle(Vec3& _axis, Scalar& _angle) const
00153   {
00154     if (fabs(W) > 0.999999)
00155     {
00156       _axis  = Vec3(1,0,0);
00157       _angle = 0;
00158     }
00159     else
00160     {
00161       _angle = 2.0 * acos(W);
00162       _axis  = Vec3(X, Y, Z).normalize();
00163     }
00164   }
00165 
00166 
00167 
00169   Matrix rotation_matrix() const
00170   {
00171     Scalar
00172       ww(W*W), xx(X*X), yy(Y*Y), zz(Z*Z), wx(W*X),
00173       wy(W*Y), wz(W*Z), xy(X*Y), xz(X*Z), yz(Y*Z);
00174 
00175     Matrix m;
00176 
00177     m(0,0) = ww + xx - yy - zz;
00178     m(1,0) = 2.0*(xy + wz);
00179     m(2,0) = 2.0*(xz - wy);
00180 
00181     m(0,1) = 2.0*(xy - wz);
00182     m(1,1) = ww - xx + yy - zz;
00183     m(2,1) = 2.0*(yz + wx);
00184 
00185     m(0,2) = 2.0*(xz + wy);
00186     m(1,2) = 2.0*(yz - wx);
00187     m(2,2) = ww - xx - yy + zz;
00188 
00189     m(0,3) = m(1,3) = m(2,3) = m(3,0) = m(3,1) = m(3,2) = 0.0;
00190     m(3,3) = 1.0;
00191 
00192     return m;
00193   }
00194 
00195 
00196 
00198   Matrix right_mult_matrix() const
00199   {
00200     Matrix m;
00201     m(0,0) =  W; m(0,1) = -X; m(0,2) = -Y; m(0,3) = -Z;
00202     m(1,0) =  X; m(1,1) =  W; m(1,2) = -Z; m(1,3) =  Y;
00203     m(2,0) =  Y; m(2,1) =  Z; m(2,2) =  W; m(2,3) = -X;
00204     m(3,0) =  Z; m(3,1) = -Y; m(3,2) =  X; m(3,3) =  W;
00205     return m;
00206   }
00207 
00208 
00210   Matrix left_mult_matrix() const
00211   {
00212     Matrix m;
00213     m(0,0) =  W; m(0,1) = -X; m(0,2) = -Y; m(0,3) = -Z;
00214     m(1,0) =  X; m(1,1) =  W; m(1,2) =  Z; m(1,3) = -Y;
00215     m(2,0) =  Y; m(2,1) = -Z; m(2,2) =  W; m(2,3) =  X;
00216     m(3,0) =  Z; m(3,1) =  Y; m(3,2) = -X; m(3,3) =  W;
00217     return m;
00218   }
00219 
00220 
00221 #undef W
00222 #undef X
00223 #undef Y
00224 #undef Z
00225 };
00226 
00227 
00228 typedef QuaternionT<float>  Quaternionf;
00229 typedef QuaternionT<double> Quaterniond;
00230 
00231 
00232 //=============================================================================
00233 } // namespace ACG
00234 //=============================================================================
00235 #endif // ACG_QUATERNION_HH defined
00236 //=============================================================================
00237 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .