00001 /*************************************************************************** \ 00002 * Copyright (C) by University Paris-Est - MISS team 00003 * Vector2.hpp created in 12 2008. 00004 * Mail : biri@univ-mlv.fr 00005 * 00006 * This file is part of the OpenKraken-math. 00007 * 00008 * The OpenKraken-math is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation; either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * The OpenKraken-math is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 * 00021 \***************************************************************************/ 00022 00023 /* 00024 * Anti-doublon 00025 */ 00026 #ifndef __OPENKN_MATH__VECTOR2_HPP__ 00027 #define __OPENKN_MATH__VECTOR2_HPP__ 00028 00029 /* 00030 * Internal Includes 00031 */ 00032 #include "Vector.hpp" 00033 00034 namespace kn{ 00035 00036 template<class T> class Vector2 : public Vector <T>{ 00037 00038 public: 00039 00044 Vector2(void) : Vector<T>(2){ } 00045 00051 Vector2(const Vector2<T>& v) : Vector<T>(2){ 00052 this->data[0] = v[0]; 00053 this->data[1] = v[1]; 00054 } 00055 00056 00063 Vector2(const Vector2<T>* v); 00064 00071 Vector2(const T& d1, const T& d2) : Vector<T>(2){ 00072 this->data[0] = d1; 00073 this->data[1] = d2; 00074 } 00075 00081 explicit Vector2(const T& d) : Vector<T>(2, d){} 00082 00089 Vector2(const T* a); 00090 00096 Vector2(const Vector<T>& v); 00097 00101 ~Vector2(void){} 00102 00103 00104 public: 00105 00106 /* 00107 * Accessor 00108 */ 00109 00114 T& x(void){ 00115 return this->data[0]; 00116 } 00117 00122 const T& x(void)const{ 00123 return this->data[0]; 00124 } 00125 00130 T& y(void){ 00131 return this->data[1]; 00132 } 00133 00138 const T& y(void)const{ 00139 return this->data[1]; 00140 } 00141 00142 00143 //using Vector<T>::operator*; 00144 00149 Vector2<T>& normalize(void); 00150 00154 Vector2<T> getNormalized(void) const; 00155 00156 00162 T operator*(const Vector2<T>& v) const; 00163 00170 inline T dot(const Vector2<T>& v) const{ 00171 return (*this)*v; 00172 } 00173 00174 00180 Vector2<T>& operator=(const Vector2<T>& v); 00181 00187 Vector2<T> operator+(const Vector2<T>& v) const; 00188 00194 Vector2<T> operator-(const Vector2<T>& v) const; 00195 00202 inline Vector2<T> operator/(const T& d) const{ 00203 return Vector2(*this) /= d; 00204 } 00205 00211 inline Vector2<T> operator*(const T& d) const{ 00212 return Vector2(*this) *= d; 00213 } 00214 00219 inline Vector2<T> operator-(void)const{ 00220 return Vector2<T>(*this) * ((T)(-1)); 00221 } 00222 00228 Vector2<T>& operator+=(const Vector2<T>& v); 00229 00235 Vector2<T>& operator-=(const Vector2<T>& v); 00236 00243 Vector2<T>& operator/=(const T& d); 00244 00250 Vector2<T>& operator*=(const T& d); 00251 00252 00253 00258 inline size_t size(void) const{ 00259 return 2; 00260 } 00261 00265 inline void fill(const T &d){ 00266 this->data[0] = this->data[1] = d; 00267 } 00268 00272 inline void setZero(void){ 00273 this->data[0] = this->data[1] = T(0.0); 00274 } 00275 }; 00276 00277 00278 template<class T> 00279 Vector2<T>::Vector2(const Vector<T>& v) :Vector<T>(2){ 00280 if(v.size() != 2) 00281 throw MathException("Incompatible size for Vector2"); 00282 this->data[0] = v[0]; 00283 this->data[1] = v[1]; 00284 } 00285 00286 template<class T> 00287 Vector2<T>::Vector2(const Vector2<T>* v) : Vector<T>(2){ 00288 if(v==0) throw MathException("Pointer is null"); 00289 this->data[0] = (*v)[0]; 00290 this->data[1] = (*v)[1]; 00291 } 00292 00293 template<class T> 00294 Vector2<T>::Vector2(const T* a) : Vector<T>(2){ 00295 if(a==0) throw MathException("Pointer is null"); 00296 this->data[0] = a[0]; 00297 this->data[1] = a[1]; 00298 } 00299 00300 00301 template<class T> 00302 Vector2<T>& Vector2<T>::normalize(void){ 00303 float norm = this->getNorm(); 00304 this->data[0] = T(this->data[0] / norm); 00305 this->data[1] = T(this->data[1] / norm); 00306 return *this; 00307 } 00308 00309 template<class T> 00310 Vector2<T> Vector2<T>::getNormalized(void) const{ 00311 Vector2<T> v(*this); 00312 v.normalize(); 00313 return v; 00314 } 00315 00316 template<class T> 00317 T Vector2<T>::operator*(const Vector2<T>& v) const{ 00318 return v[0]*this->data[0] + v[1]*this->data[1]; 00319 } 00320 00321 00322 template<class T> 00323 Vector2<T>& Vector2<T>::operator=(const Vector2<T>& v){ 00324 if(&v == this) return *this; 00325 this->data[0] = v[0]; 00326 this->data[1] = v[1]; 00327 return *this; 00328 } 00329 00330 00331 template<class T> 00332 Vector2<T> Vector2<T>::operator+(const Vector2<T>& v) const{ 00333 Vector2 tmp; 00334 tmp[0] = this->data[0] + v[0]; 00335 tmp[1] = this->data[1] + v[1]; 00336 return tmp; 00337 } 00338 00339 00340 template<class T> 00341 Vector2<T> Vector2<T>::operator-(const Vector2<T>& v) const{ 00342 Vector2 tmp; 00343 tmp[0] = this->data[0] - v[0]; 00344 tmp[1] = this->data[1] - v[1]; 00345 return tmp; 00346 } 00347 00348 template<class T> 00349 Vector2<T>& Vector2<T>::operator+=(const Vector2<T>& v){ 00350 this->data[0] += v[0]; 00351 this->data[1] += v[1]; 00352 return *this; 00353 } 00354 00355 template<class T> 00356 Vector2<T>& Vector2<T>::operator-=(const Vector2<T>& v){ 00357 this->data[0] -= v[0]; 00358 this->data[1] -= v[1]; 00359 return *this; 00360 } 00361 00362 template<class T> 00363 Vector2<T>& Vector2<T>::operator/=(const T& d){ 00364 this->data[0] /= d; 00365 this->data[1] /= d; 00366 return *this; 00367 } 00368 00369 template<class T> 00370 Vector2<T>& Vector2<T>::operator*=(const T& d){ 00371 this->data[0] *= d; 00372 this->data[1] *= d; 00373 return *this; 00374 } 00375 00382 template<class U> Vector2<U> operator*(const U& d, 00383 const Vector2<U>& v){ 00384 Vector2<U> result(v); 00385 return result*d; 00386 } 00387 00388 00389 /* 00390 * Type definition 00391 */ 00392 00393 typedef Vector2<float> Vector2f; 00394 typedef Vector2<double> Vector2d; 00395 typedef Vector2<int> Vector2i; 00396 typedef Vector2<unsigned int> Vector2u; 00397 } 00398 00399 #endif