vec3.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2009 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 
00103         static CL_Vec3<Type> normalize(const CL_Vec3<Type>& vector);
00104 
00108         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; }
00109 
00115         static CL_Vec3<Type> cross(const CL_Vec3<Type>& vector1, const CL_Vec3<Type>& vector2);
00116 
00123         static CL_Vec3<Type> rotate(const CL_Vec3<Type>& vector, const CL_Angle &angle, const CL_Vec3<Type>& axis);
00124 
00130         static CL_Vec3<Type> round(const CL_Vec3<Type>& vector);
00131 
00134 
00135 public:
00140         Type length() const;
00141 
00146         CL_Vec3<Type> &normalize();
00147 
00154         Type dot(const CL_Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
00155 
00161         CL_Angle angle(const CL_Vec3<Type>& vector) const;
00162 
00168         Type distance(const CL_Vec3<Type>& vector) const;
00169 
00175         CL_Vec3<Type> &cross(const CL_Vec3<Type>& vector);
00176 
00182         CL_Vec3<Type> &rotate(const CL_Angle &angle, const CL_Vec3<Type>& axis);
00183 
00188         CL_Vec3<Type> &round();
00189 
00193 
00194 public:
00195         const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
00196         Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
00197         operator Type *() { return (Type *) this; }
00198         operator Type * const() const { return (Type * const) this; }
00199 
00201         void operator += (const CL_Vec3<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; }
00202 
00204         void operator += ( Type value) { x+= value; y+= value; z+= value; }
00205 
00207         CL_Vec3<Type> operator + (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(vector.x + x, vector.y + y, vector.z + z);}
00208 
00210         CL_Vec3<Type> operator + (Type value) const {return CL_Vec3<Type>(value + x, value + y, value + z);}
00211 
00213         void operator -= (const CL_Vec3<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; }
00214 
00216         void operator -= ( Type value) { x-= value; y-= value; z-= value; }
00217 
00219         CL_Vec3<Type> operator - (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(x - vector.x, y - vector.y, z - vector.z);}
00220 
00222         CL_Vec3<Type> operator - (Type value) const {return CL_Vec3<Type>(x - value, y - value, z - value);}
00223 
00225         void operator *= (const CL_Vec3<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; }
00226 
00228         void operator *= ( Type value) { x*= value; y*= value; z*= value; }
00229 
00231         CL_Vec3<Type> operator * (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(vector.x * x, vector.y * y, vector.z * z);}
00232 
00234         CL_Vec3<Type> operator * (Type value) const {return CL_Vec3<Type>(value * x, value * y, value * z);}
00235 
00237         void operator /= (const CL_Vec3<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; }
00238 
00240         void operator /= ( Type value) { x/= value; y/= value; z/= value; }
00241 
00243         CL_Vec3<Type> operator / (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(x / vector.x, y / vector.y, z / vector.z);}
00244 
00246         CL_Vec3<Type> operator / (Type value) const {return CL_Vec3<Type>(x / value, y / value, z / value);}
00247 
00249         CL_Vec3<Type> &operator = (const CL_Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
00250 
00252         bool operator == (const CL_Vec3<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z));}
00253 
00255         bool operator != (const CL_Vec3<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z));}
00257 };
00258 
00259 template<typename Type>
00260 CL_Vec3<Type> operator * (const CL_Vec3<Type>& v, const CL_Mat3<Type>& matrix)
00261 {
00262         return CL_Vec3<Type>(
00263                 matrix[0*3+0]*v.x + matrix[0*3+1]*v.y + matrix[0*3+2]*v.z,
00264                 matrix[1*3+0]*v.x + matrix[1*3+1]*v.y + matrix[1*3+2]*v.z,
00265                 matrix[2*3+0]*v.x + matrix[2*3+1]*v.y + matrix[2*3+2]*v.z);
00266 }
00267 
00268 template<typename Type>
00269 CL_Vec3<Type> operator * (const CL_Mat3<Type>& matrix, const CL_Vec3<Type>& v)
00270 {
00271         return CL_Vec3<Type>(
00272                 matrix[0*3+0]*v.x + matrix[1*3+0]*v.y + matrix[2*3+0]*v.z,
00273                 matrix[0*3+1]*v.x + matrix[1*3+1]*v.y + matrix[2*3+1]*v.z,
00274                 matrix[0*3+2]*v.x + matrix[1*3+2]*v.y + matrix[2*3+2]*v.z);
00275 }
00276 
00277 template<>
00278 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); }
00279 
00280 template<>
00281 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); }
00282 
00283 template<>
00284 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; }
00285 
00286 template<>
00287 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); }
00288 
00289 template<>
00290 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); }
00291 
00292 template<>
00293 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<int> &copy) { x = (char) copy.x; y = (char) copy.y; z = (char) copy.z; }
00294 
00295 template<>
00296 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); }
00297 
00298 template<>
00299 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); }
00300 
00301 template<>
00302 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; }
00303 
00304 template<>
00305 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); }
00306 
00307 template<>
00308 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); }
00309 
00310 template<>
00311 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<int> &copy) { x = (short) copy.x; y = (short) copy.y; z = (short) copy.z; }
00312 
00313 template<>
00314 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); }
00315 
00316 template<>
00317 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); }
00318 
00319 template<>
00320 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<int> &copy) { x = (int) copy.x; y = (int) copy.y; z = (int) copy.z; }
00321 
00322 template<>
00323 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); }
00324 
00325 template<>
00326 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); }
00327 
00328 template<>
00329 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; }
00330 
00331 template<>
00332 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<float> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
00333 
00334 template<>
00335 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<double> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
00336 
00337 template<>
00338 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<int> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
00339 
00340 template<>
00341 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<float> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
00342 
00343 template<>
00344 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<double> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
00345 
00346 template<>
00347 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<int> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
00348 
00349 typedef CL_Vec3<unsigned char> CL_Vec3ub;
00350 typedef CL_Vec3<char> CL_Vec3b;
00351 typedef CL_Vec3<unsigned short> CL_Vec3us;
00352 typedef CL_Vec3<short> CL_Vec3s;
00353 typedef CL_Vec3<unsigned int> CL_Vec3ui;
00354 typedef CL_Vec3<int> CL_Vec3i;
00355 typedef CL_Vec3<float> CL_Vec3f;
00356 typedef CL_Vec3<double> CL_Vec3d;
00357 

Generated on Thu Dec 3 02:39:32 2009 for ClanLib by  doxygen 1.4.6