00001 #ifndef _VECTOR4_H
00002 #define _VECTOR4_H
00003
00012 #include "mathlib/nmath.h"
00013 #include <float.h>
00014 #include "mathlib/_vector3.h"
00015
00016
00017 class _vector4
00018 {
00019 public:
00020 static const _vector4 zero;
00021
00022 public:
00023 enum component
00024 {
00025 X = (1<<0),
00026 Y = (1<<1),
00027 Z = (1<<2),
00028 W = (1<<3),
00029 };
00030
00032 _vector4();
00034 _vector4(const float _x, const float _y, const float _z, const float _w);
00036 _vector4(const _vector4& vec);
00038 _vector4(const _vector3& vec3);
00040 void set(const float _x, const float _y, const float _z, const float _w);
00042 void set(const _vector4& v);
00044 void set(const _vector3& v);
00046 float len() const;
00048 void norm();
00050 void operator +=(const _vector4& v);
00052 void operator -=(const _vector4& v);
00054 void operator *=(const float s);
00056 bool operator ==(const _vector4& v0);
00058 bool operator !=(const _vector4& v0);
00060 _vector4& operator=(const _vector3& v);
00062 bool isequal(const _vector4& v, float tol) const;
00064 int compare(const _vector4& v, float tol) const;
00066 void minimum(const _vector4& v);
00068 void maximum(const _vector4& v);
00070 void setcomp(float val, int mask);
00072 float getcomp(int mask);
00074 int mincompmask() const;
00076 void lerp(const _vector4& v0, float lerpVal);
00078 void lerp(const _vector4& v0, const _vector4& v1, float lerpVal);
00080 void saturate();
00082 float dot(const _vector4& v0) const;
00083
00084 float x, y, z, w;
00085 };
00086
00087
00090 inline
00091 _vector4::_vector4() :
00092 x(0.0f),
00093 y(0.0f),
00094 z(0.0f),
00095 w(0.0f)
00096 {
00097
00098 }
00099
00100
00103 inline
00104 _vector4::_vector4(const float _x, const float _y, const float _z, const float _w) :
00105 x(_x),
00106 y(_y),
00107 z(_z),
00108 w(_w)
00109 {
00110
00111 }
00112
00113
00116 inline
00117 _vector4::_vector4(const _vector4& v) :
00118 x(v.x),
00119 y(v.y),
00120 z(v.z),
00121 w(v.w)
00122 {
00123
00124 }
00125
00126
00129 inline
00130 _vector4::_vector4(const _vector3& v) :
00131 x(v.x),
00132 y(v.y),
00133 z(v.z),
00134 w(1.0f)
00135 {
00136
00137 }
00138
00139
00142 inline
00143 void
00144 _vector4::set(const float _x, const float _y, const float _z, const float _w)
00145 {
00146 x = _x;
00147 y = _y;
00148 z = _z;
00149 w = _w;
00150 }
00151
00152
00155 inline
00156 void
00157 _vector4::set(const _vector4& v)
00158 {
00159 x = v.x;
00160 y = v.y;
00161 z = v.z;
00162 w = v.w;
00163 }
00164
00165
00168 inline
00169 void
00170 _vector4::set(const _vector3& v)
00171 {
00172 x = v.x;
00173 y = v.y;
00174 z = v.z;
00175 w = 1.0f;
00176 }
00177
00178
00181 inline
00182 float
00183 _vector4::len() const
00184 {
00185 return (float) sqrt(x * x + y * y + z * z + w * w);
00186 }
00187
00188
00191 inline
00192 void
00193 _vector4::norm()
00194 {
00195 float l = len();
00196 if (l > TINY)
00197 {
00198 float oneDivL = 1.0f / l;
00199 x *= oneDivL;
00200 y *= oneDivL;
00201 z *= oneDivL;
00202 w *= oneDivL;
00203 }
00204 }
00205
00206
00209 inline
00210 void
00211 _vector4::operator +=(const _vector4& v)
00212 {
00213 x += v.x;
00214 y += v.y;
00215 z += v.z;
00216 w += v.w;
00217 }
00218
00219
00222 inline
00223 void
00224 _vector4::operator -=(const _vector4& v)
00225 {
00226 x -= v.x;
00227 y -= v.y;
00228 z -= v.z;
00229 w -= v.w;
00230 }
00231
00232
00235 inline
00236 void
00237 _vector4::operator *=(const float s)
00238 {
00239 x *= s;
00240 y *= s;
00241 z *= s;
00242 w *= s;
00243 }
00244
00245
00248 inline
00249 bool
00250 _vector4::operator ==(const _vector4& rhs)
00251 {
00252 if ((this->x == rhs.x) && (this->y == rhs.y) && (this->z == rhs.z) && (this->w == rhs.w))
00253 {
00254 return true;
00255 }
00256 else
00257 {
00258 return false;
00259 }
00260 }
00261
00262
00265 inline
00266 bool
00267 _vector4::operator !=(const _vector4& rhs)
00268 {
00269 if ((this->x != rhs.x) || (this->y != rhs.y) || (this->z != rhs.z) || (this->w != rhs.w))
00270 {
00271 return true;
00272 }
00273 else
00274 {
00275 return false;
00276 }
00277 }
00278
00279
00282 inline
00283 _vector4&
00284 _vector4::operator=(const _vector3& v)
00285 {
00286 this->set(v);
00287 return *this;
00288 }
00289
00290
00293 inline
00294 bool
00295 _vector4::isequal(const _vector4& v, float tol) const
00296 {
00297 if (fabs(v.x - x) > tol) return false;
00298 else if (fabs(v.y - y) > tol) return false;
00299 else if (fabs(v.z - z) > tol) return false;
00300 else if (fabs(v.w - w) > tol) return false;
00301 return true;
00302 }
00303
00304
00307 inline
00308 int
00309 _vector4::compare(const _vector4& v, float tol) const
00310 {
00311 if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1;
00312 else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1;
00313 else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1;
00314 else if (fabs(v.w - w) > tol) return (v.w > w) ? +1 : -1;
00315 else return 0;
00316 }
00317
00318
00321 inline
00322 void
00323 _vector4::minimum(const _vector4& v)
00324 {
00325 if (v.x < x) x = v.x;
00326 if (v.y < y) y = v.y;
00327 if (v.z < z) z = v.z;
00328 if (v.w < w) w = v.w;
00329 }
00330
00331
00334 inline
00335 void
00336 _vector4::maximum(const _vector4& v)
00337 {
00338 if (v.x > x) x = v.x;
00339 if (v.y > y) y = v.y;
00340 if (v.z > z) z = v.z;
00341 if (v.w > w) w = v.w;
00342 }
00343
00344
00347 static
00348 inline
00349 _vector4 operator +(const _vector4& v0, const _vector4& v1)
00350 {
00351 return _vector4(v0.x + v1.x, v0.y + v1.y, v0.z + v1.z, v0.w + v1.w);
00352 }
00353
00354
00357 static
00358 inline
00359 _vector4 operator -(const _vector4& v0, const _vector4& v1)
00360 {
00361 return _vector4(v0.x - v1.x, v0.y - v1.y, v0.z - v1.z, v0.w - v1.w);
00362 }
00363
00364
00367 static
00368 inline
00369 _vector4 operator *(const _vector4& v0, const float& s)
00370 {
00371 return _vector4(v0.x * s, v0.y * s, v0.z * s, v0.w * s);
00372 }
00373
00374
00377 static
00378 inline
00379 _vector4 operator -(const _vector4& v)
00380 {
00381 return _vector4(-v.x, -v.y, -v.z, -v.w);
00382 }
00383
00384
00387 inline
00388 void
00389 _vector4::setcomp(float val, int mask)
00390 {
00391 if (mask & X) x = val;
00392 if (mask & Y) y = val;
00393 if (mask & Z) z = val;
00394 if (mask & W) w = val;
00395 }
00396
00397
00400 inline
00401 float
00402 _vector4::getcomp(int mask)
00403 {
00404 switch (mask)
00405 {
00406 case X: return x;
00407 case Y: return y;
00408 case Z: return z;
00409 default: return w;
00410 }
00411 }
00412
00413
00416 inline
00417 int
00418 _vector4::mincompmask() const
00419 {
00420 float minVal = x;
00421 int minComp = X;
00422 if (y < minVal)
00423 {
00424 minComp = Y;
00425 minVal = y;
00426 }
00427 if (z < minVal)
00428 {
00429 minComp = Z;
00430 minVal = z;
00431 }
00432 if (w < minVal)
00433 {
00434 minComp = W;
00435 minVal = w;
00436 }
00437 return minComp;
00438 }
00439
00440
00443 inline
00444 void
00445 _vector4::lerp(const _vector4& v0, float lerpVal)
00446 {
00447 x = v0.x + ((x - v0.x) * lerpVal);
00448 y = v0.y + ((y - v0.y) * lerpVal);
00449 z = v0.z + ((z - v0.z) * lerpVal);
00450 w = v0.w + ((w - v0.w) * lerpVal);
00451 }
00452
00453
00456 inline
00457 void
00458 _vector4::lerp(const _vector4& v0, const _vector4& v1, float lerpVal)
00459 {
00460 x = v0.x + ((v1.x - v0.x) * lerpVal);
00461 y = v0.y + ((v1.y - v0.y) * lerpVal);
00462 z = v0.z + ((v1.z - v0.z) * lerpVal);
00463 w = v0.w + ((v1.w - v0.w) * lerpVal);
00464 }
00465
00466
00467
00470 inline
00471 void
00472 _vector4::saturate()
00473 {
00474 x = n_saturate(x);
00475 y = n_saturate(y);
00476 z = n_saturate(z);
00477 w = n_saturate(w);
00478 }
00479
00480
00484 inline
00485 float _vector4::dot(const _vector4& v0) const
00486 {
00487 return (x * v0.x + y * v0.y + z * v0.z + w * v0.w);
00488 }
00489
00490
00491 #endif
00492