Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Quaternion.h
1 /***********************************************************************
2  filename: CEGUIQuaternion.h
3  created: 2/1/2011
4  author: Martin Preisler
5 
6  purpose: Defines interface for the Quaternion class
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIQuaternion_h_
31 #define _CEGUIQuaternion_h_
32 
33 #include "CEGUI/Base.h"
34 #include "CEGUI/Interpolator.h"
35 #include "CEGUI/Vector.h"
36 #include <cmath>
37 
38 // Start of CEGUI namespace section
39 namespace CEGUI
40 {
41 
68 class CEGUIEXPORT Quaternion :
69  public AllocatedObject<Quaternion>
70 {
71 public:
73  inline Quaternion(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f):
74  d_w(w),
75  d_x(x),
76  d_y(y),
77  d_z(z)
78  {}
79 
81  inline Quaternion(const Quaternion& v):
82  d_w(v.d_w),
83  d_x(v.d_x),
84  d_y(v.d_y),
85  d_z(v.d_z)
86  {}
87 
89  inline Quaternion& operator = (const Quaternion& v)
90  {
91  d_w = v.d_w;
92  d_x = v.d_x;
93  d_y = v.d_y;
94  d_z = v.d_z;
95 
96  return *this;
97  }
98 
111  static Quaternion eulerAnglesRadians(float x, float y, float z);
112 
125  static Quaternion eulerAnglesDegrees(float x, float y, float z);
126 
135  static Quaternion axisAngleRadians(const Vector3f& axis, float rotation);
136 
145  static Quaternion axisAngleDegrees(const Vector3f& axis, float rotation);
146 
148  inline bool operator == (const Quaternion& v) const
149  {
150  return (d_w == v.d_w) && (d_x == v.d_x) && (d_y == v.d_y) && (d_z == v.d_z);
151  }
152 
154  inline bool operator != (const Quaternion& v) const
155  {
156  return (d_w != v.d_w) || (d_x != v.d_x) || (d_y != v.d_y) || (d_z != v.d_z);
157  }
158 
160  inline Quaternion operator - () const
161  {
162  return Quaternion(-d_w, -d_x, -d_y, -d_z);
163  }
164 
166  inline Quaternion operator * (float v) const
167  {
168  return Quaternion(d_w * v, d_x * v, d_y * v, d_z * v);
169  }
170 
172  inline friend Quaternion operator * (float v, const Quaternion& q)
173  {
174  return Quaternion(v * q.d_w, v * q.d_x, v * q.d_y, v * q.d_z);
175  }
176 
178  inline float dot(const Quaternion& v) const
179  {
180  return d_w * v.d_w + d_x * v.d_x + d_y * v.d_y + d_z * v.d_z;
181  }
182 
184  inline Quaternion operator + (const Quaternion& v) const
185  {
186  return Quaternion(d_w + v.d_w, d_x + v.d_x, d_y + v.d_y, d_z + v.d_z);
187  }
188 
196  inline Quaternion operator * (const Quaternion& v) const
197  {
198  return Quaternion(
199  d_w * v.d_w - d_x * v.d_x - d_y * v.d_y - d_z * v.d_z,
200  d_w * v.d_x + d_x * v.d_w + d_y * v.d_z - d_z * v.d_y,
201  d_w * v.d_y + d_y * v.d_w + d_z * v.d_x - d_x * v.d_z,
202  d_w * v.d_z + d_z * v.d_w + d_x * v.d_y - d_y * v.d_x
203  );
204  }
205 
209  inline float length() const
210  {
211  return sqrtf((d_w * d_w) + (d_x * d_x) + (d_y * d_y) + (d_z * d_z));
212  }
213 
217  inline float normalise()
218  {
219  const float len = length();
220  const float factor = 1.0f / len;
221  *this = *this * factor;
222 
223  return len;
224  }
225 
241  static Quaternion slerp(const Quaternion& left, const Quaternion& right, float position, bool shortestPath = false);
242 
244  static const Quaternion ZERO;
246  static const Quaternion IDENTITY;
247 
251  inline friend std::ostream& operator << (std::ostream& s, const Quaternion& v)
252  {
253  s << "CEGUI::Quaternion(" << v.d_w << ", " << v.d_x << ", " << v.d_y << ", " << v.d_z << ")";
254  return s;
255  }
256 
258  float d_w;
260  float d_x;
262  float d_y;
264  float d_z;
265 };
266 
274 {
275 public:
277 
280 
282  virtual const String& getType() const;
283 
285  virtual String interpolateAbsolute(const String& value1,
286  const String& value2,
287  float position);
288 
290  virtual String interpolateRelative(const String& base,
291  const String& value1,
292  const String& value2,
293  float position);
294 
296  virtual String interpolateRelativeMultiply(const String& base,
297  const String& value1,
298  const String& value2,
299  float position);
300 };
301 
302 } // End of CEGUI namespace section
303 
304 #endif // end of guard _CEGUIQuaternion_h_