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 <[email protected]> 00004 Extended (and some methods removed) to 4 component by Marten Svanfeldt 00005 Templatized by Frank Richter 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 00032 #include "csextern.h" 00033 #include "csgeom/vector3.h" 00034 #include "csutil/csstring.h" 00035 00039 template<typename T> 00040 class csVector4T 00041 { 00042 public: 00043 #if !defined(__STRICT_ANSI__) && !defined(SWIG) 00044 union 00045 { 00046 struct 00047 { 00048 #endif 00050 T x; 00052 T y; 00054 T z; 00056 T w; 00057 #if !defined(__STRICT_ANSI__) && !defined(SWIG) 00058 }; 00060 T m[4]; 00061 }; 00062 #endif 00063 /* Note: Since T is used in an union above it have to be an aggregate 00064 * type (C++ Std 8.5.1). 00065 * This implies that T cannot have custom constructor, base classes, 00066 * or virtual functions. This applies to all of Ts members recursivly. 00067 */ 00068 00073 csVector4T () {} 00074 00080 csVector4T (const T& m) : x(m), y(m), z(m), w(m) {} 00081 00083 csVector4T (const T& ix, const T& iy, const T& iz = T(0), 00084 const T& iw = T(1)) 00085 : x(ix), y(iy), z(iz), w(iw) {} 00086 00088 csVector4T (const csVector4T& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} 00089 00091 csVector4T (const csVector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 00092 00094 template<typename T2> 00095 csVector4T& operator= (const csVector4T<T2>& other) 00096 { 00097 x = other.x; 00098 y = other.y; 00099 z = other.z; 00100 w = other.w; 00101 return *this; 00102 } 00103 00105 csString Description() const 00106 { 00107 csString str; 00108 str << x << "," << y << "," << z << "," << w; 00109 return str; 00110 } 00111 00113 inline friend csVector4T operator+ (const csVector4T& v1, 00114 const csVector4T& v2) 00115 { return csVector4T(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00116 00118 inline friend csVector4T operator- (const csVector4T& v1, 00119 const csVector4T& v2) 00120 { return csVector4T(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00121 00122 00124 inline friend float operator* (const csVector4T& v1, 00125 const csVector4T& v2) 00126 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00127 00129 inline friend csVector4T operator% (const csVector4T& v1, 00130 const csVector4T& v2) 00131 { 00132 return csVector4T<T> ( 00133 (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y), 00134 (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z), 00135 (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z), 00136 (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) ); 00137 } 00138 00140 void Cross (const csVector4T & v1, const csVector4T & v2) 00141 { 00142 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); 00143 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); 00144 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); 00145 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); 00146 } 00147 00149 inline friend csVector4T operator* (const csVector4T& v, T f) 00150 { return csVector4T(v.x*f, v.y*f, v.z*f, v.w*f); } 00151 00153 inline friend csVector4T operator* (float f, const csVector4T& v) 00154 { return csVector4T(v.x*f, v.y*f, v.z*f, v.w*f); } 00155 00157 inline friend csVector4T operator* (const csVector4T& v, int f) 00158 { T _f = f; return v * _f; } 00159 00161 inline friend csVector4T operator* (int f, const csVector4T& v) 00162 { T _f = f; return v * _f; } 00163 00165 inline friend csVector4T operator/ (const csVector4T& v, T f) 00166 { f = 1.0f/f; return csVector4T(v.x*f, v.y*f, v.z*f, v.w*f); } 00167 00169 inline friend csVector4T operator/ (const csVector4T& v, int f) 00170 { T _f = f; return v / _f; } 00171 00173 inline friend bool operator== (const csVector4T& v1, const csVector4T& v2) 00174 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; } 00175 00177 inline friend bool operator!= (const csVector4T& v1, const csVector4T& v2) 00178 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00179 00181 inline friend csVector4T operator>> (const csVector4T& v1, const csVector4T& v2) 00182 { return v2*(v1*v2)/(v2*v2); } 00183 00185 inline friend csVector4T operator<< (const csVector4T& v1, const csVector4T& v2) 00186 { return v1*(v1*v2)/(v1*v1); } 00187 00189 inline friend bool operator< (const csVector4T& v, float f) 00190 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00191 00193 inline friend bool operator> (float f, const csVector4T& v) 00194 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00195 00197 #ifdef __STRICT_ANSI__ 00198 inline float operator[] (size_t n) const 00199 { return (n&2)?((n&1)?w:z):((n&1)?y:x); } 00200 #else 00201 inline float operator[] (size_t n) const { return m[n]; } 00202 #endif 00203 00205 #ifdef __STRICT_ANSI__ 00206 inline float & operator[] (size_t n) 00207 { return (n&2)?((n&1)?w:z):((n&1)?y:x); } 00208 #else 00209 inline float & operator[] (size_t n) { return m[n]; } 00210 #endif 00211 00213 inline csVector4T& operator+= (const csVector4T& v) 00214 { 00215 x += v.x; 00216 y += v.y; 00217 z += v.z; 00218 w += v.w; 00219 00220 return *this; 00221 } 00222 00224 inline csVector4T& operator-= (const csVector4T& v) 00225 { 00226 x -= v.x; 00227 y -= v.y; 00228 z -= v.z; 00229 w -= v.w; 00230 00231 return *this; 00232 } 00233 00235 inline csVector4T& operator*= (T f) 00236 { x *= f; y *= f; z *= f; w *= f; return *this; } 00237 00239 inline csVector4T& operator/= (T f) 00240 { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; } 00241 00243 inline csVector4T operator+ () const { return *this; } 00244 00246 inline csVector4T operator- () const { return csVector4(-x,-y,-z, -w); } 00247 00249 inline void Set (T sx, T sy, T sz, T sw) 00250 { x = sx; y = sy; z = sz; w = sw; } 00251 00253 inline void Set (csVector4T const& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00254 00256 inline void Set (T const* v) { x = v[0]; y = v[1]; z = v[2]; w = v[3]; } 00257 00259 inline void Set (T v) { x = y = z = w = v; } 00260 00262 inline void Get (T* v) { v[0] = x; v[1] = y; v[2] = z; v[3] = w; } 00263 00265 T Norm () const { return sqrtf (x * x + y * y + z * z + w * w); } 00266 00268 T SquaredNorm () const 00269 { return x * x + y * y + z * z + w * w; } 00270 00276 csVector4T Unit () const { return (*this)/(this->Norm()); } 00277 00279 inline static T Norm (const csVector4T& v) { return v.Norm(); } 00280 00282 inline static csVector4T Unit (const csVector4T& v) { return v.Unit(); } 00283 00285 void Normalize () 00286 { 00287 T sqlen = x * x + y * y + z * z + w * w; 00288 if (sqlen < SMALL_EPSILON) return ; 00289 00290 T invlen = csQisqrt (sqlen); 00291 *this *= invlen; 00292 } 00293 00294 00296 inline bool IsZero (T precision = SMALL_EPSILON) const 00297 { return (ABS(x) < precision) && (ABS(y) < precision) 00298 && (ABS(z) < precision) && (ABS(w) < precision); 00299 } 00300 }; 00301 00305 class csVector4 : public csVector4T<float> 00306 { 00307 public: 00312 csVector4 () {} 00313 00319 csVector4 (const float& m) : csVector4T<float> (m) {} 00320 00322 csVector4 (float ix, float iy, float iz = 0, float iw = 1) 00323 : csVector4T<float> (ix, iy, iz, iw) {} 00324 00326 csVector4 (const csVector4& v) : csVector4T<float> (v) {} 00327 00329 csVector4 (const csVector4T<float>& v) : csVector4T<float> (v) {} 00330 00332 csVector4 (const csVector3 &v) : 00333 csVector4T<float> (v.x, v.y, v.z, 1.0f) {} 00334 00336 csVector4& operator= (const csVector4T<float>& other) 00337 { 00338 Set (other.x, other.y, other.z, other.w); 00339 return *this; 00340 } 00341 00343 csVector4& operator= (const csVector3& other) 00344 { 00345 Set (other.x, other.y, other.z, 1.0f); 00346 return *this; 00347 } 00348 }; 00349 00352 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.4.7