Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages
vector4.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998,1999,2000 by Jorrit Tyberghein 00003 Largely rewritten by Ivan Avramovic <ivan@avramovic.com> 00004 Extended (and some methods removed) to 4 component by Marten 00005 Svanfeldt 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public 00018 License along with this library; if not, write to the Free 00019 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 #ifndef __CS_VECTOR4_H__ 00023 #define __CS_VECTOR4_H__ 00024 00031 #include "csextern.h" 00032 #include "csgeom/vector3.h" 00033 00034 class csVector4; 00035 00036 00040 class CS_CSGEOM_EXPORT csDVector4 00041 { 00042 public: 00044 double x; 00046 double y; 00048 double z; 00050 double w; 00051 00056 csDVector4 () {} 00057 00063 csDVector4 (double m) : x(m), y(m), z(m), w(m) {} 00064 00066 csDVector4 (double ix, double iy, double iz = 0, double iw = 1) 00067 { x = ix; y = iy; z = iz; w = iw;} 00068 00070 csDVector4 (const csDVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00071 00073 csDVector4 (const csVector4&); 00074 00076 csDVector4 (const csDVector3& v) { x = v.x; y = v.y; z = v.z; w = 1.0; } 00077 00079 inline friend 00080 csDVector4 operator+ (const csDVector4& v1, const csDVector4& v2) 00081 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00082 00084 inline friend 00085 csDVector4 operator- (const csDVector4& v1, const csDVector4& v2) 00086 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00087 00089 inline friend double operator* (const csDVector4& v1, const csDVector4& v2) 00090 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00091 00093 inline friend csDVector4 operator% (const csDVector4& v1, 00094 const csDVector4& v2) 00095 { 00096 00097 return csDVector4 ( 00098 (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y), 00099 (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z), 00100 (v1.x*v2.z-v1.z-v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z), 00101 (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) ); 00102 } 00103 00105 void Cross (const csDVector4 & v1, const csDVector4 & v2) 00106 { 00107 x = (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y); 00108 y = (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z); 00109 z = (v1.x*v2.z-v1.z-v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z); 00110 w = (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w); 00111 } 00112 00114 inline friend csDVector4 operator* (const csDVector4& v, double f) 00115 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00116 00118 inline friend csDVector4 operator* (double f, const csDVector4& v) 00119 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00120 00122 inline friend csDVector4 operator/ (const csDVector4& v, double f) 00123 { f = 1.0f/f; return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00124 00126 inline friend bool operator== (const csDVector4& v1, const csDVector4& v2) 00127 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w == v2.w; } 00128 00130 inline friend bool operator!= (const csDVector4& v1, const csDVector4& v2) 00131 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00132 00134 inline friend 00135 csDVector4 operator>> (const csDVector4& v1, const csDVector4& v2) 00136 { return v2*(v1*v2)/(v2*v2); } 00137 00139 inline friend 00140 csDVector4 operator<< (const csDVector4& v1, const csDVector4& v2) 00141 { return v1*(v1*v2)/(v1*v1); } 00142 00144 inline friend bool operator< (const csDVector4& v, double f) 00145 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00146 00148 inline friend bool operator> (double f, const csDVector4& v) 00149 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00150 00152 inline double operator[](int n) const 00153 { return !n?x:n&1?y:n&2?z:w; } 00154 00156 inline double & operator[](int n) 00157 { return !n?x:n&1?y:n&2?z:w; } 00158 00160 inline csDVector4& operator+= (const csDVector4& v) 00161 { 00162 x += v.x; 00163 y += v.y; 00164 z += v.z; 00165 w += v.w; 00166 00167 return *this; 00168 } 00169 00171 inline csDVector4& operator-= (const csDVector4& v) 00172 { 00173 x -= v.x; 00174 y -= v.y; 00175 z -= v.z; 00176 w -= v.w; 00177 00178 return *this; 00179 } 00180 00182 inline csDVector4& operator*= (double f) 00183 { x *= f; y *= f; z *= f; w *= f; return *this; } 00184 00186 inline csDVector4& operator/= (double f) 00187 { x /= f; y /= f; z /= f; w /= f; return *this; } 00188 00190 inline csDVector4 operator+ () const { return *this; } 00191 00193 inline csDVector4 operator- () const { return csDVector4(-x,-y,-z,-w); } 00194 00196 inline void Set (double sx, double sy, double sz, double sw) 00197 { x = sx; y = sy; z = sz; w = sw; } 00198 00200 double Norm () const; 00201 00203 double SquaredNorm () const; 00204 00210 csDVector4 Unit () const { return (*this)/(this->Norm()); } 00211 00213 inline static double Norm (const csDVector4& v) { return v.Norm(); } 00214 00216 inline static csDVector4 Unit (const csDVector4& v) { return v.Unit(); } 00217 00219 void Normalize(); 00220 }; 00221 00222 00226 class CS_CSGEOM_EXPORT csVector4 00227 { 00228 public: 00230 float x; 00232 float y; 00234 float z; 00236 float w; 00237 00242 csVector4 () {} 00243 00249 csVector4 (float m) : x(m), y(m), z(m), w(m) {} 00250 00252 csVector4 (float ix, float iy, float iz = 0, float iw = 1) 00253 : x(ix), y(iy), z(iz), w(iw) {} 00254 00256 csVector4 (const csVector4& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} 00257 00259 csVector4 (const csDVector4 &v); 00260 00262 csVector4 (const csVector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 00263 00265 inline friend csVector4 operator+ (const csVector4& v1, const csVector4& v2) 00266 { return csVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00267 00269 inline friend csDVector4 operator+ (const csDVector4& v1, const csVector4& v2) 00270 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00271 00273 inline friend csDVector4 operator+ (const csVector4& v1, const csDVector4& v2) 00274 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00275 00277 inline friend csVector4 operator- (const csVector4& v1, const csVector4& v2) 00278 { return csVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00279 00281 inline friend csDVector4 operator- (const csVector4& v1, const csDVector4& v2) 00282 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00283 00285 inline friend csDVector4 operator- (const csDVector4& v1, const csVector4& v2) 00286 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00287 00288 00290 inline friend float operator* (const csVector4& v1, const csVector4& v2) 00291 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00292 00294 inline friend csVector4 operator% (const csVector4& v1, const csVector4& v2) 00295 { 00296 return csVector4 ( 00297 (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y), 00298 (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z), 00299 (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z), 00300 (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) ); 00301 } 00302 00304 void Cross (const csVector4 & v1, const csVector4 & v2) 00305 { 00306 x = (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y); 00307 y = (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z); 00308 z = (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z); 00309 w = (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w); 00310 } 00311 00313 inline friend csVector4 operator* (const csVector4& v, float f) 00314 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00315 00317 inline friend csVector4 operator* (float f, const csVector4& v) 00318 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00319 00321 inline friend csDVector4 operator* (const csVector4& v, double f) 00322 { return csDVector4(v) * f; } 00323 00325 inline friend csDVector4 operator* (double f, const csVector4& v) 00326 { return csDVector4(v) * f; } 00327 00329 inline friend csVector4 operator* (const csVector4& v, int f) 00330 { return v * (float)f; } 00331 00333 inline friend csVector4 operator* (int f, const csVector4& v) 00334 { return v * (float)f; } 00335 00337 inline friend csVector4 operator/ (const csVector4& v, float f) 00338 { f = 1.0f/f; return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00339 00341 inline friend csDVector4 operator/ (const csVector4& v, double f) 00342 { return csDVector4(v) / f; } 00343 00345 inline friend csVector4 operator/ (const csVector4& v, int f) 00346 { return v / (float)f; } 00347 00349 inline friend bool operator== (const csVector4& v1, const csVector4& v2) 00350 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; } 00351 00353 inline friend bool operator!= (const csVector4& v1, const csVector4& v2) 00354 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00355 00357 inline friend csVector4 operator>> (const csVector4& v1, const csVector4& v2) 00358 { return v2*(v1*v2)/(v2*v2); } 00359 00361 inline friend csVector4 operator<< (const csVector4& v1, const csVector4& v2) 00362 { return v1*(v1*v2)/(v1*v1); } 00363 00365 inline friend bool operator< (const csVector4& v, float f) 00366 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00367 00369 inline friend bool operator> (float f, const csVector4& v) 00370 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00371 00373 inline float operator[] (int n) const 00374 { return !n?x:n&1?y:n&2?z:w; } 00375 00377 inline float & operator[] (int n) 00378 { return !n?x:n&1?y:n&2?z:w; } 00379 00381 inline csVector4& operator+= (const csVector4& v) 00382 { 00383 x += v.x; 00384 y += v.y; 00385 z += v.z; 00386 w += v.w; 00387 00388 return *this; 00389 } 00390 00392 inline csVector4& operator-= (const csVector4& v) 00393 { 00394 x -= v.x; 00395 y -= v.y; 00396 z -= v.z; 00397 w -= v.w; 00398 00399 return *this; 00400 } 00401 00403 inline csVector4& operator*= (float f) 00404 { x *= f; y *= f; z *= f; w *= f; return *this; } 00405 00407 inline csVector4& operator/= (float f) 00408 { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; } 00409 00411 inline csVector4 operator+ () const { return *this; } 00412 00414 inline csVector4 operator- () const { return csVector4(-x,-y,-z, -w); } 00415 00417 inline void Set (float sx, float sy, float sz, float sw) 00418 { x = sx; y = sy; z = sz; w = sw; } 00419 00421 inline void Set (csVector4 const& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00422 00424 inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; w = v[3]; } 00425 00427 inline void Set (float v) { x = y = z = w = v; } 00428 00430 inline void Get (float* v) { v[0] = x; v[1] = y; v[2] = z; v[3] = w; } 00431 00433 float Norm () const; 00434 00436 float SquaredNorm () const 00437 { return x * x + y * y + z * z + w * w; } 00438 00444 csVector4 Unit () const { return (*this)/(this->Norm()); } 00445 00447 inline static float Norm (const csVector4& v) { return v.Norm(); } 00448 00450 inline static csVector4 Unit (const csVector4& v) { return v.Unit(); } 00451 00453 void Normalize (); 00454 00456 inline bool IsZero (float precision = SMALL_EPSILON) const 00457 { return (ABS(x) < precision) && (ABS(y) < precision) 00458 && (ABS(z) < precision) && (ABS(w) < precision); 00459 } 00460 }; 00461 00464 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.3.9.1