ClanLib  2.3.7
quaternion.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Mark Page
27 */
28 
31 
32 #pragma once
33 
34 #include "../api_core.h"
35 
36 #include "vec3.h"
37 #include "angle.h"
38 
43 template<typename Type>
45 {
46 public:
48  Type w;
49 
51  union { Type i; Type x; };
52  union { Type j; Type y; };
53  union { Type k; Type z; };
54 
55  CL_Quaternionx() : w(1), i(0), j(0), k(0) { }
56  CL_Quaternionx(Type real, Type i, Type j, Type k) : w(real), i(i), j(j), k(k) { }
57  CL_Quaternionx(Type real, const CL_Vec3<Type> &imag) : w(real), i(imag.x), j(imag.y), k(imag.z) { }
58  CL_Quaternionx(const CL_Quaternionx<Type> &copy) : w(copy.w), i(copy.i), j(copy.j), k(copy.k) { }
59  CL_Quaternionx(Type euler_x, Type euler_y, Type euler_z, CL_AngleUnit unit, CL_EulerOrder order);
60  CL_Quaternionx(const CL_Vec3<Type> &euler, CL_AngleUnit unit, CL_EulerOrder order);
61  CL_Quaternionx(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order);
62 
63  static CL_Quaternionx<Type> axis_angle(const CL_Angle &angle, const CL_Vec3f &axis);
64  static CL_Quaternionx<Type> multiply(const CL_Quaternionx<Type> &quaternion_1, const CL_Quaternionx<Type> &quaternion_2);
65 
66 
72  static CL_Quaternionx<Type> lerp(const CL_Quaternionx<Type> &quaternion_initial, const CL_Quaternionx<Type> &quaternion_final, Type lerp_time);
73 
79  static CL_Quaternionx<Type> slerp(const CL_Quaternionx<Type> &quaternion_initial, const CL_Quaternionx<Type> &quaternion_final, Type slerp_time);
80 
83 public:
87  CL_Mat4<Type> to_matrix() const;
88 
90  Type magnitude() const;
91 
95 public:
96  void set(Type euler_x, Type euler_y, Type euler_z, CL_AngleUnit unit, CL_EulerOrder order);
97  void set(const CL_Vec3<Type> &euler, CL_AngleUnit unit, CL_EulerOrder order);
98  void set(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order);
99 
106 
115 
116  CL_Quaternionx<Type> &rotate(const CL_Angle &angle, const CL_Vec3f &axis);
117 
118  CL_Quaternionx<Type> &rotate(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order);
119 
124 
131 
138 
140 
144 public:
146  CL_Quaternionx<Type> operator *(const CL_Quaternionx<Type> &mult) const { CL_Quaternionx<Type> result = mult; result.multiply(*this); return result; }
147 
149 };
150 
154 class CL_Quaternionf : public CL_Quaternionx<float>
155 {
156 public:
158  CL_Quaternionf(const CL_Quaternionx<float> &copy) : CL_Quaternionx<float>(copy) { }
159  CL_Quaternionf(float real, float i, float j, float k) : CL_Quaternionx<float>(real, i, j, k) { }
160  CL_Quaternionf(float real, const CL_Vec3<float> &imag) : CL_Quaternionx<float>(real, imag) { }
161  CL_Quaternionf(float euler_x, float euler_y, float euler_z, CL_AngleUnit unit, CL_EulerOrder order) : CL_Quaternionx<float>(euler_x, euler_y, euler_z, unit, order) { }
162  CL_Quaternionf(const CL_Vec3<float> &euler, CL_AngleUnit unit, CL_EulerOrder order) : CL_Quaternionx<float>(euler, unit, order) { }
163  CL_Quaternionf(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order) : CL_Quaternionx<float>(euler_x, euler_y, euler_z, order) { }
164 
165 };
166 
170 class CL_Quaterniond : public CL_Quaternionx<double>
171 {
172 public:
173  CL_Quaterniond() : CL_Quaternionx<double>() { }
174  CL_Quaterniond(const CL_Quaternionx<double> &copy) : CL_Quaternionx<double>(copy) { }
175  CL_Quaterniond(double real, double i, double j, double k) : CL_Quaternionx<double>(real, i, j, k) { }
176  CL_Quaterniond(double real, const CL_Vec3<double> &imag) : CL_Quaternionx<double>(real, imag) { }
177  CL_Quaterniond(double euler_x, double euler_y, double euler_z, CL_AngleUnit unit, CL_EulerOrder order) : CL_Quaternionx<double>(euler_x, euler_y, euler_z, unit, order) { }
178  CL_Quaterniond(const CL_Vec3<double> &euler, CL_AngleUnit unit, CL_EulerOrder order) : CL_Quaternionx<double>(euler, unit, order) { }
179  CL_Quaterniond(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order) : CL_Quaternionx<double>(euler_x, euler_y, euler_z, order) { }
180 };
181 
182