Crazy Eddie's GUI System  0.8.6
Quaternion.h
1 /***********************************************************************
2  created: 2/1/2011
3  author: Martin Preisler
4 
5  purpose: Defines interface for the Quaternion class
6 *************************************************************************/
7 /***************************************************************************
8  * Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  ***************************************************************************/
29 #ifndef _CEGUIQuaternion_h_
30 #define _CEGUIQuaternion_h_
31 
32 #include "CEGUI/Base.h"
33 #include "CEGUI/Interpolator.h"
34 #include "CEGUI/Vector.h"
35 #include <cmath>
36 
37 // Start of CEGUI namespace section
38 namespace CEGUI
39 {
40 
67 class CEGUIEXPORT Quaternion :
68  public AllocatedObject<Quaternion>
69 {
70 public:
72  inline Quaternion(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f):
73  d_w(w),
74  d_x(x),
75  d_y(y),
76  d_z(z)
77  {}
78 
80  inline Quaternion(const Quaternion& v):
81  d_w(v.d_w),
82  d_x(v.d_x),
83  d_y(v.d_y),
84  d_z(v.d_z)
85  {}
86 
88  inline Quaternion& operator = (const Quaternion& v)
89  {
90  d_w = v.d_w;
91  d_x = v.d_x;
92  d_y = v.d_y;
93  d_z = v.d_z;
94 
95  return *this;
96  }
97 
110  static Quaternion eulerAnglesRadians(float x, float y, float z);
111 
124  static Quaternion eulerAnglesDegrees(float x, float y, float z);
125 
134  static Quaternion axisAngleRadians(const Vector3f& axis, float rotation);
135 
144  static Quaternion axisAngleDegrees(const Vector3f& axis, float rotation);
145 
147  inline bool operator == (const Quaternion& v) const
148  {
149  return (d_w == v.d_w) && (d_x == v.d_x) && (d_y == v.d_y) && (d_z == v.d_z);
150  }
151 
153  inline bool operator != (const Quaternion& v) const
154  {
155  return (d_w != v.d_w) || (d_x != v.d_x) || (d_y != v.d_y) || (d_z != v.d_z);
156  }
157 
159  inline Quaternion operator - () const
160  {
161  return Quaternion(-d_w, -d_x, -d_y, -d_z);
162  }
163 
165  inline Quaternion operator * (float v) const
166  {
167  return Quaternion(d_w * v, d_x * v, d_y * v, d_z * v);
168  }
169 
171  inline friend Quaternion operator * (float v, const Quaternion& q)
172  {
173  return Quaternion(v * q.d_w, v * q.d_x, v * q.d_y, v * q.d_z);
174  }
175 
177  inline float dot(const Quaternion& v) const
178  {
179  return d_w * v.d_w + d_x * v.d_x + d_y * v.d_y + d_z * v.d_z;
180  }
181 
183  inline Quaternion operator + (const Quaternion& v) const
184  {
185  return Quaternion(d_w + v.d_w, d_x + v.d_x, d_y + v.d_y, d_z + v.d_z);
186  }
187 
195  inline Quaternion operator * (const Quaternion& v) const
196  {
197  return Quaternion(
198  d_w * v.d_w - d_x * v.d_x - d_y * v.d_y - d_z * v.d_z,
199  d_w * v.d_x + d_x * v.d_w + d_y * v.d_z - d_z * v.d_y,
200  d_w * v.d_y + d_y * v.d_w + d_z * v.d_x - d_x * v.d_z,
201  d_w * v.d_z + d_z * v.d_w + d_x * v.d_y - d_y * v.d_x
202  );
203  }
204 
208  inline float length() const
209  {
210  return sqrtf((d_w * d_w) + (d_x * d_x) + (d_y * d_y) + (d_z * d_z));
211  }
212 
216  inline float normalise()
217  {
218  const float len = length();
219  const float factor = 1.0f / len;
220  *this = *this * factor;
221 
222  return len;
223  }
224 
240  static Quaternion slerp(const Quaternion& left, const Quaternion& right, float position, bool shortestPath = false);
241 
243  static const Quaternion ZERO;
245  static const Quaternion IDENTITY;
246 
250  inline friend std::ostream& operator << (std::ostream& s, const Quaternion& v)
251  {
252  s << "CEGUI::Quaternion(" << v.d_w << ", " << v.d_x << ", " << v.d_y << ", " << v.d_z << ")";
253  return s;
254  }
255 
257  float d_w;
259  float d_x;
261  float d_y;
263  float d_z;
264 };
265 
273 {
274 public:
276 
279 
281  virtual const String& getType() const;
282 
284  virtual String interpolateAbsolute(const String& value1,
285  const String& value2,
286  float position);
287 
289  virtual String interpolateRelative(const String& base,
290  const String& value1,
291  const String& value2,
292  float position);
293 
295  virtual String interpolateRelativeMultiply(const String& base,
296  const String& value1,
297  const String& value2,
298  float position);
299 };
300 
301 } // End of CEGUI namespace section
302 
303 #endif // end of guard _CEGUIQuaternion_h_
virtual const String & getType() const
returns type string of this interpolator
static const Quaternion IDENTITY
Quaternion(1, 0, 0, 0)
Definition: Quaternion.h:245
Definition: MemoryAllocatedObject.h:109
float d_w
imaginary part
Definition: Quaternion.h:257
bool CEGUIEXPORT operator==(const String &str1, const String &str2)
Return true if String str1 is equal to String str2.
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
float d_y
y component of the vector part
Definition: Quaternion.h:261
float d_x
x component of the vector part
Definition: Quaternion.h:259
Quaternion(float w=1.0f, float x=0.0f, float y=0.0f, float z=0.0f)
verbatim constructor
Definition: Quaternion.h:72
float d_z
z component of the vector part
Definition: Quaternion.h:263
float dot(const Quaternion &v) const
quaternion dot product
Definition: Quaternion.h:177
static const Quaternion ZERO
Quaternion(0, 0, 0, 0)
Definition: Quaternion.h:243
String CEGUIEXPORT operator+(const String &str1, const String &str2)
Return String object that is the concatenation of the given inputs.
virtual String interpolateAbsolute(const String &value1, const String &value2, float position)
float length() const
computers and returns the length of this quaternion
Definition: Quaternion.h:208
bool CEGUIEXPORT operator!=(const String &str1, const String &str2)
Return true if String str1 is not equal to String str2.
virtual ~QuaternionSlerpInterpolator()
destructor
Definition: Quaternion.h:278
Class to represent rotation, avoids Gimbal lock.
Definition: Quaternion.h:67
virtual String interpolateRelative(const String &base, const String &value1, const String &value2, float position)
Defines a 'interpolator' class.
Definition: Interpolator.h:53
Special interpolator class for Quaternion.
Definition: Quaternion.h:272
Definition: PropertyHelper.h:611
virtual String interpolateRelativeMultiply(const String &base, const String &value1, const String &value2, float position)
String class used within the GUI system.
Definition: String.h:62
float normalise()
normalises this quaternion and returns it's length (since it has to be computed anyways) ...
Definition: Quaternion.h:216
Quaternion(const Quaternion &v)
copy constructor
Definition: Quaternion.h:80