zsig 1.0.0

include/vec.hh

Go to the documentation of this file.
00001 
00008 #ifndef ZSIG_VEC_HH
00009 #define ZSIG_VEC_HH
00010 
00011 //== INCLUDES =================================================================
00012 
00013 #include <cmath>
00014 #include <iostream>
00015 
00016 //== NAMESPACES ===============================================================
00017 
00018 namespace zsig {
00019 
00020 //== CLASS DEFINITION =========================================================
00021 
00035 template< unsigned D, class T >
00036 class vec {
00037 
00038 public:
00039 
00040         typedef vec< D, T > vec_type; 
00041 
00043         vec() { this->clear(); }
00044 
00048         vec( const vec_type& _v ) { *this = _v; }
00049 
00053         vec( const T& _s ) { *this = _s; }
00054 
00056         ~vec() { }
00057 
00059         void clear( void ) { *this = T(); }
00060 
00064         T x( void ) const { if( D>0 ) return this->coord[0]; }
00065 
00069         T y( void ) const { if( D>1 ) return this->coord[1]; }
00070 
00074         T z( void ) const { if( D>2 ) return this->coord[2]; }
00075 
00079         T w( void ) const { if( D>3 ) return this->coord[3]; }
00080 
00084         T sqrl( void ) const { return (*this) ^ (*this); }
00085 
00089         T length( void ) const { return sqrt( this->sqrl() ); }
00090 
00095         T angle( const vec_type& _v ) const { return acos( ( (*this) ^ _v ) / ( this->length() * _v.length() ) ); }
00096 
00100         vec_type normal( void ) const { return *this / length(); }
00101 
00105         vec_type normalize( void ) { return *this = this->normal(); }
00106 
00112         vec_type normalize( const T& _l ) { return *this /= _l; }
00113 
00118         vec_type& operator = ( const vec_type& _v ) {
00119                 for (unsigned i=0; i<D; i++) this->coord[i] = _v[i];
00120                 return *this;
00121         }
00122 
00127         vec_type& operator = ( const T& _s ) {
00128                 for (unsigned i=0; i<D; i++) this->coord[i] = _s;
00129                 return *this;
00130         }
00131 
00135         vec_type operator - ( void ) const {
00136                 vec_type u;
00137                 for (unsigned i=0; i<D; i++) u[i] = -this->coord[i];
00138                 return u;
00139         }
00140 
00145         vec_type operator + ( const T& _s ) const {
00146                 vec_type u;
00147                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] + _s;
00148                 return u;
00149         }
00150 
00155         vec_type operator + ( const vec_type& _v ) const {
00156                 vec_type u;
00157                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] + _v[i];
00158                 return u;
00159         }
00160 
00165         vec_type& operator += ( const vec_type& _v ) {
00166                 for (unsigned i=0; i<D; i++) this->coord[i] += _v[i];
00167                 return *this;
00168         }
00169 
00174         vec_type operator - ( const T& _s ) const {
00175                 vec_type u;
00176                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] - _s;
00177                 return u;
00178         }
00179 
00184         vec_type operator - ( const vec_type& _v ) const {
00185                 vec_type u;
00186                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] - _v[i];
00187                 return u;
00188         }
00189 
00194         vec_type& operator -= ( const vec_type& _v ) {
00195                 for (unsigned i=0; i<D; i++) this->coord[i] -= _v[i];
00196                 return *this;
00197         }
00198 
00203         vec_type operator * ( const T& _s ) const {
00204                 vec_type u;
00205                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] * _s;
00206                 return u;
00207         }
00208 
00213         vec_type operator * ( const vec_type& _v ) const {
00214                 vec_type u;
00215                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] * _v[i];
00216                 return u;
00217         }
00218 
00223         vec_type& operator *= ( const T& _s ) {
00224                 for (unsigned i=0; i<D; i++) this->coord[i] *= _s;
00225                 return *this;
00226         }
00227 
00232         vec_type& operator *= ( const vec_type& _v ) {
00233                 for (unsigned i=0; i<D; i++) this->coord[i] *= _v[i];
00234                 return *this;
00235         }
00236 
00242         friend vec_type operator * ( const T& _s,
00243                                      const vec_type& _v ) {
00244                 return _v * _s;
00245         }
00246 
00251         vec_type operator / ( const T& _s ) const {
00252                 if( _s == (T)0 ) return *this; // lazy error handling
00253                 vec_type u;
00254                 for (unsigned i=0; i<D; i++) u[i] = this->coord[i] / _s;
00255                 return u;
00256         }
00257 
00262         vec_type operator / ( const vec_type& _v ) const {
00263                 vec_type u;
00264                 for (unsigned i=0; i<D; i++)
00265                         if( _v[i] != (T)0 ) // lazy error handling
00266                                 u[i] = this->coord[i] / _v[i];
00267                 return u;
00268         }
00269 
00274         vec_type& operator /= ( const T& _s ) {
00275                 if( _s == (T)0 ) return *this; // lazy error handling
00276                 for (unsigned i=0; i<D; i++) this->coord[i] /= _s;
00277                 return *this;
00278         }
00279 
00284         vec_type& operator /= ( const vec_type& _v ) {
00285                 for (unsigned i=0; i<D; i++)
00286                         if( _v[i] != (T)0 ) // lazy error handling
00287                                 this->coord[i] /= _v[i];
00288                 return *this;
00289         }
00290 
00295         T operator ^ ( const vec_type& _v ) const {
00296                 T s = (T)0;
00297                 for (unsigned i=0; i<D; i++) s += this->coord[i] * _v[i];
00298                 return s;
00299         }
00300 
00305         vec_type operator % ( const vec_type& _v ) const {
00306                 vec_type u;
00307                 for (unsigned i=0; i<D; i++)
00308                         for (unsigned j=0; j<D-1; j++)
00309                                 u[i] += ((j%2)==0?+1:-1) * this->coord[(((i+1)%D)+1*j)%D] * _v[(((i+2)%D)+2*j)%D];
00310                 return u;
00311         }
00312 
00318         friend bool operator == ( const vec_type& _v1, const vec_type& _v2 ) {
00319                 bool b = true;
00320                 for (unsigned i=0; i<D; ++i) b &= (_v1[i] == _v2[i]);
00321                 return b;
00322         }
00323 
00329         friend bool operator == ( const vec_type& _v1,
00330                                   T& _s2 ) {
00331                 bool b = true;
00332                 for (unsigned i=0; i<D; ++i) b &= (_v1[i] == _s2);
00333                 return b;
00334         }
00335 
00341         friend bool operator != ( const vec_type& _v1,
00342                                   const vec_type& _v2 ) {
00343                 bool b = true;
00344                 for (unsigned i=0; i<D; ++i) b |= (_v1[i] != _v2[i]);
00345                 return b;
00346         }
00347 
00353         friend bool operator != ( const vec_type& _v1,
00354                                   T& _s2 ) {
00355                 bool b = true;
00356                 for (unsigned i=0; i<D; ++i) b |= (_v1[i] != _s2);
00357                 return b;
00358         }
00359 
00366         friend std::ostream& operator << ( std::ostream& out,
00367                                            const vec_type& _v ) {
00368                 if( D==0 ) return out;
00369                 out << _v.coord[0];
00370                 for (unsigned i=1; i<D; ++i)
00371                         out << " " << _v.coord[i];
00372                 return out;
00373         }
00374 
00381         friend std::istream& operator >> ( std::istream& in,
00382                                            vec_type& _v ) {
00383                 for (unsigned i=0; i<D; ++i)
00384                         in >> _v.coord[i];
00385                 return in;
00386         }
00387 
00393         T& operator [] ( const unsigned& _i ) { return this->coord[_i]; }
00394 
00400         const T& operator [] ( const unsigned& _i ) const { return this->coord[_i]; }
00401 
00406         const T* operator &( void ) const { return this->coord; }
00407 
00408 protected:
00409 
00410         T coord[D]; 
00411 
00412 };
00413 
00414 //=== IMPLEMENTATION ==========================================================
00415 
00424 template< unsigned D, class T >
00425 void project_vertex( vec< D, T >& _v, const vec< D, T >& _n, const vec< D, T >& _p ) {
00426         T t = _n ^ ( _v - _p );
00427         _v -= t * _n;
00428 }
00429 
00430 //=============================================================================
00431 } // namespace zsig
00432 //=============================================================================
00433 #endif // ZSIG_VEC_HH
00434 //=============================================================================
 All Classes Files Functions Variables Typedefs Friends