Crazy Eddie's GUI System  0.8.4
Quaternion.h
00001 /***********************************************************************
00002         created:        2/1/2011
00003         author:         Martin Preisler
00004         
00005         purpose:        Defines interface for the Quaternion class
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUIQuaternion_h_
00030 #define _CEGUIQuaternion_h_
00031 
00032 #include "CEGUI/Base.h"
00033 #include "CEGUI/Interpolator.h"
00034 #include "CEGUI/Vector.h"
00035 #include <cmath>
00036 
00037 // Start of CEGUI namespace section
00038 namespace CEGUI
00039 {
00040 
00067 class CEGUIEXPORT Quaternion :
00068     public AllocatedObject<Quaternion>
00069 {
00070 public:
00072     inline Quaternion(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f):
00073         d_w(w),
00074         d_x(x),
00075         d_y(y),
00076         d_z(z)
00077         {}
00078 
00080     inline Quaternion(const Quaternion& v):
00081         d_w(v.d_w),
00082         d_x(v.d_x),
00083         d_y(v.d_y),
00084         d_z(v.d_z)
00085     {}
00086 
00088     inline Quaternion& operator = (const Quaternion& v)
00089         {
00090         d_w = v.d_w;
00091         d_x = v.d_x;
00092         d_y = v.d_y;
00093         d_z = v.d_z;
00094         
00095         return *this;
00096         }
00097 
00110     static Quaternion eulerAnglesRadians(float x, float y, float z);
00111 
00124     static Quaternion eulerAnglesDegrees(float x, float y, float z);
00125 
00134     static Quaternion axisAngleRadians(const Vector3f& axis, float rotation);
00135 
00144     static Quaternion axisAngleDegrees(const Vector3f& axis, float rotation);
00145 
00147     inline bool operator == (const Quaternion& v) const
00148         {
00149                 return (d_w == v.d_w) && (d_x == v.d_x) && (d_y == v.d_y) && (d_z == v.d_z);
00150         }
00151 
00153     inline bool operator != (const Quaternion& v) const
00154         {
00155                 return (d_w != v.d_w) || (d_x != v.d_x) || (d_y != v.d_y) || (d_z != v.d_z);
00156         }
00157 
00159     inline Quaternion operator - () const
00160     {
00161         return Quaternion(-d_w, -d_x, -d_y, -d_z);
00162     }
00163 
00165     inline Quaternion operator * (float v) const
00166     {
00167         return Quaternion(d_w * v, d_x * v, d_y * v, d_z * v);
00168     }
00169 
00171     inline friend Quaternion operator * (float v, const Quaternion& q)
00172     {
00173         return Quaternion(v * q.d_w, v * q.d_x, v * q.d_y, v * q.d_z);
00174     }
00175 
00177     inline float dot(const Quaternion& v) const
00178     {
00179         return d_w * v.d_w + d_x * v.d_x + d_y * v.d_y + d_z * v.d_z;
00180     }
00181 
00183     inline Quaternion operator + (const Quaternion& v) const
00184     {
00185         return Quaternion(d_w + v.d_w, d_x + v.d_x, d_y + v.d_y, d_z + v.d_z);
00186     }
00187 
00195     inline Quaternion operator * (const Quaternion& v) const
00196     {
00197         return Quaternion(
00198             d_w * v.d_w - d_x * v.d_x - d_y * v.d_y - d_z * v.d_z,
00199             d_w * v.d_x + d_x * v.d_w + d_y * v.d_z - d_z * v.d_y,
00200             d_w * v.d_y + d_y * v.d_w + d_z * v.d_x - d_x * v.d_z,
00201             d_w * v.d_z + d_z * v.d_w + d_x * v.d_y - d_y * v.d_x
00202         );
00203     }
00204 
00208     inline float length() const
00209     {
00210         return sqrtf((d_w * d_w) + (d_x * d_x) + (d_y * d_y) + (d_z * d_z));
00211     }
00212 
00216     inline float normalise()
00217     {
00218         const float len = length();
00219         const float factor = 1.0f / len;
00220         *this = *this * factor;
00221 
00222         return len;
00223     }
00224 
00240     static Quaternion slerp(const Quaternion& left, const Quaternion& right, float position, bool shortestPath = false);
00241 
00243     static const Quaternion ZERO;
00245     static const Quaternion IDENTITY;
00246     
00250     inline friend std::ostream& operator << (std::ostream& s, const Quaternion& v)
00251     {
00252         s << "CEGUI::Quaternion(" << v.d_w << ", " << v.d_x << ", " << v.d_y << ", " << v.d_z << ")";
00253         return s;
00254     }
00255 
00257     float d_w;
00259     float d_x;
00261     float d_y;
00263     float d_z;
00264 };
00265 
00272 class QuaternionSlerpInterpolator : public Interpolator
00273 {
00274 public:
00275     typedef PropertyHelper<Quaternion> Helper;
00276     
00278     virtual ~QuaternionSlerpInterpolator() {}
00279 
00281     virtual const String& getType() const;
00282     
00284     virtual String interpolateAbsolute(const String& value1,
00285                                        const String& value2,
00286                                        float position);
00287     
00289     virtual String interpolateRelative(const String& base,
00290                                        const String& value1,
00291                                        const String& value2,
00292                                        float position);
00293     
00295     virtual String interpolateRelativeMultiply(const String& base,
00296                                                const String& value1,
00297                                                const String& value2,
00298                                                float position);
00299 };
00300 
00301 } // End of  CEGUI namespace section
00302 
00303 #endif  // end of guard _CEGUIQuaternion_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends