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 "csgeom/vector3.h" 00036 00037 class csVector4; 00038 00039 00043 class csDVector4 00044 { 00045 public: 00047 double x; 00049 double y; 00051 double z; 00053 double w; 00054 00060 csDVector4 () {} 00061 00067 csDVector4 (double m) : x(m), y(m), z(m), w(m) {} 00068 00070 csDVector4 (double ix, double iy, double iz = 0, double iw = 1) { x = ix; y = iy; z = iz; w = iw;} 00071 00073 csDVector4 (const csDVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00074 00076 csDVector4 (const csVector4&); 00077 00079 csDVector4 (const csDVector3& v) { x = v.x; y = v.y; z = v.z; w = 1.0; } 00080 00082 inline friend 00083 csDVector4 operator+ (const csDVector4& v1, const csDVector4& v2) 00084 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00085 00087 inline friend 00088 csDVector4 operator- (const csDVector4& v1, const csDVector4& v2) 00089 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00090 00092 inline friend double operator* (const csDVector4& v1, const csDVector4& v2) 00093 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00094 00096 inline friend csDVector4 operator% (const csDVector4& v1, const csDVector4& v2) 00097 { 00098 00099 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), 00100 (v1.z*v2.y - v1.y*v2.z) + (v1.y*v2.w - v1.w*v2.y) + (v1.z*v2.w - v1.w*v2.z), 00101 (v1.x*v2.z - v1.z-v2.x) + (v1.w*v2.x - v1.x*v2.w) + (v1.z*v2.w - v1.w*v2.z), 00102 (v1.y*v1.x - v1.x*v2.y) + (v1.w*v2.x - v1.x*v2.w) + (v1.w*v2.y - v1.y*v2.w) ); 00103 } 00104 00106 void Cross (const csDVector4 & v1, const csDVector4 & v2) 00107 { 00108 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); 00109 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); 00110 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); 00111 w = (v1.y*v1.x - v1.x*v2.y) + (v1.w*v2.x - v1.x*v2.w) + (v1.w*v2.y - v1.y*v2.w); 00112 } 00113 00115 inline friend csDVector4 operator* (const csDVector4& v, double f) 00116 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00117 00119 inline friend csDVector4 operator* (double f, const csDVector4& v) 00120 { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00121 00123 inline friend csDVector4 operator/ (const csDVector4& v, double f) 00124 { f = 1.0f/f; return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00125 00127 inline friend bool operator== (const csDVector4& v1, const csDVector4& v2) 00128 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w == v2.w; } 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 00136 csDVector4 operator>> (const csDVector4& v1, const csDVector4& v2) 00137 { return v2*(v1*v2)/(v2*v2); } 00138 00140 inline friend 00141 csDVector4 operator<< (const csDVector4& v1, const csDVector4& v2) 00142 { return v1*(v1*v2)/(v1*v1); } 00143 00145 inline friend bool operator< (const csDVector4& v, double f) 00146 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00147 00149 inline friend bool operator> (double f, const csDVector4& v) 00150 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00151 00153 inline double operator[](int n) const {return !n?x:n&1?y:n&2?z:w;} 00154 00156 inline double & operator[](int n){return !n?x:n&1?y:n&2?z:w;} 00157 00159 inline csDVector4& operator+= (const csDVector4& v) 00160 { 00161 x += v.x; 00162 y += v.y; 00163 z += v.z; 00164 w += v.w; 00165 00166 return *this; 00167 } 00168 00170 inline csDVector4& operator-= (const csDVector4& v) 00171 { 00172 x -= v.x; 00173 y -= v.y; 00174 z -= v.z; 00175 w -= v.w; 00176 00177 return *this; 00178 } 00179 00181 inline csDVector4& operator*= (double f) 00182 { x *= f; y *= f; z *= f; w *= f; return *this; } 00183 00185 inline csDVector4& operator/= (double f) 00186 { x /= f; y /= f; z /= f; w /= f; return *this; } 00187 00189 inline csDVector4 operator+ () const { return *this; } 00190 00192 inline csDVector4 operator- () const { return csDVector4(-x,-y,-z,-w); } 00193 00195 inline void Set (double sx, double sy, double sz, double sw) { x = sx; y = sy; z = sz; w = sw; } 00196 00198 double Norm () const; 00199 00201 double SquaredNorm () const; 00202 00208 csDVector4 Unit () const { return (*this)/(this->Norm()); } 00209 00211 inline static double Norm (const csDVector4& v) { return v.Norm(); } 00212 00214 inline static csDVector4 Unit (const csDVector4& v) { return v.Unit(); } 00215 00217 void Normalize(); 00218 }; 00219 00220 00224 class csVector4 00225 { 00226 public: 00228 float x; 00230 float y; 00232 float z; 00234 float w; 00235 00241 csVector4 () {} 00242 00248 csVector4 (float m) : x(m), y(m), z(m), w(m) {} 00249 00251 csVector4 (float ix, float iy, float iz = 0, float iw = 1) : x(ix), y(iy), z(iz), w(iw) {} 00252 00254 csVector4 (const csVector4& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} 00255 00257 csVector4 (const csDVector4 &v); 00258 00260 csVector4 (const csVector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 00261 00263 inline friend csVector4 operator+ (const csVector4& v1, const csVector4& v2) 00264 { return csVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00265 00267 inline friend csDVector4 operator+ (const csDVector4& v1, const csVector4& v2) 00268 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00269 00271 inline friend csDVector4 operator+ (const csVector4& v1, const csDVector4& v2) 00272 { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00273 00275 inline friend csVector4 operator- (const csVector4& v1, const csVector4& v2) 00276 { return csVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00277 00279 inline friend csDVector4 operator- (const csVector4& v1, const csDVector4& v2) 00280 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00281 00283 inline friend csDVector4 operator- (const csDVector4& v1, const csVector4& v2) 00284 { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00285 00286 00288 inline friend float operator* (const csVector4& v1, const csVector4& v2) 00289 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00290 00292 inline friend csVector4 operator% (const csVector4& v1, const csVector4& v2) 00293 { 00294 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), 00295 (v1.z*v2.y - v1.y*v2.z) + (v1.y*v2.w - v1.w*v2.y) + (v1.z*v2.w - v1.w*v2.z), 00296 (v1.x*v2.z - v1.z*v2.x) + (v1.w*v2.x - v1.x*v2.w) + (v1.z*v2.w - v1.w*v2.z), 00297 (v1.y*v1.x - v1.x*v2.y) + (v1.w*v2.x - v1.x*v2.w) + (v1.w*v2.y - v1.y*v2.w) ); 00298 } 00299 00301 void Cross (const csVector4 & v1, const csVector4 & v2) 00302 { 00303 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); 00304 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); 00305 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); 00306 w = (v1.y*v1.x - v1.x*v2.y) + (v1.w*v2.x - v1.x*v2.w) + (v1.w*v2.y - v1.y*v2.w); 00307 } 00308 00310 inline friend csVector4 operator* (const csVector4& v, float f) 00311 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00312 00314 inline friend csVector4 operator* (float f, const csVector4& v) 00315 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00316 00318 inline friend csDVector4 operator* (const csVector4& v, double f) 00319 { return csDVector4(v) * f; } 00320 00322 inline friend csDVector4 operator* (double f, const csVector4& v) 00323 { return csDVector4(v) * f; } 00324 00326 inline friend csVector4 operator* (const csVector4& v, int f) 00327 { return v * (float)f; } 00328 00330 inline friend csVector4 operator* (int f, const csVector4& v) 00331 { return v * (float)f; } 00332 00334 inline friend csVector4 operator/ (const csVector4& v, float f) 00335 { f = 1.0f/f; return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00336 00338 inline friend csDVector4 operator/ (const csVector4& v, double f) 00339 { return csDVector4(v) / f; } 00340 00342 inline friend csVector4 operator/ (const csVector4& v, int f) 00343 { return v / (float)f; } 00344 00346 inline friend bool operator== (const csVector4& v1, const csVector4& v2) 00347 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; } 00348 00350 inline friend bool operator!= (const csVector4& v1, const csVector4& v2) 00351 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00352 00354 inline friend csVector4 operator>> (const csVector4& v1, const csVector4& v2) 00355 { return v2*(v1*v2)/(v2*v2); } 00356 00358 inline friend csVector4 operator<< (const csVector4& v1, const csVector4& v2) 00359 { return v1*(v1*v2)/(v1*v1); } 00360 00362 inline friend bool operator< (const csVector4& v, float f) 00363 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00364 00366 inline friend bool operator> (float f, const csVector4& v) 00367 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00368 00370 inline float operator[] (int n) const { return !n?x:n&1?y:n&2?z:w; } 00371 00373 inline float & operator[] (int n) { return !n?x:n&1?y:n&2?z:w; } 00374 00376 inline csVector4& operator+= (const csVector4& v) 00377 { 00378 x += v.x; 00379 y += v.y; 00380 z += v.z; 00381 w += v.w; 00382 00383 return *this; 00384 } 00385 00387 inline csVector4& operator-= (const csVector4& v) 00388 { 00389 x -= v.x; 00390 y -= v.y; 00391 z -= v.z; 00392 w -= v.w; 00393 00394 return *this; 00395 } 00396 00398 inline csVector4& operator*= (float f) 00399 { x *= f; y *= f; z *= f; w *= f; return *this; } 00400 00402 inline csVector4& operator/= (float f) 00403 { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; } 00404 00406 inline csVector4 operator+ () const { return *this; } 00407 00409 inline csVector4 operator- () const { return csVector4(-x,-y,-z, -w); } 00410 00412 inline void Set (float sx, float sy, float sz, float sw) { x = sx; y = sy; z = sz; w = sw; } 00413 00415 inline void Set (const csVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00416 00418 float Norm () const; 00419 00421 float SquaredNorm () const 00422 { return x * x + y * y + z * z + w * w; } 00423 00429 csVector4 Unit () const { return (*this)/(this->Norm()); } 00430 00432 inline static float Norm (const csVector4& v) { return v.Norm(); } 00433 00435 inline static csVector4 Unit (const csVector4& v) { return v.Unit(); } 00436 00438 void Normalize (); 00439 00441 inline bool IsZero (float precision = SMALL_EPSILON) const 00442 { return (ABS(x) < precision) && (ABS(y) < precision) 00443 && (ABS(z) < precision) && (ABS(w) < precision); 00444 } 00445 }; 00446 00447 00448 00451 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.2.18