Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

vector.h

Go to the documentation of this file.
00001 #ifndef VECTOR_H
00002 #define VECTOR_H
00003 
00004 #include <vector>
00005 #include <cmath>
00006 
00007 using namespace std;
00008 
00009 typedef GLfloat SCALAR;
00010 
00011 class VECTOR
00012 {
00013 public:
00014         SCALAR x,y,z; //x,y,z coordinates
00015 public:
00016         VECTOR() : x(0), y(0), z(0) {}
00017 
00018         VECTOR( const SCALAR& a, const SCALAR& b, const SCALAR& c ) : x(a), y(b), z(c) {}
00019 
00020         //index a component
00021         //NOTE: returning a reference allows
00022         //you to assign the indexed element
00023 
00024         SCALAR& operator [] ( const long i )
00025         {
00026                 return *((&x) + i);
00027         }
00028 
00029 //compare
00030 
00031         const bool operator == ( const VECTOR& v ) const
00032         {
00033                 return (v.x==x && v.y==y && v.z==z);
00034         }
00035 
00036         const bool operator != ( const VECTOR& v ) const
00037         {
00038                 return !(v == *this);
00039         }
00040 
00041 //negate
00042 
00043         const VECTOR operator - () const
00044         {
00045                 return VECTOR( -x, -y, -z );
00046         }
00047 
00048 //assign
00049 
00050         const VECTOR& operator = ( const VECTOR& v )
00051         {
00052                 x = v.x;
00053                 y = v.y;
00054                 z = v.z;
00055                 return *this;
00056         }
00057 
00058 //increment
00059 
00060         const VECTOR& operator += ( const VECTOR& v ) 
00061         {
00062                 x+=v.x;
00063                 y+=v.y;
00064                 z+=v.z;
00065                 return *this;
00066         } 
00067 
00068 //decrement
00069 
00070         const VECTOR& operator -= ( const VECTOR& v ) 
00071         {
00072                 x-=v.x;
00073                 y-=v.y;
00074                 z-=v.z;
00075                 return *this;
00076         } 
00077 
00078 //self-multiply
00079         const VECTOR& operator *= ( const SCALAR& s )
00080         {
00081                 x*=s;
00082                 y*=s;
00083                 z*=s;
00084                 return *this;
00085         }
00086 
00087 //self-divide
00088         const VECTOR& operator /= ( const SCALAR& s )
00089         {
00090                 const SCALAR r = 1 / s;
00091                 x *= r;
00092                 y *= r;
00093                 z *= r;
00094                 return *this;
00095         }
00096 
00097 //add
00098 
00099         const VECTOR operator + ( const VECTOR& v ) const
00100         {
00101                 return VECTOR(x + v.x, y + v.y, z + v.z);
00102         }
00103 
00104 //subtract
00105 
00106         const VECTOR operator - ( const VECTOR& v ) const
00107         {
00108                 return VECTOR(x - v.x, y - v.y, z - v.z);
00109         }
00110 
00111 //post-multiply by a scalar
00112 
00113         const VECTOR operator * ( const SCALAR& s ) const
00114         {
00115                 return VECTOR( x*s, y*s, z*s );
00116         }
00117 
00118 //pre-multiply by a scalar
00119 
00120         friend inline const VECTOR operator * ( const SCALAR& s, const VECTOR& v )
00121         {
00122                 return v * s;
00123         }
00124 
00125 //divide
00126 
00127         const VECTOR operator / (SCALAR s) const
00128         {
00129                 s = 1/s;
00130                 return VECTOR( s*x, s*y, s*z );
00131         }
00132 
00133 //cross product
00134 
00135         const VECTOR cross( const VECTOR& v ) const
00136         {
00137                 //Davis, Snider, "Introduction to Vector Analysis", p. 44
00138                 return VECTOR( y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x );
00139         }
00140 
00141 //scalar dot product
00142 
00143         const SCALAR dot( const VECTOR& v ) const
00144         {
00145                 return x*v.x + y*v.y + z*v.z;
00146         }
00147 
00148 //length
00149 
00150         const SCALAR length() const
00151         {
00152                 return (SCALAR)sqrt( (double)this->dot(*this) );
00153         }
00154 
00155 //unit vector
00156 
00157         const VECTOR unit() const
00158         {
00159         return (*this) / length();
00160         }
00161 
00162 //make this a unit vector
00163 
00164         void normalize()
00165         {
00166                 (*this) /= length();
00167         }
00168 
00169 //equal within an error ‘e’
00170 
00171         const bool nearlyEquals( const VECTOR& v, const SCALAR e ) const
00172         {
00173                 return fabs(x-v.x)<e && fabs(y-v.y)<e && fabs(z-v.z)<e;
00174         }
00175 };
00176 
00177 //
00178 // A 3D position
00179 //
00180 //typedef VECTOR POINT;
00181 
00182 
00183 
00184 #endif

Generated on Thu Jan 30 21:35:43 2003 for 3DVis by doxygen1.3-rc2