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

vec3.h

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

Send questions, comments, and bug reports to:
jmk