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

Tools/Math/Vector_n.h

Go to the documentation of this file.
00001 /**
00002 * @file Vector_n.h
00003 * Contains class Vector_n
00004 *
00005 * @author <a href="mailto:stefanuhrig@gmx.net">Stefan Uhrig</a>
00006 */
00007 //------------------------------------------------------------------------------
00008 #ifndef VECTOR_N_H_INCLUDED
00009 #define VECTOR_N_H_INCLUDED
00010 //------------------------------------------------------------------------------
00011 #define VECTOR_N_STACK
00012 //------------------------------------------------------------------------------
00013 #include <math.h>
00014 #include "MVTools.h"
00015 //------------------------------------------------------------------------------
00016 // Ensure that memory version (use stack or heap) ist defined
00017 #if !defined(VECTOR_N_STACK) && !defined(VECTOR_N_HEAP)
00018 #error Define VECTOR_N_STACK if you'd like Vector_n to use stack memory or \
00019 VECTOR_N_HEAP if you'd like Vector_n to use heap memory!
00020 #endif
00021 //------------------------------------------------------------------------------
00022 /**
00023 * @class Vector_n
00024 * Represents a n-dimensional vector of type T.
00025 *
00026 * Vector_n represents a n-dimensional vector of type T. The class supplies
00027 * standard arithmetic operations.
00028 */
00029 template <class T, size_t N>
00030 class Vector_n
00031 {
00032 public:
00033   //----------------------------------------------------------------------------
00034   /**
00035   * Standard constructor
00036   *
00037   * Initializes the vector with values supplied by the standard constructor
00038   * of T.
00039   *
00040   * Complexity: n
00041   */
00042   Vector_n()
00043   {
00044     #if defined(VECTOR_N_HEAP)
00045       content = new T[N];
00046     #endif
00047 
00048     T zero = T();
00049     for (size_t i = 0; i < N; ++i)
00050       content[i] = zero;
00051   }
00052   //----------------------------------------------------------------------------
00053   /**
00054   * Constructor that initializes the vector with the values in the passed array
00055   * @param v Array with initialization values
00056   *
00057   * Complexity: n
00058   */
00059   Vector_n(const T* v)
00060   {
00061     #if defined(VECTOR_N_HEAP)
00062       content = new T[N];
00063     #endif
00064     
00065     for (size_t i = 0; i < N; ++i)
00066       content[i] = v[i];
00067   }
00068   //----------------------------------------------------------------------------
00069   /** 
00070   * Copy constructor
00071   * @param v Vector to copy
00072   *
00073   * Complexity: n
00074   */
00075   Vector_n(const Vector_n<T, N>& v)
00076   {
00077     #if defined(VECTOR_N_HEAP)
00078       content = new T[N];
00079     #endif
00080 
00081     *this = v;
00082   }
00083   //----------------------------------------------------------------------------
00084   /**
00085   * Destructor
00086   *
00087   * Complexity: n
00088   */
00089   ~Vector_n()
00090   {
00091     #if defined(VECTOR_N_HEAP)
00092       delete [] content;
00093     #endif
00094   }
00095   //----------------------------------------------------------------------------
00096   /**
00097   * Copy operator
00098   * @param v Vector to copy
00099   *
00100   * Complexity: n
00101   */
00102   Vector_n<T, N>& operator=(const Vector_n<T, N>& v)
00103   {
00104     for (size_t i = 0; i < N; ++i)
00105       content[i] = v.content[i];
00106     return *this;
00107   }
00108   //----------------------------------------------------------------------------
00109   /**
00110   * Copy operator
00111   * @param v Array with initialization values
00112   *
00113   * Complexity: n
00114   */
00115   Vector_n<T, N>& operator=(const T* v)
00116   {
00117     for (size_t i = 0; i < N; ++i)
00118       content[i] = v[i];
00119     return *this;
00120   }
00121   //----------------------------------------------------------------------------
00122   /**
00123   * Copies contents of vector to passed array
00124   * @param v Array the values are stored to
00125   *
00126   * Complexity: n
00127   */
00128   void copyTo(T* v) const
00129   {
00130     const T* con = content;
00131     for (size_t i = 0; i < N; ++i, ++v, ++con)
00132       *v = *con;
00133   }
00134   //----------------------------------------------------------------------------
00135   /**
00136   * Constant element access operator
00137   * @param i Index of element to access (first element has index 0)
00138   * @return Constant reference to value
00139   *
00140   * Complexity: 1
00141   */
00142   const T& operator[](size_t i) const
00143   {
00144     return content[i];
00145   }
00146   //----------------------------------------------------------------------------
00147   /**
00148   * Element access operator
00149   * @param i Index of element to access (first element has index 0)
00150   * @return Reference to value
00151   *
00152   * Complexity: 1
00153   */
00154   T& operator[](size_t i)
00155   {
00156     return content[i];
00157   }
00158   //----------------------------------------------------------------------------
00159   /**
00160   * Operator +=
00161   * @param v Vector to add
00162   *
00163   * Complexity: n
00164   */
00165   Vector_n<T, N>& operator+=(const Vector_n<T, N>& v)
00166   {
00167     for (size_t i = 0; i < N; ++i)
00168       content[i] += v.content[i];
00169     return *this;
00170   }
00171   //----------------------------------------------------------------------------
00172   /**
00173   * Operator -=
00174   * @param v Vector to subtract
00175   *
00176   * Complexity: n
00177   */
00178   Vector_n<T, N>& operator-=(const Vector_n<T, N>& v)
00179   {
00180     for (size_t i = 0; i < N; ++i)
00181       content[i] -= v.content[i];
00182     return *this;
00183   }
00184   //----------------------------------------------------------------------------
00185   /**
00186   * Operator *=
00187   * @param s Scalar the vector is to multiplied with
00188   *
00189   * Complexity: n
00190   */
00191   Vector_n<T, N>& operator*=(const T& s)
00192   {
00193     for (size_t i = 0; i < N; ++i)
00194     {
00195       content[i] *= s;
00196       if (MVTools::isNearInf(content[i]))
00197       {
00198         if (MVTools::isNearPosInf(content[i]))
00199           throw MVException(MVException::PosInfValue);
00200         else
00201           throw MVException(MVException::NegInfValue);
00202       }
00203     }
00204     return *this;
00205   }
00206   //----------------------------------------------------------------------------
00207   /**
00208   * Operator /=
00209   * @param s Scalar the vector is to be divided by
00210   *
00211   * Complexity: n
00212   */
00213   Vector_n<T, N>& operator/=(const T& s)
00214   {
00215     if (MVTools::isNearZero(s))
00216     {
00217       if (MVTools::isNearNegZero(s))
00218         throw MVException(MVException::DivByNegZero);
00219       else
00220         throw MVException(MVException::DivByPosZero);
00221     }
00222 
00223     for (size_t i = 0; i < N; ++i)
00224     {
00225       content[i] /= s;
00226       if (MVTools::isNearInf(content[i]))
00227       {
00228         if (MVTools::isNearPosInf(content[i]))
00229           throw MVException(MVException::PosInfValue);
00230         else
00231           throw MVException(MVException::NegInfValue);
00232       }
00233     }
00234     return *this;
00235   }
00236   //----------------------------------------------------------------------------
00237   /**
00238   * Get length of vector
00239   * @return Length of vector
00240   *
00241   * Complexity: n
00242   */
00243   T length()
00244   {
00245     T l = T();
00246     for (size_t i = 0; i < N; ++i)
00247       l += content[i]*content[i];
00248     return sqrt(l);
00249   }
00250   //----------------------------------------------------------------------------
00251 private:
00252   
00253   /** The array holding the contents of the vector */
00254   #if defined(VECTOR_N_HEAP)
00255     T* content;
00256   #elif defined(VECTOR_N_STACK)
00257     T content[N];
00258   #endif
00259 };
00260 //------------------------------------------------------------------------------
00261 /**
00262 * Operator +
00263 * @param v1 First vector
00264 * @param v2 Second vector
00265 * @return v1+v2
00266 *
00267 * Complexity: n
00268 */
00269 template<class T, size_t N>
00270 Vector_n<T, N> operator+(const Vector_n<T, N>& v1,
00271                          const Vector_n<T, N>& v2)
00272 {
00273   Vector_n<T, N> res(v1);
00274   res += v2;
00275   return res;
00276 }
00277 //------------------------------------------------------------------------------
00278 /**
00279 * Operator -
00280 * @param v1 First vector
00281 * @param v2 Second vector
00282 * @return v1-v2
00283 *
00284 * Complexity: n
00285 */
00286 template<class T, size_t N>
00287 Vector_n<T, N> operator-(const Vector_n<T, N>& v1,
00288                          const Vector_n<T, N>& v2)
00289 {
00290   Vector_n<T, N> res(v1);
00291   res -= v2;
00292   return res;
00293 }
00294 //------------------------------------------------------------------------------
00295 /**
00296 * Operator *
00297 * @param s Scalar the vector is to multiplied with
00298 * @param v Vector
00299 * @return s*v
00300 *
00301 * Complexity: n
00302 */
00303 template<class T, size_t N>
00304 Vector_n<T, N> operator*(const T& s,
00305                          const Vector_n<T, N>& v)
00306 {
00307   Vector_n<T, N> res(v);
00308   res *= s;
00309   return res;
00310 }
00311 //------------------------------------------------------------------------------
00312 /**
00313 * Operator *
00314 * @param v Vector
00315 * @param s Scalar the vector is to multiplied with
00316 * @return v*s
00317 *
00318 * Complexity: n
00319 */
00320 template<class T, size_t N>
00321 Vector_n<T, N> operator*(const Vector_n<T, N>& v,
00322                          const T& s)
00323 {
00324   Vector_n<T, N> res(v);
00325   res *= s;
00326   return res;
00327 }
00328 //------------------------------------------------------------------------------
00329 /**
00330 * Operator *
00331 * @param v1 First Vector
00332 * @param v2 Second Vector
00333 * @return Scalar product <v1,v2>
00334 *
00335 * Complexity: n
00336 */
00337 template <class T, size_t N>
00338 T operator*(const Vector_n<T, N>& v1,
00339             const Vector_n<T, N>& v2)
00340 {
00341   T res = T();
00342   for (size_t i = 0; i < N; ++i)
00343     res += v1[i]*v2[i];
00344 
00345   if (MVTools::isNearInf(res))
00346   {
00347     if (MVTools::isNearPosInf(res))
00348       throw MVException(MVException::PosInfValue);
00349     else
00350       throw MVException(MVException::NegInfValue);
00351   }
00352   return res;
00353 }
00354 //------------------------------------------------------------------------------
00355 /**
00356 * Operator /
00357 * @param v Vector
00358 * @param s Scalar the vector is to be divided by
00359 * @return v/s
00360 *
00361 * Complexity: n
00362 */
00363 template<class T, size_t N>
00364 Vector_n<T, N> operator/(const Vector_n<T, N>& v,
00365                          const T& s)
00366 {
00367   Vector_n<T, N> res(v);
00368   res /= s;
00369   return res;
00370 }
00371 //------------------------------------------------------------------------------
00372 /**
00373 * Get length of vector
00374 * @param v Vector to retrieve length of
00375 * @return Length of vector
00376 *
00377 * Complexity: n
00378 */
00379 template<class T, size_t N>
00380 T length(const Vector_n<T, N>& v)
00381 {
00382   T l = T();
00383   for (size_t i = 0; i < N; ++i)
00384     l += v[i]*v[i];
00385   return sqrt(l);
00386 }
00387 //------------------------------------------------------------------------------
00388 #endif
00389 //------------------------------------------------------------------------------

Generated on Thu Sep 23 19:57:39 2004 for GT2004 by doxygen 1.3.6