00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __VMML__VECTOR2__H__
00011 #define __VMML__VECTOR2__H__
00012
00013 #include <cmath>
00014 #include <cstdlib>
00015 #include <iomanip>
00016 #include <iostream>
00017 #include <algorithm>
00018 #include <cassert>
00019 #include <string>
00020 #include <vector>
00021 #include <sstream>
00022 #include <limits>
00023
00024
00025
00026 namespace vmml
00027 {
00028
00029 template< typename T >
00030 class Vector2
00031 {
00032 public:
00033 union
00034 {
00035 struct
00036 {
00037 T x, y;
00038 };
00039 struct
00040 {
00041 T min, max;
00042 };
00043 T xy[2];
00044 T array[2];
00045 };
00046
00047
00048 Vector2();
00049 Vector2( const T a );
00050 Vector2( const T x, const T y );
00051
00052
00053 Vector2( const float* values );
00054 Vector2( const double* values );
00055 template< typename U >
00056 Vector2( const Vector2< U >& a );
00057 ~Vector2();
00058
00059 void set( T x, T y );
00060
00061
00062
00063 void set( const float* values );
00064 void set( const double* values );
00065
00066
00067
00068
00069
00070 bool set( const std::string& values, char delimiter = ' ' );
00071
00072 bool set( const std::vector< std::string >& values );
00073
00074 const Vector2& operator=( T a );
00075 const Vector2& operator=( const Vector2& a );
00076 template< typename U >
00077 const Vector2& operator=( const Vector2< U >& a );
00078
00079 T& operator[]( size_t position);
00080 const T& operator[]( size_t position) const;
00081
00082
00083 Vector2 operator+( const T a ) const;
00084 Vector2 operator-( const T a ) const;
00085 Vector2 operator*( const T a ) const;
00086 Vector2 operator/( T a ) const;
00087
00088 const Vector2& operator+=( T a );
00089 const Vector2& operator-=( T a );
00090 const Vector2& operator*=( T a );
00091 const Vector2& operator/=( T a );
00092
00093
00094 Vector2 operator+( const Vector2& a ) const;
00095 Vector2 operator-( const Vector2& a ) const;
00096 Vector2 operator*( const Vector2& a ) const;
00097 Vector2 operator/( const Vector2& a ) const;
00098 Vector2 operator-() const;
00099
00100 const Vector2& operator+=( const Vector2& a );
00101 const Vector2& operator-=( const Vector2& a );
00102 const Vector2& operator*=( const Vector2& a );
00103 const Vector2& operator/=( const Vector2& a );
00104
00105 bool operator==( const Vector2 &a ) const;
00106 bool operator!=(const Vector2 &a ) const;
00107 bool isAkin( const Vector2 &a,
00108 const T& delta = std::numeric_limits<T>::epsilon() ) const;
00109
00110
00111
00112
00113 size_t smaller( const Vector2& a ) const;
00114 size_t smaller( const Vector2& a, const size_t axis ) const;
00115
00116
00117 size_t greater( const Vector2& a ) const;
00118 size_t greater( const Vector2& a, const size_t axis ) const;
00119
00120 T length() const;
00121 T lengthSquared() const;
00122
00123
00124 T normalize();
00125 T normalize( const float* source );
00126
00127 T normalise();
00128 T normalise( const float* source );
00129
00130
00131 Vector2 getNormalized() const;
00132
00133 void scale( T scale_factor );
00134 void invert();
00135
00136 T getMinComponent() const;
00137 T getMaxComponent() const;
00138 T getArea() const;
00139
00140 void clamp( T lower, T upper);
00141
00142
00143
00144
00145 void randomize();
00146
00147
00148
00149 bool getString( std::string& result, const std::string& delimiter = " " ) const;
00150
00151 friend std::ostream& operator << ( std::ostream& os, const Vector2& v )
00152 {
00153 const std::ios::fmtflags flags = os.flags();
00154 const int prec = os.precision();
00155
00156 os.setf( std::ios::right, std::ios::adjustfield );
00157 os.precision( 5 );
00158 os << "[" << std::setw(10) << v.x << " " << std::setw(10) << v.y << "]";
00159 os.precision( prec );
00160 os.setf( flags );
00161 return os;
00162 };
00163
00164
00165 typedef T* iterator;
00166 typedef const T* const_iterator;
00167
00168 iterator begin() { return xy; }
00169 const_iterator begin() const { return xy; };
00170 iterator end() { return xy + 2; }
00171 const_iterator end() const { return xy + 2; }
00172
00173
00174 static const Vector2 ZERO;
00175
00176 };
00177
00178 typedef Vector2< unsigned char > Vector2ub;
00179 typedef Vector2< int > Vector2i;
00180 typedef Vector2< float > Vector2f;
00181 typedef Vector2< double > Vector2d;
00182
00183 typedef Vector2< unsigned char > vec2ub;
00184 typedef Vector2< int > vec2i;
00185 typedef Vector2< float > vec2f;
00186 typedef Vector2< double > vec2d;
00187
00188 }
00189
00190
00191 #include <vmmlib/stringUtils.h>
00192
00193 namespace vmml
00194 {
00195
00196 template< typename T >
00197 const Vector2< T > Vector2< T >::ZERO( 0, 0 );
00198
00199
00200
00201 template < typename T >
00202 Vector2< T >::Vector2()
00203 {}
00204
00205
00206
00207 template < typename T >
00208 Vector2< T >::Vector2( const T a )
00209 : x(a)
00210 , y(a)
00211 {}
00212
00213
00214
00215 template < typename T >
00216 Vector2< T >::Vector2( const T i, const T j )
00217 : x(i)
00218 , y(j)
00219 {}
00220
00221
00222
00223 template < typename T >
00224 Vector2< T >::Vector2( const float* values )
00225 {
00226 assert( values && "Vector2: Nullpointer argument as source for initialisation!" );
00227 x = static_cast< T > ( values[0] );
00228 y = static_cast< T > ( values[1] );
00229 }
00230
00231
00232
00233 template < typename T >
00234 Vector2< T >::Vector2( const double* values )
00235 {
00236 assert( values && "Vector2: Nullpointer argument as source for initialisation!" );
00237 x = static_cast< T > ( values[0] );
00238 y = static_cast< T > ( values[1] );
00239 }
00240
00241
00242 template < typename T >
00243 template < typename U >
00244 Vector2< T >::Vector2( const Vector2< U >& a )
00245 : x ( static_cast< T >( a.x ) )
00246 , y ( static_cast< T >( a.y ) )
00247 {}
00248
00249
00250
00251 template < typename T >
00252 Vector2< T >::~Vector2()
00253 {}
00254
00255
00256
00257 template < typename T >
00258 void Vector2< T >::set( T xx, T yy )
00259 {
00260 x = xx;
00261 y = yy;
00262 }
00263
00264
00265
00266 template < typename T >
00267 void Vector2< T >::set( const float* values )
00268 {
00269 assert( values && "Vector2: Nullpointer argument as source for initialisation!" );
00270 x = static_cast< T > ( values[0] );
00271 y = static_cast< T > ( values[1] );
00272 }
00273
00274
00275
00276 template < typename T >
00277 void Vector2< T >::set( const double* values )
00278 {
00279 assert( values && "Vector2: Nullpointer argument as source for initialisation!" );
00280 x = static_cast< T > ( values[0] );
00281 y = static_cast< T > ( values[1] );
00282 }
00283
00284
00285
00286
00287
00288
00289
00290 template< typename T >
00291 bool
00292 Vector2< T >::set( const std::string& values, char delimiter )
00293 {
00294 std::vector< std::string > tokens;
00295 stringUtils::split( values, tokens, delimiter );
00296 return set( tokens );
00297 }
00298
00299
00300
00301
00302
00303
00304
00305 template< typename T >
00306 bool
00307 Vector2< T >::set( const std::vector< std::string >& values )
00308 {
00309 bool ok = true;
00310
00311 if ( values.size() < 2 )
00312 return false;
00313
00314 std::vector< std::string >::const_iterator it = values.begin();
00315
00316 for( size_t component = 0; ok && component < 2; ++component, ++it )
00317 {
00318 ok = stringUtils::fromString< T >( *it, xy[ component ] );
00319 }
00320
00321 return ok;
00322 }
00323
00324
00325
00326 template < typename T >
00327 const Vector2< T >& Vector2< T >::operator=( T a )
00328 {
00329 x = y = a;
00330 return *this;
00331 }
00332
00333
00334
00335 template < typename T >
00336 const Vector2< T >& Vector2< T >::operator=( const Vector2& a )
00337 {
00338 x = a.x;
00339 y = a.y;
00340 return *this;
00341 }
00342
00343
00344
00345 template < typename T >
00346 template < typename U >
00347 const Vector2< T >& Vector2< T >::operator=( const Vector2< U >& a )
00348 {
00349 x = static_cast< T > ( a.x );
00350 y = static_cast< T > ( a.y );
00351 return *this;
00352 }
00353
00354
00355
00356 template < typename T >
00357 T& Vector2< T >::operator[]( size_t position)
00358 {
00359 assert( position < 2 && "Vector2::operator[] Invalid component index!" );
00360 return xy[position];
00361 }
00362
00363
00364
00365 template < typename T >
00366 const T& Vector2< T >::operator[]( size_t position) const
00367 {
00368 assert( position < 2 && "Vector2::operator[] Invalid component index!" );
00369 return xy[position];
00370 }
00371
00372
00373
00374 template < typename T >
00375 T Vector2< T >::length() const
00376 {
00377 return sqrt( lengthSquared( ));
00378 }
00379
00380 template <>
00381 inline float Vector2< float >::length() const
00382 {
00383 return sqrtf( lengthSquared( ));
00384 }
00385
00386
00387
00388 template < typename T >
00389 T Vector2< T >::lengthSquared() const
00390 {
00391 return x * x + y * y;
00392 }
00393
00394
00395
00396 template < typename T >
00397 T Vector2< T >::normalise()
00398 {
00399 const T l = length();
00400 if ( l == 0 )
00401 return 0;
00402 x /= l;
00403 y /= l;
00404 return l;
00405 }
00406
00407
00408
00409 template < typename T >
00410 T Vector2< T >::normalise( const float* source )
00411 {
00412 Vector2< T > a ( source );
00413 const T l = a.length();
00414 if ( l == 0 )
00415 return 0;
00416
00417 source[0] /= l;
00418 source[1] /= l;
00419 return l;
00420 }
00421
00422
00423
00424 template < typename T >
00425 T Vector2< T >::normalize()
00426 {
00427 const T l = length();
00428 if ( l == 0 )
00429 return 0;
00430 x /= l;
00431 y /= l;
00432 return l;
00433 }
00434
00435
00436
00437 template < typename T >
00438 T Vector2< T >::normalize( const float* source )
00439 {
00440 Vector2< T > a ( source );
00441 const T l = a.length();
00442 if ( l == 0 )
00443 return 0;
00444
00445 source[0] /= l;
00446 source[1] /= l;
00447 return l;
00448 }
00449
00450
00451
00452 template< typename T >
00453 Vector2< T > Vector2< T >::getNormalized() const
00454 {
00455 Vector2< T > n( *this );
00456 n.normalize();
00457 return n;
00458 }
00459
00460
00461
00462 template < typename T >
00463 void Vector2< T >::scale( T scale_factor )
00464 {
00465 operator*=( scale_factor );
00466 }
00467
00468
00469
00470 template < typename T >
00471 Vector2< T > Vector2< T >::operator+( const T a ) const
00472 {
00473 return Vector2( x + a, y + a );
00474 }
00475
00476
00477
00478 template < typename T >
00479 Vector2< T > Vector2< T >::operator-( const T a ) const
00480 {
00481 return Vector2( x - a, y - a );
00482 }
00483
00484
00485
00486 template < typename T >
00487 Vector2< T > Vector2< T >::operator*( const T a ) const
00488 {
00489 return Vector2( x * a, y * a );
00490 }
00491
00492
00493
00494 template < typename T >
00495 Vector2< T > Vector2< T >::operator/( T a ) const
00496 {
00497 assert( a != 0.0f );
00498 a = 1.0f / a;
00499 return Vector2( x * a, y * a );
00500 }
00501
00502
00503
00504 template < typename T >
00505 const Vector2< T >& Vector2< T >::operator+=( T a )
00506 {
00507 x += a;
00508 y += a;
00509 return *this;
00510 }
00511
00512
00513
00514 template < typename T >
00515 const Vector2< T >& Vector2< T >::operator-=( T a )
00516 {
00517 x -= a;
00518 y -= a;
00519 return *this;
00520 }
00521
00522
00523
00524 template < typename T >
00525 const Vector2< T >& Vector2< T >::operator*=( T a )
00526 {
00527 x *= a;
00528 y *= a;
00529 return *this;
00530 }
00531
00532
00533
00534 template < typename T >
00535 const Vector2< T >& Vector2< T >::operator/=( T a )
00536 {
00537 a = 1.0f / a;
00538 x *= a;
00539 y *= a;
00540 return *this;
00541 }
00542
00543
00544
00545
00546 template < typename T >
00547 Vector2< T > Vector2< T >::operator+( const Vector2 &a ) const
00548 {
00549 return Vector2( x + a.x, y + a.y );
00550 }
00551
00552
00553
00554 template < typename T >
00555 Vector2< T > Vector2< T >::operator-( const Vector2 &a ) const
00556 {
00557 return Vector2( x - a.x, y - a.y );
00558 }
00559
00560
00561
00562 template < typename T >
00563 Vector2< T > Vector2< T >::operator*( const Vector2 &a ) const
00564 {
00565 return Vector2( x * a.x, y * a.y );
00566 }
00567
00568
00569
00570 template < typename T >
00571 Vector2< T > Vector2< T >::operator/( const Vector2 &a ) const
00572 {
00573 return Vector2( x / a.x, y / a.y );
00574 }
00575
00576
00577
00578 template < typename T >
00579 Vector2< T > Vector2< T >::operator-() const
00580 {
00581 return Vector2( -x, -y );
00582 }
00583
00584
00585
00586 template < typename T >
00587 const Vector2< T >& Vector2< T >::operator+=( const Vector2 &a )
00588 {
00589 x += a.x;
00590 y += a.y;
00591 return *this;
00592 }
00593
00594
00595
00596 template < typename T >
00597 const Vector2< T >& Vector2< T >::operator-=( const Vector2 &a )
00598 {
00599 x -= a.x;
00600 y -= a.y;
00601 return *this;
00602 }
00603
00604
00605
00606 template < typename T >
00607 const Vector2< T >& Vector2< T >::operator*=( const Vector2 &a )
00608 {
00609 x *= a.x;
00610 y *= a.y;
00611 return *this;
00612 }
00613
00614
00615
00616 template < typename T >
00617 const Vector2< T >& Vector2< T >::operator/=( const Vector2 &a )
00618 {
00619 x /= a.x;
00620 y /= a.y;
00621 return *this;
00622 }
00623
00624
00625
00626 template < typename T >
00627 bool Vector2< T >::operator==( const Vector2 &a ) const
00628 {
00629 return ( x == a.x && y == a.y );
00630 }
00631
00632
00633
00634 template < typename T >
00635 bool Vector2< T >::operator!=(const Vector2 &a ) const
00636 {
00637 return ( x != a.x || y != a.y );
00638 }
00639
00640 template < typename T >
00641 bool Vector2< T >::isAkin( const Vector2& rhs, const T& delta ) const
00642 {
00643 if( fabs( x-rhs.x ) > delta || fabs( y-rhs.y ) > delta )
00644
00645 return false;
00646 return true;
00647 }
00648
00649
00650
00651 template < typename T >
00652 size_t
00653 Vector2< T >::smaller( const Vector2< T >& a ) const
00654 {
00655 size_t result = 0;
00656 if ( x < a.x )
00657 result |= 1;
00658 if ( y < a.y )
00659 result |= 2;
00660 return result;
00661 }
00662
00663
00664 template < typename T >
00665 size_t
00666 Vector2< T >::smaller( const Vector2< T >& a, const size_t axis ) const
00667 {
00668 return ( xy[ axis ] < a.xy[ axis ] ) ? 1 << axis : 0;
00669 }
00670
00671
00672
00673 template < typename T >
00674 size_t
00675 Vector2< T >::greater( const Vector2< T >& a ) const
00676 {
00677 size_t result = 0;
00678 if ( x > a.x )
00679 result |= 1;
00680 if ( y > a.y )
00681 result |= 2;
00682 return result;
00683 }
00684
00685
00686 template < typename T >
00687 size_t
00688 Vector2< T >::greater( const Vector2< T >& a, const size_t axis ) const
00689 {
00690 return ( xy[ axis ] > a.xy[ axis ] ) ? 1 << axis : 0;
00691 }
00692
00693
00694
00695 template < typename T >
00696 void Vector2< T >::invert()
00697 {
00698 x = -x;
00699 y = -y;
00700 }
00701
00702
00703
00704 template < typename T >
00705 T Vector2< T >::getMaxComponent() const
00706 {
00707 return std::max( x,y );
00708 }
00709
00710
00711
00712 template < typename T >
00713 T Vector2< T >::getMinComponent() const
00714 {
00715 return std::min( x,y );
00716 }
00717
00718
00719
00720 template < typename T >
00721 T Vector2< T >::getArea() const
00722 {
00723 return x * y;
00724 }
00725
00726
00727 template< typename T >
00728 void
00729 Vector2< T >::clamp( T lower, T upper )
00730 {
00731 assert( lower <= upper );
00732
00733 if ( x < lower )
00734 x = lower;
00735 else if ( x > upper )
00736 x = upper;
00737 if ( y < lower )
00738 y = lower;
00739 else if ( y > upper )
00740 y = upper;
00741 }
00742
00743
00744
00745 template< typename T >
00746 void
00747 Vector2< T >::randomize()
00748 {
00749 x = (double) rand() / RAND_MAX;
00750 y = (double) rand() / RAND_MAX;
00751 }
00752
00753
00754
00755
00756
00757 template< typename T >
00758 bool
00759 Vector2< T >::getString( std::string& result, const std::string& delimiter ) const
00760 {
00761 std::string tmp;
00762 bool ok = true;
00763 for( size_t component = 0; component < 2; ++component )
00764 {
00765 ok = stringUtils::toString< T >( xy[ component ], tmp );
00766 result += tmp;
00767 result += delimiter;
00768 }
00769 return ok;
00770 }
00771
00772
00773 }
00774
00775 #endif