00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00015
00016 #ifndef vec4_H
00017 #define vec4_H
00018
00019 #include <math/config.hpp>
00020 #include <math/vecbase.hpp>
00021
00022 namespace math {
00023 template<typename T> class vec4: public vecbase<T,4> {
00024 public:
00025 T& w() { return vecbase<T,4>::v[0]; }
00026 T& x() { return vecbase<T,4>::v[1]; }
00027 T& y() { return vecbase<T,4>::v[2]; }
00028 T& z() { return vecbase<T,4>::v[3]; }
00029
00033 vec4(const vec3<T> & rhs) {
00034 x() = rhs.v[0];
00035 y() = rhs.v[1];
00036 z() = rhs.v[2];
00037 w() = 1.0f;
00038 }
00039 vec4(vec3<T> const & rhs,T const & newW) {
00040 x() = rhs.v[0];
00041 y() = rhs.v[1];
00042 z() = rhs.v[2];
00043 w() = newW;
00044 }
00045 vec4() { vecbase<T,4>::loadZero(); }
00046 vec4(T newX, T newY, T newZ, T newW) {
00047 x() = (newX);
00048 y() = (newY);
00049 z() = (newZ);
00050 w() = (newW);
00051 }
00052 vec4(T const * rhs): vecbase<T,4>(rhs) {}
00053 vec4(const vec4 & rhs): vecbase<T,4>(rhs) {}
00060 void rotateX(T angle) {
00061 (*this)=GetRotatedX(angle);
00062
00063 }
00064 vec4<T> getRotatedX(T angle) const {
00065 math::vec3<T> v3d(x, y, z);
00066
00067 v3d.rotateX(angle);
00068
00069 return math::vec4<T>(v3d, w);
00070 }
00071 void rotateY(T angle) {
00072 (*this)=GetRotatedY(angle);
00073 }
00074 vec4<T> getRotatedY(T angle) const {
00075 math::vec3<T> v3d(x, y, z);
00076
00077 v3d.rotateY(angle);
00078
00079 return math::vec4<T>(v3d, w);
00080 }
00081 void rotateZ(T angle) {
00082 (*this)=GetRotatedZ(angle);
00083 }
00084
00085 vec4<T> getRotatedZ(T angle) const {
00086 math::vec3<T> v3d(x, y, z);
00087
00088 v3d.RotateZ(angle);
00089
00090 return math::vec4<T>(v3d, w);
00091 }
00092 void rotateAxis(T angle, const math::vec3<T> & axis) {
00093 (*this)=GetRotatedAxis(angle, axis);
00094 }
00095
00096 math::vec4<T> getRotatedAxis(T angle, const math::vec3<T> & axis) const
00097 {
00098 math::vec3<T> v3d(x, y, z);
00099
00100 v3d.RotateAxis(angle, axis);
00101
00102 return math::vec4<T>(v3d, w);
00103 }
00109 bool operator==(const math::vec4<T> & rhs) const {
00110 return vecbase<T,4>::operator==(rhs);
00111 }
00112 bool operator!=(const vec4 & rhs) const {
00113 return vecbase<T,4>::operator==(rhs);
00114 }
00117 operator math::vec3<T>() {
00118 if(w==0.0f || w==1.0f)
00119 return math::vec3<T>(x, y, z);
00120 else
00121 return math::vec3<T>(x/w, y/w, z/w);
00122 }
00123 math::mat44<T> operator*(math::vec4<T> const & rhs) {
00124 math::mat44<T> ret(
00125 x*rhs.x, y*rhs.x, z*rhs.x, w*rhs.x,
00126 x*rhs.y, y*rhs.y, z*rhs.y, w*rhs.y,
00127 x*rhs.z, y*rhs.z, z*rhs.z, w*rhs.z,
00128 x*rhs.w, y*rhs.w, z*rhs.w, w*rhs.w);
00129
00130 return ret;
00131 }
00132 void print()
00133 {
00134 printf("%f %f %f %f\n",x,y,z,w);
00135 }
00136
00137
00138
00139
00140
00141 ~vec4() {}
00142
00143 void Set(T newX, T newY, T newZ, T newW)
00144 { x=newX; y=newY; z=newZ; w=newW; }
00145
00146
00147 void SetX(T newX) {x = newX;}
00148 void SetY(T newY) {y = newY;}
00149 void SetZ(T newZ) {z = newZ;}
00150 void SetW(T newW) {w = newW;}
00151
00152 T GetX() const {return x;}
00153 T GetY() const {return y;}
00154 T GetZ() const {return z;}
00155 T GetW() const {return w;}
00156
00157 void LoadZero(void)
00158 { x=0.0f; y=0.0f; z=0.0f; w=0.0f; }
00159
00160 void LoadOne(void)
00161 { x=1.0f; y=1.0f; z=1.0f; w=1.0f; }
00162
00163
00164
00165 T dot(const vec4 & rhs) {
00166 return vecbase<T,4>::dot(rhs);
00167 }
00168
00169 vec4 lerp(const vec4 & v2, T factor) const
00170 { return (*this)*(1.0f-factor)+v2*factor; }
00171
00172 vec4 QuadraticInterpolate(const vec4 & v2, const vec4 & v3, T factor) const
00173 { return (*this)*(1.0f-factor)*(1.0f-factor) + v2*2*factor*(1.0f-factor) + v3*factor*factor;}
00174
00175
00176 vec4 operator+(const vec4 & rhs) const
00177 { return vec4(x+rhs.x, y+rhs.y, z+rhs.z, w+rhs.w); }
00178 vec4 operator+(const T & rhs) const
00179 { return vec4(x+rhs, y+rhs, z+rhs, w+rhs); }
00180
00181 vec4 operator-(const vec4 & rhs) const
00182 { return vec4(x-rhs.x, y-rhs.y, z-rhs.z, w-rhs.w); }
00183
00184 vec4 operator*(const T rhs) const
00185 { return vec4(x*rhs, y*rhs, z*rhs, w*rhs); }
00186
00187 vec4 operator/(const T rhs) const
00188 { return rhs==0.0f ? vec4(0.0f, 0.0f, 0.0f, 0.0f): vec4(x/rhs, y/rhs, z/rhs, w/rhs); }
00189
00190
00191
00192
00193
00194
00195 void operator+=(const vec4 & rhs)
00196 { x+=rhs.x; y+=rhs.y; z+=rhs.z; w+=rhs.w; }
00197
00198 void operator-=(const vec4 & rhs)
00199 { x-=rhs.x; y-=rhs.y; z-=rhs.z; w-=rhs.w; }
00200
00201 void operator*=(const T rhs)
00202 { x*=rhs; y*=rhs; z*=rhs; w*=rhs; }
00203
00204 void operator/=(const T rhs)
00205 { if(rhs==0.0f)
00206 return;
00207 else
00208 { x/=rhs; y/=rhs; z/=rhs; w/=rhs; }
00209 }
00210
00211
00212 operator T* () const {return (T*) this;}
00213 operator const T* () const {return (const T*) this;}
00214
00215 };
00216 }
00217
00218 #endif //vec3<T>_H