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

vec2.h

Go to the documentation of this file.
00001 /*
00002  * $Id: vec2.h,v 1.7 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 // vec2.h
00020 
00021 #ifndef GUTZ_VEC2_H
00022 #define GUTZ_VEC2_H
00023 
00024 #include "mm.h"
00025 #include <iostream>
00026 #include <sstream>
00027 
00028 namespace gutz
00029 {
00030 template <class T>
00031 class vec3;
00032 
00033 template <class T>
00034 class vec4;
00035 
00036 template <class T>
00037 class mat2;
00038 
00039 template <class T>
00040 class vec2
00041 {
00042 public:
00043         inline vec2 ();
00044     inline vec2 (const T );
00045     inline vec2 (const T, const T);
00046     inline vec2 (const vec2&); 
00047     inline vec2 (const vec3<T>&);
00048     inline vec2 (const vec4<T>&);
00049     inline ~vec2 () {}
00050 
00051     inline vec2& operator= (const vec2&);
00052     inline vec2& operator+= (const vec2&);
00053     inline vec2& operator-= (const vec2&);
00054     inline vec2& operator*= (const T);
00055     inline vec2& operator/= (const T);
00056     inline vec2& operator*= (const vec2&); // Component wise
00057     inline vec2& operator/= (const vec2&);
00058     inline vec2& operator*= (const mat2<T>&);
00059   
00060     inline vec2 operator+ (const vec2&) const;
00061     inline vec2 operator- (const vec2&) const;
00062     inline vec2 operator- () const;
00063     inline vec2 operator* (const T) const;
00064     inline vec2 operator/ (const T) const;
00065     inline vec2 operator* (const vec2&) const; // Component wise
00066     inline vec2 operator/ (const vec2&) const;
00067 
00068     inline bool operator== (const vec2&) const;
00069     inline bool operator!= (const vec2&) const;
00070     inline bool operator< (const vec2&) const;
00071     inline bool operator> (const vec2&) const;
00072     inline bool operator<= (const vec2&) const;
00073     inline bool operator>= (const vec2&) const;
00074 
00075     inline T* v ();
00076         inline const T* v () const;
00077     inline void set(const T, const T);
00078     inline T& operator[] (const int);
00079         inline const T& operator[] (const int) const;
00080 
00081     inline T norm () const;
00082     inline T norm2 () const;
00083     inline T normalize ();
00084 
00085     inline vec2 abs () const;
00086     inline T dot (const vec2&) const;
00087 
00088     inline bool equal (const vec2&, const T) const;
00089 
00090 public:
00091         //data element names, (w) as in (w,h) is not allowed
00092         //   since "w" is taken by the (x,y,z,w) of vec4
00093     union { T x; T R; T s; T v0;};
00094     union { T y; T G; T t; T v1;};
00095 };
00096 
00097 
00098 //-----------------------------------------------------------------------------
00099 // Usefull typedefs
00100 //-----------------------------------------------------------------------------
00101 typedef vec2<char> vec2b;
00102 typedef vec2<uchar> vec2ub;
00103 typedef vec2<char> vec2c;
00104 typedef vec2<uchar> vec2uc;
00105 typedef vec2<short> vec2s;
00106 typedef vec2<ushort> vec2us;
00107 typedef vec2<int> vec2i;
00108 typedef vec2<uint> vec2ui;
00109 typedef vec2<llong> vec2ll;
00110 typedef vec2<ullong> vec2ull;
00111 typedef vec2<float> vec2f;
00112 typedef vec2<double> vec2d;
00113 
00114 #define str2vec2b str2vec2<char>
00115 #define str2vec2ub str2vec2<uchar>
00116 #define str2vec2c str2vec2<char>
00117 #define str2vec2uc str2vec2<uchar>
00118 #define str2vec2s str2vec2<short>
00119 #define str2vec2us str2vec2<ushort>
00120 #define str2vec2i str2vec2<int>
00121 #define str2vec2ui str2vec2<uint>
00122 #define str2vec2l str2vec2<long>
00123 #define str2vec2ul str2vec2<ulong>
00124 #define str2vec2ll str2vec2<llong>
00125 #define str2vec2ull str2vec2<ullong>
00126 #define str2vec2f str2vec2<float>
00127 #define str2vec2d str2vec2<double>
00128 
00129 //-----------------------------------------------------------------------------
00130 // external functions
00131 //-----------------------------------------------------------------------------
00132 
00133 template <class T>
00134 inline
00135 vec2<T>
00136 operator* (const T, const vec2<T>&);
00137 
00138 template <class T>
00139 std::ostream&
00140 operator<< (std::ostream& os, const vec2<T>& v)
00141 {
00142     os << v.x << " " << v.y;
00143     return os;
00144 }
00145 
00146 template <class T>
00147 std::istream&
00148 operator>> (std::istream& is, vec2<T>& v)
00149 {
00150     is >> v.x;
00151     is >> v.y;
00152     return is;
00153 }
00154 
00155 template <class T>
00156 bool
00157 str2vec2 (const std::string& s, vec2<T>& v)
00158 {
00159     std::istringstream z(s);
00160     vec2<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();
00166     if (!status) v = w;
00167     return status;
00168 }
00169 
00170 template <class T>
00171 bool
00172 str2vec2 (const std::string*, vec2<T>&);
00173 
00174 //-----------------------------------------------------------------------------
00175 // Implementation
00176 //-----------------------------------------------------------------------------
00177 
00178 template <class T>
00179 vec2<T>::vec2 ()
00180     : x(0), y(0) {}
00181 
00182 
00183 template <class T>
00184 vec2<T>::vec2 (const T v0)
00185     : x(v0), y(v0) {}
00186 
00187 template <class T>
00188 vec2<T>::vec2 (const T x0, const T y0)
00189     : x(x0), y(y0) {}
00190 
00191 template <class T>
00192 vec2<T>::vec2 (const vec2& v)
00193 : x(v.x), y(v.y) {}
00194 
00195 template <class T>
00196 vec2<T>::vec2 (const vec3<T>& v)
00197     : x(v.x), y(v.y) {}
00198 
00199 template <class T>
00200 vec2<T>::vec2 (const vec4<T>& v)
00201     : x(v.x), y(v.y) {}
00202 
00203 template <class T>
00204 vec2<T>&
00205 vec2<T>::operator= (const vec2& v)
00206 {
00207         if( v != *this ) {
00208                 x = v.x; y = v.y;
00209         }
00210         return *this;
00211 }
00212 
00213 template <class T>
00214 vec2<T>&
00215 vec2<T>::operator+= (const vec2& v)
00216 {
00217     x += v.x; y += v.y;
00218     return *this;
00219 }
00220 
00221 template <class T>
00222 vec2<T>&
00223 vec2<T>::operator-= (const vec2& v)
00224 {
00225     x -= v.x; y -= v.y;
00226     return *this;
00227 }
00228 
00229 template <class T>
00230 vec2<T>&
00231 vec2<T>::operator*= (const T c)
00232 {
00233     x *= c; y *= c;
00234     return *this;
00235 }
00236 
00237 template <class T>
00238 vec2<T>&
00239 vec2<T>::operator/= (const T c)
00240 {
00241     x /= c; y /= c;
00242     return *this;
00243 }
00244 
00245 template <class T>
00246 vec2<T>&
00247 vec2<T>::operator*= (const vec2& v)
00248 {
00249     x *= v.x; y *= v.y;
00250     return *this;
00251 }
00252 
00253 template <class T>
00254 vec2<T>&
00255 vec2<T>::operator/= (const vec2& v)
00256 {
00257     x /= v.x; y /= v.y;
00258     return *this;
00259 }
00260 
00261 template <class T>
00262 vec2<T>
00263 vec2<T>::operator+ (const vec2& v) const
00264 {
00265     return vec2(x + v.x, y + v.y);
00266 }
00267 
00268 template <class T>
00269 vec2<T>
00270 vec2<T>::operator- (const vec2& v) const
00271 {
00272     return vec2(x - v.x, y - v.y);
00273 }
00274 
00275 template <class T>
00276 vec2<T>
00277 vec2<T>::operator- () const
00278 {
00279     return vec2(-x, -y);
00280 }
00281 
00282 template <class T>
00283 vec2<T>
00284 vec2<T>::operator* (const T c) const
00285 {
00286     return vec2(c*x, c*y);
00287 }
00288 
00289 template <class T>
00290 vec2<T>
00291 vec2<T>::operator/ (const T c) const
00292 {
00293     return vec2(x/c, y/c);
00294 }
00295 
00296 template <class T>
00297 vec2<T>
00298 operator/(const T c, const vec2<T> &v)
00299 {
00300    return vec2<T>(c/v.x, c/v.y);
00301 }
00302 
00303 template <class T>
00304 vec2<T>
00305 vec2<T>::operator* (const vec2& v) const
00306 {
00307     return vec2(v.x*x, v.y*y);
00308 }
00309 
00310 template <class T>
00311 vec2<T>
00312 vec2<T>::operator/ (const vec2& v) const
00313 {
00314     return vec2(x/v.x, y/v.y);
00315 }
00316 
00317 template <class T>
00318 bool
00319 vec2<T>::operator== (const vec2<T>& v) const
00320 {
00321     return (x == v.x && y == v.y);
00322 }
00323 
00324 template <class T>
00325 bool
00326 vec2<T>::operator!= (const vec2<T>& v) const
00327 {
00328     return (x != v.x || y != v.y);
00329 }
00330 
00331 template <class T>
00332 bool
00333 vec2<T>::operator< (const vec2<T>& v) const
00334 {
00335     return (x < v.x && y < v.y);
00336 }
00337 
00338 template <class T>
00339 bool
00340 vec2<T>::operator> (const vec2<T>& v) const
00341 {
00342     return (x > v.x && y > v.y);
00343 }
00344 
00345 template <class T>
00346 bool
00347 vec2<T>::operator<= (const vec2<T>& v) const
00348 {
00349     return (x <= v.x && y <= v.y);
00350 }
00351 
00352 template <class T>
00353 bool
00354 vec2<T>::operator>= (const vec2<T>& v) const
00355 {
00356     return (x >= v.x && y >= v.y);
00357 }
00358 
00359 template <class T>
00360 T*
00361 vec2<T>::v ()
00362 {
00363     return &x;
00364 }
00365 
00366 template <class T>
00367 const T*
00368 vec2<T>::v () const
00369 {
00370     return &x;
00371 }
00372 
00373 template <class T>
00374 void
00375 vec2<T>::set (const T x0, const T y0)
00376 {
00377     x = x0; y = y0;
00378 }
00379 
00380 template <class T>
00381 T&
00382 vec2<T>::operator[] (const int i)
00383 {
00384     return v()[i];
00385 }
00386 
00387 template <class T>
00388 const T&
00389 vec2<T>::operator[] (const int i) const
00390 {
00391         return v()[i];
00392 }
00393 
00394 template <class T>
00395 T
00396 vec2<T>::norm () const
00397 {
00398     return sqrt(x*x + y*y);
00399 }
00400 
00401 template <class T>
00402 T
00403 vec2<T>::norm2 () const
00404 {
00405     return x*x + y*y;
00406 }
00407 
00408 template <class T>
00409 T
00410 vec2<T>::normalize ()
00411 {
00412     T norm = sqrt(x*x + y*y);
00413     T d = 1/norm;
00414     x *= d; y *= d;
00415     return norm;
00416 }
00417 
00418 template <class T>
00419 vec2<T>
00420 vec2<T>::abs () const
00421 {
00422     return vec2<T>(mm_abs(x), mm_abs(y));
00423 }
00424 
00425 template <class T>
00426 T
00427 vec2<T>::dot (const vec2& v) const
00428 {
00429     return x*v.x + y*v.y;
00430 }
00431 
00432 template <class T>
00433 bool
00434 vec2<T>::equal (const vec2& v, const T tol) const
00435 {
00436     T dx = x - v.x;
00437     T dy = y - v.y;
00438     return (dx*dx + dy*dy) <= tol*tol;
00439 }
00440 
00441 template <class T>
00442 vec2<T>
00443 operator* (const T c, const vec2<T>& v)
00444 {
00445     return vec2<T> (v.x*c, v.y*c);
00446 }
00447 
00448 template <class T>
00449 bool
00450 str2vec2 (const std::string* s, vec2<T>& v)
00451 {
00452     if (!s) return true;
00453     return str2vec2(*s, v);
00454 }
00455 
00456 
00457 #if __win32
00458 inline 
00459 vec2d
00460 mm_castf2d (vec2f v)
00461 {
00462     return vec2d(v.x, v.y);
00463 }
00464 
00465 inline 
00466 vec2f
00467 mm_castd2f (vec2d v)
00468 {
00469     return vec2f((float)v.x, (float)v.y);
00470 }
00471 #else //__real_os
00472 template <class T1, class T2>
00473 vec2<T2>
00474 mm_cast (vec2<T1> v)
00475 {
00476     return vec2<T2>(v.x, v.y);
00477 }
00478 #endif
00479 
00480 }// End of namespace gutz
00481 
00482 #endif // vec2_h

Send questions, comments, and bug reports to:
jmk