TCMathCoord3D.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 - 2008 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: TCMathCoord3D.h 801 2008-01-23 19:46:25Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TCCOORD3D_H_
00037 #define _TCCOORD3D_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 Coord3D
00066    {
00067    public:
00069       Coord3D()
00070       {
00071          m_data[0] = 0;
00072          m_data[1] = 0;
00073          m_data[2] = 0;
00074       }
00075 
00079       explicit Coord3D(const T vIn)
00080       {
00081          m_data[0] = vIn;
00082          m_data[1] = vIn;
00083          m_data[2] = vIn;
00084       }
00085 
00089       explicit Coord3D(const T vIn[])
00090       {
00091          m_data[0] = vIn[0];
00092          m_data[1] = vIn[1];
00093          m_data[2] = vIn[2];
00094       }
00095 
00104       Coord3D(const T xIn, const T yIn, const T zIn)
00105       {
00106          m_data[0] = xIn;
00107          m_data[1] = yIn;
00108          m_data[2] = zIn;
00109       }
00110 
00116       Coord3D(const Coord3D<T>& coord)
00117       {
00118          m_data[0]=coord.m_data[0];
00119          m_data[1]=coord.m_data[1];
00120          m_data[2]=coord.m_data[2];
00121       } 
00122 
00123 
00125       inline const T& operator[] (sint32 pos) const { return m_data[pos];}
00127       inline T& operator[] (sint32 pos) { return m_data[pos];}
00128 
00129       // --------------------------------------------------
00130       // Assignment operators
00131       Coord3D<T>& operator= (const Coord3D<T>& a) { m_data[0]=a.m_data[0]; m_data[1]=a.m_data[1]; m_data[2]=a.m_data[2];    return *this;}
00132       Coord3D<T>& operator= (const T &vIn)        { m_data[0]=m_data[1]=m_data[2]=vIn;                                      return *this;}
00133       Coord3D<T>& operator+=(const Coord3D<T>& b) { m_data[0]+=b.m_data[0]; m_data[1]+=b.m_data[1]; m_data[2]+=b.m_data[2]; return *this;}
00134       Coord3D<T>& operator+=(const T &b)          { m_data[0]+=b; m_data[1]+=b; m_data[2]+=b;                               return *this;}
00135       Coord3D<T>& operator-=(const Coord3D<T>& b) { m_data[0]-=b.m_data[0]; m_data[1]-=b.m_data[1]; m_data[2]-=b.m_data[2]; return *this;}
00136       Coord3D<T>& operator-=(const T &b)          { m_data[0]-=b; m_data[1]-=b; m_data[2]-=b;                               return *this;}
00137       Coord3D<T>& operator*=(const T &vIn)        { m_data[0]*=vIn; m_data[1]*=vIn; m_data[2]*=vIn;                         return *this;}
00138       Coord3D<T>& operator/=(const T &vIn)        { m_data[0]/=vIn; m_data[1]/=vIn; m_data[2]/=vIn;                         return *this;}
00139       // --------------------------------------------------
00140 
00141       
00143       operator T*() {return m_data;}
00145       operator const T*() const {return m_data;}
00146 
00148       double Length2() const { return (m_data[0]*m_data[0] + m_data[1]*m_data[1] + m_data[2]*m_data[2]);}
00150       double Length()  const { return std::sqrt(Length2());}
00155       void Normalize()
00156       {
00157          double len = Length2();
00158          if (len > 0.0)
00159          {
00160             len = std::sqrt(len);
00161             m_data[0] = (T)((double)(m_data[0])/len);
00162             m_data[1] = (T)((double)(m_data[1])/len);
00163             m_data[2] = (T)((double)(m_data[2])/len);
00164          }
00165       }
00166       
00170       T MaxValue() const
00171       {
00172          return Util::Max(m_data[0], m_data[1], m_data[2]);
00173       }
00174 
00175       typedef T DataType;
00176       enum
00177       {
00178          NUM_COMPONENTS = 3
00179       };
00180    private:
00182       T m_data[NUM_COMPONENTS];      
00183    };
00184 
00185    template <class T>
00186    inline const Coord3D<T> operator^(const Coord3D<T> &a, const Coord3D<T> &b)
00187    {
00188       return Coord3D<T>(a[1] * b[2] - a[2] * b[1],
00189                         a[2] * b[0] - a[0] * b[2],
00190                         a[0] * b[1] - a[1] * b[0]);
00191    }
00192 
00193    template <class T>
00194    inline const T operator*(const Coord3D<T> &a, const Coord3D<T> &b)
00195    {
00196       return (a[0] * b[0] + a[1] * b[1] + a[2] * b[2]);
00197    }
00198 
00199    template <class T>
00200    inline const Coord3D<T> operator*(const Coord3D<T> &v, const T &vIn)
00201    {
00202       return Coord3D<T>(v[0]*vIn, v[1]*vIn, v[2]*vIn);
00203    }
00204 
00205    template <class T>
00206    inline const Coord3D<T> operator*(const T &vIn, const Coord3D<T> &v)
00207    {
00208       return Coord3D<T>(v[0]*vIn, v[1]*vIn, v[2]*vIn);
00209    }
00210 
00211    template <class T>
00212    inline const Coord3D<T> operator/(const Coord3D<T> &v, const T &vIn)
00213    {
00214       return Coord3D<T>(v[0]/vIn, v[1]/vIn, v[2]/vIn);
00215    }
00216 
00217    template <class T>
00218    inline const Coord3D<T> operator/(const T &vIn, const Coord3D<T> &v)
00219    {
00220       return Coord3D<T>(v[0]/vIn, v[1]/vIn, v[2]/vIn);
00221    }
00222 
00223    template <class T>
00224    inline const Coord3D<T> operator/(const Coord3D <T> &in, const Coord3D <T> &in2)
00225    {
00226       
00227       return Coord3D<T>(in[0]/in2[0], in[1]/in2[1], in[2]/in2[2]);
00228 
00229    }
00230 
00231    template <class T>
00232    inline const Coord3D<T> operator+(const Coord3D<T>& v, const Coord3D<T>& b)
00233    {
00234       return Coord3D<T>(v[0]+b[0], v[1]+b[1], v[2]+b[2]);
00235    }
00236 
00237    template <class T>
00238    inline const Coord3D<T> operator+(const Coord3D<T>& v, const T& b)
00239    {
00240       return Coord3D<T>(v[0]+b, v[1]+b, v[2]+b);
00241    }
00242 
00243    template <class T>
00244    inline const Coord3D<T> operator-(const Coord3D<T>& v, const Coord3D<T>& b)
00245    {
00246       Coord3D<T> a;
00247       a[0] = v[0]-b[0];
00248       a[1] = v[1]-b[1];
00249       a[2] = v[2]-b[2];
00250       
00251       return a;
00252    }
00253 
00254    template <class T>
00255    inline const Coord3D<T> operator-(const Coord3D<T>& v, const T& b)
00256    {
00257       return Coord3D<T>(v[0]-b, v[1]-b, v[2]-b);
00258    }
00259 
00260    template <class T>
00261    inline const Coord3D<T> operator-(const Coord3D<T>& v)
00262    {
00263       return Coord3D<T>(-v[0], -v[1], -v[2]);
00264    }
00265 
00266 
00267    // ------------------------------------------------------------------------------------------------------------
00268    // operators for coordinates checking
00269    // ------------------------------------------------------------------------------------------------------------
00270    template <class T>
00271    inline bool operator>(const Coord3D<T> &a, const Coord3D<T> &b)
00272    {
00273       if (a[0] > b[0]) return true;
00274       if (a[0] < b[0]) return false;
00275       if (a[1] > b[1]) return true;
00276       if (a[1] < b[1]) return false;
00277       if (a[2] > b[2]) return true;
00278       if (a[2] < b[2]) return false;
00279       return false;
00280    }
00281 
00282    template <class T>
00283    inline bool operator<(const Coord3D<T> &a, const Coord3D<T> &b)
00284    {
00285       if (a[0] < b[0]) return true;
00286       if (a[0] > b[0]) return false;
00287       if (a[1] < b[1]) return true;
00288       if (a[1] > b[1]) return false;
00289       if (a[2] < b[2]) return true;
00290       if (a[2] > b[2]) return false;
00291       return false;
00292    }
00293 
00294    template <class T>
00295    inline bool operator==(const Coord3D<T> &a, const Coord3D<T> &b)
00296    {
00297       return (a[0]==b[0] && a[1]==b[1] && a[2]==b[2]);
00298    }
00299 
00300    template <class T>
00301    inline bool operator!=(const Coord3D<T> &a, const Coord3D<T> &b)
00302    {
00303       return (a[0]!=b[0] || a[1]!=b[1] || a[2]!=b[2]);
00304    }
00305 
00306    template <class T>
00307    inline bool operator>=(const Coord3D<T> &a, const Coord3D<T> &b)
00308    {
00309       return (a==b || a>b);
00310    }
00311 
00312    template <class T>
00313    inline bool operator<=(const Coord3D<T> &a, const Coord3D<T> &b) 
00314    {
00315       return (a==b || a<b);
00316    }
00317 
00325    template <class T>
00326    inline StreamPtr operator>>(StreamPtr stream, Coord3D<T>&coord)
00327    {
00328       return stream >> coord[0] >> coord[1] >> coord[2];
00329    }
00337    template <class T>
00338    inline StreamPtr operator<<(StreamPtr stream, const Coord3D<T>&coord)
00339    {
00340       return stream << coord[0] << space << coord[1] << space << coord[2];
00341    }
00342 
00347 } // namespace Math
00348 } // namespace TC
00349 
00350 #endif //_TCCOORD3D_H_

Copyright (c) Thomas Goessler 2003 - 2008