vec4.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 **    Magnus Norddahl
00027 **    Mark Page
00028 **    Harry Storbacka
00029 */
00030 
00033 
00034 #pragma once
00035 
00036 #include "../api_core.h"
00037 #include "cl_math.h"
00038 #include "vec1.h"
00039 #include "vec2.h"
00040 #include "vec3.h"
00041 
00042 template<typename Type>
00043 class CL_Vec1;
00044 
00045 template<typename Type>
00046 class CL_Vec2;
00047 
00048 template<typename Type>
00049 class CL_Vec3;
00050 
00051 template<typename Type>
00052 class CL_Vec4;
00053 
00054 template<typename Type>
00055 class CL_Mat2;
00056 
00057 template<typename Type>
00058 class CL_Mat3;
00059 
00060 template<typename Type>
00061 class CL_Mat4;
00062 
00063 template<typename Type>
00064 class CL_Sizex;
00065 
00066 template<typename Type>
00067 class CL_Pointx;
00068 
00069 class CL_Angle;
00070 
00077 template<typename Type>
00078 class CL_Vec4
00079 {
00080 public:
00081         typedef Type datatype;
00082 
00083         union { Type x; Type s; Type r; };
00084         union { Type y; Type t; Type g; };
00085         union { Type z; Type u; Type b; };
00086         union { Type w; Type v; Type a; };
00087 
00088         CL_Vec4() : x(0), y(0), z(0), w(0) { }
00089         CL_Vec4(const CL_Vec1<Type> &copy) { x = copy.x; y = 0; z = 0; w = 0; }
00090         CL_Vec4(const CL_Vec2<Type> &copy) { x = copy.x; y = copy.y; z = 0; w = 0; }
00091         CL_Vec4(const CL_Vec3<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; w = 0; }
00092         CL_Vec4(const Type &p1, const Type &p2 = 0, const Type &p3 = 0, const Type &p4 = 0) : x(p1), y(p2), z(p3), w(p4) { }
00093         CL_Vec4(const Type *array_xyzw) : x(array_xyzw[0]), y(array_xyzw[1]), z(array_xyzw[2]), w(array_xyzw[3]) { }
00094 
00100         static CL_Vec4<Type> normalize3(const CL_Vec4<Type> &vector);
00101 
00107         static CL_Vec4<Type> normalize4(const CL_Vec4<Type> &vector);
00108 
00116         static Type dot3(const CL_Vec4<Type>& vector1, const CL_Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
00117 
00125         static Type dot4(const CL_Vec4<Type>& vector1, const CL_Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z + vector1.w*vector2.w; }
00126 
00132         static CL_Vec4<Type> cross3(const CL_Vec4<Type>& vector1, const CL_Vec4<Type>& vector2);
00133 
00143         static CL_Vec4<Type> rotate3(const CL_Vec4<Type>& vector, const CL_Angle &angle, const CL_Vec4<Type>& axis);
00144 
00151         static CL_Vec4<Type> round(const CL_Vec4<Type>& vector);
00152 
00155 
00156 public:
00162         Type length3() const;
00163 
00169         Type length4() const;
00170 
00175         CL_Vec4<Type> &normalize3();
00176 
00181         CL_Vec4<Type> &normalize4();
00182 
00189         Type dot3(const CL_Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z;}
00190 
00197         Type dot4(const CL_Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z + w*vector.w;}
00198 
00204         CL_Angle angle3(const CL_Vec4<Type>& vector) const;
00205 
00211         Type distance3(const CL_Vec4<Type>& vector) const;
00212 
00218         Type distance4(const CL_Vec4<Type>& vector) const;
00219 
00226         CL_Vec4<Type> &cross3(const CL_Vec4<Type>& vector);
00227 
00236         CL_Vec4<Type> &rotate3(const CL_Angle &angle, const CL_Vec4<Type>& axis);
00237 
00243         CL_Vec4<Type> &round();
00244 
00248 
00249 public:
00250         const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
00251         Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
00252         operator Type *() { return (Type *) this; }
00253         operator Type * const() const { return (Type * const) this; }
00254 
00256         void operator += (const CL_Vec4<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; w+= vector.w; }
00257 
00259         void operator += ( Type value) { x+= value; y+= value; z+= value; w+= value; }
00260 
00262         CL_Vec4<Type> operator + (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(vector.x + x, vector.y + y, vector.z + z, vector.w + w);}
00263 
00265         CL_Vec4<Type> operator + (Type value) const {return CL_Vec4<Type>(value + x, value + y, value + z, value + w);}
00266 
00268         void operator -= (const CL_Vec4<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; w-= vector.w; }
00269 
00271         void operator -= ( Type value) { x-= value; y-= value; z-= value; w-= value; }
00272 
00274         CL_Vec4<Type> operator - (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(x - vector.x, y - vector.y, z - vector.z, w - vector.w);}
00275 
00277         CL_Vec4<Type> operator - (Type value) const {return CL_Vec4<Type>(x - value, y - value, z - value, w - value);}
00278 
00280         void operator *= (const CL_Vec4<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; w*= vector.w; }
00281 
00283         void operator *= ( Type value) { x*= value; y*= value; z*= value; w*= value; }
00284 
00286         CL_Vec4<Type> operator * (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(vector.x * x, vector.y * y, vector.z * z, vector.w * w);}
00287 
00289         CL_Vec4<Type> operator * (Type value) const {return CL_Vec4<Type>(value * x, value * y, value * z, value * w);}
00290 
00292         void operator /= (const CL_Vec4<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; w/= vector.w; }
00293 
00295         void operator /= ( Type value) { x/= value; y/= value; z/= value; w/= value; }
00296 
00298         CL_Vec4<Type> operator / (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(x / vector.x, y / vector.y, z / vector.z, w / vector.w);}
00299 
00301         CL_Vec4<Type> operator / (Type value) const {return CL_Vec4<Type>(x / value, y / value, z / value, w / value);}
00302 
00304         CL_Vec4<Type> &operator = (const CL_Vec4<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; w = vector.w; return *this; }
00305 
00307         bool operator == (const CL_Vec4<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z) && (w == vector.w));}
00308 
00310         bool operator != (const CL_Vec4<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z) || (w != vector.w));}
00312 };
00313 
00314 template<typename Type>
00315 CL_Vec4<Type> operator * (const CL_Vec4<Type>& v, const CL_Mat4<Type>& matrix)
00316 {
00317         return CL_Vec4<Type>(
00318                 matrix[0*4+0]*v.x + matrix[0*4+1]*v.y + matrix[0*4+2]*v.z + matrix[0*4+3]*v.w,
00319                 matrix[1*4+0]*v.x + matrix[1*4+1]*v.y + matrix[1*4+2]*v.z + matrix[1*4+3]*v.w,
00320                 matrix[2*4+0]*v.x + matrix[2*4+1]*v.y + matrix[2*4+2]*v.z + matrix[2*4+3]*v.w,
00321                 matrix[3*4+0]*v.x + matrix[3*4+1]*v.y + matrix[3*4+2]*v.z + matrix[3*4+3]*v.w);
00322 }
00323 
00324 template<typename Type>
00325 CL_Vec4<Type> operator * (const CL_Mat4<Type>& matrix, const CL_Vec4<Type>& v)
00326 {
00327         return CL_Vec4<Type>(
00328                 matrix[0*4+0]*v.x + matrix[1*4+0]*v.y + matrix[2*4+0]*v.z + matrix[3*4+0]*v.w,
00329                 matrix[0*4+1]*v.x + matrix[1*4+1]*v.y + matrix[2*4+1]*v.z + matrix[3*4+1]*v.w,
00330                 matrix[0*4+2]*v.x + matrix[1*4+2]*v.y + matrix[2*4+2]*v.z + matrix[3*4+2]*v.w,
00331                 matrix[0*4+3]*v.x + matrix[1*4+3]*v.y + matrix[2*4+3]*v.z + matrix[3*4+3]*v.w);
00332 }
00333 
00334 typedef CL_Vec4<unsigned char> CL_Vec4ub;
00335 typedef CL_Vec4<char> CL_Vec4b;
00336 typedef CL_Vec4<unsigned short> CL_Vec4us;
00337 typedef CL_Vec4<short> CL_Vec4s;
00338 typedef CL_Vec4<unsigned int> CL_Vec4ui;
00339 typedef CL_Vec4<int> CL_Vec4i;
00340 typedef CL_Vec4<float> CL_Vec4f;
00341 typedef CL_Vec4<double> CL_Vec4d;
00342