Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglVec2.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglVec2.hpp
00021  *  Created: 8 March 1999
00022  *  Summary: 2D templated vector class
00023  *****************************************************************************/
00024 
00025 #ifndef __SGL_VEC2_HPP
00026 #define __SGL_VEC2_HPP
00027 
00028 #include <sgl.h>
00029 
00030 template <class T> class sglMat2;
00031 
00032 // ---------------------------------------------------------------------------
00033 
00034 template <class T>
00035 class sglVec2
00036 {
00037 public:
00038    // constructors
00039    sglVec2() {};
00040 
00041    sglVec2(T x, T y)
00042       { m_vector[0] = x; m_vector[1] = y; }
00043 
00044    template <class S>
00045    sglVec2(const sglVec2<S>& vec)
00046       { m_vector[0] = (T)vec[0]; m_vector[1] = (T)vec[1]; }
00047 
00048    // destructor
00049    ~sglVec2() {};  // not virtual, do not subclass
00050 
00051    inline T *getPtr() const { return (T *)m_vector; }
00052 
00053    // indexing operators
00054    inline T& operator[](unsigned int i)             { return m_vector[i]; }
00055    inline const T& operator[](unsigned int i) const { return m_vector[i]; }
00056 
00057    // set from any other type
00058    template <class S>
00059    inline void set(S x, S y) { m_vector[0] = (T)x; m_vector[1] = (T)y; }  
00060 
00061    // assignment operator
00062    template <class S>
00063    inline sglVec2 &operator=(const sglVec2<S>& vec)
00064       {
00065          m_vector[0] = (T)vec[0];  m_vector[1] = (T)vec[1]; return *this;
00066       }
00067 
00068    // boolean operators
00069    template <class S>
00070    inline bool operator==(const sglVec2<S>& vec) const
00071       {
00072          return (m_vector[0] == (T)vec[0] && m_vector[1] == (T)vec[1]);
00073       }
00074    template <class S>
00075    inline bool operator!=(const sglVec2<S>& vec) const
00076       {
00077          return (m_vector[0] != (T)vec[0] || m_vector[1] != (T)vec[1]);
00078       }
00079 
00080    template <class S>
00081    inline bool operator<(const sglVec2<S>& vec) const
00082       {
00083          return (dot(*this) < vec.dot(vec));
00084       }
00085    template <class S>
00086    inline bool operator>(const sglVec2<S>& vec) const
00087       {
00088          return (dot(*this) > vec.dot(vec));
00089       }
00090 
00091    // math operators
00092    template <class S>
00093    inline sglVec2& operator+=(const sglVec2<S>& vec)
00094       {
00095          m_vector[0] += (T)vec[0]; m_vector[1] += (T)vec[1]; return *this;
00096       }
00097    template <class S>
00098    inline sglVec2& operator-=(const sglVec2<S>& vec)
00099       {
00100          m_vector[0] -= (T)vec[0]; m_vector[1] -= (T)vec[1]; return *this;
00101       }
00102    inline sglVec2& operator*=(const T s)
00103       {
00104          m_vector[0] *= s; m_vector[1] *= s; return *this;
00105       }
00106 
00107    template <class S>
00108    inline const sglVec2<T> operator+(const sglVec2<S> &rhs) const
00109       {
00110          return sglVec2(m_vector[0]+(T)rhs[0],
00111                         m_vector[1]+(T)rhs[1]);
00112       }
00113    template <class S>
00114    inline const sglVec2<T> operator-(const sglVec2<S> &rhs) const
00115       {
00116          return sglVec2(m_vector[0]-(T)rhs[0],
00117                         m_vector[1]-(T)rhs[1]);
00118       }
00119    inline const sglVec2<T> operator*(T scalar) const
00120       {
00121          return sglVec2(m_vector[0]*(T)scalar, m_vector[1]*(T)scalar);
00122       }
00123 
00124    // explicit math operations (for backward compatibility)
00125    template <class S1, class S2>
00126    inline void add(const sglVec2<S1>& vec1, const sglVec2<S2>& vec2)
00127       {
00128          m_vector[0] = (T)vec1[0] + (T)vec2[0];
00129          m_vector[1] = (T)vec2[1] + (T)vec1[1];
00130       }
00131    template <class S1, class S2>
00132    inline void sub(const sglVec2<S1>& vec1, const sglVec2<S2>& vec2)
00133       {
00134          m_vector[0] = (T)vec1[0] - (T)vec2[0];
00135          m_vector[1] = (T)vec1[1] - (T)vec2[1];
00136       }
00137    template <class S>
00138    inline void scale(const sglVec2<S>& vec, T s)
00139       {
00140          m_vector[0] = (T)vec[0]*s;
00141          m_vector[1] = (T)vec[1]*s;
00142       }
00143 
00144    // other ops
00145    template <class S>
00146    inline bool almostEqual(const sglVec2<S>& vec, double eps) const
00147       {
00148          return (SGL_ALMOST_EQUAL(m_vector[0], vec[0], eps) &&
00149                  SGL_ALMOST_EQUAL(m_vector[1], vec[1], eps));
00150       }
00151 
00152    template <class S>
00153    inline T dot(const sglVec2<S>& vec) const
00154       {
00155          return (m_vector[0]*(T)vec[0] + m_vector[1]*(T)vec[1]);
00156       }
00157 
00158    template <class S, class R>
00159    inline static R dot(const sglVec2<R>& vec1, const sglVec2<S>& vec2)
00160       {
00161          return vec1[0]*(R)vec2[0] + vec1[1]*(R)vec2[1];
00162       }
00163 
00164    inline T length() const
00165       {
00166          return (T) sqrt(dot(*this));
00167       }
00168 
00169    template <class S>
00170    inline T distance(const sglVec2<S>& vec) const
00171       {
00172          return (T) sqrt(((T)vec[0] - m_vector[0])*((T)vec[0] - m_vector[0]) +
00173                          ((T)vec[1] - m_vector[1])*((T)vec[1] - m_vector[1]));
00174       }
00175 
00176    inline T normalize()
00177       {
00178          T len = length();
00179          if (len) *this *= (1.0/len);
00180          return len;
00181       }
00182 
00183    template <class S>
00184    inline T normalize(const sglVec2<S>& vec)
00185       {
00186          T len = vec.length();
00187          if (len) scale(vec, 1.0/len);
00188          return len;
00189       }
00190 
00191    // stream operators
00192 #if defined(TEMPLATE_OSTREAM_HACK)
00193    template <class S>
00194    friend ostream& operator<< (ostream& ostrm, const sglVec2<S>& vec);
00195 
00196    template <class S>
00197    friend istream& operator>> (istream& istrm, sglVec2<S>& vec);
00198 #else
00199    friend ostream& operator<< (ostream& ostrm, const sglVec2<T>& vec)
00200         {
00201             return (ostrm << "(" << vec[0] << ", " << vec[1] << ")");
00202         }
00203 
00204    friend istream& operator>> (istream& istrm, sglVec2<T>& vec)
00205         {
00206             return (istrm >> vec[0] >> vec[1]);
00207         }
00208 #endif
00209 
00210 private:
00211    T m_vector[2];
00212 };
00213 
00214 //----------------------------------------------------------------------------
00215 #if defined(TEMPLATE_OSTREAM_HACK)
00216 template <class T>
00217 inline ostream& operator<<(ostream& ostrm, const sglVec2<T>& vec)
00218 {
00219     return (ostrm << "(" << vec[0] << ", " << vec[1] << ")");
00220 }
00221 
00222 template <class T>
00223 inline istream& operator>>(istream& istrm, sglVec2<T>& vec)
00224 {
00225     return (istrm >> vec[0] >> vec[1]);
00226 }
00227 #endif
00228 //----------------------------------------------------------------------------
00229 
00230 #endif

Generated at Mon Jul 1 18:00:06 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001