KeosVector3.h

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #ifndef KEOS_VECTOR3_H
00023 #define KEOS_VECTOR3_H
00024 
00025 #include "KeosPrerequisites.h"
00026 
00027 namespace Keos
00028 {
00029   //-----------------------------------------------------------------------
00033   template <class T>
00034   class CVector3
00035   {
00036   public :
00037 
00040     CVector3(T X = 0, T Y = 0, T Z = 0);
00041 
00044     void Set(T X, T Y, T Z);
00045 
00048     T Length() const;
00049 
00052     T LengthSq() const;
00053 
00056     void Normalize();
00057 
00058     // arithmetic operations
00059     CVector3<T> operator +() const;
00060     CVector3<T> operator -() const;
00061 
00062     CVector3<T> operator +(const CVector3<T>& v) const;
00063     CVector3<T> operator -(const CVector3<T>& v) const;
00064 
00065     const CVector3<T>& operator +=(const CVector3<T>& v);
00066     const CVector3<T>& operator -=(const CVector3<T>& v);
00067 
00068     const CVector3<T>& operator *=(T t);
00069     const CVector3<T>& operator /=(T t);
00070 
00071     bool operator ==(const CVector3<T>& v) const;
00072     bool operator !=(const CVector3<T>& v) const;
00073 
00077     operator T*();
00078 
00079     // Vector components
00080     T x, y, z;
00081   };
00082 
00083   // Globals functions
00084   template <class T> CVector3<T>   operator * (const CVector3<T>& v, T t);
00085   template <class T> CVector3<T>   operator / (const CVector3<T>& v, T t);
00086   template <class T> CVector3<T>   operator * (T t, const CVector3<T>& v);
00087   template <class T> T             VectorDot  (const CVector3<T>& v1, const CVector3<T>& v2);
00088   template <class T> CVector3<T>   VectorCross(const CVector3<T>& v1, const CVector3<T>& v2);
00089   template <class T> CVector3<T>  VectorBetweenTwoPoints(const CVector3<T>& p1, const CVector3<T>& p2);
00090   template <class T> std::istream& operator >>(std::istream& Stream, CVector3<T>& Vector);
00091   template <class T> std::ostream& operator <<(std::ostream& Stream, const CVector3<T>& Vector);
00092 
00093   // Usual types of vectors
00094   typedef CVector3<int>   TVector3I;
00095   typedef CVector3<float> TVector3F;
00096 
00097 
00098   //=======================================================================
00099   // CVector3 inline functions
00100   //=======================================================================
00101 
00102   //-----------------------------------------------------------------------
00103   template <class T>
00104   inline CVector3<T>::CVector3(T X, T Y, T Z) :
00105       x(X),
00106       y(Y),
00107       z(Z)
00108   {}
00109 
00110   //-----------------------------------------------------------------------
00111   template <class T>
00112   inline void CVector3<T>::Set(T X, T Y, T Z)
00113   {
00114     x = X;
00115     y = Y;
00116     z = Z;
00117   }
00118 
00119   //-----------------------------------------------------------------------
00120   template <class T>
00121   inline T CVector3<T>::Length() const
00122   {
00123     return std::sqrt(LengthSq());
00124   }
00125 
00126   //-----------------------------------------------------------------------
00127   template <class T>
00128   inline T CVector3<T>::LengthSq() const
00129   {
00130     return x * x + y * y + z * z;
00131   }
00132 
00133   //-----------------------------------------------------------------------
00134   template <class T>
00135   inline void CVector3<T>::Normalize()
00136   {
00137     T Norm = Length();
00138 
00139     if (std::abs(Norm) > std::numeric_limits<T>::epsilon())
00140     {
00141       x /= Norm;
00142       y /= Norm;
00143       z /= Norm;
00144     }
00145   }
00146 
00147   //-----------------------------------------------------------------------
00148   template <class T>
00149   inline CVector3<T> CVector3<T>::operator +() const
00150   {
00151     return this;
00152   }
00153 
00154   //-----------------------------------------------------------------------
00155   template <class T>
00156   inline CVector3<T> CVector3<T>::operator -() const
00157   {
00158     return CVector3<T>(-x, -y, -z);
00159   }
00160 
00161   //-----------------------------------------------------------------------
00162   template <class T>
00163   inline CVector3<T> CVector3<T>::operator +(const CVector3<T>& v) const
00164   {
00165     return CVector3<T>(x + v.x, y + v.y, z + v.z);
00166   }
00167 
00168   //-----------------------------------------------------------------------
00169   template <class T>
00170   inline CVector3<T> CVector3<T>::operator -(const CVector3<T>& v) const
00171   {
00172     return CVector3<T>(x - v.x, y - v.y, z - v.z);
00173   }
00174 
00175   //-----------------------------------------------------------------------
00176   template <class T>
00177   inline const CVector3<T>& CVector3<T>::operator +=(const CVector3<T>& v)
00178   {
00179     x += v.x;
00180     y += v.y;
00181     z += v.z;
00182 
00183     return *this;
00184   }
00185 
00186   //-----------------------------------------------------------------------
00187   template <class T>
00188   inline const CVector3<T>& CVector3<T>::operator -=(const CVector3<T>& v)
00189   {
00190     x -= v.x;
00191     y -= v.y;
00192     z -= v.z;
00193 
00194     return *this;
00195   }
00196 
00197   //-----------------------------------------------------------------------
00198   template <class T>
00199   inline const CVector3<T>& CVector3<T>::operator *=(T t)
00200   {
00201     x *= t;
00202     y *= t;
00203     z *= t;
00204 
00205     return *this;
00206   }
00207 
00208   //-----------------------------------------------------------------------
00209   template <class T>
00210   inline const CVector3<T>& CVector3<T>::operator /=(T t)
00211   {
00212     x /= t;
00213     y /= t;
00214     z /= t;
00215 
00216     return *this;
00217   }
00218 
00219   //-----------------------------------------------------------------------
00220   template <class T>
00221   inline bool CVector3<T>::operator ==(const CVector3<T>& v) const
00222   {
00223     return ((std::abs(x - v.x) <= std::numeric_limits<T>::epsilon()) &&
00224             (std::abs(y - v.y) <= std::numeric_limits<T>::epsilon()) &&
00225             (std::abs(z - v.z) <= std::numeric_limits<T>::epsilon()));
00226   }
00227 
00228   //-----------------------------------------------------------------------
00229   template <class T>
00230   inline bool CVector3<T>::operator !=(const CVector3<T>& v) const
00231   {
00232     return !(*this == v);
00233   }
00234 
00235   //-----------------------------------------------------------------------
00236   template <class T>
00237   inline CVector3<T>::operator T*()
00238   {
00239     return &x;
00240   }
00241 
00242   //-----------------------------------------------------------------------
00243   template <class T>
00244   inline CVector3<T> operator *(const CVector3<T>& v, T t)
00245   {
00246     return CVector3<T>(v.x * t, v.y * t, v.z * t);
00247   }
00248 
00249   //-----------------------------------------------------------------------
00250   template <class T>
00251   inline CVector3<T> operator /(const CVector3<T>& v, T t)
00252   {
00253     return CVector3<T>(v.x / t, v.y / t, v.z / t);
00254   }
00255 
00256   //-----------------------------------------------------------------------
00257   template <class T>
00258   inline CVector3<T> operator *(T t, const CVector3<T>& v)
00259   {
00260     return v * t;
00261   }
00262 
00263   //-----------------------------------------------------------------------
00264   template <class T>
00265   inline T VectorDot(const CVector3<T>& v1, const CVector3<T>& v2)
00266   {
00267     return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00268   }
00269 
00270   //-----------------------------------------------------------------------
00271   template <class T>
00272   inline CVector3<T> VectorCross(const CVector3<T>& v1, const CVector3<T>& v2)
00273   {
00274     return CVector3<T>(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
00275   }
00276 
00277   //-----------------------------------------------------------------------
00278   template <class T>
00279   inline CVector3<T> VectorBetweenTwoPoints(const CVector3<T>& p1, const CVector3<T>& p2)
00280   {
00281     return CVector3<T>(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
00282   }
00283 
00284   //-----------------------------------------------------------------------
00285   template <class T>
00286   inline std::istream& operator >>(std::istream& Stream, CVector3<T>& Vector)
00287   {
00288     return Stream >> Vector.x >> Vector.y >> Vector.z;
00289   }
00290 
00291   //-----------------------------------------------------------------------
00292   template <class T>
00293   inline std::ostream& operator <<(std::ostream& Stream, const CVector3<T>& Vector)
00294   {
00295     return Stream << Vector.x << " " << Vector.y << " " << Vector.z;
00296   }
00297 
00298 
00299 } // namespace Keos
00300 
00301 
00302 #endif // KEOS_VECTOR3_H

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1