00001
00002
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*()
00031 { return (Type *)&x; }
00032 operator const Type*() const
00033 { return &x; }
00034
00035 Vec4& operator = (const Vec4& A)
00036 { x=A.x; y=A.y; z=A.z; w=A.w;
00037 return(*this); };
00038
00039 int operator == (const Vec4& A) const
00040 { return (x==A.x && y==A.y &&
00041 z==A.z && w==A.w); }
00042
00043 Vec4 operator + (const Vec4& A) const
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
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
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
00053 { Vec4 Scaled(x*s, y*s, z*s, w*s);
00054 return(Scaled); };
00055 Vec4 operator / (const Type s) const
00056 { Vec4 Scaled(x/s, y/s, z/s, w/s);
00057 return(Scaled); };
00058 Vec4 operator & (const Vec4& A) const
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)
00063 { return Vec4(v.x*s, v.y*s, v.z*s, v.w*s); }
00064
00065 Vec4& operator += (const Vec4& A)
00066 { x+=A.x; y+=A.y; z+=A.z; w+=A.w;
00067 return *this; }
00068 Vec4& operator -= (const Vec4& A)
00069 { x-=A.x; y-=A.y; z-=A.z; w-=A.w;
00070 return *this; }
00071 Vec4& operator *= (const Type s)
00072 { x*=s; y*=s; z*=s; w*=s;
00073 return *this; }
00074 Vec4& operator /= (const Type s)
00075 { x/=s; y/=s; z/=s; w/=s;
00076 return *this; }
00077 Vec4& operator &= (const Vec4& A)
00078 { x*=A.x; y*=A.y; z*=A.z; w*=A.w; return *this; }
00079 Vec4 operator - (void) const
00080 { Vec4 Negated(-x, -y, -z, -w);
00081 return(Negated); };
00082
00083
00084
00085
00086
00087
00088
00089
00090 Type Length (void) const
00091 { return ((Type)sqrt(x*x+y*y+z*z+w*w)); };
00092 Type LengthSqr (void) const
00093 { return (x*x+y*y+z*z+w*w); };
00094 Vec4& Normalize (void)
00095 { Type L = Length();
00096 if (L>0) { x/=L; y/=L; z/=L; w/=L; }
00097 return *this;
00098 };
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