arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

vec4.h

Go to the documentation of this file.
00001 /*
00002  * $Id: vec4.h,v 1.5 2003/09/15 17:15:31 jmk Exp $
00003  */
00004 ///////////////////////////////////////////////////////////////////////////
00005 //              _____________  ______________________    __   __  _____
00006 //             /  ________  |  |   ___   ________   /   |  \ /  \   |
00007 //            |  |       |  |_ |  |_ |  |       /  /    \__  |      |
00008 //            |  |  ___  |  || |  || |  |      /  /        | |      |
00009 //            |  | |   \ |  || |  || |  |     /  /      \__/ \__/ __|__
00010 //            |  | |_@  ||  || |  || |  |    /  /          Institute
00011 //            |  |___/  ||  ||_|  || |  |   /  /_____________________
00012 //             \_______/  \______/ | |__|  /___________________________
00013 //                        |  |__|  |
00014 //                         \______/
00015 //                    University of Utah       
00016 //                           2002
00017 ///////////////////////////////////////////////////////////////////////////
00018 
00019 // vec4.h
00020 
00021 #ifndef vec4_h
00022 #define vec4_h
00023 
00024 #include "mm.h"
00025 
00026 namespace gutz {
00027 
00028 template <class T>
00029 class vec2;
00030 
00031 template <class T>
00032 class vec3;
00033 
00034 template <class T>
00035 class mat4;
00036 
00037 template <class T>
00038 class vec4
00039 {  
00040 public:
00041         inline vec4 ();
00042     inline vec4 (const T );
00043     inline vec4 (const T, const T, const T, const T);
00044     // inline vec4 (const vec4&); use default
00045     inline vec4 (const vec2<T>&, const T = 0, const T = 0);
00046     inline vec4 (const vec3<T>&, const T = 0);
00047     inline ~vec4 () {}
00048     
00049     // inline vec4& operator= (const vec4&); use default
00050     inline vec4& operator+= (const vec4&);
00051     inline vec4& operator-= (const vec4&);
00052     inline vec4& operator*= (const T);
00053     inline vec4& operator/= (const T);
00054     inline vec4& operator*= (const vec4&);
00055     inline vec4& operator/= (const vec4&);
00056     inline vec4& operator*= (const mat4<T>&);
00057     
00058     inline vec4 operator+ (const vec4&) const;
00059     inline vec4 operator- (const vec4&) const;
00060     inline vec4 operator- () const;
00061     inline vec4 operator* (const T) const;
00062     inline vec4 operator/ (const T) const;
00063     inline vec4 operator* (const vec4&) const;
00064     inline vec4 operator/ (const vec4&) const;
00065 
00066     inline bool operator== (const vec4&) const;
00067     inline bool operator!= (const vec4&) const;
00068     inline bool operator< (const vec4&) const;
00069     inline bool operator> (const vec4&) const;
00070     inline bool operator<= (const vec4&) const;
00071     inline bool operator>= (const vec4&) const;
00072 
00073     inline T* v ();
00074         inline const T* v () const;
00075     inline void set (const T, const T, const T, const T);
00076     inline T& operator[] (const int);
00077         inline const T& operator[] (const int) const;
00078 
00079     inline T norm () const;
00080     inline T norm2 () const;
00081     inline T normalize ();
00082     
00083     inline vec4 abs () const;
00084 
00085     inline T dot (const vec4&) const;
00086     
00087     inline bool equal (const vec4&, T) const;
00088 
00089 public:
00090     union { T x; T R; T s; T v0;};
00091     union { T y; T G; T t; T v1;};
00092     union { T z; T B; T r; T v2;};
00093     union { T w; T A; T q; T v3;};
00094 };
00095 
00096 //-----------------------------------------------------------------------------
00097 // typedefs
00098 //-----------------------------------------------------------------------------
00099 
00100 typedef vec4<char> vec4c;
00101 typedef vec4<uchar> vec4uc;
00102 typedef vec4<char> vec4b;
00103 typedef vec4<uchar> vec4ub;
00104 typedef vec4<short> vec4s;
00105 typedef vec4<ushort> vec4us;
00106 typedef vec4<int> vec4i;
00107 typedef vec4<uint> vec4ui;
00108 typedef vec4<llong> vec4ll;
00109 typedef vec4<ullong> vec4ull;
00110 typedef vec4<float> vec4f;
00111 typedef vec4<double> vec4d;
00112 
00113 #define str2vec4c str2vec4<char>
00114 #define str2vec4uc str2vec4<uchar>
00115 #define str2vec4b str2vec4<char>
00116 #define str2vec4ub str2vec4<uchar>
00117 #define str2vec4s str2vec4<short>
00118 #define str2vec4us str2vec4<ushort>
00119 #define str2vec4i str2vec4<int>
00120 #define str2vec4ui str2vec4<uint>
00121 #define str2vec4l str2vec4<long>
00122 #define str2vec4ul str2vec4<ulong>
00123 #define str2vec4ll str2vec4<llong>
00124 #define str2vec4ull str2vec4<ullong>
00125 #define str2vec4f str2vec4<float>
00126 #define str2vec4d str2vec4<double>
00127 
00128 //-----------------------------------------------------------------------------
00129 // external functions
00130 //-----------------------------------------------------------------------------
00131 template <class T>
00132 vec4<T>
00133 operator* (const T, const vec4<T>&); /// defined below
00134 
00135 template <class T>
00136 std::ostream&
00137 operator<< (std::ostream& os, const vec4<T>& v)
00138 {
00139     os << v.x << " " << v.y << " " << v.z << " " << v.w;
00140     return os;
00141 }
00142 
00143 template <class T>
00144 std::istream&
00145 operator>> (std::istream& is, vec4<T>& v)
00146 {
00147     is >> v.x;
00148     is >> v.y;
00149     is >> v.z;
00150     is >> v.w;
00151     return is;
00152 }
00153 
00154 template <class T>
00155 bool
00156 str2vec4 (const std::string& s, vec4<T>& v)
00157 {
00158     std::istringstream z(s);
00159     vec4<T> w;
00160     bool status = z.eof();
00161     z >> w.x;
00162     status = status || z.fail() || z.eof();
00163     z >> w.y;
00164     status = status || z.fail() || z.eof();
00165     z >> w.z;
00166     status = status || z.fail() || z.eof();
00167     z >> w.w;
00168     status = status || z.fail();
00169     if (!status) v = w;
00170     return status;
00171 }
00172 
00173 
00174 template <class T>
00175 bool
00176 str2vec4 (const std::string*, vec4<T>*);
00177 
00178 //-----------------------------------------------------------------------------
00179 // implementation
00180 //-----------------------------------------------------------------------------
00181 
00182 template <class T>
00183 vec4<T>::vec4 ()
00184     : x(0), y(0), z(0), w(0) {}
00185 
00186 template <class T>
00187 vec4<T>::vec4 (const T v0)
00188     : x(v0), y(v0), z(v0), w(v0) {}
00189 
00190 template <class T>
00191 vec4<T>::vec4 (T x0, T y0, T z0, T w0)
00192     : x(x0), y(y0), z(z0), w(w0) {}
00193 
00194 template <class T>
00195 vec4<T>::vec4 (const vec2<T>& v, const T c0, const T c1)
00196     : x(v.x), y(v.y), z(c0), w(c1) {}
00197 
00198 template <class T>
00199 vec4<T>::vec4 (const vec3<T>& v, const T c)
00200     : x(v.x), y(v.y), z(v.z), w(c) {}
00201 
00202 template <class T>
00203 vec4<T>&
00204 vec4<T>::operator+= (const vec4& v)
00205 {
00206     x += v.x; y += v.y; z += v.z; w += v.w;
00207     return *this;
00208 }
00209 
00210 template <class T>
00211 vec4<T>&
00212 vec4<T>::operator-= (const vec4& v)
00213 {
00214     x -= v.x; y -= v.y; z -= v.z; w -= v.w;
00215     return *this;
00216 }
00217 
00218 template <class T>
00219 vec4<T>&
00220 vec4<T>::operator*= (T c)
00221 {
00222     x *= c; y *= c; z *= c; w *= c;
00223     return *this;
00224 }
00225 
00226 template <class T>
00227 vec4<T>&
00228 vec4<T>::operator/= (T c)
00229 {
00230     x /= c; y /= c; z /= c; w /= c;
00231     return *this;
00232 }
00233 
00234 template <class T>
00235 vec4<T>&
00236 vec4<T>::operator*= (const vec4<T>& v)
00237 {
00238     x *= v.x; y *= v.y; z *= v.z; w *= v.w;
00239     return *this;
00240 }
00241 
00242 template <class T>
00243 vec4<T>&
00244 vec4<T>::operator/= (const vec4<T>& v)
00245 {
00246     x /= v.x; y /= v.y; z /= v.z; w /= v.w;
00247     return *this;
00248 }
00249 
00250 template <class T>
00251 vec4<T>&
00252 vec4<T>::operator*= (const mat4<T>& M)
00253 {
00254     vec4<T> temp(x, y, z, w);  
00255     x = M.m[ 0]*temp.x + M.m[ 4]*temp.y + M.m[ 8]*temp.z + M.m[12]*temp.w;
00256     y = M.m[ 1]*temp.x + M.m[ 5]*temp.y + M.m[ 9]*temp.z + M.m[13]*temp.w;
00257     z = M.m[ 2]*temp.x + M.m[ 6]*temp.y + M.m[10]*temp.z + M.m[14]*temp.w;
00258     w = M.m[ 3]*temp.x + M.m[ 7]*temp.y + M.m[11]*temp.z + M.m[15]*temp.w;
00259     return *this;
00260 }
00261 
00262 template <class T>
00263 vec4<T>
00264 vec4<T>::operator+ (const vec4& v) const
00265 {
00266     return vec4(x + v.x, y + v.y, z + v.z, w + v.w);
00267 }
00268 
00269 template <class T>
00270 vec4<T>
00271 vec4<T>::operator- (const vec4& v) const
00272 {
00273     return vec4(x - v.x, y - v.y, z - v.z, w - v.w);
00274 }
00275 
00276 template <class T>
00277 vec4<T>
00278 vec4<T>::operator- () const
00279 {
00280     return vec4(-x, -y, -z, -w);
00281 }
00282 
00283 template <class T>
00284 vec4<T>
00285 vec4<T>::operator* (T c) const
00286 {
00287     return vec4(c*x, c*y, c*z, c*w);
00288 }
00289 
00290 template <class T>
00291 vec4<T>
00292 vec4<T>::operator/ (T c) const
00293 {
00294     return vec4(x/c, y/c, z/c, w/c);
00295 }
00296 
00297 template <class T>
00298 vec4<T>
00299 vec4<T>::operator* (const vec4& v) const
00300 {
00301     return vec4(v.x*x, v.y*y, v.z*z, v.w*w);
00302 }
00303 
00304 template <class T>
00305 vec4<T>
00306 vec4<T>::operator/ (const vec4& v) const
00307 {
00308     return vec4(v.x/x, v.y/y, v.z/z, v.w/w);
00309 }
00310 
00311 template <class T>
00312 bool
00313 vec4<T>::operator== (const vec4<T>& v) const
00314 {
00315     return (x == v.x && y == v.y && z == v.z && z == v.w);
00316 }
00317 
00318 template <class T>
00319 bool
00320 vec4<T>::operator!= (const vec4<T>& v) const
00321 {
00322     return (x != v.x || y != v.y || z != v.z || w != v.w);
00323 }
00324 
00325 template <class T>
00326 bool
00327 vec4<T>::operator< (const vec4<T>& v) const
00328 {
00329     return (x < v.x && y < v.y && z < v.z && w < v.w);
00330 }
00331 
00332 template <class T>
00333 bool
00334 vec4<T>::operator> (const vec4<T>& v) const
00335 {
00336     return (x > v.x && y > v.y && z > v.z && w > v.w);
00337 }
00338 
00339 template <class T>
00340 bool
00341 vec4<T>::operator<= (const vec4<T>& v) const
00342 {
00343     return (x <= v.x && y <= v.y && z <= v.z && w <= v.w);
00344 }
00345 
00346 template <class T>
00347 bool
00348 vec4<T>::operator>= (const vec4<T>& v) const
00349 {
00350     return (x >= v.x && y >= v.y && z >= v.z && w >= v.w);
00351 }
00352 
00353 template <class T>
00354 T*
00355 vec4<T>::v ()
00356 {
00357     return &x;
00358 }
00359 
00360 template <class T>
00361 const T*
00362 vec4<T>::v () const
00363 {
00364     return &x;
00365 }
00366 
00367 template <class T>
00368 void
00369 vec4<T>::set (const T x0, const T y0, const T z0, const T w0)
00370 {
00371     x = x0; y = y0; z = z0; w = w0;
00372 }
00373 
00374 template <class T>
00375 T&
00376 vec4<T>::operator[] (const int i)
00377 {
00378     return v()[i];
00379 }
00380 
00381 template <class T>
00382 const T&
00383 vec4<T>::operator[] (const int i) const
00384 {
00385         return v()[i];
00386 }
00387 
00388 template <class T>
00389 T
00390 vec4<T>::norm () const
00391 {
00392     return sqrt(x*x + y*y + z*z + w*w);
00393 }
00394 
00395 template <class T>
00396 T
00397 vec4<T>::norm2 () const
00398 {
00399     return x*x + y*y + z*z + w*w;
00400 }
00401 
00402 template <class T>
00403 T
00404 vec4<T>::normalize ()
00405 {
00406     T norm = sqrt(x*x + y*y + z*z + w*w);
00407     T d = 1/norm;
00408     x *= d; y *= d; z *= d; w *= d;
00409     return norm;
00410 }
00411 
00412 
00413 template <class T>
00414 vec4<T>
00415 vec4<T>::abs () const
00416 {
00417     return vec4<T>(mm_abs(x), mm_abs(y), mm_abs(z), mm_abs(w));
00418 }
00419 
00420 template <class T>
00421 T
00422 vec4<T>::dot (const vec4<T>& v) const
00423 {
00424     return x*v.x + y*v.y + z*v.z + w*v.w;
00425 }
00426 
00427 template <class T>
00428 bool
00429 vec4<T>::equal(const vec4& v, T tol) const
00430 {
00431     T dx = x - v.x;
00432     T dy = y - v.y;
00433     T dz = z - v.z;
00434     T dw = w - v.w;
00435     return (dx*dx + dy*dy + dz*dz + dw*dw) <= tol*tol;
00436 }
00437 
00438 template <class T>
00439 vec4<T>
00440 operator* (const T c, const vec4<T>& v)
00441 {
00442     return vec4<T> (v.x*c, v.y*c, v.z*c, v.w*c);
00443 }
00444 
00445 template <class T>
00446 bool
00447 str2vec4 (const std::string* s, vec4<T>& v)
00448 {
00449     if (!s) return true;
00450     return str2vec4(*s, v);
00451 }
00452 
00453 
00454 #if __win32
00455 inline 
00456 vec4d
00457 mm_castf2d (vec4f v)
00458 {
00459     return vec4d(v.x, v.y, v.z, v.w);
00460 }
00461 
00462 inline 
00463 vec4f
00464 mm_castd2f (vec4d v)
00465 {
00466     return vec4f((float)v.x, (float)v.y, (float)v.z, (float)v.w);
00467 }
00468 #else //__real_os
00469 template <class T1, class T2>
00470 vec4<T2>
00471 mm_cast (vec4<T1> v)
00472 {
00473     return vec4<T2>(v.x, v.y, v.z, v.w);
00474 }
00475 #endif
00476 
00477 } // End of namespace gutz
00478 
00479 #endif // vec4_h
00480 

Send questions, comments, and bug reports to:
jmk