TCMathCoord4D.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2007 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCMathCoord4D.h 884 2008-07-24 09:48:48Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TC_MATH_COORD4D_H_
00037 #define _TC_MATH_COORD4D_H_
00038 
00039 #include "TCMathUtil.h"
00040 #include "TCStream.h"
00041 #include "TCUtil.h"
00042 
00043 #include <cmath>
00044 
00045 namespace TC
00046 {
00047    namespace Math
00048    {
00064       template <class T>
00065       class Coord4D
00066       {
00067       public:
00069          Coord4D()
00070          {
00071             m_data[0] = 0;
00072             m_data[1] = 0;
00073             m_data[2] = 0;
00074             m_data[3] = 0;
00075          }
00076 
00080          explicit Coord4D(T vIn)
00081          {
00082             m_data[0] = vIn;
00083             m_data[1] = vIn;
00084             m_data[2] = vIn;
00085             m_data[3] = vIn;
00086          }
00087 
00096          Coord4D(T xIn, T yIn, T zIn, T wIn)
00097          {
00098             m_data[0] = xIn;
00099             m_data[1] = yIn;
00100             m_data[2] = zIn;
00101             m_data[3] = wIn;
00102          }
00103 
00109          Coord4D(const Coord4D<T>& coord)
00110          {
00111             m_data[0]=coord.m_data[0];
00112             m_data[1]=coord.m_data[1];
00113             m_data[2]=coord.m_data[2];
00114             m_data[3]=coord.m_data[3];
00115          } 
00116 
00117 
00119          inline const T& operator[] (sint32 pos) const { return m_data[pos];}
00121          inline T& operator[] (sint32 pos) { return m_data[pos];}
00122 
00123          // --------------------------------------------------
00124          // Assignment operators
00125          Coord4D<T>& operator=(const Coord4D<T>& a) 
00126          { 
00127             m_data[0]=a.m_data[0]; 
00128             m_data[1]=a.m_data[1]; 
00129             m_data[2]=a.m_data[2];    
00130             m_data[3]=a.m_data[3];    
00131             return *this;
00132          }
00133          Coord4D<T>& operator+=(const Coord4D<T>& b) 
00134          {
00135             m_data[0]+=b.m_data[0]; 
00136             m_data[1]+=b.m_data[1]; 
00137             m_data[2]+=b.m_data[2]; 
00138             m_data[3]+=b.m_data[3]; 
00139             return *this;
00140          }
00141          Coord4D<T>& operator-=(const Coord4D<T>& b) 
00142          {
00143             m_data[0]-=b.m_data[0]; 
00144             m_data[1]-=b.m_data[1]; 
00145             m_data[2]-=b.m_data[2]; 
00146             m_data[3]-=b.m_data[3]; 
00147             return *this;
00148          }
00149          Coord4D<T>& operator*=(const T &vIn)        
00150          {
00151             m_data[0]*=vIn; 
00152             m_data[1]*=vIn; 
00153             m_data[2]*=vIn;                         
00154             m_data[3]*=vIn;                         
00155             return *this;
00156          }
00157          Coord4D<T>& operator/=(const T &vIn)        
00158          { 
00159             m_data[0]/=vIn; 
00160             m_data[1]/=vIn; 
00161             m_data[2]/=vIn;                        
00162             m_data[3]/=vIn;                        
00163             return *this;
00164          }
00165          // --------------------------------------------------
00166 
00167 
00169          operator T*() {return m_data;}
00171          operator const T*() const {return m_data;}
00172 
00174          double Length2() const 
00175          { 
00176             return m_data[0]*m_data[0] + 
00177                    m_data[1]*m_data[1] + 
00178                    m_data[2]*m_data[2] + 
00179                    m_data[3]*m_data[3];
00180          }
00182          double Length()  const { return std::sqrt(Length2());}
00187          void Normalize()
00188          {
00189             double len = Length2();
00190             if (len > 0.0)
00191             {
00192                len = std::sqrt(len);
00193                m_data[0] = (T)((double)(m_data[0])/len);
00194                m_data[1] = (T)((double)(m_data[1])/len);
00195                m_data[2] = (T)((double)(m_data[2])/len);
00196                m_data[3] = (T)((double)(m_data[2])/len);
00197             }
00198          }
00199 
00200          typedef T DataType;
00201       private:
00203          T m_data[4];      
00204       };
00205 
00206       template <class T>
00207       inline Coord4D<T> operator*(const Coord4D<T> &v, const T &vIn)
00208       {
00209          return Coord4D<T>(v[0]*vIn, v[1]*vIn, v[2]*vIn, v[3]*vIn);
00210       }
00211 
00212       template <class T>
00213       inline Coord4D<T> operator*(const T &vIn, const Coord4D<T> &v)
00214       {
00215          return Coord4D<T>(v[0]*vIn, v[1]*vIn, v[2]*vIn, v[3]*vIn);
00216       }
00217 
00218       template <class T>
00219       inline Coord4D<T> operator/(const Coord4D<T> &v, const T &vIn)
00220       {
00221          return Coord4D<T>(v[0]/vIn, v[1]/vIn, v[2]/vIn, v[3]/vIn);
00222       }
00223 
00224       template <class T>
00225       inline Coord4D<T> operator/(const T &vIn, const Coord4D<T> &v)
00226       {
00227          return Coord4D<T>(v[0]/vIn, v[1]/vIn, v[2]/vIn, v[3]/vIn);
00228       }
00229 
00230       template <class T>
00231       inline Coord4D<T> operator+(const Coord4D<T>& v, const Coord4D<T>& b)
00232       {
00233          return Coord4D<T>(v[0]+b[0], v[1]+b[1], v[2]+b[2], v[3]+b[3]);
00234       }
00235 
00236       template <class T>
00237       inline Coord4D<T> operator-(const Coord4D<T>& v, const Coord4D<T>& b)
00238       {
00239          return Coord4D<T>(v[0]-b[0], v[1]-b[1], v[2]-b[2], v[3]-b[3]);
00240       }
00241 
00242       template <class T>
00243       inline Coord4D<T> operator-(const Coord4D<T>& v)
00244       {
00245          return Coord4D<T>(-v[0], -v[1], -v[2], , -v[3]);
00246       }
00247 
00248 
00249       // ------------------------------------------------------------------------------------------------------------
00250       // operators for coordinates checking
00251       // ------------------------------------------------------------------------------------------------------------
00252       template <class T>
00253       inline bool operator>(const Coord4D<T> &a, const Coord4D<T> &b)
00254       {
00255          if (a[0] > b[0]) return true;
00256          if (a[0] < b[0]) return false;
00257          if (a[1] > b[1]) return true;
00258          if (a[1] < b[1]) return false;
00259          if (a[2] > b[2]) return true;
00260          if (a[2] < b[2]) return false;
00261          if (a[2] > b[3]) return true;
00262          if (a[2] < b[3]) return false;
00263          return false;
00264       }
00265 
00266       template <class T>
00267       inline bool operator<(const Coord4D<T> &a, const Coord4D<T> &b)
00268       {
00269          if (a[0] < b[0]) return true;
00270          if (a[0] > b[0]) return false;
00271          if (a[1] < b[1]) return true;
00272          if (a[1] > b[1]) return false;
00273          if (a[2] < b[2]) return true;
00274          if (a[2] > b[2]) return false;
00275          if (a[3] < b[3]) return true;
00276          if (a[3] > b[3]) return false;
00277          return false;
00278       }
00279 
00280       template <class T>
00281       inline bool operator==(const Coord4D<T> &a, const Coord4D<T> &b)
00282       {
00283          return (a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3]);
00284       }
00285 
00286       template <class T>
00287       inline bool operator!=(const Coord4D<T> &a, const Coord4D<T> &b)
00288       {
00289          return (a[0]!=b[0] || a[1]!=b[1] || a[2]!=b[2] || a[3]!=b[3]);
00290       }
00291 
00292       template <class T>
00293       inline bool operator>=(const Coord4D<T> &a, const Coord4D<T> &b)
00294       {
00295          return (a==b || a>b);
00296       }
00297 
00298       template <class T>
00299       inline bool operator<=(const Coord4D<T> &a, const Coord4D<T> &b) 
00300       {
00301          return (a==b || a<b);
00302       }
00303 
00311       template <class T>
00312       inline StreamPtr operator>>(StreamPtr stream, Coord4D<T>&coord)
00313       {
00314          return stream >> coord[0] >> coord[1] >> coord[2] >> coord[3];
00315       }
00323       template <class T>
00324       inline StreamPtr operator<<(StreamPtr stream, const Coord4D<T>&coord)
00325       {
00326          return stream << coord[0] << space << coord[1] << space << coord[2] << space << coord[3];
00327       }
00328 
00333    } // namespace Math
00334 } // namespace TC
00335 
00336 #endif //_TC_MATH_COORD4D_H_

Copyright (c) Thomas Goessler 2003 - 2008