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 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 00049 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 cannot have custom ctors. 00064 * So be careful when creating new Ts; e.g.: don't use T(x), but something 00065 * like T y = x. 00066 */ 00067 00072 csVector4T () {} 00073 00079 csVector4T (const T& m) : x(m), y(m), z(m), w(m) {} 00080 00082 csVector4T (const T& ix, const T& iy, const T& iz = T(0), 00083 const T& iw = T(1)) 00084 : x(ix), y(iy), z(iz), w(iw) {} 00085 00087 csVector4T (const csVector4T& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} 00088 00090 csVector4T (const csVector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 00091 00093 template<typename T2> 00094 csVector4T& operator= (const csVector4T<T2>& other) 00095 { 00096 x = other.x; 00097 y = other.y; 00098 z = other.z; 00099 w = other.w; 00100 return *this; 00101 } 00102 00104 csString Description() const 00105 { 00106 csString str; 00107 str << x << "," << y << "," << z << "," << w; 00108 return str; 00109 } 00110 00112 inline friend csVector4T operator+ (const csVector4T& v1, 00113 const csVector4T& v2) 00114 { return csVector4T(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); } 00115 00117 inline friend csVector4T operator- (const csVector4T& v1, 00118 const csVector4T& v2) 00119 { return csVector4T(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); } 00120 00121 00123 inline friend float operator* (const csVector4T& v1, 00124 const csVector4T& v2) 00125 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; } 00126 00128 inline friend csVector4T operator% (const csVector4T& v1, 00129 const csVector4T& v2) 00130 { 00131 return csVector4T<T> ( 00132 (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y), 00133 (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z), 00134 (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z), 00135 (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) ); 00136 } 00137 00139 void Cross (const csVector4T & v1, const csVector4T & v2) 00140 { 00141 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); 00142 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); 00143 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); 00144 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); 00145 } 00146 00148 inline friend csVector4T operator* (const csVector4T& v, T f) 00149 { return csVector4T(v.x*f, v.y*f, v.z*f, v.w*f); } 00150 00152 inline friend csVector4T operator* (float f, const csVector4T& v) 00153 { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); } 00154 00156 inline friend csVector4T operator* (const csVector4T& v, int f) 00157 { T _f = f; return v * _f; } 00158 00160 inline friend csVector4T operator* (int f, const csVector4T& v) 00161 { T _f = f; return v * _f; } 00162 00164 inline friend csVector4T operator/ (const csVector4T& v, T f) 00165 { f = 1.0f/f; return csVector4T(v.x*f, v.y*f, v.z*f, v.w*f); } 00166 00168 inline friend csVector4T operator/ (const csVector4T& v, int f) 00169 { T _f = f; return v / _f; } 00170 00172 inline friend bool operator== (const csVector4T& v1, const csVector4T& v2) 00173 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; } 00174 00176 inline friend bool operator!= (const csVector4T& v1, const csVector4T& v2) 00177 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; } 00178 00180 inline friend csVector4T operator>> (const csVector4T& v1, const csVector4T& v2) 00181 { return v2*(v1*v2)/(v2*v2); } 00182 00184 inline friend csVector4T operator<< (const csVector4T& v1, const csVector4T& v2) 00185 { return v1*(v1*v2)/(v1*v1); } 00186 00188 inline friend bool operator< (const csVector4T& v, float f) 00189 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00190 00192 inline friend bool operator> (float f, const csVector4T& v) 00193 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; } 00194 00196 #ifdef __STRICT_ANSI__ 00197 inline float operator[] (int n) const 00198 { return (n&2)?((n&1)?w:z):((n&1)?y:x); } 00199 #else 00200 inline float operator[] (int n) const { return m[n]; } 00201 #endif 00202 00204 #ifdef __STRICT_ANSI__ 00205 inline float & operator[] (int n) 00206 { return (n&2)?((n&1)?w:z):((n&1)?y:x); } 00207 #else 00208 inline float & operator[] (int n) { return m[n]; } 00209 #endif 00210 00212 inline csVector4T& operator+= (const csVector4T& v) 00213 { 00214 x += v.x; 00215 y += v.y; 00216 z += v.z; 00217 w += v.w; 00218 00219 return *this; 00220 } 00221 00223 inline csVector4T& operator-= (const csVector4T& v) 00224 { 00225 x -= v.x; 00226 y -= v.y; 00227 z -= v.z; 00228 w -= v.w; 00229 00230 return *this; 00231 } 00232 00234 inline csVector4T& operator*= (T f) 00235 { x *= f; y *= f; z *= f; w *= f; return *this; } 00236 00238 inline csVector4T& operator/= (T f) 00239 { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; } 00240 00242 inline csVector4T operator+ () const { return *this; } 00243 00245 inline csVector4T operator- () const { return csVector4(-x,-y,-z, -w); } 00246 00248 inline void Set (T sx, T sy, T sz, T sw) 00249 { x = sx; y = sy; z = sz; w = sw; } 00250 00252 inline void Set (csVector4T const& v) { x = v.x; y = v.y; z = v.z; w = v.w; } 00253 00255 inline void Set (T const* v) { x = v[0]; y = v[1]; z = v[2]; w = v[3]; } 00256 00258 inline void Set (T v) { x = y = z = w = v; } 00259 00261 inline void Get (T* v) { v[0] = x; v[1] = y; v[2] = z; v[3] = w; } 00262 00264 T Norm () const { return sqrtf (x * x + y * y + z * z + w * w); } 00265 00267 T SquaredNorm () const 00268 { return x * x + y * y + z * z + w * w; } 00269 00275 csVector4T Unit () const { return (*this)/(this->Norm()); } 00276 00278 inline static T Norm (const csVector4T& v) { return v.Norm(); } 00279 00281 inline static csVector4T Unit (const csVector4T& v) { return v.Unit(); } 00282 00284 void Normalize () 00285 { 00286 T sqlen = x * x + y * y + z * z + w * w; 00287 if (sqlen < SMALL_EPSILON) return ; 00288 00289 T invlen = csQisqrt (sqlen); 00290 *this *= invlen; 00291 } 00292 00293 00295 inline bool IsZero (T precision = SMALL_EPSILON) const 00296 { return (ABS(x) < precision) && (ABS(y) < precision) 00297 && (ABS(z) < precision) && (ABS(w) < precision); 00298 } 00299 }; 00300 00304 class csVector4 : public csVector4T<float> 00305 { 00306 public: 00311 csVector4 () {} 00312 00318 csVector4 (const float& m) : csVector4T<float> (m) {} 00319 00321 csVector4 (float ix, float iy, float iz = 0, float iw = 1) 00322 : csVector4T<float> (ix, iy, iz, iw) {} 00323 00325 csVector4 (const csVector4& v) : csVector4T<float> (v) {} 00326 00328 csVector4 (const csVector4T<float>& v) : csVector4T<float> (v) {} 00329 00331 csVector4 (const csVector3 &v) : 00332 csVector4T<float> (v.x, v.y, v.z, 1.0f) {} 00333 00335 csVector4& operator= (const csVector4T<float>& other) 00336 { 00337 Set (other.x, other.y, other.z, other.w); 00338 return *this; 00339 } 00340 00342 csVector4& operator= (const csVector3& other) 00343 { 00344 Set (other.x, other.y, other.z, 1.0f); 00345 return *this; 00346 } 00347 }; 00348 00351 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.4.6