csgeom/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 #ifndef __CS_CSSYSDEFS_H__ 00032 #error "cssysdef.h must be included in EVERY source file!" 00033 #endif 00034 00035 #include "csextern.h" 00036 00037 #include "csgeom/vector3.h" 00038 00039 class csVector4; 00040 00041 00045 class CS_CSGEOM_EXPORT csDVector4 00046 { 00047 public: 00049 double x; 00051 double y; 00053 double z; 00055 double w; 00056 00061 csDVector4 () {} 00062 00068 csDVector4 (double m) : x(m), y(m), z(m), w(m) {} 00069 00071 csDVector4 (double ix, double iy, double iz = 0, double iw = 1) { x = ix; y = iy; z = iz; w = iw;} 00072 00074 csDVector4 (const csDVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00075 00077 csDVector4 (const csVector4&); 00078 00080 csDVector4 (const csDVector3& v) { x = v.x; y = v.y; z = v.z; w = 1.0; } 00081 00083 inline friend 00084 csDVector4 operator+ (const csDVector4& v1, const csDVector4& v2) 00085 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00086 00088 inline friend 00089 csDVector4 operator- (const csDVector4& v1, const csDVector4& v2) 00090 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00091 00093 inline friend double operator* (const csDVector4& v1, const csDVector4& v2) 00094 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00095 00097 inline friend csDVector4 operator% (const csDVector4& v1, const csDVector4& v2) 00098 { 00099 00100 return csDVector4 ( (v1.x*v2.y - v1.y*v2.x) + (v1.x*v2.z - v1.z*v2.x) + (v1.y*v2.z - v1.z*v2.y), 00101 (v1.z*v2.y - v1.y*v2.z) + (v1.y*v2.w - v1.w*v2.y) + (v1.z*v2.w - v1.w*v2.z), 00102 (v1.x*v2.z - v1.z-v2.x) + (v1.w*v2.x - v1.x*v2.w) + (v1.z*v2.w - v1.w*v2.z), 00103 (v1.y*v2.x - v1.x*v2.y) + (v1.w*v2.x - v1.x*v2.w) + (v1.w*v2.y - v1.y*v2.w) ); 00104 } 00105 00107 void Cross (const csDVector4 & v1, const csDVector4 & v2) 00108 { 00109 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); 00110 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); 00111 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); 00112 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); 00113 } 00114 00116 inline friend csDVector4 operator* (const csDVector4& v, double f) 00117 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00118 00120 inline friend csDVector4 operator* (double f, const csDVector4& v) 00121 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00122 00124 inline friend csDVector4 operator/ (const csDVector4& v, double f) 00125 { f = 1.0f/f; return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00126 00128 inline friend bool operator== (const csDVector4& v1, const csDVector4& v2) 00129 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w == v2.w; } 00130 00132 inline friend bool operator!= (const csDVector4& v1, const csDVector4& v2) 00133 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00134 00136 inline friend 00137 csDVector4 operator>> (const csDVector4& v1, const csDVector4& v2) 00138 { return v2*(v1*v2)/(v2*v2); } 00139 00141 inline friend 00142 csDVector4 operator<< (const csDVector4& v1, const csDVector4& v2) 00143 { return v1*(v1*v2)/(v1*v1); } 00144 00146 inline friend bool operator< (const csDVector4& v, double f) 00147 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00148 00150 inline friend bool operator> (double f, const csDVector4& v) 00151 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00152 00154 inline double operator[](int n) const 00155 { return !n?x:n&1?y:n&2?z:w; } 00156 00158 inline double & operator[](int n) 00159 { return !n?x:n&1?y:n&2?z:w; } 00160 00162 inline csDVector4& operator+= (const csDVector4& v) 00163 { 00164 x += v.x; 00165 y += v.y; 00166 z += v.z; 00167 w += v.w; 00168 00169 return *this; 00170 } 00171 00173 inline csDVector4& operator-= (const csDVector4& v) 00174 { 00175 x -= v.x; 00176 y -= v.y; 00177 z -= v.z; 00178 w -= v.w; 00179 00180 return *this; 00181 } 00182 00184 inline csDVector4& operator*= (double f) 00185 { x *= f; y *= f; z *= f; w *= f; return *this; } 00186 00188 inline csDVector4& operator/= (double f) 00189 { x /= f; y /= f; z /= f; w /= f; return *this; } 00190 00192 inline csDVector4 operator+ () const { return *this; } 00193 00195 inline csDVector4 operator- () const { return csDVector4(-x,-y,-z,-w); } 00196 00198 inline void Set (double sx, double sy, double sz, double sw) { x = sx; y = sy; z = sz; w = sw; } 00199 00201 double Norm () const; 00202 00204 double SquaredNorm () const; 00205 00211 csDVector4 Unit () const { return (*this)/(this->Norm()); } 00212 00214 inline static double Norm (const csDVector4& v) { return v.Norm(); } 00215 00217 inline static csDVector4 Unit (const csDVector4& v) { return v.Unit(); } 00218 00220 void Normalize(); 00221 }; 00222 00223 00227 class CS_CSGEOM_EXPORT csVector4 00228 { 00229 public: 00231 float x; 00233 float y; 00235 float z; 00237 float w; 00238 00243 csVector4 () {} 00244 00250 csVector4 (float m) : x(m), y(m), z(m), w(m) {} 00251 00253 csVector4 (float ix, float iy, float iz = 0, float iw = 1) : 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 ( (v1.x*v2.y - v1.y*v2.x) + (v1.x*v2.z - v1.z*v2.x) + (v1.y*v2.z - v1.z*v2.y), 00297 (v1.z*v2.y - v1.y*v2.z) + (v1.y*v2.w - v1.w*v2.y) + (v1.z*v2.w - v1.w*v2.z), 00298 (v1.x*v2.z - v1.z*v2.x) + (v1.w*v2.x - v1.x*v2.w) + (v1.z*v2.w - v1.w*v2.z), 00299 (v1.y*v2.x - v1.x*v2.y) + (v1.w*v2.x - v1.x*v2.w) + (v1.w*v2.y - v1.y*v2.w) ); 00300 } 00301 00303 void Cross (const csVector4 & v1, const csVector4 & v2) 00304 { 00305 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); 00306 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); 00307 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); 00308 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); 00309 } 00310 00312 inline friend csVector4 operator* (const csVector4& v, float f) 00313 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00314 00316 inline friend csVector4 operator* (float f, const csVector4& v) 00317 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00318 00320 inline friend csDVector4 operator* (const csVector4& v, double f) 00321 { return csDVector4(v) * f; } 00322 00324 inline friend csDVector4 operator* (double f, const csVector4& v) 00325 { return csDVector4(v) * f; } 00326 00328 inline friend csVector4 operator* (const csVector4& v, int f) 00329 { return v * (float)f; } 00330 00332 inline friend csVector4 operator* (int f, const csVector4& v) 00333 { return v * (float)f; } 00334 00336 inline friend csVector4 operator/ (const csVector4& v, float f) 00337 { f = 1.0f/f; return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00338 00340 inline friend csDVector4 operator/ (const csVector4& v, double f) 00341 { return csDVector4(v) / f; } 00342 00344 inline friend csVector4 operator/ (const csVector4& v, int f) 00345 { return v / (float)f; } 00346 00348 inline friend bool operator== (const csVector4& v1, const csVector4& v2) 00349 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; } 00350 00352 inline friend bool operator!= (const csVector4& v1, const csVector4& v2) 00353 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00354 00356 inline friend csVector4 operator>> (const csVector4& v1, const csVector4& v2) 00357 { return v2*(v1*v2)/(v2*v2); } 00358 00360 inline friend csVector4 operator<< (const csVector4& v1, const csVector4& v2) 00361 { return v1*(v1*v2)/(v1*v1); } 00362 00364 inline friend bool operator< (const csVector4& v, float f) 00365 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00366 00368 inline friend bool operator> (float f, const csVector4& v) 00369 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00370 00372 inline float operator[] (int n) const 00373 { return !n?x:n&1?y:n&2?z:w; } 00374 00376 inline float & operator[] (int n) 00377 { return !n?x:n&1?y:n&2?z:w; } 00378 00380 inline csVector4& operator+= (const csVector4& v) 00381 { 00382 x += v.x; 00383 y += v.y; 00384 z += v.z; 00385 w += v.w; 00386 00387 return *this; 00388 } 00389 00391 inline csVector4& operator-= (const csVector4& v) 00392 { 00393 x -= v.x; 00394 y -= v.y; 00395 z -= v.z; 00396 w -= v.w; 00397 00398 return *this; 00399 } 00400 00402 inline csVector4& operator*= (float f) 00403 { x *= f; y *= f; z *= f; w *= f; return *this; } 00404 00406 inline csVector4& operator/= (float f) 00407 { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; } 00408 00410 inline csVector4 operator+ () const { return *this; } 00411 00413 inline csVector4 operator- () const { return csVector4(-x,-y,-z, -w); } 00414 00416 inline void Set (float sx, float sy, float sz, float sw) { x = sx; y = sy; z = sz; w = sw; } 00417 00419 inline void Set (const csVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00420 00422 float Norm () const; 00423 00425 float SquaredNorm () const 00426 { return x * x + y * y + z * z + w * w; } 00427 00433 csVector4 Unit () const { return (*this)/(this->Norm()); } 00434 00436 inline static float Norm (const csVector4& v) { return v.Norm(); } 00437 00439 inline static csVector4 Unit (const csVector4& v) { return v.Unit(); } 00440 00442 void Normalize (); 00443 00445 inline bool IsZero (float precision = SMALL_EPSILON) const 00446 { return (ABS(x) < precision) && (ABS(y) < precision) 00447 && (ABS(z) < precision) && (ABS(w) < precision); 00448 } 00449 }; 00450 00453 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.2.18