00001 #ifndef AI__VECTOR4_H
00002 #define AI__VECTOR4_H
00003
00012 #include "AI_Math.h"
00013 #include <float.h>
00014
00015 class AI_TVector4
00016 {
00017 public:
00018 enum component
00019 {
00020 X = (1<<0),
00021 Y = (1<<1),
00022 Z = (1<<2),
00023 W = (1<<3),
00024 };
00025
00027 AI_TVector4();
00029 AI_TVector4(const float _x, const float _y, const float _z, const float _w);
00031 AI_TVector4(const AI_TVector4& vec);
00033 AI_TVector4(const AI_TVector3& vec3);
00035 void set(const float _x, const float _y, const float _z, const float _w);
00037 void set(const AI_TVector4& v);
00039 void set(const AI_TVector3& v);
00041 float len() const;
00043 void norm();
00045 void operator +=(const AI_TVector4& v);
00047 void operator -=(const AI_TVector4& v);
00049 void operator *=(const float s);
00051 AI_TVector4& operator=(const AI_TVector3& v);
00053 bool isequal(const AI_TVector4& v, float tol) const;
00055 int compare(const AI_TVector4& v, float tol) const;
00057 void minimum(const AI_TVector4& v);
00059 void maximum(const AI_TVector4& v);
00061 void setcomp(float val, int mask);
00063 float getcomp(int mask);
00065 int mincompmask() const;
00067 void lerp(const AI_TVector4& v0, float lerpVal);
00069 void lerp(const AI_TVector4& v0, const AI_TVector4& v1, float lerpVal);
00071 void saturate();
00073 float dot(const AI_TVector4& v0) const;
00074
00075 float x, y, z, w;
00076 };
00077
00078
00081 inline
00082 AI_TVector4::AI_TVector4() :
00083 x(0.0f),
00084 y(0.0f),
00085 z(0.0f),
00086 w(0.0f)
00087 {
00088
00089 }
00090
00091
00094 inline
00095 AI_TVector4::AI_TVector4(const float _x, const float _y, const float _z, const float _w) :
00096 x(_x),
00097 y(_y),
00098 z(_z),
00099 w(_w)
00100 {
00101
00102 }
00103
00104
00107 inline
00108 AI_TVector4::AI_TVector4(const AI_TVector4& v) :
00109 x(v.x),
00110 y(v.y),
00111 z(v.z),
00112 w(v.w)
00113 {
00114
00115 }
00116
00117
00120 inline
00121 AI_TVector4::AI_TVector4(const AI_TVector3& v) :
00122 x(v.x),
00123 y(v.y),
00124 z(v.z),
00125 w(1.0f)
00126 {
00127
00128 }
00129
00130
00133 inline
00134 void
00135 AI_TVector4::set(const float _x, const float _y, const float _z, const float _w)
00136 {
00137 x = _x;
00138 y = _y;
00139 z = _z;
00140 w = _w;
00141 }
00142
00143
00146 inline
00147 void
00148 AI_TVector4::set(const AI_TVector4& v)
00149 {
00150 x = v.x;
00151 y = v.y;
00152 z = v.z;
00153 w = v.w;
00154 }
00155
00156
00159 inline
00160 void
00161 AI_TVector4::set(const AI_TVector3& v)
00162 {
00163 x = v.x;
00164 y = v.y;
00165 z = v.z;
00166 w = 1.0f;
00167 }
00168
00169
00172 inline
00173 float
00174 AI_TVector4::len() const
00175 {
00176 return (float) sqrt(x * x + y * y + z * z + w * w);
00177 }
00178
00179
00182 inline
00183 void
00184 AI_TVector4::norm()
00185 {
00186 float l = len();
00187 if (l > AI_TINY)
00188 {
00189 float oneDivL = 1.0f / l;
00190 x *= oneDivL;
00191 y *= oneDivL;
00192 z *= oneDivL;
00193 w *= oneDivL;
00194 }
00195 }
00196
00197
00200 inline
00201 void
00202 AI_TVector4::operator +=(const AI_TVector4& v)
00203 {
00204 x += v.x;
00205 y += v.y;
00206 z += v.z;
00207 w += v.w;
00208 }
00209
00210
00213 inline
00214 void
00215 AI_TVector4::operator -=(const AI_TVector4& v)
00216 {
00217 x -= v.x;
00218 y -= v.y;
00219 z -= v.z;
00220 w -= v.w;
00221 }
00222
00223
00226 inline
00227 void
00228 AI_TVector4::operator *=(const float s)
00229 {
00230 x *= s;
00231 y *= s;
00232 z *= s;
00233 w *= s;
00234 }
00235
00236
00239 inline
00240 AI_TVector4&
00241 AI_TVector4::operator=(const AI_TVector3& v)
00242 {
00243 this->set(v);
00244 return *this;
00245 }
00246
00247
00250 inline
00251 bool
00252 AI_TVector4::isequal(const AI_TVector4& v, float tol) const
00253 {
00254 if (fabs(v.x - x) > tol) return false;
00255 else if (fabs(v.y - y) > tol) return false;
00256 else if (fabs(v.z - z) > tol) return false;
00257 else if (fabs(v.w - w) > tol) return false;
00258 return true;
00259 }
00260
00261
00264 inline
00265 int
00266 AI_TVector4::compare(const AI_TVector4& v, float tol) const
00267 {
00268 if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1;
00269 else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1;
00270 else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1;
00271 else if (fabs(v.w - w) > tol) return (v.w > w) ? +1 : -1;
00272 else return 0;
00273 }
00274
00275
00278 inline
00279 void
00280 AI_TVector4::minimum(const AI_TVector4& v)
00281 {
00282 if (v.x < x) x = v.x;
00283 if (v.y < y) y = v.y;
00284 if (v.z < z) z = v.z;
00285 if (v.w < w) w = v.w;
00286 }
00287
00288
00291 inline
00292 void
00293 AI_TVector4::maximum(const AI_TVector4& v)
00294 {
00295 if (v.x > x) x = v.x;
00296 if (v.y > y) y = v.y;
00297 if (v.z > z) z = v.z;
00298 if (v.w > w) w = v.w;
00299 }
00300
00301
00304 static
00305 inline
00306 AI_TVector4 operator +(const AI_TVector4& v0, const AI_TVector4& v1)
00307 {
00308 return AI_TVector4(v0.x + v1.x, v0.y + v1.y, v0.z + v1.z, v0.w + v1.w);
00309 }
00310
00311
00314 static
00315 inline
00316 AI_TVector4 operator -(const AI_TVector4& v0, const AI_TVector4& v1)
00317 {
00318 return AI_TVector4(v0.x - v1.x, v0.y - v1.y, v0.z - v1.z, v0.w - v1.w);
00319 }
00320
00321
00324 static
00325 inline
00326 AI_TVector4 operator *(const AI_TVector4& v0, const float& s)
00327 {
00328 return AI_TVector4(v0.x * s, v0.y * s, v0.z * s, v0.w * s);
00329 }
00330
00331
00334 static
00335 inline
00336 AI_TVector4 operator -(const AI_TVector4& v)
00337 {
00338 return AI_TVector4(-v.x, -v.y, -v.z, -v.w);
00339 }
00340
00341
00344 inline
00345 void
00346 AI_TVector4::setcomp(float val, int mask)
00347 {
00348 if (mask & X) x = val;
00349 if (mask & Y) y = val;
00350 if (mask & Z) z = val;
00351 if (mask & W) w = val;
00352 }
00353
00354
00357 inline
00358 float
00359 AI_TVector4::getcomp(int mask)
00360 {
00361 switch (mask)
00362 {
00363 case X: return x;
00364 case Y: return y;
00365 case Z: return z;
00366 default: return w;
00367 }
00368 }
00369
00370
00373 inline
00374 int
00375 AI_TVector4::mincompmask() const
00376 {
00377 float minVal = x;
00378 int minComp = X;
00379 if (y < minVal)
00380 {
00381 minComp = Y;
00382 minVal = y;
00383 }
00384 if (z < minVal)
00385 {
00386 minComp = Z;
00387 minVal = z;
00388 }
00389 if (w < minVal)
00390 {
00391 minComp = W;
00392 minVal = w;
00393 }
00394 return minComp;
00395 }
00396
00397
00400 inline
00401 void
00402 AI_TVector4::lerp(const AI_TVector4& v0, float lerpVal)
00403 {
00404 x = v0.x + ((x - v0.x) * lerpVal);
00405 y = v0.y + ((y - v0.y) * lerpVal);
00406 z = v0.z + ((z - v0.z) * lerpVal);
00407 w = v0.w + ((w - v0.w) * lerpVal);
00408 }
00409
00410
00413 inline
00414 void
00415 AI_TVector4::lerp(const AI_TVector4& v0, const AI_TVector4& v1, float lerpVal)
00416 {
00417 x = v0.x + ((v1.x - v0.x) * lerpVal);
00418 y = v0.y + ((v1.y - v0.y) * lerpVal);
00419 z = v0.z + ((v1.z - v0.z) * lerpVal);
00420 w = v0.w + ((v1.w - v0.w) * lerpVal);
00421 }
00422
00423
00424
00427 inline
00428 void
00429 AI_TVector4::saturate()
00430 {
00431 x = ai_saturate(x);
00432 y = ai_saturate(y);
00433 z = ai_saturate(z);
00434 w = ai_saturate(w);
00435 }
00436
00437
00441 inline
00442 float AI_TVector4::dot(const AI_TVector4& v0) const
00443 {
00444 return ( x * v0.x + y * v0.y + z * v0.z + w * v0.w );
00445 }
00446
00447
00448 #endif
00449