00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __VMML__VECTOR3__H__
00018 #define __VMML__VECTOR3__H__
00019
00020 #include <algorithm>
00021 #include <cassert>
00022 #include <cmath>
00023 #include <cstdlib>
00024 #include <iomanip>
00025 #include <iostream>
00026 #include <limits>
00027 #include <string>
00028 #include <vector>
00029
00030
00031
00032 namespace vmml
00033 {
00034 template< typename T > class Vector2;
00035 template< typename T > class Vector4;
00036
00037 template< typename T >
00038 class Vector3
00039 {
00040 public:
00041 union
00042 {
00043 struct
00044 {
00045 T x, y, z;
00046 };
00047 struct
00048 {
00049 T r, g, b;
00050 };
00051 T xyz[3];
00052 T rgb[3];
00053 T array[3];
00054 };
00055
00056
00057 Vector3();
00058 Vector3( const T a );
00059 Vector3( const T i, const T j, const T k );
00060 Vector3( const Vector2< T >& xy, const T z );
00061 Vector3( const Vector4< T >& from );
00062
00063
00064 template< typename U >
00065 Vector3( const Vector3< U >& orig );
00066
00067
00068
00069
00070 Vector3( const float* values );
00071 Vector3( const double* values );
00072
00073 ~Vector3();
00074
00075 void set( T xx, T yy, T zz );
00076
00077 void set( const float* values );
00078 void set( const double* values );
00079
00080
00081
00082
00083
00084 bool set( const std::string& values, char delimiter = ' ' );
00085
00086 bool set( const std::vector< std::string >& values );
00087
00088 const Vector3& operator=( T a );
00089 const Vector3& operator=( const Vector3& rhs );
00090 template< typename U >
00091 const Vector3& operator=( const Vector3< U >& rhs );
00092
00093 T& operator[]( size_t position);
00094 const T& operator[]( size_t position) const;
00095
00096
00097 Vector3 operator+( const T a ) const;
00098 Vector3 operator-( const T a ) const;
00099 Vector3 operator*( const T a ) const;
00100 inline Vector3 operator/( T a ) const;
00101
00102 const Vector3& operator+=( T a );
00103 const Vector3& operator-=( T a );
00104 const Vector3& operator*=( T a );
00105 inline const Vector3& operator/=( T a );
00106
00107
00108 Vector3 operator+( const Vector3& rhs ) const;
00109 Vector3 operator-( const Vector3& rhs ) const;
00110 Vector3 operator*( const Vector3& rhs ) const;
00111 Vector3 operator/( const Vector3& rhs ) const;
00112 Vector3 operator-() const;
00113
00114 const Vector3& operator+=( const Vector3& rhs );
00115 const Vector3& operator-=( const Vector3& rhs );
00116 const Vector3& operator*=( const Vector3& rhs );
00117 const Vector3& operator/=( const Vector3& rhs );
00118
00119 bool operator==( const Vector3& rhs ) const;
00120 bool operator!=(const Vector3& rhs ) const;
00121 bool isAkin( const Vector3& rhs,
00122 const T& delta = std::numeric_limits<T>::epsilon() ) const;
00123
00124
00125 void clamp( T lower, T upper );
00126
00127
00128
00129
00130 size_t smaller( const Vector3& rhs ) const;
00131 size_t smaller( const Vector3& rhs, const size_t axis ) const;
00132
00133 size_t greater( const Vector3& rhs ) const;
00134 size_t greater( const Vector3& rhs, const size_t axis ) const;
00135
00136
00137 T length() const;
00138 T lengthSquared() const;
00139
00140 T distance( const Vector3& other ) const;
00141 T distanceSquared( const Vector3& other ) const;
00142
00143
00144 T normalize();
00145 static T normalize( float* source );
00146
00147 inline T normalise();
00148 inline static T normalise( float* source );
00149
00150
00151 Vector3 getNormalized() const;
00152
00153 void scale( T scale_factor );
00154
00155
00156 Vector3 cross( const Vector3& rhs ) const;
00157
00158 void cross( const Vector3 &a, const Vector3 &b);
00159 T dot( const Vector3& rhs) const;
00160 static T dot( const Vector3& a, const Vector3& b);
00161
00162 void invert();
00163
00164
00165 void normal( const Vector3& aa, const Vector3& bb, const Vector3& cc );
00166
00167 Vector3 normal( const Vector3& aa, const Vector3& bb );
00168
00169 Vector3 rotate( T theta, T rx, T ry, T rz );
00170
00171 T getMinComponent();
00172 T getMaxComponent();
00173
00174
00175
00176
00177 void randomize();
00178
00179
00180
00181 bool getString( std::string& result, const std::string& delimiter = " " ) const;
00182
00183 friend std::ostream& operator << ( std::ostream& os, const Vector3& v )
00184 {
00185 const std::ios::fmtflags flags = os.flags();
00186 const int prec = os.precision();
00187
00188 os.setf( std::ios::right, std::ios::adjustfield );
00189 os.precision( 5 );
00190 os << "[" << std::setw(10) << v.x << " " << std::setw(10) << v.y
00191 << " " << std::setw(10) << v.z << "]";
00192 os.precision( prec );
00193 os.setf( flags );
00194 return os;
00195 };
00196
00197
00198 typedef T* iterator;
00199 typedef const T* const_iterator;
00200
00201 iterator begin() { return xyz; }
00202 const_iterator begin() const { return xyz; };
00203 iterator end() { return xyz + 3; }
00204 const_iterator end() const { return xyz + 3; }
00205
00206 static const Vector3 FORWARD;
00207 static const Vector3 BACKWARD;
00208 static const Vector3 UP;
00209 static const Vector3 DOWN;
00210 static const Vector3 LEFT;
00211 static const Vector3 RIGHT;
00212
00213 static const Vector3 ONE;
00214 static const Vector3 ZERO;
00215
00216 static const Vector3 UNIT_X;
00217 static const Vector3 UNIT_Y;
00218 static const Vector3 UNIT_Z;
00219
00220 };
00221
00222
00223 typedef Vector3< float > Vector3f;
00224 typedef Vector3< double > Vector3d;
00225 typedef Vector3< int > Vector3i;
00226 typedef Vector3< unsigned char > Vector3ub;
00227
00228 typedef Vector3< float > vec3f;
00229 typedef Vector3< double > vec3d;
00230 typedef Vector3< int > vec3i;
00231 typedef Vector3< unsigned char > vec3ub;
00232
00233 }
00234
00235
00236 #include <vmmlib/stringUtils.h>
00237 #include <vmmlib/vector2.h>
00238 #include <vmmlib/vector4.h>
00239
00240 namespace vmml
00241 {
00242
00243 template< typename T > const Vector3< T > Vector3< T >::FORWARD( 0, 0, -1 );
00244 template< typename T > const Vector3< T > Vector3< T >::BACKWARD( 0, 0, 1 );
00245 template< typename T > const Vector3< T > Vector3< T >::UP( 0, 1, 0 );
00246 template< typename T > const Vector3< T > Vector3< T >::DOWN( 0, -1, 0 );
00247 template< typename T > const Vector3< T > Vector3< T >::LEFT( -1, 0, 0 );
00248 template< typename T > const Vector3< T > Vector3< T >::RIGHT( 1, 0, 0 );
00249
00250 template< typename T > const Vector3< T > Vector3< T >::ONE( 1, 1, 1 );
00251 template< typename T > const Vector3< T > Vector3< T >::ZERO( 0, 0, 0 );
00252
00253 template< typename T > const Vector3< T > Vector3< T >::UNIT_X( 1, 0, 0 );
00254 template< typename T > const Vector3< T > Vector3< T >::UNIT_Y( 0, 1, 0 );
00255 template< typename T > const Vector3< T > Vector3< T >::UNIT_Z( 0, 0, 1 );
00256
00257
00258 template < typename T >
00259 Vector3< T >::Vector3()
00260 {}
00261
00262 template < typename T >
00263 Vector3< T >::Vector3( const T a )
00264 : x(a)
00265 , y(a)
00266 , z(a)
00267 {}
00268
00269 template < typename T >
00270 Vector3< T >::Vector3( const T i, const T j, const T k )
00271 : x(i)
00272 , y(j)
00273 , z(k)
00274 {}
00275
00276
00277
00278 template < typename T >
00279 Vector3< T >::Vector3( const Vector4<T>& from )
00280 {
00281 const T wInv = 1./from.w;
00282 x = from.x * wInv;
00283 y = from.y * wInv;
00284 z = from.z * wInv;
00285 }
00286
00287
00288
00289 template < typename T >
00290 Vector3< T >::Vector3( const Vector2< T >& xy, const T z_ )
00291 : x( xy.x )
00292 , y( xy.y )
00293 , z( z_ )
00294 {}
00295
00296
00297
00298 template < typename T >
00299 template < typename U >
00300 Vector3< T >::Vector3( const Vector3< U >& rhs )
00301 : x( static_cast< T >( rhs.x ) )
00302 , y( static_cast< T >( rhs.y ) )
00303 , z( static_cast< T >( rhs.z ) )
00304 {}
00305
00306
00307
00308 template < typename T >
00309 Vector3< T >::Vector3( const float* values )
00310 {
00311 assert( values && "Vector3: Nullpointer argument as source for initialisation!" );
00312 x = static_cast< T > ( values[0] );
00313 y = static_cast< T > ( values[1] );
00314 z = static_cast< T > ( values[2] );
00315 }
00316
00317
00318
00319 template < typename T >
00320 Vector3< T >::Vector3( const double* values )
00321 {
00322 assert( values && "Vector3: Nullpointer argument as source for initialisation!" );
00323 x = static_cast< T > ( values[0] );
00324 y = static_cast< T > ( values[1] );
00325 z = static_cast< T > ( values[2] );
00326 }
00327
00328
00329
00330 template < typename T >
00331 Vector3< T >::~Vector3()
00332 {}
00333
00334
00335
00336 template < typename T >
00337 void Vector3< T >::set( T xx, T yy, T zz )
00338 {
00339 x = xx;
00340 y = yy;
00341 z = zz;
00342 }
00343
00344
00345
00346 template < typename T >
00347 void Vector3< T >::set( const float* values )
00348 {
00349 assert( values && "Vector3: Nullpointer argument as source for initialisation!" );
00350 x = static_cast< T > ( values[0] );
00351 y = static_cast< T > ( values[1] );
00352 z = static_cast< T > ( values[2] );
00353 }
00354
00355
00356
00357 template < typename T >
00358 void Vector3< T >::set( const double* values )
00359 {
00360 assert( values && "Vector3: Nullpointer argument as source for initialisation!" );
00361 x = static_cast< T > ( values[0] );
00362 y = static_cast< T > ( values[1] );
00363 z = static_cast< T > ( values[2] );
00364 }
00365
00366
00367
00368
00369
00370
00371 template< typename T >
00372 bool
00373 Vector3< T >::set( const std::string& values, char delimiter )
00374 {
00375 std::vector< std::string > tokens;
00376 stringUtils::split( values, tokens, delimiter );
00377 return set( tokens );
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 template< typename T >
00387 bool
00388 Vector3< T >::set( const std::vector< std::string >& values )
00389 {
00390 bool ok = true;
00391
00392 if ( values.size() < 3 )
00393 return false;
00394
00395 std::vector< std::string >::const_iterator it = values.begin();
00396
00397 for( size_t component = 0; ok && component < 3; ++component, ++it )
00398 {
00399 ok = stringUtils::fromString< T >( *it, xyz[ component ] );
00400 }
00401
00402 return ok;
00403 }
00404
00405
00406
00407 template < typename T >
00408 const Vector3< T >& Vector3< T >::operator=( T a )
00409 {
00410 x = y = z = a;
00411 return *this;
00412 }
00413
00414
00415
00416 template < typename T >
00417 const Vector3< T >& Vector3< T >::operator=( const Vector3& rhs )
00418 {
00419 x = rhs.x;
00420 y = rhs.y;
00421 z = rhs.z;
00422 return *this;
00423 }
00424
00425
00426
00427 template < typename T >
00428 template < typename U >
00429 const Vector3< T >& Vector3< T >::operator=( const Vector3< U >& orig )
00430 {
00431 x = static_cast< T > ( orig.x );
00432 y = static_cast< T > ( orig.y );
00433 z = static_cast< T > ( orig.z );
00434 return *this;
00435 }
00436
00437
00438
00439 template < typename T >
00440 T& Vector3< T >::operator[]( size_t index )
00441 {
00442 assert( index < 3 && "Vector3::operator[] Invalid component index!" );
00443 return xyz[ index ];
00444 }
00445
00446
00447
00448
00449 template < typename T >
00450 const T& Vector3< T >::operator[]( size_t index ) const
00451 {
00452 assert( index < 3 && "Vector3::operator[] Invalid component index!" );
00453 return xyz[ index ];
00454 }
00455
00456
00457
00458 template < typename T >
00459 T Vector3< T >::length() const
00460 {
00461 return sqrt( lengthSquared( ) );
00462 }
00463
00464 template <>
00465 inline float Vector3< float >::length() const
00466 {
00467 return sqrtf( lengthSquared( ));
00468 }
00469
00470
00471
00472 template < typename T >
00473 T Vector3< T >::lengthSquared() const
00474 {
00475 return x * x + y * y + z * z;
00476 }
00477
00478
00479
00480 template < typename T >
00481 T
00482 Vector3< T >::distance( const Vector3& other ) const
00483 {
00484 return (*this - other).length();
00485 }
00486
00487
00488
00489 template < typename T >
00490 T
00491 Vector3< T >::distanceSquared( const Vector3& other ) const
00492 {
00493 return (*this - other).lengthSquared();
00494 }
00495
00496
00497
00498 template < typename T >
00499 T Vector3< T >::normalise()
00500 {
00501 return normalize();
00502 }
00503
00504
00505
00506
00507 template < typename T >
00508 T Vector3< T >::normalise( float* source )
00509 {
00510 return normalize( source );
00511 }
00512
00513
00514
00515 template < typename T >
00516 T Vector3< T >::normalize()
00517 {
00518 const T l = length();
00519 if ( l == 0 )
00520 return 0;
00521
00522 const T ll = 1.0 / l;
00523 x *= ll;
00524 y *= ll;
00525 z *= ll;
00526 return l;
00527 }
00528
00529
00530
00531
00532 template < typename T >
00533 T Vector3< T >::normalize( float* source )
00534 {
00535 Vector3< float >* a = ( Vector3< float >* ) source;
00536 const T l = a->length();
00537 if ( l == 0 )
00538 return 0;
00539
00540 const T ll = 1.0 / l;
00541 source[0] *= ll;
00542 source[1] *= ll;
00543 source[2] *= ll;
00544 return l;
00545 }
00546
00547
00548
00549 template< typename T >
00550 Vector3< T > Vector3< T >::getNormalized() const
00551 {
00552 Vector3< T > n( *this );
00553 n.normalize();
00554 return n;
00555 }
00556
00557
00558
00559 template < typename T >
00560 void Vector3< T >::scale( T scale_factor )
00561 {
00562 operator*=( scale_factor );
00563 }
00564
00565
00566
00567 template < typename T >
00568 Vector3< T > Vector3< T >::operator+( const T a ) const
00569 {
00570 return Vector3( x + a, y + a, z + a );
00571 }
00572
00573
00574
00575 template < typename T >
00576 Vector3< T > Vector3< T >::operator-( const T a ) const
00577 {
00578 return Vector3( x - a, y - a, z - a );
00579 }
00580
00581
00582
00583 template < typename T >
00584 Vector3< T > Vector3< T >::operator*( const T a ) const
00585 {
00586 return Vector3( x * a, y * a, z * a );
00587 }
00588
00589
00590
00591 template < typename T >
00592 inline Vector3< T > Vector3< T >::operator/( T a ) const
00593 {
00594 return Vector3( x / a, y / a, z / a );
00595 }
00596
00597
00598
00599 template <>
00600 inline Vector3< float > Vector3< float >::operator/( float a ) const
00601 {
00602 assert( a != 0.0f );
00603 a = 1.0f / a;
00604 return Vector3( x * a, y * a, z * a );
00605 }
00606
00607
00608
00609 template <>
00610 inline Vector3< double > Vector3< double >::operator/( double a ) const
00611 {
00612 assert( a != 0.0 );
00613 a = 1.0 / a;
00614 return Vector3( x * a, y * a, z * a );
00615 }
00616
00617
00618
00619 template < typename T >
00620 const Vector3< T >& Vector3< T >::operator+=( T a )
00621 {
00622 x += a;
00623 y += a;
00624 z += a;
00625 return *this;
00626 }
00627
00628
00629
00630 template < typename T >
00631 const Vector3< T >& Vector3< T >::operator-=( T a )
00632 {
00633 x -= a;
00634 y -= a;
00635 z -= a;
00636 return *this;
00637 }
00638
00639
00640
00641 template < typename T >
00642 const Vector3< T >& Vector3< T >::operator*=( T a )
00643 {
00644 x *= a;
00645 y *= a;
00646 z *= a;
00647 return *this;
00648 }
00649
00650
00651
00652 template < typename T >
00653 inline const Vector3< T >& Vector3< T >::operator/=( T a )
00654 {
00655 x /= a;
00656 y /= a;
00657 z /= a;
00658 return *this;
00659 }
00660
00661
00662 template <>
00663 inline const Vector3< float >& Vector3< float >::operator/=( float a )
00664 {
00665 a = 1.0f / a;
00666 x *= a;
00667 y *= a;
00668 z *= a;
00669 return *this;
00670 }
00671
00672
00673
00674 template <>
00675 inline const Vector3< double >& Vector3< double >::operator/=( double a )
00676 {
00677 a = 1.0 / a;
00678 x *= a;
00679 y *= a;
00680 z *= a;
00681 return *this;
00682 }
00683
00684
00685
00686
00687
00688 template < typename T >
00689 Vector3< T > Vector3< T >::operator+( const Vector3 &rhs ) const
00690 {
00691 return Vector3( x + rhs.x, y + rhs.y, z + rhs.z );
00692 }
00693
00694
00695
00696 template < typename T >
00697 Vector3< T > Vector3< T >::operator-( const Vector3 &rhs ) const
00698 {
00699 return Vector3( x - rhs.x, y - rhs.y, z - rhs.z );
00700 }
00701
00702
00703
00704 template < typename T >
00705 Vector3< T > Vector3< T >::operator*( const Vector3 &rhs ) const
00706 {
00707 return Vector3( x * rhs.x, y * rhs.y, z * rhs.z );
00708 }
00709
00710
00711
00712 template < typename T >
00713 Vector3< T > Vector3< T >::operator/( const Vector3 &rhs ) const
00714 {
00715 return Vector3( x / rhs.x, y / rhs.y, z / rhs.z );
00716 }
00717
00718
00719
00720 template < typename T >
00721 Vector3< T > Vector3< T >::operator-() const
00722 {
00723 return Vector3( -x, -y, -z );
00724 }
00725
00726
00727
00728 template < typename T >
00729 const Vector3< T >& Vector3< T >::operator+=( const Vector3& rhs )
00730 {
00731 x += rhs.x;
00732 y += rhs.y;
00733 z += rhs.z;
00734 return *this;
00735 }
00736
00737
00738
00739 template < typename T >
00740 const Vector3< T >& Vector3< T >::operator-=( const Vector3& rhs )
00741 {
00742 x -= rhs.x;
00743 y -= rhs.y;
00744 z -= rhs.z;
00745 return *this;
00746 }
00747
00748
00749
00750 template < typename T >
00751 const Vector3< T >& Vector3< T >::operator*=( const Vector3& rhs )
00752 {
00753 x *= rhs.x;
00754 y *= rhs.y;
00755 z *= rhs.z;
00756 return *this;
00757 }
00758
00759
00760
00761 template < typename T >
00762 const Vector3< T >& Vector3< T >::operator/=( const Vector3& rhs )
00763 {
00764 x /= rhs.x;
00765 y /= rhs.y;
00766 z /= rhs.z;
00767 return *this;
00768 }
00769
00770
00771
00772 template < typename T >
00773 size_t
00774 Vector3< T >::smaller( const Vector3< T >& rhs ) const
00775 {
00776 size_t result = 0;
00777 if ( x < rhs.x )
00778 result |= 1;
00779 if ( y < rhs.y )
00780 result |= 2;
00781 if ( z < rhs.z )
00782 result |= 4;
00783 return result;
00784 }
00785
00786
00787
00788 template < typename T >
00789 size_t
00790 Vector3< T >::smaller( const Vector3< T >& rhs, const size_t axis ) const
00791 {
00792 return ( xyz[ axis ] < rhs.xyz[ axis ] ) ? 1 << axis : 0;
00793 }
00794
00795
00796
00797 template < typename T >
00798 size_t
00799 Vector3< T >::greater( const Vector3< T >& rhs ) const
00800 {
00801 size_t result = 0;
00802 if ( x > rhs.x )
00803 result |= 1;
00804 if ( y > rhs.y )
00805 result |= 2;
00806 if ( z > rhs.z )
00807 result |= 4;
00808 return result;
00809 }
00810
00811
00812
00813 template < typename T >
00814 size_t
00815 Vector3< T >::greater( const Vector3< T >& rhs, const size_t axis ) const
00816 {
00817 return ( xyz[ axis ] > rhs.xyz[ axis ] ) ? 1 << axis : 0;
00818 }
00819
00820
00821
00822
00823 template < typename T >
00824 Vector3< T > Vector3< T >::cross( const Vector3& rhs ) const
00825 {
00826 return Vector3( y * rhs.z - z * rhs.y,
00827 z * rhs.x - x * rhs.z,
00828 x * rhs.y - y * rhs.x );
00829 }
00830
00831
00832
00833
00834 template < typename T >
00835 void Vector3< T >::cross( const Vector3& aa, const Vector3& bb )
00836 {
00837 x = aa.y * bb.z - aa.z * bb.y;
00838 y = aa.z * bb.x - aa.x * bb.z;
00839 z = aa.x * bb.y - aa.y * bb.x;
00840 }
00841
00842
00843
00844 template < typename T >
00845 T Vector3< T >::dot( const Vector3& rhs) const
00846 {
00847 return x * rhs.x + y * rhs.y + z * rhs.z;
00848 }
00849
00850
00851
00852 template < typename T >
00853 T Vector3< T >::dot( const Vector3& aa, const Vector3& bb )
00854 {
00855 return aa.x * bb.x + aa.y * bb.y + aa.z * bb.z;
00856 }
00857
00858
00859
00860 template < typename T >
00861 bool Vector3< T >::operator==( const Vector3& rhs ) const
00862 {
00863 return ( x == rhs.x && y == rhs.y && z == rhs.z );
00864 }
00865
00866
00867
00868 template < typename T >
00869 bool
00870 Vector3< T >::operator!=(const Vector3& rhs ) const
00871 {
00872 return ( x != rhs.x || y != rhs.y || z != rhs.z );
00873 }
00874
00875
00876
00877 template < typename T >
00878 bool
00879 Vector3< T >::isAkin( const Vector3& rhs, const T& delta )const
00880 {
00881 if( fabs( x-rhs.x ) > delta || fabs( y-rhs.y ) > delta ||
00882 fabs( z-rhs.z ) > delta )
00883
00884 return false;
00885 return true;
00886 }
00887
00888
00889
00890 template< >
00891 inline bool
00892 Vector3< float >::isAkin( const Vector3& rhs, const float& delta )const
00893 {
00894 if( fabsf( x-rhs.x ) > delta || fabsf( y-rhs.y ) > delta ||
00895 fabsf( z-rhs.z ) > delta )
00896
00897 return false;
00898 return true;
00899 }
00900
00901
00902 template < typename T >
00903 void
00904 Vector3< T >::clamp( T lower, T upper )
00905 {
00906 assert( lower <= upper );
00907
00908 if ( x < lower )
00909 x = lower;
00910 else if ( x > upper )
00911 x = upper;
00912 if ( y < lower )
00913 y = lower;
00914 else if ( y > upper )
00915 y = upper;
00916 if ( z < lower )
00917 z = lower;
00918 else if ( z > upper )
00919 z = upper;
00920
00921 }
00922
00923
00924
00925 template < typename T >
00926 void Vector3< T >::invert()
00927 {
00928 x = -x;
00929 y = -y;
00930 z = -z;
00931 }
00932
00933
00934
00935
00936 template< typename T >
00937 void Vector3< T >::normal( const Vector3< T >& aa,
00938 const Vector3< T >& bb,
00939 const Vector3< T >& cc )
00940 {
00941 Vector3< T > u,v;
00942
00943
00944 u = bb - aa;
00945 v = cc - aa;
00946 cross( u, v );
00947 normalize();
00948 }
00949
00950
00951
00952
00953 template< typename T >
00954 Vector3< T > Vector3< T >::normal( const Vector3< T >& aa,
00955 const Vector3< T >& bb )
00956 {
00957 Vector3< T > tmp;
00958 tmp.normal( *this, aa, bb);
00959 return tmp;
00960 }
00961
00962
00963
00964
00965 template < typename T >
00966 Vector3< T > Vector3< T >::rotate( T theta, T rx, T ry, T rz )
00967 {
00968 Vector3 axis( rx, ry, rz );
00969 axis.normalize();
00970
00971 const T costheta = static_cast< T >( cos( theta ));
00972 const T sintheta = static_cast< T >( sin( theta ));
00973
00974 return Vector3(
00975 (costheta + ( 1.0f - costheta ) * axis.x * axis.x ) * x +
00976 (( 1 - costheta ) * axis.x * axis.y - axis.z * sintheta ) * y +
00977 (( 1 - costheta ) * axis.x * axis.z + axis.y * sintheta ) * z,
00978
00979 (( 1 - costheta ) * axis.x * axis.y + axis.z * sintheta ) * x +
00980 ( costheta + ( 1 - costheta ) * axis.y * axis.y ) * y +
00981 (( 1 - costheta ) * axis.y * axis.z - axis.x * sintheta ) * z,
00982
00983 (( 1 - costheta ) * axis.x * axis.z - axis.y * sintheta ) * x +
00984 (( 1 - costheta ) * axis.y * axis.z + axis.x * sintheta ) * y +
00985 ( costheta + ( 1 - costheta ) * axis.z * axis.z ) * z );
00986 }
00987
00988
00989
00990
00991 template<>
00992 inline Vector3< float > Vector3< float >::rotate( float theta, float rx,
00993 float ry, float rz )
00994 {
00995 Vector3< float > axis( rx, ry, rz );
00996 axis.normalize();
00997
00998 const float costheta = cosf( theta );
00999 const float sintheta = sinf( theta );
01000
01001 return Vector3< float >(
01002 (costheta + ( 1.0f - costheta ) * axis.x * axis.x ) * x +
01003 (( 1 - costheta ) * axis.x * axis.y - axis.z * sintheta ) * y +
01004 (( 1 - costheta ) * axis.x * axis.z + axis.y * sintheta ) * z,
01005
01006 (( 1 - costheta ) * axis.x * axis.y + axis.z * sintheta ) * x +
01007 ( costheta + ( 1 - costheta ) * axis.y * axis.y ) * y +
01008 (( 1 - costheta ) * axis.y * axis.z - axis.x * sintheta ) * z,
01009
01010 (( 1 - costheta ) * axis.x * axis.z - axis.y * sintheta ) * x +
01011 (( 1 - costheta ) * axis.y * axis.z + axis.x * sintheta ) * y +
01012 ( costheta + ( 1 - costheta ) * axis.z * axis.z ) * z );
01013 }
01014
01015
01016
01017
01018 template < typename T >
01019 T Vector3< T >::getMaxComponent()
01020 {
01021 T m = std::max( x,y );
01022 m = std::max( m, z);
01023 return m;
01024 }
01025
01026
01027
01028
01029 template < typename T >
01030 T Vector3< T >::getMinComponent()
01031 {
01032 T m = std::min( x,y );
01033 m = std::min( m, z);
01034 return m;
01035 }
01036
01037
01038
01039 template < typename T >
01040 void
01041 Vector3< T >::randomize()
01042 {
01043 x = (double) rand() / RAND_MAX;
01044 y = (double) rand() / RAND_MAX;
01045 z = (double) rand() / RAND_MAX;
01046 }
01047
01048
01049
01050
01051
01052 template< typename T >
01053 bool
01054 Vector3< T >::getString( std::string& result, const std::string& delimiter ) const
01055 {
01056 std::string tmp;
01057 bool ok = true;
01058 for( size_t component = 0; component < 3; ++component )
01059 {
01060 ok = stringUtils::toString< T >( xyz[ component ], tmp );
01061 result += tmp;
01062 result += delimiter;
01063 }
01064 return ok;
01065 }
01066
01067
01068
01069 }
01070
01071 #endif