An Implementation of the Marching Cubes Algorithm  1.0
3dvector.hpp
Go to the documentation of this file.
00001 
00025 #ifndef t3DVECTOR_HPP
00026 #define t3DVECTOR_HPP
00027 
00028 #include <cmath>                // sqrt()
00029 #include <sstream>              // std::stringstream
00030 
00031 #include "mathmacros.hpp"       // is_equal(), is_zero(), sqr(), INVERSE()
00032 #include "exceptionobject.hpp"  // ExceptionObject 
00033 
00034 
00049 namespace common {
00050 
00051 
00057   class t3DVector {
00058   public:
00059     // ---------------------------------------------------------------
00060     //
00061     // Public methods
00062     //
00063     // ---------------------------------------------------------------
00064 
00070     t3DVector() :
00071       _x( 0 ) ,
00072       _y( 0 ) , 
00073       _z( 0 ) 
00074     {
00075     }
00076 
00077 
00086     t3DVector( const t3DVector& v ) : 
00087       _x( v._x ) , 
00088       _y( v._y ) , 
00089       _z( v._z ) 
00090     {
00091     }
00092 
00093 
00104     t3DVector( 
00105               double x ,
00106               double y ,
00107               double z 
00108              )
00109     {
00110       this->set_coordinates( x , y , z ) ;
00111     }
00112 
00113 
00122     void set_coordinates( const t3DVector& v )
00123     {
00124       _x = v._x ;
00125       _y = v._y ;
00126       _z = v._z ;
00127     }
00128 
00129 
00139     void set_coordinates( double x , double y , double  z )
00140     {
00141       _x = x ;
00142       _y = y ;
00143       _z = z ;
00144     }
00145 
00146 
00156     bool operator==( const t3DVector& v ) const
00157     {
00158       return is_equal( _x , v._x ) && 
00159              is_equal( _y , v._y ) &&
00160              is_equal( _z , v._z ) ;
00161     }
00162 
00163 
00174     bool operator!=( const t3DVector& v ) const
00175     {
00176       return *this == v ;
00177     }
00178 
00179 
00187     t3DVector& operator=( const t3DVector& v )
00188     {
00189       _x = v._x ;
00190       _y = v._y ;
00191       _z = v._z ;
00192       
00193       return *this ;
00194     }
00195 
00196 
00207     t3DVector& operator+=( const t3DVector& v )
00208     {
00209       _x += v._x ;
00210       _y += v._y ;
00211       _z += v._z ;
00212       
00213       return *this ;
00214     }
00215 
00216 
00227     t3DVector& operator-=( const t3DVector& v ) 
00228     {
00229       _x -= v._x ;
00230       _y -= v._y ;
00231       _z -= v._z ;
00232       
00233       return *this;
00234     }
00235 
00236 
00246     t3DVector& operator*=( double alpha )
00247     {
00248       _x *= alpha ;
00249       _y *= alpha ;
00250       _z *= alpha ;
00251       
00252       return *this ;
00253     }
00254 
00255 
00266     t3DVector operator-() const
00267     {
00268       return t3DVector( -_x , -_y , -_z ) ;
00269     }
00270 
00271 
00283     t3DVector operator+( const t3DVector& v ) const
00284     {
00285       return t3DVector(
00286                         _x + v._x , 
00287                         _y + v._y , 
00288                         _z + v._z
00289                       ) ;
00290     }
00291 
00292 
00304     t3DVector operator-( const t3DVector& v ) const
00305     {
00306       return t3DVector(
00307                         _x - v._x , 
00308                         _y - v._y , 
00309                         _z - v._z
00310                       ) ;
00311     }
00312 
00313 
00325     t3DVector operator*( double alpha ) const
00326     {
00327       return t3DVector(
00328                         _x * alpha , 
00329                         _y * alpha , 
00330                         _z * alpha
00331                       ) ;
00332     }
00333 
00334 
00346     friend t3DVector operator*( double alpha , const t3DVector& v ) ;
00347 
00348 
00359     double operator*( const t3DVector& v ) const
00360     {
00361       return _x * v._x + _y * v._y + _z * v._z ;
00362     }
00363 
00364 
00374     double operator[]( unsigned i ) const throw ( ExceptionObject )
00375     {
00376       if ( i > 2 ) {
00377         std::stringstream ss (
00378                               std::stringstream::in | 
00379                               std::stringstream::out
00380                              ) ;
00381         ss << "Array index is out of range" ;
00382         throw ExceptionObject( __FILE__ , __LINE__ , ss.str() ) ;  
00383       }
00384 
00385 
00386       if ( i == 0 ) {
00387         return _x ;
00388       }
00389       else if ( i == 1 ) {
00390         return _y ;
00391       }
00392 
00393       return _z ;
00394     }
00395 
00396 
00407     double& operator[]( unsigned i ) throw ( ExceptionObject )
00408     {
00409       if ( i > 2 ) {
00410         std::stringstream ss (
00411                               std::stringstream::in | 
00412                               std::stringstream::out
00413                              ) ;
00414         ss << "Array index is out of range" ;
00415         throw ExceptionObject( __FILE__ , __LINE__ , ss.str() ) ;  
00416       }
00417 
00418 
00419       if ( i == 0 ) {
00420         return _x ;
00421       }
00422       else if ( i == 1 ) {
00423         return _y ;
00424       }
00425 
00426       return _z ;
00427     }
00428 
00429 
00437     double length() const 
00438     {
00439       return sqrt( sqr( _x ) + sqr( _y ) + sqr( _z ) ) ;
00440     }
00441 
00442 
00452     bool is_null() const 
00453     {
00454       return is_zero( _x ) && is_zero( _y ) && is_zero( _z ) ;
00455     }
00456 
00457 
00467     t3DVector& negate()
00468     {
00469       _x = -_x ;
00470       _y = -_y ;
00471       _z = -_z ;
00472       
00473       return *this ;
00474     }
00475 
00476 
00487     double inner( const t3DVector& v ) const
00488     {
00489       return this->operator*( v ) ;
00490     }
00491 
00492 
00501     t3DVector& normalize() 
00502     {
00503       double inv_norm = INVERSE( this->length() ) ;
00504 
00505       _x *= inv_norm ;
00506       _y *= inv_norm ;
00507       _z *= inv_norm ;
00508 
00509       return *this ;
00510     }
00511 
00512 
00513 
00525     t3DVector cross( const t3DVector& v ) const
00526     {
00527       return t3DVector(
00528                        _y * v._z - _z * v._y ,
00529                        _z * v._x - _x * v._z ,
00530                        _x * v._y - _y * v._x
00531                       ) ;
00532     }
00533 
00534 
00535     // ---------------------------------------------------------------
00536     //
00537     // Public attributes
00538     //
00539     // ---------------------------------------------------------------
00540 
00541     double _x ;    
00542     double _y ;    
00543     double _z ;    
00544 
00545   } ;
00546 
00547 
00559   inline t3DVector operator*( double alpha , const t3DVector& v )
00560   {
00561     return v * alpha ;
00562   }
00563 
00564 }         // end of namespace common
00565 
00566  //end of group class.
00568 
00569 #endif   // t3DVECTOR_HPP