vec3.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 "vec4.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_Vec3
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 
00087         CL_Vec3() : x(0), y(0), z(0) { }
00088         CL_Vec3(const CL_Vec2<Type> &copy) { x = copy.x; y = copy.y; z = 0; }
00089         CL_Vec3(const CL_Vec1<Type> &copy) { x = copy.x; y = 0; z = 0; }
00090         CL_Vec3(const CL_Vec4<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; }
00091 
00092         CL_Vec3(const CL_Vec3<double> &copy);
00093         CL_Vec3(const CL_Vec3<float> &copy);
00094         CL_Vec3(const CL_Vec3<int> &copy);
00095 
00096         CL_Vec3(const Type &p1, const Type &p2 = 0, const Type &p3 = 0) : x(p1), y(p2), z(p3) { }
00097         CL_Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) { }
00098 
00104         static CL_Vec3<Type> normalize(const CL_Vec3<Type>& vector);
00105 
00109         static Type dot(const CL_Vec3<Type>& vector1, const CL_Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
00110 
00116         static CL_Vec3<Type> cross(const CL_Vec3<Type>& vector1, const CL_Vec3<Type>& vector2);
00117 
00124         static CL_Vec3<Type> rotate(const CL_Vec3<Type>& vector, const CL_Angle &angle, const CL_Vec3<Type>& axis);
00125 
00131         static CL_Vec3<Type> round(const CL_Vec3<Type>& vector);
00132 
00135 
00136 public:
00141         Type length() const;
00142 
00147         CL_Vec3<Type> &normalize();
00148 
00155         Type dot(const CL_Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
00156 
00162         CL_Angle angle(const CL_Vec3<Type>& vector) const;
00163 
00169         Type distance(const CL_Vec3<Type>& vector) const;
00170 
00176         CL_Vec3<Type> &cross(const CL_Vec3<Type>& vector);
00177 
00183         CL_Vec3<Type> &rotate(const CL_Angle &angle, const CL_Vec3<Type>& axis);
00184 
00189         CL_Vec3<Type> &round();
00190 
00194 
00195 public:
00196         const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
00197         Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
00198         operator Type *() { return (Type *) this; }
00199         operator Type * const() const { return (Type * const) this; }
00200 
00202         void operator += (const CL_Vec3<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; }
00203 
00205         void operator += ( Type value) { x+= value; y+= value; z+= value; }
00206 
00208         CL_Vec3<Type> operator + (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(vector.x + x, vector.y + y, vector.z + z);}
00209 
00211         CL_Vec3<Type> operator + (Type value) const {return CL_Vec3<Type>(value + x, value + y, value + z);}
00212 
00214         void operator -= (const CL_Vec3<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; }
00215 
00217         void operator -= ( Type value) { x-= value; y-= value; z-= value; }
00218 
00220         CL_Vec3<Type> operator - (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(x - vector.x, y - vector.y, z - vector.z);}
00221 
00223         CL_Vec3<Type> operator - (Type value) const {return CL_Vec3<Type>(x - value, y - value, z - value);}
00224 
00226         void operator *= (const CL_Vec3<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; }
00227 
00229         void operator *= ( Type value) { x*= value; y*= value; z*= value; }
00230 
00232         CL_Vec3<Type> operator * (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(vector.x * x, vector.y * y, vector.z * z);}
00233 
00235         CL_Vec3<Type> operator * (Type value) const {return CL_Vec3<Type>(value * x, value * y, value * z);}
00236 
00238         void operator /= (const CL_Vec3<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; }
00239 
00241         void operator /= ( Type value) { x/= value; y/= value; z/= value; }
00242 
00244         CL_Vec3<Type> operator / (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(x / vector.x, y / vector.y, z / vector.z);}
00245 
00247         CL_Vec3<Type> operator / (Type value) const {return CL_Vec3<Type>(x / value, y / value, z / value);}
00248 
00250         CL_Vec3<Type> &operator = (const CL_Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
00251 
00253         bool operator == (const CL_Vec3<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z));}
00254 
00256         bool operator != (const CL_Vec3<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z));}
00258 };
00259 
00262 template<typename Type>
00263 CL_Vec3<Type> operator * (const CL_Vec3<Type>& v, const CL_Mat3<Type>& matrix)
00264 {
00265         return CL_Vec3<Type>(
00266                 matrix[0*3+0]*v.x + matrix[0*3+1]*v.y + matrix[0*3+2]*v.z,
00267                 matrix[1*3+0]*v.x + matrix[1*3+1]*v.y + matrix[1*3+2]*v.z,
00268                 matrix[2*3+0]*v.x + matrix[2*3+1]*v.y + matrix[2*3+2]*v.z);
00269 }
00270 
00273 template<typename Type>
00274 CL_Vec3<Type> operator * (const CL_Mat3<Type>& matrix, const CL_Vec3<Type>& v)
00275 {
00276         return CL_Vec3<Type>(
00277                 matrix[0*3+0]*v.x + matrix[1*3+0]*v.y + matrix[2*3+0]*v.z,
00278                 matrix[0*3+1]*v.x + matrix[1*3+1]*v.y + matrix[2*3+1]*v.z,
00279                 matrix[0*3+2]*v.x + matrix[1*3+2]*v.y + matrix[2*3+2]*v.z);
00280 }
00281 
00282 template<>
00283 inline CL_Vec3<unsigned char>::CL_Vec3(const CL_Vec3<float> &copy) { x = (unsigned char) floor(copy.x +0.5f); y = (unsigned char) floor(copy.y + 0.5f); z = (unsigned char) floor(copy.z + 0.5f); }
00284 
00285 template<>
00286 inline CL_Vec3<unsigned char>::CL_Vec3(const CL_Vec3<double> &copy) { x = (unsigned char) floor(copy.x+0.5); y = (unsigned char) floor(copy.y+0.5); z = (unsigned char) floor(copy.z + 0.5); }
00287 
00288 template<>
00289 inline CL_Vec3<unsigned char>::CL_Vec3(const CL_Vec3<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; z = (unsigned char) copy.z; }
00290 
00291 template<>
00292 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<float> &copy) { x = (char) floor(copy.x +0.5f); y = (char) floor(copy.y + 0.5f); z = (char) floor(copy.z + 0.5f); }
00293 
00294 template<>
00295 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<double> &copy) { x = (char) floor(copy.x+0.5); y = (char) floor(copy.y+0.5); z = (char) floor(copy.z + 0.5); }
00296 
00297 template<>
00298 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<int> &copy) { x = (char) copy.x; y = (char) copy.y; z = (char) copy.z; }
00299 
00300 template<>
00301 inline CL_Vec3<unsigned short>::CL_Vec3(const CL_Vec3<float> &copy) { x = (unsigned short) floor(copy.x +0.5f); y = (unsigned short) floor(copy.y + 0.5f); z = (unsigned short) floor(copy.z + 0.5f); }
00302 
00303 template<>
00304 inline CL_Vec3<unsigned short>::CL_Vec3(const CL_Vec3<double> &copy) { x = (unsigned short) floor(copy.x+0.5); y = (unsigned short) floor(copy.y+0.5); z = (unsigned short) floor(copy.z + 0.5); }
00305 
00306 template<>
00307 inline CL_Vec3<unsigned short>::CL_Vec3(const CL_Vec3<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; z = (unsigned short) copy.z; }
00308 
00309 template<>
00310 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<float> &copy) { x = (short) floor(copy.x +0.5f); y = (short) floor(copy.y + 0.5f); z = (short) floor(copy.z + 0.5f); }
00311 
00312 template<>
00313 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<double> &copy) { x = (short) floor(copy.x+0.5); y = (short) floor(copy.y+0.5); z = (short) floor(copy.z + 0.5); }
00314 
00315 template<>
00316 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<int> &copy) { x = (short) copy.x; y = (short) copy.y; z = (short) copy.z; }
00317 
00318 template<>
00319 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<float> &copy) { x = (int) floor(copy.x +0.5f); y = (int) floor(copy.y + 0.5f); z = (int) floor(copy.z + 0.5f); }
00320 
00321 template<>
00322 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<double> &copy) { x = (int) floor(copy.x+0.5); y = (int) floor(copy.y+0.5); z = (int) floor(copy.z + 0.5); }
00323 
00324 template<>
00325 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<int> &copy) { x = (int) copy.x; y = (int) copy.y; z = (int) copy.z; }
00326 
00327 template<>
00328 inline CL_Vec3<unsigned int>::CL_Vec3(const CL_Vec3<float> &copy) { x = (unsigned int) floor(copy.x +0.5f); y = (unsigned int) floor(copy.y + 0.5f); z = (unsigned int) floor(copy.z + 0.5f); }
00329 
00330 template<>
00331 inline CL_Vec3<unsigned int>::CL_Vec3(const CL_Vec3<double> &copy) { x = (unsigned int) floor(copy.x+0.5); y = (unsigned int) floor(copy.y+0.5); z = (unsigned int) floor(copy.z + 0.5); }
00332 
00333 template<>
00334 inline CL_Vec3<unsigned int>::CL_Vec3(const CL_Vec3<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; z = (unsigned int) copy.z; }
00335 
00336 template<>
00337 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<float> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
00338 
00339 template<>
00340 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<double> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
00341 
00342 template<>
00343 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<int> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
00344 
00345 template<>
00346 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<float> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
00347 
00348 template<>
00349 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<double> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
00350 
00351 template<>
00352 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<int> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
00353 
00354 typedef CL_Vec3<unsigned char> CL_Vec3ub;
00355 typedef CL_Vec3<char> CL_Vec3b;
00356 typedef CL_Vec3<unsigned short> CL_Vec3us;
00357 typedef CL_Vec3<short> CL_Vec3s;
00358 typedef CL_Vec3<unsigned int> CL_Vec3ui;
00359 typedef CL_Vec3<int> CL_Vec3i;
00360 typedef CL_Vec3<float> CL_Vec3f;
00361 typedef CL_Vec3<double> CL_Vec3d;
00362