cVector3.h

00001 // Copyright (c) 2008-2010 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones
00002 // This file is part of the "cAudio Engine"
00003 // For conditions of distribution and use, see copyright notice in cAudio.h
00004 
00005 #ifndef CVECTOR3_H
00006 #define CVECTOR3_H
00007 
00008 #include <math.h>
00009 
00010 namespace cAudio
00011 {
00013         const float Epsilon = 0.000001f;
00014 
00016         inline bool float_equals(const float a, const float b)
00017         {
00018                 return (a + Epsilon >= b) && (a - Epsilon <= b);
00019         }
00020 
00022         class cVector3
00023         {
00024         public:
00025                 float x, y, z;
00026 
00028                 cVector3(void) : x(0), y(0), z(0)
00029                 {
00030                 }
00031 
00032                 cVector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
00033                 {
00034                 }
00035 
00037                 cVector3(float n) : x(n), y(n), z(n)
00038                 {
00039                 }
00040 
00041                 cVector3(const cVector3& other) : x(other.x), y(other.y), z(other.z)
00042                 {
00043                 }
00044 
00045                 cVector3 operator-() const { return cVector3(-x, -y, -z); }
00046                 cVector3& operator=(const cVector3& other) { x = other.x; y = other.y; z = other.z; return *this; }
00047                 cVector3 operator+(const cVector3& other) const { return cVector3(x + other.x, y + other.y, z + other.z); }
00048                 cVector3& operator+=(const cVector3& other) { x+=other.x; y+=other.y; z+=other.z; return *this; }
00049                 cVector3 operator+(const float val) const { return cVector3(x + val, y + val, z + val); }
00050                 cVector3& operator+=(const float val) { x+=val; y+=val; z+=val; return *this; }
00051 
00052                 cVector3 operator-(const cVector3& other) const { return cVector3(x - other.x, y - other.y, z - other.z); }
00053                 cVector3& operator-=(const cVector3& other) { x-=other.x; y-=other.y; z-=other.z; return *this; }
00054                 cVector3 operator-(const float val) const { return cVector3(x - val, y - val, z - val); }
00055                 cVector3& operator-=(const float val) { x-=val; y-=val; z-=val; return *this; }
00056 
00057                 cVector3 operator*(const cVector3& other) const { return cVector3(x * other.x, y * other.y, z * other.z); }
00058                 cVector3& operator*=(const cVector3& other) { x*=other.x; y*=other.y; z*=other.z; return *this; }
00059                 cVector3 operator*(const float v) const { return cVector3(x * v, y * v, z * v); }
00060                 cVector3& operator*=(const float v) { x*=v; y*=v; z*=v; return *this; }
00061 
00062                 cVector3 operator/(const cVector3& other) const { return cVector3(x / other.x, y / other.y, z / other.z); }
00063                 cVector3& operator/=(const cVector3& other) { x/=other.x; y/=other.y; z/=other.z; return *this; }
00064                 cVector3 operator/(const float v) const { float i=(float)1.0/v; return cVector3(x * i, y * i, z * i); }
00065                 cVector3& operator/=(const float v) { float i=(float)1.0/v; x*=i; y*=i; z*=i; return *this; }
00066 
00067                 bool operator<=(const cVector3& other) const { return x<=other.x && y<=other.y && z<=other.z;}
00068                 bool operator>=(const cVector3& other) const { return x>=other.x && y>=other.y && z>=other.z;}
00069                 bool operator<(const cVector3& other) const { return x<other.x && y<other.y && z<other.z;}
00070                 bool operator>(const cVector3& other) const { return x>other.x && y>other.y && z>other.z;}
00071 
00072                 bool operator==(const cVector3& other) const
00073                 {
00074                         return float_equals(x, other.x) &&
00075                                         float_equals(y, other.y) &&
00076                                         float_equals(z, other.z);
00077                 }
00078 
00079                 bool operator!=(const cVector3& other) const
00080                 {
00081                         return !(float_equals(x, other.x) &&
00082                                         float_equals(y, other.y) &&
00083                                         float_equals(z, other.z));
00084                 }
00085 
00086                 operator const float*() const { return &x; }
00087 
00088                 operator float*() { return &x; }
00089 
00090                 const float operator[] ( int i ) const { return ( ( float* ) &x ) [i]; }
00091 
00092                 float &operator[] ( int i ) { return ( ( float* ) &x ) [i]; }
00093 
00095                 float length() const
00096                 {
00097                         return sqrtf( x*x + y*y + z*z );
00098                 }
00099 
00101                 void normalize()
00102                 {
00103                         float invLen = 1.0f / length();
00104                         x *= invLen;
00105                         y *= invLen;
00106                         z *= invLen;
00107                 }
00108 
00110                 float dot( const cVector3& other ) const
00111                 {
00112                         return ( x * other.x + y * other.y + z * other.z );
00113                 }
00114 
00116                 cVector3 cross( const cVector3& other ) const
00117                 {
00118                         return cVector3( y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x );
00119                 }
00120 
00122                 void set( float nx, float ny, float nz )
00123                 {
00124                         x = nx;
00125                         y = ny;
00126                         z = nz;
00127                 }
00128 
00130                 void set( float n )
00131                 {
00132                         x = y = z = n;
00133                 }
00134 
00136                 void set( const cVector3& other )
00137                 {
00138                         x = other.x;
00139                         y = other.y;
00140                         z = other.z;
00141                 }
00142 
00143                 void getAsArray(float* output)
00144                 {
00145                         output[0] = x;
00146                         output[1] = y;
00147                         output[2] = z;
00148                 }
00149         };
00150 };
00151 
00152 #endif 
 All Classes Namespaces Functions Variables Enumerations

Generated on Sat Feb 20 22:55:07 2010 for cAudio by  doxygen 1.6.2