00001 /*************************************************************************** \ 00002 * Copyright (C) by University Paris-Est - MISS team 00003 * Vector3.hpp created in 09 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__VECTOR3_HPP__ 00027 #define __OPENKN_MATH__VECTOR3_HPP__ 00028 00029 /* 00030 * Internal Includes 00031 */ 00032 #include "Vector2.hpp" 00033 00034 namespace kn{ 00035 00036 00037 template<class T> class Vector3 : public Vector <T>{ 00038 00039 public: 00040 00045 Vector3(void) : Vector<T>(3){ } 00046 00052 Vector3(const Vector3<T>& v) : Vector<T>(3){ 00053 this->data[0] = v[0]; 00054 this->data[1] = v[1]; 00055 this->data[2] = v[2]; 00056 } 00057 00058 00065 Vector3(const Vector3<T>* v); 00066 00067 00073 Vector3(const Vector2<T>& v,const T& d1) : Vector<T>(3){ 00074 this->data[0] = v[0]; 00075 this->data[1] = v[1]; 00076 this->data[2] = d1; 00077 } 00078 00084 Vector3(const T& d1, const Vector2<T>& v) : Vector<T>(3){ 00085 this->data[0] = d1; 00086 this->data[1] = v[0]; 00087 this->data[2] = v[1]; 00088 } 00089 00090 00098 Vector3(const T& d1, const T& d2, const T& d3) : Vector<T>(3){ 00099 this->data[0] = d1; 00100 this->data[1] = d2; 00101 this->data[2] = d3; 00102 } 00103 00109 Vector3(const T& d) : Vector<T>(3, d){} 00110 00117 Vector3(const T* a); 00118 00124 Vector3(const Vector<T>& v); 00125 00129 ~Vector3(void){} 00130 00131 00132 public: 00133 00134 /* 00135 * Accessor 00136 */ 00137 00142 T& x(void){ 00143 return this->data[0]; 00144 } 00145 00150 const T& x(void)const{ 00151 return this->data[0]; 00152 } 00153 00158 T& y(void){ 00159 return this->data[1]; 00160 } 00161 00166 const T& y(void)const{ 00167 return this->data[1]; 00168 } 00169 00174 T& z(void){ 00175 return this->data[2]; 00176 } 00177 00182 const T& z(void)const{ 00183 return this->data[2]; 00184 } 00185 00186 //using Vector<T>::operator*; 00191 Vector3<T>& normalize(void); 00192 00196 Vector3<T> getNormalized(void) const; 00197 00203 T operator*(const Vector3<T>& v) const; 00204 00210 T dot(const Vector3<T>& v) const{ 00211 return (*this)*v; 00212 } 00213 00219 Vector3<T>& operator=(const Vector3<T>& v); 00220 00226 Vector3<T> operator+(const Vector3<T>& v) const; 00227 00233 Vector3<T> operator-(const Vector3<T>& v) const; 00234 00240 inline Vector3<T> operator/(const T& d) const{ 00241 return Vector3(*this) /= d; 00242 } 00243 00249 inline Vector3<T> operator*(const T& d) const{ 00250 return Vector3(*this) *= d; 00251 } 00252 00257 inline Vector3<T> operator-(void)const{ 00258 return Vector3<T>(*this) * ((T)(-1)); 00259 } 00260 00266 Vector3<T>& operator+=(const Vector3<T>& v); 00267 00273 Vector3<T>& operator-=(const Vector3<T>& v); 00274 00281 Vector3<T>& operator/=(const T& d); 00282 00288 Vector3<T>& operator*=(const T& d); 00289 00290 00295 Vector3<T> operator^(const Vector3<T>& v)const; 00296 00301 inline Vector3<T> cross(const Vector3<T>& v)const{ 00302 return (*this)^v; 00303 } 00304 00309 inline size_t size(void) const{ 00310 return 3; 00311 } 00312 00316 inline void fill(const T &d){ 00317 this->data[0] = this->data[1] = this->data[2] = d; 00318 } 00319 00323 inline void setZero(void){ 00324 this->data[0] = this->data[1] = this->data[2] = T(0.0); 00325 } 00326 }; 00327 00328 00329 template<class T> 00330 Vector3<T>::Vector3(const Vector<T>& v) :Vector<T>(3){ 00331 if(v.size() != 3) 00332 throw MathException("Incompatible size for Vector3"); 00333 this->data[0] = v[0]; 00334 this->data[1] = v[1]; 00335 this->data[2] = v[2]; 00336 } 00337 00338 template<class T> 00339 Vector3<T>::Vector3(const Vector3<T>* v) : Vector<T>(3){ 00340 if(v==0) throw MathException("Pointer is null"); 00341 this->data[0] = (*v)[0]; 00342 this->data[1] = (*v)[1]; 00343 this->data[2] = (*v)[2]; 00344 } 00345 00346 template<class T> 00347 Vector3<T>::Vector3(const T* a) : Vector<T>(3){ 00348 if(a==0) throw MathException("Pointer is null"); 00349 this->data[0] = a[0]; 00350 this->data[1] = a[1]; 00351 this->data[2] = a[2]; 00352 } 00353 00354 template<class T> 00355 Vector3<T>& Vector3<T>::normalize(void){ 00356 float norm = this->getNorm(); 00357 this->data[0] = T(this->data[0] / norm); 00358 this->data[1] = T(this->data[1] / norm); 00359 this->data[2] = T(this->data[2] / norm); 00360 return *this; 00361 } 00362 00363 template<class T> 00364 Vector3<T> Vector3<T>::getNormalized(void) const{ 00365 Vector3<T> v(*this); 00366 v.normalize(); 00367 return v; 00368 } 00369 00370 template<class T> 00371 T Vector3<T>::operator*(const Vector3<T>& v) const{ 00372 return v[0]*this->data[0] + v[1]*this->data[1] + v[2]*this->data[2]; 00373 } 00374 00375 00376 00377 template<class T> 00378 Vector3<T>& Vector3<T>::operator=(const Vector3<T>& v){ 00379 if(&v == this) return *this; 00380 this->data[0] = v[0]; 00381 this->data[1] = v[1]; 00382 this->data[2] = v[2]; 00383 return *this; 00384 } 00385 00386 00387 template<class T> 00388 Vector3<T> Vector3<T>::operator+(const Vector3<T>& v) const{ 00389 Vector3 tmp; 00390 tmp[0] = this->data[0] + v[0]; 00391 tmp[1] = this->data[1] + v[1]; 00392 tmp[2] = this->data[2] + v[2]; 00393 return tmp; 00394 } 00395 00396 00397 template<class T> 00398 Vector3<T> Vector3<T>::operator-(const Vector3<T>& v) const{ 00399 Vector3 tmp; 00400 tmp[0] = this->data[0] - v[0]; 00401 tmp[1] = this->data[1] - v[1]; 00402 tmp[2] = this->data[2] - v[2]; 00403 return tmp; 00404 } 00405 00406 template<class T> 00407 Vector3<T>& Vector3<T>::operator+=(const Vector3<T>& v){ 00408 this->data[0] += v[0]; 00409 this->data[1] += v[1]; 00410 this->data[2] += v[2]; 00411 return *this; 00412 } 00413 00414 template<class T> 00415 Vector3<T>& Vector3<T>::operator-=(const Vector3<T>& v){ 00416 this->data[0] -= v[0]; 00417 this->data[1] -= v[1]; 00418 this->data[2] -= v[2]; 00419 return *this; 00420 } 00421 00422 template<class T> 00423 Vector3<T>& Vector3<T>::operator/=(const T& d){ 00424 if(d==T(0)) throw MathException("variable is null"); 00425 this->data[0] /= d; 00426 this->data[1] /= d; 00427 this->data[2] /= d; 00428 return *this; 00429 } 00430 00431 template<class T> 00432 Vector3<T>& Vector3<T>::operator*=(const T& d){ 00433 this->data[0] *= d; 00434 this->data[1] *= d; 00435 this->data[2] *= d; 00436 return *this; 00437 } 00438 00439 template<class T> 00440 Vector3<T> Vector3<T>::operator^(const Vector3<T>& v)const{ 00441 Vector3<T> tmp; 00442 tmp[0] = this->data[1]*v[2] - this->data[2]*v[1]; 00443 tmp[1] = this->data[2]*v[0] - this->data[0]*v[2]; 00444 tmp[2] = this->data[0]*v[1] - this->data[1]*v[0]; 00445 return tmp; 00446 } 00447 00448 00449 00450 00457 template<class U> Vector3<U> operator*(const U& d, 00458 const Vector3<U>& v){ 00459 Vector3<U> result(v); 00460 return result*d; 00461 } 00462 00463 00464 /* 00465 * Type definition 00466 */ 00467 00468 typedef Vector3<float> Vector3f; 00469 typedef Vector3<double> Vector3d; 00470 typedef Vector3<int> Vector3i; 00471 } 00472 00473 00474 #endif