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