quaternion.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Mark Page
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "../api_core.h"
00035 
00036 #include "vec3.h"
00037 #include "angle.h"
00038 
00043 template<typename Type>
00044 class CL_Quaternionx
00045 {
00046 public:
00048         Type w;
00049 
00051         union { Type i; Type x; };
00052         union { Type j; Type y; };
00053         union { Type k; Type z; };
00054 
00055         CL_Quaternionx() : w(1), i(0), j(0), k(0) { }
00056         CL_Quaternionx(Type real, Type i, Type j, Type k) : w(real), i(i), j(j), k(k) { }
00057         CL_Quaternionx(Type real, const CL_Vec3<Type> &imag) : w(real), i(imag.x), j(imag.y), k(imag.z) { }
00058         CL_Quaternionx(const CL_Quaternionx<Type> &copy) : w(copy.w), i(copy.i), j(copy.j), k(copy.k) { }
00059         CL_Quaternionx(Type euler_x, Type euler_y, Type euler_z, CL_AngleUnit unit, CL_EulerOrder order);
00060         CL_Quaternionx(const CL_Vec3<Type> &euler, CL_AngleUnit unit, CL_EulerOrder order);
00061         CL_Quaternionx(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order);
00062 
00063         static CL_Quaternionx<Type> axis_angle(const CL_Angle &angle, const CL_Vec3f &axis);
00064         static CL_Quaternionx<Type> multiply(const CL_Quaternionx<Type> &quaternion_1, const CL_Quaternionx<Type> &quaternion_2);
00065 
00066 
00072         static CL_Quaternionx<Type> lerp(const CL_Quaternionx<Type> &quaternion_initial, const CL_Quaternionx<Type> &quaternion_final, Type lerp_time);
00073 
00079         static CL_Quaternionx<Type> slerp(const CL_Quaternionx<Type> &quaternion_initial, const CL_Quaternionx<Type> &quaternion_final, Type slerp_time);
00080 
00083 public:
00087         CL_Mat4<Type> to_matrix() const;
00088 
00090         Type magnitude() const;
00091 
00095 public:
00096         void set(Type euler_x, Type euler_y, Type euler_z, CL_AngleUnit unit, CL_EulerOrder order);
00097         void set(const CL_Vec3<Type> &euler, CL_AngleUnit unit, CL_EulerOrder order);
00098         void set(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order);
00099 
00105         CL_Quaternionx<Type> &multiply(const CL_Mat4<Type> &matrix);    
00106 
00114         CL_Quaternionx<Type> &multiply(const CL_Quaternionx<Type> &quaternion);
00115 
00116         CL_Quaternionx<Type> &rotate(const CL_Angle &angle, const CL_Vec3f &axis);
00117 
00118         CL_Quaternionx<Type> &rotate(const CL_Angle &euler_x, const CL_Angle &euler_y, const CL_Angle &euler_z, CL_EulerOrder order);
00119 
00123         CL_Quaternionx<Type> &normalize();
00124 
00130         CL_Quaternionx<Type> &inverse();
00131 
00137         CL_Vec3<Type> rotate_vector(const CL_Vec3<Type> &v);
00138 
00139         CL_Vec4<Type> rotate_vector(const CL_Vec4<Type> &v);
00140 
00144 public:
00146         CL_Quaternionx<Type> operator *(const CL_Quaternionx<Type> &mult) const { CL_Quaternionx<Type> result = mult; result.multiply(*this); return result; }
00147 
00149 };
00150 
00154 class CL_Quaternionf : public CL_Quaternionx<float>
00155 {
00156 public:
00157         CL_Quaternionf() : CL_Quaternionx<float>() { }
00158         CL_Quaternionf(const CL_Quaternionx<float> &copy) : CL_Quaternionx<float>(copy) { }
00159         CL_Quaternionf(float real, float i, float j, float k) : CL_Quaternionx<float>(real, i, j, k) { }
00160         CL_Quaternionf(float real, const CL_Vec3<float> &imag) : CL_Quaternionx<float>(real, imag) { }
00161         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) { }
00162         CL_Quaternionf(const CL_Vec3<float> &euler, CL_AngleUnit unit, CL_EulerOrder order) : CL_Quaternionx<float>(euler, unit, order) { }
00163         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) { }
00164 
00165 };
00166 
00170 class CL_Quaterniond : public CL_Quaternionx<double>
00171 {
00172 public:
00173         CL_Quaterniond() : CL_Quaternionx<double>() { }
00174         CL_Quaterniond(const CL_Quaternionx<double> &copy) : CL_Quaternionx<double>(copy) { }
00175         CL_Quaterniond(double real, double i, double j, double k) : CL_Quaternionx<double>(real, i, j, k) { }
00176         CL_Quaterniond(double real, const CL_Vec3<double> &imag) : CL_Quaternionx<double>(real, imag) { }
00177         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) { }
00178         CL_Quaterniond(const CL_Vec3<double> &euler, CL_AngleUnit unit, CL_EulerOrder order) : CL_Quaternionx<double>(euler, unit, order) { }
00179         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) { }
00180 };
00181 
00182