• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

step/stepcore

vector.h

Go to the documentation of this file.
00001 /* This file is part of StepCore library.
00002    Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
00003 
00004    StepCore library is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2 of the License, or
00007    (at your option) any later version.
00008 
00009    StepCore library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with StepCore; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017 */
00018 
00023 #ifndef STEPCORE_VECTOR_H
00024 #define STEPCORE_VECTOR_H
00025 
00026 #include <cmath>
00027 #include <QMetaType>
00028 #include "util.h"
00029 
00030 namespace StepCore
00031 {
00032 
00036 template<typename T, int N>
00037 class Vector
00038 {
00039 public:
00041     Vector();
00042 
00044     explicit Vector(T v);
00045 
00047     explicit Vector(T x, T y);
00048 
00050     explicit Vector(T x, T y, T z);
00051 
00053     Vector(const Vector& a);
00054 
00056     Vector& operator=(const Vector& a);
00057 
00059     void setZero();
00060 
00062     bool isZero();
00063 
00066     bool isZero(T epsilon);
00067 
00069     T  operator[](unsigned int i) const { return _array[i]; }
00071     T& operator[](unsigned int i) { return _array[i]; }
00072 
00074     const T* array() const { return _array; }
00076     T* array() { return _array; }
00077 
00079     int dimension() const { return N; }
00080 
00082     T innerProduct(const Vector& a) const;
00083 
00085     T norm2() const { return innerProduct(*this); }
00086 
00088     T norm() const { return T(sqrt(norm2())); }
00089 
00091     Vector unit() const { double n = norm(); return n!=0 ? (*this) / n : Vector<T,N>(0); }
00092 
00094     void invert();
00096     Vector operator-() const;
00097 
00099     Vector& operator+=(const Vector<T,N>& b);
00101     Vector& operator-=(const Vector<T,N>& b);
00102 
00104     template<typename T1, int N1> friend Vector<T1,N1> operator+(const Vector<T1,N1>& a, const Vector<T1,N1>& b);
00106     template<typename T1, int N1> friend Vector<T1,N1> operator-(const Vector<T1,N1>& a, const Vector<T1,N1>& b);
00107 
00109     Vector& operator*=(T d);
00111     Vector& operator/=(T d);
00112 
00114     template<typename T1, int N1> friend Vector<T1,N1> operator*(T1 d, const Vector<T1,N1>& a);
00116     template<typename T1, int N1> friend Vector<T1,N1> operator*(const Vector<T1,N1>& a, T1 d);
00118     template<typename T1, int N1> friend Vector<T1,N1> operator/(const Vector<T1,N1>& a, T1 d);
00119 
00121     bool operator==(const Vector<T,N>& b);
00123     bool operator!=(const Vector<T,N>& b);
00124 
00126     Vector cMultiply(const Vector<T,N>& b) const;
00128     Vector cDivide(const Vector<T,N>& b) const;
00130     Vector cSquare() const { return cMultiply(*this); }
00132     Vector cAbs() const;
00133 
00134 protected:
00135     T _array[N];
00136 };
00137 
00139 typedef Vector<double,2> Vector2d;
00141 typedef Vector<double,3> Vector3d;
00142 
00144 typedef Vector<int,2> Vector2i;
00146 typedef Vector<int,3> Vector3i;
00147 
00148 
00150 template<typename T, int N>
00151 inline Vector<T,N>::Vector()
00152 {
00153 }
00154 
00155 template<typename T, int N>
00156 inline Vector<T,N>::Vector(T v)
00157 {
00158     for(int i=0; i<N; ++i) _array[i] = v;
00159 }
00160 
00161 template<typename T, int N>
00162 inline Vector<T,N>::Vector(T x, T y)
00163 {
00164     _array[0] = x;
00165     _array[1] = y;
00166 }
00167 
00168 template<typename T, int N>
00169 inline Vector<T,N>::Vector(T x, T y, T z)
00170 {
00171     _array[0] = x;
00172     _array[1] = y;
00173     _array[2] = z;
00174 }
00175 
00176 template<typename T, int N>
00177 inline Vector<T,N>::Vector(const Vector<T,N>& a)
00178 {
00179     for(int i=0; i<N; ++i) _array[i] = a._array[i];
00180 }
00181 
00182 template<typename T, int N>
00183 inline Vector<T,N>& Vector<T,N>::operator=(const Vector<T,N>& a)
00184 {
00185     if(&a != this)
00186         for(int i=0; i<N; ++i) _array[i] = a._array[i];
00187     return *this;
00188 }
00189 
00190 template<typename T, int N>
00191 inline void Vector<T,N>::setZero()
00192 {
00193     for(int i=0; i<N; ++i) _array[i] = 0;
00194 }
00195 
00196 template<typename T, int N>
00197 inline bool Vector<T,N>::isZero()
00198 {
00199     for(int i=0; i<N; ++i)
00200         if(_array[i] != 0) return false;
00201     return true;
00202 }
00203 
00204 template<typename T, int N>
00205 inline bool Vector<T,N>::isZero(T epsilon)
00206 {
00207     for(int i=0; i<N; ++i)
00208         if(_array[i] >= epsilon || _array[i] <= -epsilon) return false;
00209     return true;
00210 }
00211 
00212 template<typename T, int N>
00213 inline T Vector<T,N>::innerProduct(const Vector<T,N>& a) const
00214 {
00215     T ret = 0;
00216     for(int i=0; i<N; ++i) ret += _array[i]*a._array[i];
00217     return ret;
00218 }
00219 
00220 template<typename T, int N>
00221 inline void Vector<T,N>::invert()
00222 {
00223     for(int i=0; i<N; ++i) _array[i] = -_array[i];
00224 }
00225 
00226 template<typename T, int N>
00227 inline Vector<T,N> Vector<T,N>::operator-() const
00228 {
00229     Vector<T,N> ret(this);
00230     ret.invert();
00231     return ret;
00232 }
00233 
00234 template<typename T, int N>
00235 inline Vector<T,N>& Vector<T,N>::operator+=(const Vector<T,N>& b)
00236 {
00237     for(int i=0; i<N; ++i) _array[i] += b._array[i];
00238     return *this;
00239 }
00240 
00241 template<typename T, int N>
00242 inline Vector<T,N>& Vector<T,N>::operator-=(const Vector<T,N>& b)
00243 {
00244     for(int i=0; i<N; ++i) _array[i] -= b._array[i];
00245     return *this;
00246 }
00247 
00248 template<typename T, int N>
00249 inline Vector<T,N> operator+(const Vector<T,N>& a, const Vector<T,N>& b)
00250 {
00251     Vector<T,N> ret(a);
00252     ret+=b;
00253     return ret;
00254 }
00255 
00256 template<typename T, int N>
00257 inline Vector<T,N> operator-(const Vector<T,N>& a, const Vector<T,N>& b)
00258 {
00259     Vector<T,N> ret(a);
00260     ret-=b;
00261     return ret;
00262 }
00263 
00264 template<typename T, int N>
00265 inline Vector<T,N>& Vector<T,N>::operator*=(T d)
00266 {
00267     for(int i=0; i<N; i++) _array[i] *= d;
00268     return *this;
00269 }
00270 
00271 template<typename T, int N>
00272 inline Vector<T,N>& Vector<T,N>::operator/=(T d)
00273 {
00274     for(int i=0; i<N; i++) _array[i] /= d;
00275     return *this;
00276 }
00277 
00278 template<typename T, int N>
00279 inline Vector<T,N> operator*(T d, const Vector<T,N>& a)
00280 {
00281     Vector<T,N> ret(a);
00282     ret*=d;
00283     return ret;
00284 }
00285 
00286 template<typename T, int N>
00287 inline Vector<T,N> operator*(const Vector<T,N>& a, T d)
00288 {
00289     return d*a;
00290 }
00291 
00292 template<typename T, int N>
00293 inline Vector<T,N> operator/(const Vector<T,N>& a, T d)
00294 {
00295     return (1/d)*a;
00296 }
00297 
00298 template<typename T, int N>
00299 inline bool Vector<T,N>::operator==(const Vector<T,N>& b)
00300 {
00301     for(int i=0; i<N; ++i)
00302         if(_array[i] != b._array[i]) return false;
00303     return true;
00304 }
00305 
00306 template<typename T, int N>
00307 inline bool Vector<T,N>::operator!=(const Vector<T,N>& b)
00308 {
00309     for(int i=0; i<N; ++i)
00310         if(_array[i] == b._array[i]) return false;
00311     return true;
00312 }
00313 
00314 template<typename T, int N>
00315 inline Vector<T,N> Vector<T,N>::cMultiply(const Vector<T,N>& b) const
00316 {
00317     Vector<T,N> ret;
00318     for(int i=0; i<N; i++) ret._array[i] = _array[i] * b._array[i];
00319     return ret;
00320 }
00321 
00322 template<typename T, int N>
00323 inline Vector<T,N> Vector<T,N>::cDivide(const Vector<T,N>& b) const
00324 {
00325     Vector<T,N> ret;
00326     for(int i=0; i<N; i++) ret._array[i] = _array[i] / b._array[i];
00327     return ret;
00328 }
00329 
00330 template<typename T, int N>
00331 inline Vector<T,N> Vector<T,N>::cAbs() const
00332 {
00333     Vector<T,N> ret;
00334     for(int i=0; i<N; i++) ret._array[i] = fabs(_array[i]);
00335     return ret;
00336 }
00337 
00338 } // namespace StepCore
00339 
00340 // XXX: move it to types.h
00341 Q_DECLARE_METATYPE(StepCore::Vector2d)
00342 Q_DECLARE_METATYPE(StepCore::Vector3d)
00343 Q_DECLARE_METATYPE(StepCore::Vector2i)
00344 Q_DECLARE_METATYPE(StepCore::Vector3i)
00345 
00346 #endif
00347 

step/stepcore

Skip menu "step/stepcore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal