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

vector.h

00001 //FILE:         vector.h
00002 //AUTHOR:       Nathan Cournia <nathan@cournia.com>
00003 
00004 #ifndef COURNIA_VECTOR_H
00005 #define COURNIA_VECTOR_H 1
00006 
00007 #include <iostream>
00008 #include <cassert>
00009 #include "mathematics.h"
00010 
00012 namespace math {
00013         class vector3;
00014 }
00015 
00017 math::vector3 operator* ( real_t lhs, const math::vector3& rhs );
00018 std::ostream& operator<< ( std::ostream& o, const math::vector3& vec );
00019 
00020 namespace math {
00021         math::vector3 cross( const math::vector3& lhs, const math::vector3& rhs );
00022         void add( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00023         void subtract( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00024         void multiply( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00025         void divide( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00026         void multiply( math::vector3& dest, const math::vector3& lhs, real_t rhs );
00027         void multiply( math::vector3& dest, real_t rhs, const math::vector3& lhs );
00028         real_t dot( const math::vector3& lhs, const math::vector3& rhs ); 
00029 }
00030 
00032 class math::vector3
00033 {
00034 private:
00035         real_t m_data[ 3 ]; 
00036 
00038 
00045         friend math::vector3 operator* ( real_t lhs, const math::vector3& rhs );
00046 
00048 
00053         friend std::ostream& operator<< ( std::ostream& o, const math::vector3& vec );
00054 
00056 
00061         friend math::vector3 math::cross( const math::vector3& lhs, const math::vector3& rhs );
00062 
00064 
00073         friend void math::add( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00074 
00076 
00085         friend void math::subtract( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00086 
00088 
00096         friend void math::multiply( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00097 
00099 
00108         friend void math::divide( math::vector3& dest, const math::vector3& lhs, const math::vector3& rhs );
00109 
00111 
00121         friend void math::multiply( math::vector3& dest, const math::vector3& lhs, real_t rhs );
00122 
00124 
00134         friend void math::multiply( math::vector3& dest, real_t rhs, const math::vector3& lhs );
00135 
00136 
00138 
00143         friend real_t math::dot( const math::vector3& lhs, const math::vector3& rhs );
00144 
00145 public:
00147 
00150         vector3( void ) {
00151                 m_data[ 0 ] = 0.0;
00152                 m_data[ 1 ] = 0.0;
00153                 m_data[ 2 ] = 0.0;
00154         }
00155         
00157 
00160         vector3( real_t x_, real_t y_, real_t z_ ) {
00161                 m_data[ 0 ] = x_;
00162                 m_data[ 1 ] = y_;
00163                 m_data[ 2 ] = z_;
00164         }
00165 
00167 
00171         vector3( real_t *r3 ) {
00172                 m_data[ 0 ] = r3[ 0 ];
00173                 m_data[ 1 ] = r3[ 1 ];
00174                 m_data[ 2 ] = r3[ 2 ];
00175         }
00176 
00178         vector3( const math::vector3& p ) {
00179                 m_data[ 0 ] = p.m_data[ 0 ];
00180                 m_data[ 1 ] = p.m_data[ 1 ];
00181                 m_data[ 2 ] = p.m_data[ 2 ];
00182         }
00183         
00185         inline math::vector3& operator= ( const math::vector3& p ) {
00186                 m_data[ 0 ] = p.m_data[ 0 ];
00187                 m_data[ 1 ] = p.m_data[ 1 ];
00188                 m_data[ 2 ] = p.m_data[ 2 ];
00189                 return *this;
00190         }
00191 
00193         inline void set( real_t x_, real_t y_, real_t z_ ) {
00194                 m_data[ 0 ] = x_;
00195                 m_data[ 1 ] = y_;
00196                 m_data[ 2 ] = z_;
00197         }
00198 
00200 
00206         inline real_t operator( ) ( unsigned int index ) const {
00207                 assert( index < 3 );
00208                 return m_data[ index ];
00209         }
00210 
00212 
00217         inline real_t& operator( ) ( unsigned int index ) {
00218                 assert( index < 3 );
00219                 return m_data[ index ];
00220         }
00221 
00223 
00229         inline math::vector3 operator+ ( const math::vector3& rhs ) const {
00230                 return math::vector3( 
00231                         (m_data[ 0 ] + rhs.m_data[ 0 ]), 
00232                         (m_data[ 1 ] + rhs.m_data[ 1 ]),
00233                         (m_data[ 2 ] + rhs.m_data[ 2 ])
00234                 );
00235         }
00236 
00238 
00244         inline math::vector3 operator- ( const math::vector3& rhs ) const {
00245                 return math::vector3( 
00246                         (m_data[ 0 ] - rhs.m_data[ 0 ]), 
00247                         (m_data[ 1 ] - rhs.m_data[ 1 ]),
00248                         (m_data[ 2 ] - rhs.m_data[ 2 ])
00249                 );
00250         }
00251 
00253 
00258         inline math::vector3 operator* ( const math::vector3& rhs ) const {
00259                 return math::vector3( 
00260                         (m_data[ 0 ] * rhs.m_data[ 0 ]), 
00261                         (m_data[ 1 ] * rhs.m_data[ 1 ]),
00262                         (m_data[ 2 ] * rhs.m_data[ 2 ])
00263                 );
00264         }
00265 
00267 
00273         inline math::vector3 operator/ ( const math::vector3& rhs ) const {
00274                 return math::vector3( 
00275                         (m_data[ 0 ] / rhs.m_data[ 0 ]), 
00276                         (m_data[ 1 ] / rhs.m_data[ 1 ]),
00277                         (m_data[ 2 ] / rhs.m_data[ 2 ])
00278                 );
00279         }
00280 
00282 
00285         inline math::vector3 operator- ( void ) const {
00286                 return math::vector3( 
00287                         (m_data[ 0 ] * -1.0), 
00288                         (m_data[ 1 ] * -1.0),
00289                         (m_data[ 2 ] * -1.0)
00290                 );
00291         }
00292 
00294 
00301         inline math::vector3 operator* ( real_t rhs ) const {
00302                 return math::vector3( 
00303                         (m_data[ 0 ] * rhs), 
00304                         (m_data[ 1 ] * rhs),
00305                         (m_data[ 2 ] * rhs)
00306                 );
00307         }
00308 
00310 
00316         inline math::vector3 operator/ ( real_t rhs ) const {
00317                 return math::vector3( 
00318                         (m_data[ 0 ] / rhs), 
00319                         (m_data[ 1 ] / rhs),
00320                         (m_data[ 2 ] / rhs)
00321                 );
00322         }
00323 
00325 
00328         inline void operator-= ( const math::vector3& v ) {
00329                 m_data[ 0 ] -= v.m_data[ 0 ];
00330                 m_data[ 1 ] -= v.m_data[ 1 ];
00331                 m_data[ 2 ] -= v.m_data[ 2 ];
00332         }
00333 
00335 
00338         inline void operator+= ( const math::vector3& v ) {
00339                 m_data[ 0 ] += v.m_data[ 0 ];
00340                 m_data[ 1 ] += v.m_data[ 1 ];
00341                 m_data[ 2 ] += v.m_data[ 2 ];
00342         }
00343 
00345 
00349         inline void operator*= ( const math::vector3 &rhs ) {
00350                 m_data[ 0 ] *= rhs.m_data[ 0 ];
00351                 m_data[ 1 ] *= rhs.m_data[ 1 ];
00352                 m_data[ 2 ] *= rhs.m_data[ 2 ];
00353         }
00354 
00356 
00361         inline void operator/= ( const math::vector3 &rhs ) {
00362                 m_data[ 0 ] /= rhs.m_data[ 0 ];
00363                 m_data[ 1 ] /= rhs.m_data[ 1 ];
00364                 m_data[ 2 ] /= rhs.m_data[ 2 ];
00365         }
00366 
00368 
00372         inline void operator-= ( real_t rhs ) {
00373                 m_data[ 0 ] -= rhs;
00374                 m_data[ 1 ] -= rhs;
00375                 m_data[ 2 ] -= rhs;
00376         }
00377 
00379 
00383         inline void operator+= ( real_t rhs ) {
00384                 m_data[ 0 ] += rhs;
00385                 m_data[ 1 ] += rhs;
00386                 m_data[ 2 ] += rhs;
00387         }
00388 
00390 
00394         inline void operator*= ( real_t rhs ) {
00395                 m_data[ 0 ] *= rhs;
00396                 m_data[ 1 ] *= rhs;
00397                 m_data[ 2 ] *= rhs;
00398         }
00399 
00401 
00406         inline void operator/= ( real_t rhs ) {
00407                 m_data[ 0 ] /= rhs;
00408                 m_data[ 1 ] /= rhs;
00409                 m_data[ 2 ] /= rhs;
00410         }
00411 
00413         inline real_t x( void ) const { 
00414                 return m_data[ 0 ];
00415         }
00416         
00418         inline real_t y( void ) const { 
00419                 return m_data[ 1 ];
00420         }
00421 
00423         inline real_t z( void ) const { 
00424                 return m_data[ 2 ];
00425         }
00426         
00428         inline void x( real_t x_ ) {
00429                 m_data[ 0 ] = x_;
00430         }
00431         
00433         inline void y( real_t y_ ) {
00434                 m_data[ 1 ] = y_;
00435         }
00436         
00438         inline void z( real_t z_ ) {
00439                 m_data[ 2 ] = z_;
00440         }
00441 
00443 
00446         inline double length( void ) const {
00447                 return std::sqrt( (m_data[ 0 ] * m_data[ 0 ]) +
00448                         (m_data[ 1 ] * m_data[ 1 ]) +
00449                         (m_data[ 2 ] * m_data[ 2 ])
00450                 );
00451         }
00452 
00454 
00457         inline void normalize( void ) {
00458                 double len = length( );
00459                 if( !math::equals( len, 0.0 ) ) {
00460                         m_data[ 0 ] /= len;
00461                         m_data[ 1 ] /= len;
00462                         m_data[ 2 ] /= len;
00463                 }
00464         }
00465 
00467 
00471         inline void negate( void )
00472         {
00473                 m_data[ 0 ] *= -1.0;
00474                 m_data[ 1 ] *= -1.0;
00475                 m_data[ 2 ] *= -1.0;
00476         }
00477 
00479 
00484         inline bool operator== ( const math::vector3& rhs ) const {
00485                 return (math::equals( m_data[ 0 ], rhs.m_data[ 0 ] ) &&
00486                         math::equals( m_data[ 1 ], rhs.m_data[ 1 ] ) &&
00487                         math::equals( m_data[ 2 ], rhs.m_data[ 2 ] )
00488                 );
00489         }
00490 
00492 
00497         inline bool operator!= ( const math::vector3& rhs ) const {
00498                 return !(math::equals( m_data[ 0 ], rhs.m_data[ 0 ] ) &&
00499                         math::equals( m_data[ 1 ], rhs.m_data[ 1 ] ) &&
00500                         math::equals( m_data[ 2 ], rhs.m_data[ 2 ] )
00501                 );
00502         }
00503 
00505 
00508         inline operator const real_t*( void ) const {
00509                 return m_data;
00510         }
00511         
00513         static const vector3 ZERO; 
00514         static const vector3 X_AXIS; 
00515         static const vector3 Y_AXIS; 
00516         static const vector3 Z_AXIS; 
00517 };
00518         
00519 #endif

Generated on Tue Feb 11 18:49:42 2003 for uber by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002