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) 00072 { x = ix; y = iy; z = iz; w = iw;} 00073 00075 csDVector4 (const csDVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00076 00078 csDVector4 (const csVector4&); 00079 00081 csDVector4 (const csDVector3& v) { x = v.x; y = v.y; z = v.z; w = 1.0; } 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 00090 csDVector4 operator- (const csDVector4& v1, const csDVector4& v2) 00091 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00092 00094 inline friend double operator* (const csDVector4& v1, const csDVector4& v2) 00095 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00096 00098 inline friend csDVector4 operator% (const csDVector4& v1, 00099 const csDVector4& v2) 00100 { 00101 00102 return csDVector4 ( 00103 (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y), 00104 (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z), 00105 (v1.x*v2.z-v1.z-v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z), 00106 (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) ); 00107 } 00108 00110 void Cross (const csDVector4 & v1, const csDVector4 & v2) 00111 { 00112 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); 00113 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); 00114 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); 00115 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); 00116 } 00117 00119 inline friend csDVector4 operator* (const csDVector4& v, double f) 00120 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00121 00123 inline friend csDVector4 operator* (double f, const csDVector4& v) 00124 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00125 00127 inline friend csDVector4 operator/ (const csDVector4& v, double f) 00128 { f = 1.0f/f; return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00129 00131 inline friend bool operator== (const csDVector4& v1, const csDVector4& v2) 00132 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w == v2.w; } 00133 00135 inline friend bool operator!= (const csDVector4& v1, const csDVector4& v2) 00136 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00137 00139 inline friend 00140 csDVector4 operator>> (const csDVector4& v1, const csDVector4& v2) 00141 { return v2*(v1*v2)/(v2*v2); } 00142 00144 inline friend 00145 csDVector4 operator<< (const csDVector4& v1, const csDVector4& v2) 00146 { return v1*(v1*v2)/(v1*v1); } 00147 00149 inline friend bool operator< (const csDVector4& v, double f) 00150 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00151 00153 inline friend bool operator> (double f, const csDVector4& v) 00154 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00155 00157 inline double operator[](int n) const 00158 { return !n?x:n&1?y:n&2?z:w; } 00159 00161 inline double & operator[](int n) 00162 { return !n?x:n&1?y:n&2?z:w; } 00163 00165 inline csDVector4& operator+= (const csDVector4& v) 00166 { 00167 x += v.x; 00168 y += v.y; 00169 z += v.z; 00170 w += v.w; 00171 00172 return *this; 00173 } 00174 00176 inline csDVector4& operator-= (const csDVector4& v) 00177 { 00178 x -= v.x; 00179 y -= v.y; 00180 z -= v.z; 00181 w -= v.w; 00182 00183 return *this; 00184 } 00185 00187 inline csDVector4& operator*= (double f) 00188 { x *= f; y *= f; z *= f; w *= f; return *this; } 00189 00191 inline csDVector4& operator/= (double f) 00192 { x /= f; y /= f; z /= f; w /= f; return *this; } 00193 00195 inline csDVector4 operator+ () const { return *this; } 00196 00198 inline csDVector4 operator- () const { return csDVector4(-x,-y,-z,-w); } 00199 00201 inline void Set (double sx, double sy, double sz, double sw) 00202 { x = sx; y = sy; z = sz; w = sw; } 00203 00205 double Norm () const; 00206 00208 double SquaredNorm () const; 00209 00215 csDVector4 Unit () const { return (*this)/(this->Norm()); } 00216 00218 inline static double Norm (const csDVector4& v) { return v.Norm(); } 00219 00221 inline static csDVector4 Unit (const csDVector4& v) { return v.Unit(); } 00222 00224 void Normalize(); 00225 }; 00226 00227 00231 class CS_CSGEOM_EXPORT csVector4 00232 { 00233 public: 00235 float x; 00237 float y; 00239 float z; 00241 float w; 00242 00247 csVector4 () {} 00248 00254 csVector4 (float m) : x(m), y(m), z(m), w(m) {} 00255 00257 csVector4 (float ix, float iy, float iz = 0, float iw = 1) 00258 : x(ix), y(iy), z(iz), w(iw) {} 00259 00261 csVector4 (const csVector4& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} 00262 00264 csVector4 (const csDVector4 &v); 00265 00267 csVector4 (const csVector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 00268 00270 inline friend csVector4 operator+ (const csVector4& v1, const csVector4& v2) 00271 { return csVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00272 00274 inline friend csDVector4 operator+ (const csDVector4& v1, const csVector4& v2) 00275 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00276 00278 inline friend csDVector4 operator+ (const csVector4& v1, const csDVector4& v2) 00279 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00280 00282 inline friend csVector4 operator- (const csVector4& v1, const csVector4& v2) 00283 { return csVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00284 00286 inline friend csDVector4 operator- (const csVector4& v1, const csDVector4& v2) 00287 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00288 00290 inline friend csDVector4 operator- (const csDVector4& v1, const csVector4& v2) 00291 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00292 00293 00295 inline friend float operator* (const csVector4& v1, const csVector4& v2) 00296 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00297 00299 inline friend csVector4 operator% (const csVector4& v1, const csVector4& v2) 00300 { 00301 return csVector4 ( 00302 (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y), 00303 (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z), 00304 (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z), 00305 (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) ); 00306 } 00307 00309 void Cross (const csVector4 & v1, const csVector4 & v2) 00310 { 00311 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); 00312 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); 00313 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); 00314 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); 00315 } 00316 00318 inline friend csVector4 operator* (const csVector4& v, float f) 00319 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00320 00322 inline friend csVector4 operator* (float f, const csVector4& v) 00323 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00324 00326 inline friend csDVector4 operator* (const csVector4& v, double f) 00327 { return csDVector4(v) * f; } 00328 00330 inline friend csDVector4 operator* (double f, const csVector4& v) 00331 { return csDVector4(v) * f; } 00332 00334 inline friend csVector4 operator* (const csVector4& v, int f) 00335 { return v * (float)f; } 00336 00338 inline friend csVector4 operator* (int f, const csVector4& v) 00339 { return v * (float)f; } 00340 00342 inline friend csVector4 operator/ (const csVector4& v, float f) 00343 { f = 1.0f/f; return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00344 00346 inline friend csDVector4 operator/ (const csVector4& v, double f) 00347 { return csDVector4(v) / f; } 00348 00350 inline friend csVector4 operator/ (const csVector4& v, int f) 00351 { return v / (float)f; } 00352 00354 inline friend bool operator== (const csVector4& v1, const csVector4& v2) 00355 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; } 00356 00358 inline friend bool operator!= (const csVector4& v1, const csVector4& v2) 00359 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00360 00362 inline friend csVector4 operator>> (const csVector4& v1, const csVector4& v2) 00363 { return v2*(v1*v2)/(v2*v2); } 00364 00366 inline friend csVector4 operator<< (const csVector4& v1, const csVector4& v2) 00367 { return v1*(v1*v2)/(v1*v1); } 00368 00370 inline friend bool operator< (const csVector4& v, float f) 00371 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00372 00374 inline friend bool operator> (float f, const csVector4& v) 00375 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00376 00378 inline float operator[] (int n) const 00379 { return !n?x:n&1?y:n&2?z:w; } 00380 00382 inline float & operator[] (int n) 00383 { return !n?x:n&1?y:n&2?z:w; } 00384 00386 inline csVector4& operator+= (const csVector4& v) 00387 { 00388 x += v.x; 00389 y += v.y; 00390 z += v.z; 00391 w += v.w; 00392 00393 return *this; 00394 } 00395 00397 inline csVector4& operator-= (const csVector4& v) 00398 { 00399 x -= v.x; 00400 y -= v.y; 00401 z -= v.z; 00402 w -= v.w; 00403 00404 return *this; 00405 } 00406 00408 inline csVector4& operator*= (float f) 00409 { x *= f; y *= f; z *= f; w *= f; return *this; } 00410 00412 inline csVector4& operator/= (float f) 00413 { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; } 00414 00416 inline csVector4 operator+ () const { return *this; } 00417 00419 inline csVector4 operator- () const { return csVector4(-x,-y,-z, -w); } 00420 00422 inline void Set (float sx, float sy, float sz, float sw) 00423 { x = sx; y = sy; z = sz; w = sw; } 00424 00426 inline void Set (csVector4 const& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00427 00429 inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; w = v[3]; } 00430 00432 inline void Set (float v) { x = y = z = w = v; } 00433 00435 inline void Get (float* v) { v[0] = x; v[1] = y; v[2] = z; v[3] = w; } 00436 00438 float Norm () const; 00439 00441 float SquaredNorm () const 00442 { return x * x + y * y + z * z + w * w; } 00443 00449 csVector4 Unit () const { return (*this)/(this->Norm()); } 00450 00452 inline static float Norm (const csVector4& v) { return v.Norm(); } 00453 00455 inline static csVector4 Unit (const csVector4& v) { return v.Unit(); } 00456 00458 void Normalize (); 00459 00461 inline bool IsZero (float precision = SMALL_EPSILON) const 00462 { return (ABS(x) < precision) && (ABS(y) < precision) 00463 && (ABS(z) < precision) && (ABS(w) < precision); 00464 } 00465 }; 00466 00469 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.2.18
