Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

vec4f.hpp

00001 //==========================================================================
00002 // vec4.hpp : 4d vector class template. Works for any integer or real type.
00003 //==========================================================================
00004 
00005 #ifndef VEC4_H
00006 #define VEC4_H
00007 
00008 #include <stdio.h>
00009 #include <math.h>
00010 
00011 template <class Type>
00012 class Vec4
00013 {
00014   public:
00015     Type x, y, z, w;
00016 
00017     Vec4 (void) 
00018       {};
00019     Vec4 (const Type X, const Type Y, const Type Z, const Type W) 
00020       { x=X; y=Y; z=Z; w=W; };
00021     Vec4 (const Vec4& v) 
00022       { x=v.x; y=v.y; z=v.z; w=v.w; };
00023     Vec4 (const Type v[4])
00024       { x=v[0]; y=v[1]; z=v[2]; w=v[3]; };
00025     void Set (const Type X, const Type Y, const Type Z, const Type W)
00026       { x=X; y=Y; z=Z; w=W; }
00027     void Set (const Type v[4])
00028       { x=v[0]; y=v[1]; z=v[2]; w=v[3]; };
00029 
00030     operator Type*()                             // Type * CONVERSION
00031       { return (Type *)&x; }
00032     operator const Type*() const                 // CONST Type * CONVERSION
00033       { return &x; }
00034 
00035     Vec4& operator = (const Vec4& A)            // ASSIGNMENT (=)
00036       { x=A.x; y=A.y; z=A.z; w=A.w; 
00037         return(*this);  };
00038 
00039     int operator == (const Vec4& A) const        // COMPARISON (==)
00040       { return (x==A.x && y==A.y && 
00041         z==A.z && w==A.w); }
00042 
00043     Vec4 operator + (const Vec4& A) const       // ADDITION (+)
00044       { Vec4 Sum(x+A.x, y+A.y, z+A.z, w+A.w); 
00045         return(Sum); };
00046     Vec4 operator - (const Vec4& A) const       // SUBTRACTION (-)
00047       { Vec4 Diff(x-A.x, y-A.y, z-A.z, w-A.w);
00048         return(Diff); };
00049     Type operator * (const Vec4& A) const       // DOT-PRODUCT (*)
00050       { Type DotProd = x*A.x+y*A.y+z*A.z+w*A.w; 
00051         return(DotProd); };
00052     Vec4 operator * (const Type s) const        // MULTIPLY BY SCALAR (*)
00053       { Vec4 Scaled(x*s, y*s, z*s, w*s); 
00054         return(Scaled); };
00055     Vec4 operator / (const Type s) const        // DIVIDE BY SCALAR (/)
00056       { Vec4 Scaled(x/s, y/s, z/s, w/s);
00057         return(Scaled); };
00058     Vec4 operator & (const Vec4& A) const       // COMPONENT MULTIPLY (&)
00059       { Vec4 CompMult(x*A.x, y*A.y, z*A.z, w*A.w);
00060         return(CompMult); }
00061 
00062     friend inline Vec4 operator *(Type s, const Vec4& v)  // SCALAR MULT s*V
00063       { return Vec4(v.x*s, v.y*s, v.z*s, v.w*s); }
00064 
00065     Vec4& operator += (const Vec4& A)      // ACCUMULATED VECTOR ADDITION (+=)
00066       { x+=A.x; y+=A.y; z+=A.z; w+=A.w; 
00067         return *this; }
00068     Vec4& operator -= (const Vec4& A)      // ACCUMULATED VECTOR SUBTRCT (-=)
00069       { x-=A.x; y-=A.y; z-=A.z; w-=A.w;
00070         return *this; }
00071     Vec4& operator *= (const Type s)       // ACCUMULATED SCALAR MULT (*=)
00072       { x*=s; y*=s; z*=s; w*=s; 
00073         return *this; }
00074     Vec4& operator /= (const Type s)       // ACCUMULATED SCALAR DIV (/=)
00075       { x/=s; y/=s; z/=s; w/=s;
00076         return *this; }
00077     Vec4& operator &= (const Vec4& A)  // ACCUMULATED COMPONENT MULTIPLY (&=)
00078       { x*=A.x; y*=A.y; z*=A.z; w*=A.w; return *this; }
00079     Vec4 operator - (void) const          // NEGATION (-)
00080       { Vec4 Negated(-x, -y, -z, -w);
00081         return(Negated); };
00082 
00083 /*
00084     const Type& operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
00085       { return( (i==0)?x:((i==1)?y:((i==2)?z:w)) ); };
00086     Type & operator [] (const int i)
00087       { return( (i==0)?x:((i==1)?y:((i==2)?z:w)) ); };
00088 */
00089 
00090     Type Length (void) const                     // LENGTH OF VECTOR
00091       { return ((Type)sqrt(x*x+y*y+z*z+w*w)); };
00092     Type LengthSqr (void) const                  // LENGTH OF VECTOR (SQUARED)
00093       { return (x*x+y*y+z*z+w*w); };
00094     Vec4& Normalize (void)                       // NORMALIZE VECTOR
00095       { Type L = Length();                       // CALCULATE LENGTH
00096         if (L>0) { x/=L; y/=L; z/=L; w/=L; }
00097         return *this;
00098       };                                          // DIV COMPONENTS BY LENGTH
00099 
00100     void Wdiv(void)
00101       { x/=w; y/=w; z/=w; w=1; }
00102 
00103     void Print() const
00104       { printf("(%.3f, %.3f, %.3f, %.3f)\n",x, y, z, w); }
00105 
00106     static Vec4 ZERO;
00107 };
00108 
00109 typedef Vec4<float> Vec4f;
00110 typedef Vec4<double> Vec4d;
00111 
00112 template<class Type> Vec4<Type> Vec4<Type>::ZERO = Vec4<Type>(0,0,0,0);
00113 
00114 #endif
00115 
00116 

Generated at Fri Oct 12 15:12:22 2001 for GLVU by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001