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

Vector4.h

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007         Adapted from
00008 
00009         The Universe Development Kit
00010         Copyright (C) 2000  Sean O'Neil
00011         s_p_oneil@hotmail.com
00012 
00013     This library is free software; you can redistribute it and/or
00014     modify it under the terms of the GNU Lesser General Public
00015     License as published by the Free Software Foundation; either
00016     version 2.1 of the License, or (at your option) any later version.
00017 
00018     This library is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021     Lesser General Public License for more details.
00022 
00023     You should have received a copy of the GNU Lesser General Public
00024     License along with this library; if not, write to the Free Software
00025     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027     $Id: Vector4.h,v 1.5 2002/01/17 18:57:37 tksuoran Exp $
00028 */
00029 
00030 
00031 #ifndef TEDDY__MATHS__VECTOR_4_H
00032 #define TEDDY__MATHS__VECTOR_4_H
00033 
00034 
00035 #include "Teddy/Maths/Vector.h"
00036 #include "Teddy/SysSupport/StdMaths.h"
00037 
00038 
00039 namespace Teddy {
00040 namespace Maths {
00041 
00042 
00051 #ifndef SWIG
00052 #define Vector4         TVector4<float>
00053 #define IntVector4      TVector4<int>
00054 #define FloatVector4    TVector4<float>
00055 #define DoubleVector4   TVector4<double>
00056 #define ByteVector4     TVector4<unsigned char>
00057 #define VECTOR4_EPSILON 0.00001
00058 #endif
00059 
00060 
00068 template <typename T> class TVector4 {
00069 public:
00070     T v[4];
00071 
00072 public:
00073     //  Constructors
00074     TVector4(){}
00075 
00076     TVector4( const T a, const T b, const T c, const T d ){
00077         v[0] = a;
00078         v[1] = b;
00079         v[2] = c;
00080         v[3] = d;
00081     }
00082 
00083     TVector4( const T            t              ){ *this = t;   }
00084     TVector4( const T           *pt             ){ *this = pt;  }
00085     TVector4( const TVector<T>  &vec            ){ *this = vec; }
00086     TVector4( const TVector<T>  &vec, const T t ){ v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; v[3] = t; }
00087     TVector4( const TVector4<T> &vec            ){ *this = vec; }
00088 
00089     //  Casting and unary operators
00090                  operator       T* ()                    { return v; }
00091     T           &operator       [] ( const int n )       { return v[n]; }
00092                  operator const T* ()              const { return v; }
00093     T            operator       [] ( const int n ) const { return v[n]; }
00094     TVector4<T>  operator        - ()              const { return TVector4<T>(-v[0], -v[1], -v[2], -v[3]); }
00095 
00096     //  Equal and comparison operators
00097     void operator =( const T            t   )       { v[0] = v[1] = v[2] = v[3] = t; }
00098     void operator =( const T           *pt  )       { v[0] = pt[0]; v[1] = pt[1]; v[2] = pt[2]; v[3] = pt[3]; }
00099     void operator =( const TVector <T> &vec )       { v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; v[3] = 0; }
00100     void operator =( const TVector4<T> &vec )       { v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; v[3] = vec.v[3]; }
00101 
00102     bool operator==( const TVector4<T> &vec ) const {
00103         return (
00104             Abs(v[0] - vec.v[0]) <= (T)VECTOR4_EPSILON && 
00105             Abs(v[1] - vec.v[1]) <= (T)VECTOR4_EPSILON && 
00106             Abs(v[2] - vec.v[2]) <= (T)VECTOR4_EPSILON && 
00107             Abs(v[3] - vec.v[3]) <= (T)VECTOR4_EPSILON
00108         );
00109     }
00110 
00111     bool operator!=( const TVector4<T> &vec ) const {
00112         return !(*this == v);
00113     }
00114 
00115     //  Arithmetic operators (vector with scalar)
00116           TVector4<T>  operator+ ( const T t ) const { return TVector4<T>(v[0]+t, v[1]+t, v[2]+t, v[3]+t); }
00117           TVector4<T>  operator- ( const T t ) const { return TVector4<T>(v[0]-t, v[1]-t, v[2]-t, v[3]-t); }
00118           TVector4<T>  operator* ( const T t ) const { return TVector4<T>(v[0]*t, v[1]*t, v[2]*t, v[3]*t); }
00119           TVector4<T>  operator/ ( const T t ) const { return TVector4<T>(v[0]/t, v[1]/t, v[2]/t, v[3]/t); }
00120     const TVector4<T> &operator+=( const T t )       { v[0] += t; v[1] += t; v[2] += t; v[3] += t; return *this; }
00121     const TVector4<T> &operator-=( const T t )       { v[0] -= t; v[1] -= t; v[2] -= t; v[3] -= t; return *this; }
00122     const TVector4<T> &operator*=( const T t )       { v[0] *= t; v[1] *= t; v[2] *= t; v[3] *= t; return *this; }
00123     const TVector4<T> &operator/=( const T t )       { v[0] /= t; v[1] /= t; v[2] /= t; v[3] /= t; return *this; }
00124 
00125     //  Arithmetic operators (vector with vector)
00126           TVector4<T>  operator+ ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]+vec.v[0], v[1]+vec.v[1], v[2]+vec.v[2], v[3]+vec.v[3]); }
00127           TVector4<T>  operator- ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]-vec.v[0], v[1]-vec.v[1], v[2]-vec.v[2], v[3]-vec.v[3]); }
00128           TVector4<T>  operator* ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]*vec.v[0], v[1]*vec.v[1], v[2]*vec.v[2], v[3]*vec.v[3]); }
00129           TVector4<T>  operator/ ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]/vec.v[0], v[1]/vec.v[1], v[2]/vec.v[2], v[3]/vec.v[3]); }
00130     const TVector4<T> &operator+=( const TVector4<T> &vec )       { v[0] += vec.v[0]; v[1] += vec.v[1]; v[2] += vec.v[2]; v[3] += vec.v[3]; return *this; }
00131     const TVector4<T> &operator-=( const TVector4<T> &vec )       { v[0] -= vec.v[0]; v[1] -= vec.v[1]; v[2] -= vec.v[2]; v[3] -= vec.v[3]; return *this; }
00132     const TVector4<T> &operator*=( const TVector4<T> &vec )       { v[0] *= vec.v[0]; v[1] *= vec.v[1]; v[2] *= vec.v[2]; v[3] *= vec.v[3]; return *this; }
00133     const TVector4<T> &operator/=( const TVector4<T> &vec )       { v[0] /= vec.v[0]; v[1] /= vec.v[1]; v[2] /= vec.v[2]; v[3] /= vec.v[3]; return *this; }
00134 
00135     //  Dot product
00136     T                  operator| ( const TVector4<T> &vec ) const { return v[0]*vec[0] + v[1]*vec[1] + v[2]*vec[2] + v[3]*vec[3]; }
00137 
00138     //  Magnitude/normalize methods
00139     T    magnitudeSquared() const { return v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]; }
00140     T    magnitude       () const { return (T)sqrt( magnitudeSquared() ); }
00141     void normalize       ()       { *this /= Magnitude(); }
00142 };
00143 
00144 
00145 #ifdef SWIG
00146 %template(Vector4)         TVector4<float>;
00147 %template(IntVector4)      TVector4<int>;
00148 %template(FloatVector4)    TVector4<float>;
00149 %template(DoubleVector4)   TVector4<double>;
00150 #endif
00151 
00152 
00153 };  //  namespace Maths
00154 };  //  namespace Teddy
00155 
00156 
00157 #endif  //  TEDDY__MATHS__VECTOR_4_H
00158