1 //-----------------------------------------------------------------------------
3 //-----------------------------------------------------------------------------
4 // GLVU : Copyright 1997 - 2002
5 // The University of North Carolina at Chapel Hill
6 //-----------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software
8 // and its documentation for any purpose is hereby granted without
9 // fee, provided that the above copyright notice appear in all copies
10 // and that both that copyright notice and this permission notice
11 // appear in supporting documentation. Binaries may be compiled with
12 // this software without any royalties or restrictions.
14 // The University of North Carolina at Chapel Hill makes no representations
15 // about the suitability of this software for any purpose. It is provided
16 // "as is" without express or implied warranty.
18 //==========================================================================
19 // vec4.hpp : 4d vector class template. Works for any integer or real type.
20 //==========================================================================
36 Vec4 (const Type X, const Type Y, const Type Z, const Type W)
37 { x=X; y=Y; z=Z; w=W; };
39 { x=v.x; y=v.y; z=v.z; w=v.w; };
40 Vec4 (const Type v[4])
41 { x=v[0]; y=v[1]; z=v[2]; w=v[3]; };
42 void Set (const Type X, const Type Y, const Type Z, const Type W)
43 { x=X; y=Y; z=Z; w=W; }
44 void Set (const Type v[4])
45 { x=v[0]; y=v[1]; z=v[2]; w=v[3]; };
47 operator Type*() // Type * CONVERSION
48 { return (Type *)&x; }
49 operator const Type*() const // CONST Type * CONVERSION
52 Vec4& operator = (const Vec4& A) // ASSIGNMENT (=)
53 { x=A.x; y=A.y; z=A.z; w=A.w;
56 bool operator == (const Vec4& A) const // COMPARISON (==)
57 { return (x==A.x && y==A.y &&
59 bool operator != (const Vec4& A) const // COMPARISON (!=)
60 { return (x!=A.x || y!=A.y ||
63 Vec4 operator + (const Vec4& A) const // ADDITION (+)
64 { Vec4 Sum(x+A.x, y+A.y, z+A.z, w+A.w);
66 Vec4 operator - (const Vec4& A) const // SUBTRACTION (-)
67 { Vec4 Diff(x-A.x, y-A.y, z-A.z, w-A.w);
69 Type operator * (const Vec4& A) const // DOT-PRODUCT (*)
70 { Type DotProd = x*A.x+y*A.y+z*A.z+w*A.w;
72 Vec4 operator * (const Type s) const // MULTIPLY BY SCALAR (*)
73 { Vec4 Scaled(x*s, y*s, z*s, w*s);
75 Vec4 operator / (const Type s) const // DIVIDE BY SCALAR (/)
76 { Vec4 Scaled(x/s, y/s, z/s, w/s);
78 Vec4 operator & (const Vec4& A) const // COMPONENT MULTIPLY (&)
79 { Vec4 CompMult(x*A.x, y*A.y, z*A.z, w*A.w);
82 friend inline Vec4 operator *(Type s, const Vec4& v) // SCALAR MULT s*V
83 { return Vec4(v.x*s, v.y*s, v.z*s, v.w*s); }
85 Vec4& operator += (const Vec4& A) // ACCUMULATED VECTOR ADDITION (+=)
86 { x+=A.x; y+=A.y; z+=A.z; w+=A.w;
88 Vec4& operator -= (const Vec4& A) // ACCUMULATED VECTOR SUBTRCT (-=)
89 { x-=A.x; y-=A.y; z-=A.z; w-=A.w;
91 Vec4& operator *= (const Type s) // ACCUMULATED SCALAR MULT (*=)
92 { x*=s; y*=s; z*=s; w*=s;
94 Vec4& operator /= (const Type s) // ACCUMULATED SCALAR DIV (/=)
95 { x/=s; y/=s; z/=s; w/=s;
97 Vec4& operator &= (const Vec4& A) // ACCUMULATED COMPONENT MULTIPLY (&=)
98 { x*=A.x; y*=A.y; z*=A.z; w*=A.w; return *this; }
99 Vec4 operator - (void) const // NEGATION (-)
100 { Vec4 Negated(-x, -y, -z, -w);
104 const Type& operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
105 { return( (i==0)?x:((i==1)?y:((i==2)?z:w)) ); };
106 Type & operator [] (const int i)
107 { return( (i==0)?x:((i==1)?y:((i==2)?z:w)) ); };
110 Type Length (void) const // LENGTH OF VECTOR
111 { return ((Type)sqrt(x*x+y*y+z*z+w*w)); };
112 Type LengthSqr (void) const // LENGTH OF VECTOR (SQUARED)
113 { return (x*x+y*y+z*z+w*w); };
114 Vec4& Normalize (void) // NORMALIZE VECTOR
115 { Type L = Length(); // CALCULATE LENGTH
116 if (L>0) { x/=L; y/=L; z/=L; w/=L; }
118 }; // DIV COMPONENTS BY LENGTH
121 { x/=w; y/=w; z/=w; w=1; }
124 { printf("(%.3f, %.3f, %.3f, %.3f)\n",x, y, z, w); }
129 typedef Vec4<float> Vec4f;
130 typedef Vec4<double> Vec4d;
132 template<class Type> Vec4<Type> Vec4<Type>::ZERO = Vec4<Type>(0,0,0,0);