Back to index

nux  3.0.0
Vector3.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef VECTOR3_H
00024 #define VECTOR3_H
00025 
00026 #include "../Exception.h"
00027 
00028 #include "Vector4.h"
00029 
00030 namespace nux
00031 {
00032 
00033   template <typename T>
00034   class Vec3
00035   {
00036   public:
00037     inline Vec3();
00038     inline Vec3 (const T &, const T &, const T &);
00039     inline ~Vec3();
00040     inline Vec3 (const Vec3 &);
00041 
00042     inline Vec3<T>& operator = (const Vec3<T>&);
00043 
00044     inline bool operator == (const Vec3<T>&) const;
00045     inline bool operator != (const Vec3<T>&) const;
00046     inline Vec3<T> operator + (const Vec3<T>&) const;
00047     inline Vec3<T> operator * (const Vec3<T>&) const;
00048     inline Vec3<T> operator - (const Vec3<T>&) const;
00049     inline Vec3<T> operator - () const;
00050 
00051     inline Vec3<T>& operator *= (const Vec3<T>&);
00052     inline Vec3<T>& operator += (const Vec3<T>&);
00053     inline Vec3<T>& operator -= (const Vec3<T>&);
00054 
00055     inline Vec3<T> operator / (const T &) const;
00056     inline Vec3<T> operator * (const T &) const;
00057     inline Vec3<T>& operator /= (const T &);
00058     inline Vec3<T>& operator *= (const T &);
00059 
00060     inline T &operator [] (int i);
00061     inline const T &operator [] (int i) const;
00062 
00063     inline T Length() const;
00064     inline T LengthSquared() const;
00065     inline T DotProduct (const Vec3<T>&) const;
00066     inline Vec3 CrossProduct (const Vec3<T>&) const;
00067     inline void Normalize();
00068 
00069     template <typename U> friend Vec3<U> operator* (const U &, const Vec3<U>&);
00070 
00071     //friend  Vec3<T> operator * (T, Vec3&);
00072 
00073     T x, y, z;
00074   };
00075 
00076   template <typename T>
00077   inline Vec3<T>::Vec3()
00078   {
00079     x = 0;
00080     y = 0;
00081     z = 0;
00082   }
00083 
00084   template <typename T>
00085   inline Vec3<T>::~Vec3()
00086   {
00087 
00088   }
00089 
00090   template <typename T>
00091   inline Vec3<T>::Vec3 (const T &fx, const T &fy, const T &fz)
00092   {
00093     x = fx;
00094     y = fy;
00095     z = fz;
00096   }
00097 
00098 //Vec3::Vec3(double fx, double fy, double fz)
00099 //{
00100 //    x = T(fx);
00101 //    y = T(fy);
00102 //    z = T(fz);
00103 //}
00104 //
00105 //Vec3::Vec3(int fx, int fy, int fz)
00106 //{
00107 //    x = T(fx);
00108 //    y = T(fy);
00109 //    z = T(fz);
00110 //}
00111 
00112   template <typename T>
00113   Vec3<T>::Vec3 (const Vec3<T>& v)
00114   {
00115     x = v.x;
00116     y = v.y;
00117     z = v.z;
00118   }
00119 
00120   template <typename T>
00121   Vec3<T>& Vec3<T>::operator = (const Vec3<T>& v)
00122   {
00123     x = v.x;
00124     y = v.y;
00125     z = v.z;
00126     return *this;
00127   }
00128 
00129   template <typename T>
00130   bool Vec3<T>::operator == (const Vec3<T>& v) const
00131   {
00132     if ( (x == v.x) &&
00133          (y == v.y) &&
00134          (z == v.z) )
00135     {
00136       return true;
00137     }
00138 
00139     return false;
00140   }
00141 
00142   template <typename T>
00143   bool Vec3<T>::operator != (const Vec3<T>& v) const
00144   {
00145     return ! (*this == v);
00146   }
00147 
00148   template <typename T>
00149   Vec3<T> Vec3<T>::operator + (const Vec3<T>& v) const
00150   {
00151     return Vec3<T> (x + v.x, y + v.y, z + v.z);
00152   }
00153 
00154   template <typename T>
00155   Vec3<T> Vec3<T>::operator * (const Vec3 &v) const
00156   {
00157     return Vec3<T> (x * v.x, y * v.y, z * v.z);
00158   }
00159 
00160   template <typename T>
00161   Vec3<T> Vec3<T>::operator - (const Vec3 &v) const
00162   {
00163     return Vec3<T> (x - v.x, y - v.y, z - v.z);
00164   }
00165 
00166   template <typename T>
00167   Vec3<T> Vec3<T>::operator - () const
00168   {
00169     return Vec3 (-x, -y, -z);
00170   }
00171 
00172   template <typename T>
00173   Vec3<T>& Vec3<T>::operator *= (const Vec3 &v)
00174   {
00175     x *= v.x;
00176     y *= v.y;
00177     z *= v.z;
00178     return *this;
00179   }
00180 
00181   template <typename T>
00182   Vec3<T>& Vec3<T>::operator += (const Vec3 &v)
00183   {
00184     x += v.x;
00185     y += v.y;
00186     z += v.z;
00187     return *this;
00188   }
00189 
00190   template <typename T>
00191   Vec3<T>& Vec3<T>::operator -= (const Vec3 &v)
00192   {
00193     x -= v.x;
00194     y -= v.y;
00195     z -= v.z;
00196     return *this;
00197   }
00198 
00199   template <typename T>
00200   Vec3<T> Vec3<T>::operator / (const T &f) const
00201   {
00202     if (f == 0)
00203     {
00204       throw DivisionByZeroException();
00205     }
00206 
00207     return Vec3<T> (x / f, y / f, z / f);
00208   }
00209 
00210   template <typename T>
00211   Vec3<T> Vec3<T>::operator * (const T &f) const
00212   {
00213     return Vec3<T> (x * f, y * f, z * f);
00214   }
00215 
00216   template <typename T>
00217   Vec3<T>& Vec3<T>::operator /= (const T &f)
00218   {
00219     if (f == 0)
00220     {
00221       throw DivisionByZeroException();
00222     }
00223 
00224     x = x / f;
00225     y = y / f;
00226     z = z / f;
00227     return *this;
00228   }
00229 
00230   template <typename T>
00231   Vec3<T>& Vec3<T>::operator *= (const T &f)
00232   {
00233     x = x * f;
00234     y = y * f;
00235     z = z * f;
00236     return *this;
00237   }
00238 
00240   template <typename T>
00241   T &Vec3<T>::operator [] (int i)
00242   {
00243     assert (i >= 0);
00244     assert (i <= 2);
00245     return * (&x + i);
00246   }
00247 
00249   template <typename T>
00250   const T &Vec3<T>::operator [] (int i) const
00251   {
00252     assert (i >= 0);
00253     assert (i <= 2);
00254     return * (&x + i);
00255   }
00256 
00257   template <typename T>
00258   T Vec3<T>::Length() const
00259   {
00260     return sqrt (x * x + y * y + z * z);
00261   }
00262 
00263   template <typename T>
00264   T Vec3<T>::LengthSquared() const
00265   {
00266     return (x * x + y * y + z * z);
00267   }
00268 
00269   template <typename T>
00270   T Vec3<T>::DotProduct (const Vec3<T>& v) const
00271   {
00272     return x * v.x + y * v.y + z * v.z;
00273   }
00274 
00275   template <typename T>
00276   Vec3<T> Vec3<T>::CrossProduct (const Vec3<T>& v) const
00277   {
00278     return Vec3<T> (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
00279   }
00280 
00281   template <typename T>
00282   void Vec3<T>::Normalize()
00283   {
00284     T l;
00285     l = Length();
00286 
00287     if (l == 0)
00288     {
00289       throw DivisionByZeroException();
00290     }
00291 
00292     x = x / l;
00293     y = y / l;
00294     z = z / l;
00295   }
00296 
00297   template <typename T>
00298   T DotProduct (const Vec3<T>& lhs, const Vec3<T>& rhs)
00299   {
00300     T out;
00301     out = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
00302     return out;
00303   }
00304 
00305   template <typename T>
00306   const Vec3<T> CrossProduct (const Vec3<T>& lhs, const Vec3<T>& rhs)
00307   {
00308     Vec3<T> out;
00309     out.x = lhs.y * rhs.z - lhs.z * rhs.y;
00310     out.y = lhs.z * rhs.x - lhs.x * rhs.z;
00311     out.z = lhs.x * rhs.y - lhs.y * rhs.x;
00312 
00313     return out;
00314   }
00315 
00316   template <typename U>
00317   inline Vec3<U> operator * (const U &f, const Vec3<U>& v)
00318   {
00319     return v * f;
00320   }
00321 
00322   typedef Vec3<float> Vector3;
00323   typedef Vec3<float> Vertex3;
00324 
00325 }
00326 
00327 #endif // VECTOR3_H